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