Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/r/nym-moultest123/sapin/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186406
  max_deposit = "5000000ugnot"

render.gno

package sapin

import "strconv"

const defaultSize int = 3

func Render(path string) string {
	size := defaultSize

	if path != "" {
		var err error
		size, err = strconv.Atoi(path)
		if err != nil {
			panic(err)
		}
	}

	sapin := Sapin(size)
	output := "```\n" + sapin + "```\n"
	return output
}

render_example_test.gno

package sapin

// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// ```
	//          *
	//         ***
	//        *****
	//       *******
	//        *****
	//       *******
	//      *********
	//     ***********
	//    *************
	//      *********
	//     ***********
	//    *************
	//   ***************
	//  *****************
	// *******************
	//         |||
	//         |||
	//         |||
	// ```
}

render_test.gno

package sapin

import (
	"testing"
)

func TestRender(t *testing.T) {
	tt := []struct {
		path string
		want string
	}{
		{"1", "```" + `
   *
  ***
 *****
*******
  |||
` + "```\n"},
		{"", "```" + `
         *
        ***
       *****
      *******
       *****
      *******
     *********
    ***********
   *************
     *********
    ***********
   *************
  ***************
 *****************
*******************
        |||
        |||
        |||
` + "```\n"},
	}

	for _, tc := range tt {
		name := tc.path
		t.Run(name, func(t *testing.T) {
			got := Render(tc.path)
			want := tc.want
			if got != want {
				t.Errorf("expected \n%s\n, got \n%s\n.", want, got)
			}
		})
	}
}

sapin.gno

package sapin

import "strings"

func Sapin(size int) string {
	if size < 1 {
		return ""
	}
	var b strings.Builder
	for floor := 0; floor < size; floor++ {
		for j := 0; j < floor+4; j++ {
			spaces := size*2 - (floor-1)*2 - j + size - 2
			bodies := j*2 + 1 + floor*4
			b.WriteString(strings.Repeat(" ", spaces))
			b.WriteString(strings.Repeat("*", bodies))
			b.WriteString("\n")
		}
	}

	for i := 0; i < size; i++ {
		spaces := (size-1)*3 + 2
		b.WriteString(strings.Repeat(" ", spaces))
		b.WriteString("|||\n")
	}
	return b.String()
}

sapin_test.gno

package sapin

import (
	"fmt"
	"testing"
)

func TestSapin(t *testing.T) {
	tt := []struct {
		size int
		want string
	}{
		{1, `
   *
  ***
 *****
*******
  |||
`},
		{2, `
      *
     ***
    *****
   *******
    *****
   *******
  *********
 ***********
*************
     |||
     |||
`},
		{3, `
         *
        ***
       *****
      *******
       *****
      *******
     *********
    ***********
   *************
     *********
    ***********
   *************
  ***************
 *****************
*******************
        |||
        |||
        |||
`},
		{4, `
            *
           ***
          *****
         *******
          *****
         *******
        *********
       ***********
      *************
        *********
       ***********
      *************
     ***************
    *****************
   *******************
      *************
     ***************
    *****************
   *******************
  *********************
 ***********************
*************************
           |||
           |||
           |||
           |||
`},
		{5, `
               *
              ***
             *****
            *******
             *****
            *******
           *********
          ***********
         *************
           *********
          ***********
         *************
        ***************
       *****************
      *******************
         *************
        ***************
       *****************
      *******************
     *********************
    ***********************
   *************************
       *****************
      *******************
     *********************
    ***********************
   *************************
  ***************************
 *****************************
*******************************
              |||
              |||
              |||
              |||
              |||
`},
		// {50, ``},
		// {51, ``},
	}

	for _, tc := range tt {
		name := fmt.Sprintf("%d", tc.size)
		t.Run(name, func(t *testing.T) {
			got := Sapin(tc.size)
			want := tc.want[1:]
			if got != want {
				t.Errorf("expected \n%s\n, got \n%s\n.", want, got)
			}
		})
	}
}