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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

form_test.gno

2.13 Kb · 69 lines
 1package toolbox
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10func TestQueryString(t *testing.T) {
11	got := NewQuery("lookup").Field("q", "username or g1 address").String()
12	want := "<gno-form path=\"lookup\">\n" +
13		"<gno-input name=\"q\" type=\"text\" placeholder=\"username or g1 address\" />\n" +
14		"</gno-form>\n"
15	uassert.Equal(t, want, got)
16}
17
18func TestQueryNoPathNoPlaceholder(t *testing.T) {
19	got := NewQuery("").Field("q", "").String()
20	want := "<gno-form>\n<gno-input name=\"q\" type=\"text\" />\n</gno-form>\n"
21	uassert.Equal(t, want, got)
22}
23
24func TestQueryTwoFields(t *testing.T) {
25	q := NewQuery("compare").Field("a", "first").Field("b", "second")
26	uassert.Equal(t, 2, q.Len())
27	got := q.String()
28	uassert.True(t, strings.Contains(got, `name="a"`))
29	uassert.True(t, strings.Contains(got, `name="b"`))
30	uassert.Equal(t, 1, strings.Count(got, "<gno-form"))
31}
32
33// TestQueryEmptyRendersNothing: a form with no input is a submit button with
34// nothing to submit, so it is not rendered at all.
35func TestQueryEmptyRendersNothing(t *testing.T) {
36	uassert.Equal(t, "", NewQuery("lookup").String())
37	uassert.Equal(t, "", NewQuery("lookup").Field("", "ignored").String())
38}
39
40// TestQueryEscapesAttributes: gnoweb parses these with a real HTML tokenizer,
41// so an unescaped quote ends the attribute and everything after it becomes
42// further attributes of the same tag.
43func TestQueryEscapesAttributes(t *testing.T) {
44	got := NewQuery(`a"b`).Field("q", `<script>&"`).String()
45	uassert.True(t, strings.Contains(got, `path="a&quot;b"`))
46	uassert.True(t, strings.Contains(got, `placeholder="&lt;script&gt;&amp;&quot;"`))
47	uassert.False(t, strings.Contains(got, "<script>"))
48}
49
50func TestAttr(t *testing.T) {
51	cases := []struct {
52		name string
53		in   string
54		want string
55	}{
56		{"plain", "username", "username"},
57		{"quote", `a"b`, "a&quot;b"},
58		{"ampersand", "a&b", "a&amp;b"},
59		{"angle", "<b>", "&lt;b&gt;"},
60		{"ampersand before quote", `&"`, "&amp;&quot;"},
61		{"already escaped stays literal", "&amp;", "&amp;amp;"},
62		{"empty", "", ""},
63	}
64	for _, tc := range cases {
65		t.Run(tc.name, func(t *testing.T) {
66			uassert.Equal(t, tc.want, attr(tc.in))
67		})
68	}
69}