fqname_test.gno

2.13 Kb ยท 74 lines
 1package fqname
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/demo/uassert"
 7)
 8
 9func TestParse(t *testing.T) {
10	tests := []struct {
11		input           string
12		expectedPkgPath string
13		expectedName    string
14	}{
15		{"gno.land/p/demo/avl.Tree", "gno.land/p/demo/avl", "Tree"},
16		{"gno.land/p/demo/avl", "gno.land/p/demo/avl", ""},
17		{"gno.land/p/demo/avl.Tree.Node", "gno.land/p/demo/avl", "Tree.Node"},
18		{"gno.land/p/demo/avl/nested.Package.Func", "gno.land/p/demo/avl/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/demo/avl", "Tree", "[gno.land/p/demo/avl](/p/demo/avl).Tree"},
62		{"gno.land/p/demo/avl", "", "[gno.land/p/demo/avl](/p/demo/avl)"},
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}