-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui_test.go
175 lines (160 loc) · 4.29 KB
/
ui_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package jaws
import (
"bytes"
"errors"
"html/template"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/linkdata/deadlock"
)
type testStringer struct{}
func (s testStringer) String() string {
return "I_Am_A_testStringer"
}
func TestRequest_NewElement_DebugPanicsIfNotComparable(t *testing.T) {
notHashableUI := struct {
*UiSpan
x map[int]int
}{
UiSpan: NewUiSpan(testHtmlGetter("foo")),
x: map[int]int{},
}
if newErrNotComparable(notHashableUI) == nil {
t.FailNow()
}
if !deadlock.Debug {
return
}
defer func() {
if x := recover(); x != nil {
if err, ok := x.(error); ok {
if !errors.Is(err, ErrNotComparable) {
t.Errorf("%T", err)
}
return
}
}
t.Fail()
}()
nextJid = 0
rq := newTestRequest()
defer rq.Close()
rq.NewElement(notHashableUI)
t.Fail()
}
func TestRequest_JawsRender_DebugOutput(t *testing.T) {
is := newTestHelper(t)
rq := newTestRequest()
defer rq.Close()
rq.Jaws.Debug = true
rq.UI(&testUi{renderFn: func(e *Element, w io.Writer, params []any) error {
e.Tag(Tag("footag"))
e.Tag(e.Request)
e.Tag(testStringer{})
return nil
}})
h := rq.BodyString()
t.Log(h)
if !strings.Contains(h, "tags=[n/a]") {
is.True(strings.Contains(h, "footag"))
is.True(strings.Contains(h, "*jaws.testUi"))
is.True(strings.Contains(h, testStringer{}.String()))
}
}
func TestRequest_InsideTemplate(t *testing.T) {
jw := New()
defer jw.Close()
nextJid = 4
const tmplText = "(" +
"{{$.Initial.URL.Path}}" +
"{{$.A `a`}}" +
"{{$.Button `button`}}" +
"{{$.Checkbox .TheBool `checkbox`}}" +
"{{$.Container `container` .TheContainer}}" +
"{{$.Date .TheTime `date`}}" +
"{{$.Div `div`}}" +
"{{$.Img `img`}}" +
"{{$.Label `label`}}" +
"{{$.Li `li`}}" +
"{{$.Number .TheNumber}}" +
"{{$.Password .TheString}}" +
"{{$.Radio .TheBool}}" +
"{{$.Range .TheNumber}}" +
"{{$.Select .TheSelector}}" +
"{{$.Span `span`}}" +
"{{$.Tbody .TheContainer}}" +
"{{$.Td `td`}}" +
"{{$.Template `nested` .TheDot}}" +
"{{$.Text .TheString}}" +
"{{$.Textarea .TheString}}" +
"{{$.Tr `tr`}}" +
")"
const nestedTmplText = "<x id=\"{{$.Jid}}\">" +
"{{$.Initial.URL.Path}}" +
"{{with .Dot}}{{.}}{{$.Span `span2`}}{{end}}" +
"</x>"
const want = "(" +
"/path" +
"<a id=\"Jid.5\">a</a>" +
"<button id=\"Jid.6\" type=\"button\">button</button>" +
"<input id=\"Jid.7\" type=\"checkbox\" checkbox checked>" +
"<container id=\"Jid.8\"></container>" +
"<input id=\"Jid.9\" type=\"date\" value=\"1901-02-03\" date>" +
"<div id=\"Jid.10\">div</div>" +
"<img id=\"Jid.11\" src=\"img\">" +
"<label id=\"Jid.12\">label</label>" +
"<li id=\"Jid.13\">li</li>" +
"<input id=\"Jid.14\" type=\"number\" value=\"1.2\">" +
"<input id=\"Jid.15\" type=\"password\" value=\"bar\">" +
"<input id=\"Jid.16\" type=\"radio\" checked>" +
"<input id=\"Jid.17\" type=\"range\" value=\"1.2\">" +
"<select id=\"Jid.18\"></select>" +
"<span id=\"Jid.19\">span</span>" +
"<tbody id=\"Jid.20\"></tbody>" +
"<td id=\"Jid.21\">td</td>" +
"<x id=\"Jid.22\">/pathdot<span id=\"Jid.23\">span2</span></x>" +
"<input id=\"Jid.24\" type=\"text\" value=\"bar\">" +
"<textarea id=\"Jid.25\">bar</textarea>" +
"<tr id=\"Jid.26\">tr</tr>" +
")"
jw.AddTemplateLookuper(template.Must(template.New("nested").Parse(nestedTmplText)))
tmpl := template.Must(template.New("normal").Parse(tmplText))
w := httptest.NewRecorder()
w.Body = &bytes.Buffer{}
hr := httptest.NewRequest(http.MethodGet, "/path", nil)
rq := jw.NewRequest(hr)
testDate, _ := time.Parse(ISO8601, "1901-02-03")
dot := struct {
RequestWriter
TheBool BoolSetter
TheContainer Container
TheTime TimeSetter
TheNumber FloatSetter
TheString StringSetter
TheSelector SelectHandler
TheDot any
}{
RequestWriter: RequestWriter{rq, w},
TheBool: newTestSetter(true),
TheContainer: &testContainer{},
TheTime: newTestSetter(testDate),
TheNumber: newTestSetter(float64(1.2)),
TheString: newTestSetter("bar"),
TheSelector: &testNamedBoolArray{
setCalled: make(chan struct{}),
NamedBoolArray: NewNamedBoolArray(),
},
TheDot: "dot",
}
if err := tmpl.Execute(w, dot); err != nil {
t.Fatal(err)
}
w.Flush()
if x := w.Body.String(); x != want {
t.Errorf("mismatch:\nwant %q\n got %q", want, x)
}
}