mdform_test.gno
9.69 Kb · 398 lines
1package mdform_test
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/jeronimoalbi/mdform"
8 "gno.land/p/nt/uassert/v0"
9 "gno.land/p/nt/urequire/v0"
10)
11
12// cur is a zero-value realm used as a placeholder when forwarding to
13// uassert/urequire dispatch helpers that gained an `rlm realm` param.
14// These tests pass `func()` callbacks (no crossing inside the callback),
15// so rlm is ignored — a nil realm here is safe.
16var cur realm
17
18func TestNew(cur realm, t *testing.T) {
19 tests := []struct {
20 name string
21 attrs []string
22 markdown string
23 err string
24 }{
25 {
26 name: "ok",
27 attrs: []string{"exec", "FunctionName"},
28 markdown: `<gno-form exec="FunctionName"></gno-form>`,
29 },
30 {
31 name: "no attributes",
32 markdown: `<gno-form></gno-form>`,
33 },
34 {
35 name: "uneven attributes",
36 attrs: []string{"exec"},
37 err: "expected an even number of attribute arguments",
38 },
39 {
40 name: "invalid attribute",
41 attrs: []string{"foo", ""},
42 err: "invalid attribute: foo",
43 },
44 }
45
46 for _, tc := range tests {
47 t.Run(tc.name, func(t *testing.T) {
48 var markdown string
49 fn := func() {
50 f := mdform.New(tc.attrs...)
51 markdown = strings.ReplaceAll(f.String(), "\n", "")
52 }
53
54 if tc.err != "" {
55 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form to fail")
56 return
57 }
58
59 urequire.NotPanics(t, cur, fn, "expect form to be created")
60 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
61 })
62 }
63}
64
65func TestFormInput(cur realm, t *testing.T) {
66 tests := []struct {
67 name string
68 inputName string
69 attrs []string
70 markdown string
71 err string
72 }{
73 {
74 name: "ok",
75 inputName: "test",
76 attrs: []string{"value", "foo"},
77 markdown: `<gno-form><gno-input name="test" value="foo" /></gno-form>`,
78 },
79 {
80 name: "no attributes",
81 inputName: "test",
82 markdown: `<gno-form><gno-input name="test" /></gno-form>`,
83 },
84 {
85 name: "empty name",
86 inputName: " ",
87 err: "form input name is required",
88 },
89 {
90 name: "radio type",
91 inputName: "test",
92 attrs: []string{"type", "radio"},
93 err: "use form.Radio() to create inputs of type radio",
94 },
95 {
96 name: "checkbox type",
97 inputName: "test",
98 attrs: []string{"type", "checkbox"},
99 err: "use form.Checkbox() to create inputs of type checkbox",
100 },
101 {
102 name: "uneven attributes",
103 inputName: "test",
104 attrs: []string{"value"},
105 err: "expected an even number of attribute arguments",
106 },
107 {
108 name: "invalid attribute",
109 inputName: "test",
110 attrs: []string{"foo", ""},
111 err: "invalid attribute: foo",
112 },
113 }
114
115 for _, tc := range tests {
116 t.Run(tc.name, func(t *testing.T) {
117 var markdown string
118 fn := func() {
119 f := mdform.New()
120 f.Input(tc.inputName, tc.attrs...)
121 markdown = strings.ReplaceAll(f.String(), "\n", "")
122 }
123
124 if tc.err != "" {
125 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form input to fail")
126 return
127 }
128
129 urequire.NotPanics(t, cur, fn, "expect form input to be created")
130 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
131 })
132 }
133}
134
135func TestFormRadio(cur realm, t *testing.T) {
136 tests := []struct {
137 name string
138 inputName string
139 value string
140 attrs []string
141 markdown string
142 err string
143 }{
144 {
145 name: "ok",
146 inputName: "test",
147 value: "foo",
148 attrs: []string{"readonly", "true"},
149 markdown: `<gno-form><gno-input type="radio" name="test" value="foo" readonly="true" /></gno-form>`,
150 },
151 {
152 name: "no attributes",
153 inputName: "test",
154 value: "foo",
155 markdown: `<gno-form><gno-input type="radio" name="test" value="foo" /></gno-form>`,
156 },
157 {
158 name: "empty name",
159 inputName: " ",
160 err: "form radio input name is required",
161 },
162 {
163 name: "ignore type attribute",
164 inputName: "test",
165 attrs: []string{"type", "text"},
166 markdown: `<gno-form><gno-input type="radio" name="test" value="" /></gno-form>`,
167 },
168 {
169 name: "ignore value attribute",
170 inputName: "test",
171 attrs: []string{"value", "foo"},
172 markdown: `<gno-form><gno-input type="radio" name="test" value="" /></gno-form>`,
173 },
174 {
175 name: "uneven attributes",
176 inputName: "test",
177 attrs: []string{"readonly"},
178 err: "expected an even number of attribute arguments",
179 },
180 {
181 name: "invalid attribute",
182 inputName: "test",
183 attrs: []string{"foo", ""},
184 err: "invalid attribute: foo",
185 },
186 }
187
188 for _, tc := range tests {
189 t.Run(tc.name, func(t *testing.T) {
190 var markdown string
191 fn := func() {
192 f := mdform.New()
193 f.Radio(tc.inputName, tc.value, tc.attrs...)
194 markdown = strings.ReplaceAll(f.String(), "\n", "")
195 }
196
197 if tc.err != "" {
198 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form radio input to fail")
199 return
200 }
201
202 urequire.NotPanics(t, cur, fn, "expect form radio input to be created")
203 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
204 })
205 }
206}
207
208func TestFormCheckbox(cur realm, t *testing.T) {
209 tests := []struct {
210 name string
211 inputName string
212 value string
213 attrs []string
214 markdown string
215 err string
216 }{
217 {
218 name: "ok",
219 inputName: "test",
220 value: "foo",
221 attrs: []string{"readonly", "true"},
222 markdown: `<gno-form><gno-input type="checkbox" name="test" value="foo" readonly="true" /></gno-form>`,
223 },
224 {
225 name: "no attributes",
226 inputName: "test",
227 value: "foo",
228 markdown: `<gno-form><gno-input type="checkbox" name="test" value="foo" /></gno-form>`,
229 },
230 {
231 name: "empty name",
232 inputName: " ",
233 err: "form checkbox input name is required",
234 },
235 {
236 name: "ignore type attribute",
237 inputName: "test",
238 attrs: []string{"type", "text"},
239 markdown: `<gno-form><gno-input type="checkbox" name="test" value="" /></gno-form>`,
240 },
241 {
242 name: "ignore value attribute",
243 inputName: "test",
244 attrs: []string{"value", "foo"},
245 markdown: `<gno-form><gno-input type="checkbox" name="test" value="" /></gno-form>`,
246 },
247 {
248 name: "uneven attributes",
249 inputName: "test",
250 attrs: []string{"readonly"},
251 err: "expected an even number of attribute arguments",
252 },
253 {
254 name: "invalid attribute",
255 inputName: "test",
256 attrs: []string{"foo", ""},
257 err: "invalid attribute: foo",
258 },
259 }
260
261 for _, tc := range tests {
262 t.Run(tc.name, func(t *testing.T) {
263 var markdown string
264 fn := func() {
265 f := mdform.New()
266 f.Checkbox(tc.inputName, tc.value, tc.attrs...)
267 markdown = strings.ReplaceAll(f.String(), "\n", "")
268 }
269
270 if tc.err != "" {
271 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form checkbox input to fail")
272 return
273 }
274
275 urequire.NotPanics(t, cur, fn, "expect form checkbox input to be created")
276 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
277 })
278 }
279}
280
281func TestFormTextarea(cur realm, t *testing.T) {
282 tests := []struct {
283 name string
284 inputName string
285 attrs []string
286 markdown string
287 err string
288 }{
289 {
290 name: "ok",
291 inputName: "test",
292 attrs: []string{"value", "foo"},
293 markdown: `<gno-form><gno-textarea name="test" value="foo" /></gno-form>`,
294 },
295 {
296 name: "no attributes",
297 inputName: "test",
298 markdown: `<gno-form><gno-textarea name="test" /></gno-form>`,
299 },
300 {
301 name: "empty name",
302 inputName: " ",
303 err: "form textarea name is required",
304 },
305 {
306 name: "uneven attributes",
307 inputName: "test",
308 attrs: []string{"value"},
309 err: "expected an even number of attribute arguments",
310 },
311 {
312 name: "invalid attribute",
313 inputName: "test",
314 attrs: []string{"foo", ""},
315 err: "invalid attribute: foo",
316 },
317 }
318
319 for _, tc := range tests {
320 t.Run(tc.name, func(t *testing.T) {
321 var markdown string
322 fn := func() {
323 f := mdform.New()
324 f.Textarea(tc.inputName, tc.attrs...)
325 markdown = strings.ReplaceAll(f.String(), "\n", "")
326 }
327
328 if tc.err != "" {
329 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form textarea to fail")
330 return
331 }
332
333 urequire.NotPanics(t, cur, fn, "expect form textarea to be created")
334 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
335 })
336 }
337}
338
339func TestFormSelect(cur realm, t *testing.T) {
340 tests := []struct {
341 name string
342 inputName string
343 value string
344 attrs []string
345 markdown string
346 err string
347 }{
348 {
349 name: "ok",
350 inputName: "test",
351 value: "foo",
352 attrs: []string{"readonly", "true"},
353 markdown: `<gno-form><gno-select name="test" value="foo" readonly="true" /></gno-form>`,
354 },
355 {
356 name: "no attributes",
357 inputName: "test",
358 value: "foo",
359 markdown: `<gno-form><gno-select name="test" value="foo" /></gno-form>`,
360 },
361 {
362 name: "empty name",
363 inputName: " ",
364 err: "form select name is required",
365 },
366 {
367 name: "uneven attributes",
368 inputName: "test",
369 attrs: []string{"value"},
370 err: "expected an even number of attribute arguments",
371 },
372 {
373 name: "invalid attribute",
374 inputName: "test",
375 attrs: []string{"foo", ""},
376 err: "invalid attribute: foo",
377 },
378 }
379
380 for _, tc := range tests {
381 t.Run(tc.name, func(t *testing.T) {
382 var markdown string
383 fn := func() {
384 f := mdform.New()
385 f.Select(tc.inputName, tc.value, tc.attrs...)
386 markdown = strings.ReplaceAll(f.String(), "\n", "")
387 }
388
389 if tc.err != "" {
390 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form textarea to fail")
391 return
392 }
393
394 urequire.NotPanics(t, cur, fn, "expect form textarea to be created")
395 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")
396 })
397 }
398}