Skip to content Search Apps Documentation Source Content 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.

helplink_test.gno

3.90 Kb · 105 lines
  1package helplink
  2
  3import (
  4	"chain/runtime"
  5	"testing"
  6
  7	"gno.land/p/nt/urequire/v0"
  8)
  9
 10func TestFunc(t *testing.T) {
 11	cd := runtime.ChainDomain()
 12	tests := []struct {
 13		title     string
 14		fn        string
 15		args      []string
 16		want      string
 17		realm_XXX Realm
 18	}{
 19		{"Example", "foo", []string{"bar", "1", "baz", "2"}, "[Example](/p/moul/helplink/v0$help&func=foo&bar=1&baz=2)", ""},
 20		{"Realm Example", "foo", []string{"bar", "1", "baz", "2"}, "[Realm Example](/r/lorem/ipsum$help&func=foo&bar=1&baz=2)", Realm(cd + "/r/lorem/ipsum")},
 21		{"Single Arg", "testFunc", []string{"key", "value"}, "[Single Arg](/p/moul/helplink/v0$help&func=testFunc&key=value)", ""},
 22		{"No Args", "noArgsFunc", []string{}, "[No Args](/p/moul/helplink/v0$help&func=noArgsFunc)", ""},
 23		{"Odd Args", "oddArgsFunc", []string{"key"}, "[Odd Args](/p/moul/helplink/v0$help&func=oddArgsFunc&error=odd+number+of+arguments)", ""},
 24	}
 25
 26	for _, tt := range tests {
 27		t.Run(tt.title, func(t *testing.T) {
 28			if tt.fn == "oddArgsFunc" {
 29				defer func() {
 30					if r := recover(); r != nil {
 31						if r != "odd number of arguments" {
 32							t.Errorf("expected panic with message 'odd number of arguments', got: %v", r)
 33						}
 34					} else {
 35						t.Error("expected panic for odd number of arguments, but did not panic")
 36					}
 37				}()
 38			}
 39			got := tt.realm_XXX.Func(tt.title, tt.fn, tt.args...)
 40			urequire.Equal(t, tt.want, got)
 41		})
 42	}
 43}
 44
 45func TestFuncURL(t *testing.T) {
 46	cd := runtime.ChainDomain()
 47	tests := []struct {
 48		fn        string
 49		args      []string
 50		want      string
 51		realm_XXX Realm
 52	}{
 53		{"foo", []string{"bar", "1", "baz", "2"}, "/p/moul/helplink/v0$help&func=foo&bar=1&baz=2", ""},
 54		{"testFunc", []string{"key", "value"}, "/p/moul/helplink/v0$help&func=testFunc&key=value", ""},
 55		{"noArgsFunc", []string{}, "/p/moul/helplink/v0$help&func=noArgsFunc", ""},
 56		{"oddArgsFunc", []string{"key"}, "/p/moul/helplink/v0$help&func=oddArgsFunc&error=odd+number+of+arguments", ""},
 57		{"foo", []string{"bar", "1", "baz", "2"}, "/r/lorem/ipsum$help&func=foo&bar=1&baz=2", Realm(cd + "/r/lorem/ipsum")},
 58		{"testFunc", []string{"key", "value"}, "/r/lorem/ipsum$help&func=testFunc&key=value", Realm(cd + "/r/lorem/ipsum")},
 59		{"noArgsFunc", []string{}, "/r/lorem/ipsum$help&func=noArgsFunc", Realm(cd + "/r/lorem/ipsum")},
 60		{"oddArgsFunc", []string{"key"}, "/r/lorem/ipsum$help&func=oddArgsFunc&error=odd+number+of+arguments", Realm(cd + "/r/lorem/ipsum")},
 61		{"foo", []string{"bar", "1", "baz", "2"}, "https://gno.world/r/lorem/ipsum$help&func=foo&bar=1&baz=2", "gno.world/r/lorem/ipsum"},
 62		{"testFunc", []string{"key", "value"}, "https://gno.world/r/lorem/ipsum$help&func=testFunc&key=value", "gno.world/r/lorem/ipsum"},
 63		{"noArgsFunc", []string{}, "https://gno.world/r/lorem/ipsum$help&func=noArgsFunc", "gno.world/r/lorem/ipsum"},
 64		{"oddArgsFunc", []string{"key"}, "https://gno.world/r/lorem/ipsum$help&func=oddArgsFunc&error=odd+number+of+arguments", "gno.world/r/lorem/ipsum"},
 65	}
 66
 67	for _, tt := range tests {
 68		title := tt.fn
 69		t.Run(title, func(t *testing.T) {
 70			if tt.fn == "oddArgsFunc" {
 71				defer func() {
 72					if r := recover(); r != nil {
 73						if r != "odd number of arguments" {
 74							t.Errorf("expected panic with message 'odd number of arguments', got: %v", r)
 75						}
 76					} else {
 77						t.Error("expected panic for odd number of arguments, but did not panic")
 78					}
 79				}()
 80			}
 81			got := tt.realm_XXX.FuncURL(tt.fn, tt.args...)
 82			urequire.Equal(t, tt.want, got)
 83		})
 84	}
 85}
 86
 87func TestHome(t *testing.T) {
 88	testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/helplink"))
 89	cd := runtime.ChainDomain()
 90	tests := []struct {
 91		realm_XXX Realm
 92		want      string
 93	}{
 94		{"", "/r/test/helplink$help"},
 95		{Realm(cd + "/r/lorem/ipsum"), "/r/lorem/ipsum$help"},
 96		{"gno.world/r/lorem/ipsum", "https://gno.world/r/lorem/ipsum$help"},
 97	}
 98
 99	for _, tt := range tests {
100		t.Run(string(tt.realm_XXX), func(t *testing.T) {
101			got := tt.realm_XXX.Home()
102			urequire.Equal(t, tt.want, got)
103		})
104	}
105}