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

render_test.gno

2.85 Kb · 88 lines
 1package toolbox
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10func TestIndex(t *testing.T) {
11	var c Catalog
12	uassert.NoError(t, c.Set(Tool{
13		Slug:  "usernames",
14		Path:  "gno.land/r/moul/toolbox/usernames",
15		Title: "Usernames",
16		Desc:  "resolve a username or an address",
17		Tags:  []string{"identity", "read"},
18	}))
19
20	got := Index(c.List())
21	uassert.True(t, strings.Contains(got, "[Usernames](/r/moul/toolbox/usernames)"))
22	uassert.True(t, strings.Contains(got, "resolve a username or an address"))
23	uassert.True(t, strings.Contains(got, "identity, read"))
24}
25
26func TestIndexEmpty(t *testing.T) {
27	uassert.True(t, strings.Contains(Index(nil), "No tools yet."))
28}
29
30// TestIndexEscapesCells pins the escaping contract: a description is written
31// by whoever wrote the entry, and a pipe in one would otherwise add a column to
32// every row below it. The title is link text inside a cell, so it needs the
33// pipe escaped there too.
34func TestIndexEscapesCells(t *testing.T) {
35	got := Index([]Tool{{
36		Slug:  "evil",
37		Path:  "gno.land/r/moul/toolbox/evil",
38		Title: "Ev|il",
39		Desc:  "a | b",
40		Tags:  []string{"x|y"},
41	}})
42	uassert.False(t, strings.Contains(got, "a | b"))
43	uassert.True(t, strings.Contains(got, `[Ev\|il](/r/moul/toolbox/evil)`))
44	uassert.True(t, strings.Contains(got, `x\|y`))
45}
46
47// TestIndexEscapesTitleExactlyOnce is the double-wrap regression, and the
48// values are copied from what the escapers actually produce rather than
49// guessed: md.Link runs its text through sanitize.InlineText itself, so
50// md.Link(ui.Cell(title), url) yields `[a\\\*b]` where one pass yields
51// `[a\*b]`, and the reader sees a stray backslash.
52func TestIndexEscapesTitleExactlyOnce(t *testing.T) {
53	got := Index([]Tool{{
54		Slug:  "star",
55		Path:  "gno.land/r/moul/toolbox/star",
56		Title: "a*b",
57		Desc:  "c*d",
58	}})
59	uassert.True(t, strings.Contains(got, `[a\*b](/r/moul/toolbox/star)`))
60	uassert.False(t, strings.Contains(got, `a\\`))
61	uassert.True(t, strings.Contains(got, `c\*d`))
62}
63
64func TestCard(t *testing.T) {
65	got := Card(Tool{
66		Slug:  "usernames",
67		Path:  "gno.land/r/moul/toolbox/usernames",
68		Title: "Usernames",
69		Desc:  "resolve a username or an address",
70		Tags:  []string{"identity"},
71	})
72	uassert.True(t, strings.Contains(got, "## Usernames"))
73	uassert.True(t, strings.Contains(got, "resolve a username or an address"))
74	uassert.True(t, strings.Contains(got, "`gno.land/r/moul/toolbox/usernames`"))
75	uassert.True(t, strings.Contains(got, "/r/moul/toolbox/usernames"))
76	uassert.True(t, strings.Contains(got, "Tags: identity"))
77}
78
79func TestCardMinimal(t *testing.T) {
80	got := Card(Tool{Slug: "x", Path: "gno.land/r/moul/toolbox/x/v0", Title: "X"})
81	uassert.True(t, strings.Contains(got, "## X"))
82	uassert.False(t, strings.Contains(got, "Tags:"))
83}
84
85func TestNav(t *testing.T) {
86	uassert.Equal(t, "[toolbox](/r/moul/toolbox) / **Usernames**\n",
87		Nav("/r/moul/toolbox", "Usernames"))
88}