helplink_test.gno

3.77 Kb ยท 104 lines
  1package helplink
  2
  3import (
  4	"std"
  5	"testing"
  6
  7	"gno.land/p/demo/urequire"
  8)
  9
 10func TestFunc(t *testing.T) {
 11	cd := std.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$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$help&func=testFunc&key=value)", ""},
 22		{"No Args", "noArgsFunc", []string{}, "[No Args](/p/moul/helplink$help&func=noArgsFunc)", ""},
 23		{"Odd Args", "oddArgsFunc", []string{"key"}, "[Odd Args](/p/moul/helplink$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 := std.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$help&func=foo&bar=1&baz=2", ""},
 54		{"testFunc", []string{"key", "value"}, "/p/moul/helplink$help&func=testFunc&key=value", ""},
 55		{"noArgsFunc", []string{}, "/p/moul/helplink$help&func=noArgsFunc", ""},
 56		{"oddArgsFunc", []string{"key"}, "/p/moul/helplink$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	cd := std.ChainDomain()
 89	tests := []struct {
 90		realm_XXX Realm
 91		want      string
 92	}{
 93		{"", "$help"},
 94		{Realm(cd + "/r/lorem/ipsum"), "/r/lorem/ipsum$help"},
 95		{"gno.world/r/lorem/ipsum", "https://gno.world/r/lorem/ipsum$help"},
 96	}
 97
 98	for _, tt := range tests {
 99		t.Run(string(tt.realm_XXX), func(t *testing.T) {
100			got := tt.realm_XXX.Home()
101			urequire.Equal(t, tt.want, got)
102		})
103	}
104}