Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

nestedpkg_test.gno

3.42 Kb · 86 lines
 1package nestedpkg
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestNsFromPath(t *testing.T) {
10	cases := []struct {
11		path string
12		want string
13	}{
14		{"gno.land/r/moul/home", "moul"},
15		{"gno.land/p/moul/svg/v0", "moul"},
16		{"gno.land/r/demo/boards", "demo"},
17		{"gno.land/r/moul", "moul"},
18		// Fewer than three segments has no namespace slot.
19		{"gno.land/r", ""},
20		{"gno.land", ""},
21		{"", ""},
22		// The helper is domain-agnostic: it just takes the third segment.
23		{"example.com/r/alice/thing", "alice"},
24	}
25	for _, c := range cases {
26		uassert.Equal(t, c.want, nsFromPath(c.path), "nsFromPath("+c.path+")")
27	}
28}
29
30// What these tests can and cannot reach, because it is not obvious:
31//
32//   - testing.SetRealm sets the realm the helpers read as **current**
33//     (rlm.PkgPath()); this test package is what they see as **previous**
34//     (rlm.Previous().PkgPath()).
35//   - SetRealm's argument must be an `r/` path. PkgPath() panics with
36//     "should only be called for Realms" on a `p/` path.
37//
38// This package is `p/moul/nestedpkg/v0`, so previous is always a `p/` path
39// while current is always an `r/` path. A `p/` path can therefore never be a
40// prefix of the current one or vice versa, which makes the *matching* branches
41// of IsCallerSubPath/IsCallerParentPath unreachable from here — exercising
42// those needs a realm-hosted caller (an `r/` test package). The non-matching
43// branches and the whole namespace comparison are covered below.
44const selfNS = "moul"
45
46func TestIsCallerSubPathRejectsUnrelated(cur realm, t *testing.T) {
47	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
48	uassert.False(t, IsCallerSubPath(0, cur), "an unrelated realm is not an ancestor of the caller")
49}
50
51func TestIsCallerParentPathRejectsUnrelated(cur realm, t *testing.T) {
52	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
53	uassert.False(t, IsCallerParentPath(0, cur), "an unrelated realm is not a descendant of the caller")
54}
55
56func TestIsSameNamespace(cur realm, t *testing.T) {
57	testing.SetRealm(testing.NewCodeRealm("gno.land/r/" + selfNS + "/anything"))
58	uassert.True(t, IsSameNamespace(0, cur), "same namespace across p/ and r/")
59
60	testing.SetRealm(testing.NewCodeRealm("gno.land/r/someoneelse/thing"))
61	uassert.False(t, IsSameNamespace(0, cur), "different namespace")
62}
63
64func TestAssertCallerIsSubPathPanics(cur realm, t *testing.T) {
65	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
66	uassert.PanicsWithMessage(t, cur,
67		"call restricted to nested packages. current realm is gno.land/r/other/thing/, previous realm is gno.land/p/moul/nestedpkg/v0/",
68		func() { AssertCallerIsSubPath(0, cur) })
69}
70
71func TestAssertCallerIsParentPathPanics(cur realm, t *testing.T) {
72	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
73	uassert.PanicsWithMessage(t, cur,
74		"call restricted to parent packages. current realm is gno.land/r/other/thing/, previous realm is gno.land/p/moul/nestedpkg/v0/",
75		func() { AssertCallerIsParentPath(0, cur) })
76}
77
78func TestAssertIsSameNamespaceAllowsAndPanics(cur realm, t *testing.T) {
79	testing.SetRealm(testing.NewCodeRealm("gno.land/r/" + selfNS + "/anything"))
80	AssertIsSameNamespace(0, cur) // same namespace: must not panic
81
82	testing.SetRealm(testing.NewCodeRealm("gno.land/r/someoneelse/thing"))
83	uassert.PanicsWithMessage(t, cur,
84		"call restricted to packages from the same namespace. current realm is someoneelse/, previous realm is "+selfNS+"/",
85		func() { AssertIsSameNamespace(0, cur) })
86}