pkgpath_test.gno
2.27 Kb · 74 lines
1package gnopm
2
3import (
4 "testing"
5
6 "gno.land/p/nt/uassert/v0"
7)
8
9func TestValidPkgPath(t *testing.T) {
10 cases := []struct {
11 name string
12 in string
13 want bool
14 }{
15 {"versioned package", "gno.land/p/moul/md/v1", true},
16 {"versioned realm", "gno.land/r/moul/gnopm/registry/v0", true},
17 {"unversioned realm", "gno.land/r/sys/users", true},
18 {"address namespace", "gno.land/r/" + addrNS + "/scratch/v0", true},
19
20 {"empty", "", false},
21 {"too few elements", "gno.land/p/moul", false},
22 {"domain without a dot", "gnoland/p/moul/md/v1", false},
23 {"unknown kind", "gno.land/x/moul/md/v1", false},
24 {"leading slash", "/gno.land/p/moul/md/v1", false},
25 {"trailing slash", "gno.land/p/moul/md/v1/", false},
26 {"double slash", "gno.land/p//moul/md", false},
27 {"parent traversal", "gno.land/p/moul/../md", false},
28 {"space", "gno.land/p/moul/m d", false},
29 }
30 for _, tc := range cases {
31 uassert.Equal(t, tc.want, ValidPkgPath(tc.in), tc.name)
32 }
33}
34
35func TestSplitPkgPath(t *testing.T) {
36 domain, kind, ns, rest, ok := SplitPkgPath("gno.land/r/moul/gnopm/registry/v0")
37 uassert.True(t, ok, "splits")
38 uassert.Equal(t, "gno.land", domain)
39 uassert.Equal(t, "r", kind)
40 uassert.Equal(t, "moul", ns)
41 uassert.Equal(t, "gnopm/registry/v0", rest)
42
43 _, _, _, _, ok = SplitPkgPath("nonsense")
44 uassert.False(t, ok, "rejects a non-path")
45
46 uassert.Equal(t, "moul", Namespace("gno.land/p/moul/md/v1"))
47 uassert.Equal(t, "", Namespace("nonsense"))
48}
49
50func TestVersion(t *testing.T) {
51 cases := []struct {
52 in, want string
53 }{
54 {"gno.land/p/moul/md/v1", "v1"},
55 {"gno.land/p/moul/md/v0", "v0"},
56 {"gno.land/p/moul/md/v10", "v10"},
57 {"gno.land/r/sys/users", ""}, // unversioned is legal
58 {"gno.land/r/moul/home", ""}, // the one path that can never carry one
59 {"gno.land/p/moul/md/vx", ""}, // not a number
60 {"gno.land/p/moul/md/v", ""}, // no digits
61 {"short", ""},
62 }
63 for _, tc := range cases {
64 uassert.Equal(t, tc.want, Version(tc.in), tc.in)
65 }
66}
67
68func TestAddressNamespace(t *testing.T) {
69 uassert.True(t, AddressNamespace(addrNS), "a real address")
70 uassert.False(t, AddressNamespace("moul"), "a name")
71 uassert.False(t, AddressNamespace(""), "empty")
72 // b, i, o and 1 are exactly the characters bech32 drops.
73 uassert.False(t, AddressNamespace("g1bbbfred47kzduec920z88wfr64ylksmdcedlf5"), "wrong length")
74}