derive_test.gno

1.34 Kb ยท 50 lines
 1package derive
 2
 3import (
 4	"strings"
 5	"testing"
 6)
 7
 8func TestParsePkgPath(t *testing.T) {
 9	cases := []struct {
10		name     string
11		path     string
12		expected string
13	}{
14		// r/ namespace
15		{"full r path", "gno.land/r/leon/home", "gno.land/r/leon/home"},
16		{"relative r path with slash", "/r/leon/home", "gno.land/r/leon/home"},
17		{"relative r path no slash", "r/leon/home", "gno.land/r/leon/home"},
18
19		// p/ namespace
20		{"full p path", "gno.land/p/leon/home", "gno.land/p/leon/home"},
21		{"relative p path with slash", "/p/leon/home", "gno.land/p/leon/home"},
22		{"relative p path no slash", "p/leon/home", "gno.land/p/leon/home"},
23
24		// invalid cases
25		{"random text", "randomtext", ""},
26		{"random text", "randomtext with space", ""},
27		{"empty", "", ""},
28		{"wrong namespace q/", "q/leon/home", ""},
29		{"no pkgpath query param", "", ""},
30		{"malformed url", "::::", ""},
31	}
32
33	for _, c := range cases {
34		t.Run(c.name, func(t *testing.T) {
35			url := "gno.land/r/leon/derive?pkgpath=" + c.path
36			result := parsePkgPath(url)
37			if result != c.expected {
38				t.Errorf("for input %q expected %q, got %q", c.path, c.expected, result)
39			}
40		})
41	}
42}
43
44func TestDerive(t *testing.T) {
45	got := Render("?pkgpath=gno.land/r/leon/home")
46	if !strings.Contains(got, "g1n0z9kw63c6ze8fgle93s2e86hfm2qz025cgkey") {
47		// manually checked the above address
48		t.Fatal("Failed derivation!")
49	}
50}