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

fqname_test.gno

2.14 Kb · 74 lines
 1package fqname
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestParse(t *testing.T) {
10	tests := []struct {
11		input           string
12		expectedPkgPath string
13		expectedName    string
14	}{
15		{"gno.land/p/nt/avl/v0.Tree", "gno.land/p/nt/avl/v0", "Tree"},
16		{"gno.land/p/nt/avl/v0", "gno.land/p/nt/avl/v0", ""},
17		{"gno.land/p/nt/avl/v0.Tree.Node", "gno.land/p/nt/avl/v0", "Tree.Node"},
18		{"gno.land/p/nt/avl/v0/nested.Package.Func", "gno.land/p/nt/avl/v0/nested", "Package.Func"},
19		{"path/filepath.Split", "path/filepath", "Split"},
20		{"path.Split", "path", "Split"},
21		{"path/filepath", "path/filepath", ""},
22		{"path", "path", ""},
23		{"", "", ""},
24	}
25
26	for _, tt := range tests {
27		pkgpath, name := Parse(tt.input)
28		uassert.Equal(t, tt.expectedPkgPath, pkgpath, "Package path did not match")
29		uassert.Equal(t, tt.expectedName, name, "Name did not match")
30	}
31}
32
33func TestConstruct(t *testing.T) {
34	tests := []struct {
35		pkgpath  string
36		name     string
37		expected string
38	}{
39		{"gno.land/r/demo/foo20", "Token", "gno.land/r/demo/foo20.Token"},
40		{"gno.land/r/demo/foo20", "", "gno.land/r/demo/foo20"},
41		{"path", "", "path"},
42		{"path", "Split", "path.Split"},
43		{"path/filepath", "", "path/filepath"},
44		{"path/filepath", "Split", "path/filepath.Split"},
45		{"", "JustName", ".JustName"},
46		{"", "", ""},
47	}
48
49	for _, tt := range tests {
50		result := Construct(tt.pkgpath, tt.name)
51		uassert.Equal(t, tt.expected, result, "Constructed FQName did not match expected")
52	}
53}
54
55func TestRenderLink(t *testing.T) {
56	tests := []struct {
57		pkgPath  string
58		slug     string
59		expected string
60	}{
61		{"gno.land/p/nt/avl/v0", "Tree", "[gno.land/p/nt/avl/v0](/p/nt/avl/v0).Tree"},
62		{"gno.land/p/nt/avl/v0", "", "[gno.land/p/nt/avl/v0](/p/nt/avl/v0)"},
63		{"github.com/a/b", "C", "github.com/a/b.C"},
64		{"example.com/pkg", "Func", "example.com/pkg.Func"},
65		{"gno.land/r/demo/foo20", "Token", "[gno.land/r/demo/foo20](/r/demo/foo20).Token"},
66		{"gno.land/r/demo/foo20", "", "[gno.land/r/demo/foo20](/r/demo/foo20)"},
67		{"", "", ""},
68	}
69
70	for _, tt := range tests {
71		result := RenderLink(tt.pkgPath, tt.slug)
72		uassert.Equal(t, tt.expected, result, "Rendered link did not match expected")
73	}
74}