join multiple select results by $OFS
[perl/html-form-simple.git] / t / html.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 plan tests => 52;
9
10 use_ok('HTML::Form::Simple');
11
12 my $form = HTML::Form::Simple->new;
13 ok($form, 'new form');
14
15 # form
16
17 is(
18         $form->start,
19         '<form>',
20         'empty start'
21 );
22
23 is(
24         $form->start({method => 'get', action => '', ignore => undef}),
25         '<form action="" method="get">',
26         'start with attributes'
27 );
28
29 is(
30         eval { $form->start('should be a hashref') },
31         undef,
32         'invalid attributes'
33 );
34
35 is(
36         $form->stop,
37         '</form>',
38         'stop'
39 );
40
41 # hidden
42
43 is(
44         $form->hidden,
45         '<input type="hidden">',
46         'empty hidden'
47 );
48
49 is(
50         $form->hidden(foo => 'bar'),
51         '<input name="foo" type="hidden" value="bar">',
52         'hidden'
53 );
54
55 is(
56         $form->hidden(undef, undef, {value => 'bar', name => 'foo', id => 'foo'}),
57         '<input id="foo" name="foo" type="hidden" value="bar">',
58         'hidden options'
59 );
60
61 # submit
62
63 is(
64         $form->submit,
65         '<input type="submit">',
66         'empty submit'
67 );
68
69 is(
70         $form->submit('<OK>'),
71         '<input type="submit" value="&lt;OK>">',
72         'submit value'
73 );
74
75 is(
76         $form->submit({disabled => 1, id => 'test', type => 'button'}),
77         '<input disabled id="test" type="button">',
78         'submit attributes'
79 );
80
81 is(
82         $form->submit('<OK>', {type => '', value => 'override', id => ''}),
83         '<input value="&lt;OK>">',
84         'submit overrides'
85 );
86
87 # input
88
89 is(
90         $form->input,
91         '<input type="text">',
92         'empty input'
93 );
94
95 is(
96         $form->input(undef, undef, undef),
97         '<input type="text">',
98         'explicit empty input'
99 );
100
101 is(
102         $form->input('test'),
103         '<input id="test" name="test" type="text">',
104         'input with name'
105 );
106
107 is(
108         $form->input(undef, 'test'),
109         '<input type="text" value="test">',
110         'input with value'
111 );
112
113 is(
114         $form->input(undef, {value => 'test'}),
115         '<input type="text" value="test">',
116         'input with attribute value'
117 );
118
119 is(
120         $form->input({name => 'test', value => ''}),
121         '<input id="test" name="test" type="text" value="">',
122         'input with only attributes'
123 );
124
125 is(
126         $form->input('', '', {
127                 disabled => 0,
128                 something => undef,
129                 class => undef,
130                 style => '',
131                 name => 'ignore',
132                 value => 'overrides',
133         }),
134         '<input name="" type="text" value="">',
135         'input with empty attributes'
136 );
137
138 is(
139         $form->input(undef, undef, {name => '0', value => '0'}),
140         '<input id="0" name="0" type="text" value="0">',
141         'input overrides'
142 );
143
144 is(
145         $form->input('name', {id => ''}),
146         '<input name="name" type="text">',
147         'input with id override'
148 );
149
150 is(
151         $form->input('<">', '<">', {id => '>"<'}),
152         '<input id=">&quot;&lt;" name="&lt;&quot;>" type="text" value="&lt;&quot;>">',
153         'input quoting'
154 );
155
156 is(
157         $form->input(undef, {disabled => 'something'}),
158         '<input disabled type="text">',
159         'disabled input'
160 );
161
162 is(
163         $form->input({type => 'password'}),
164         '<input type="password">',
165         'password'
166 );
167
168 # textarea
169
170 is(
171         $form->input({rows => 0}),
172         '<textarea rows="0"></textarea>',
173         'minimal textarea'
174 );
175
176 is(
177         $form->input(foo => 'bar', {cols => 42, rows => 1, disabled => 1}),
178         '<textarea disabled cols="42" id="foo" name="foo" rows="1">bar</textarea>',
179         'textarea'
180 );
181
182 is(
183         $form->input(undef, qq{<foo>&bl'a"\n    .}, {cols => undef, rows => '<">'}),
184         qq{<textarea rows="&lt;&quot;>">&lt;foo>&amp;bl'a&quot;\n    .</textarea>},
185         'textarea quoting'
186 );
187
188 # select
189
190 is_deeply(
191         [ $form->select ],
192         [ qw(<select> </select>) ], # malformed html: at least 1 option required
193         'empty select'
194 );
195
196 is_deeply(
197         [ $form->select(undef, [], '', {name => '', class => ''}) ],
198         [ qw(<select> </select>) ],
199         'explicit empty select'
200 );
201
202 is_deeply(
203         [ $form->select(undef, [undef]) ],
204         [ qw(<select> <option> </select>) ],
205         'minimal select'
206 );
207
208 is_deeply(
209         [ $form->select(foo => [1, 2]) ],
210         [
211                 '<select id="foo" name="foo">',
212                 '<option value="1">', '<option value="2">',
213                 '</select>'
214         ],
215         'select contents'
216 );
217
218 is_deeply(
219         [ $form->select(foo => [1, 2], 3) ],
220         [
221                 '<select id="foo" name="foo">',
222                 '<option value="1">', '<option value="2">',
223                 '</select>'
224         ],
225         'select invalid default'
226 );
227
228 is_deeply(
229         [ $form->select(undef, [1, 2], 2) ],
230         [
231                 '<select>',
232                 '<option value="1">', '<option selected value="2">',
233                 '</select>'
234         ],
235         'select default'
236 );
237
238 is_deeply(
239         [
240                 $form->select(foo => [
241                         '<">', {value => 2, disabled => 1, selected => 0, class => 1}, {selected => 1}
242                 ], '2', {id => '', class => 'a <b', value => 1})
243         ],
244         [
245                 '<select class="a &lt;b" name="foo">',
246                 '<option value="&lt;&quot;>">',
247                 '<option selected disabled class="1" value="2">',
248                 '<option selected>',
249                 '</select>'
250         ],
251         'complex select'
252 );
253
254 is(
255         scalar $form->select(foo => [1, 2]),
256         '<select id="foo" name="foo"><option value="1"><option value="2"></select>',
257         'select scalar'
258 );
259
260 # radio
261
262 is_deeply(
263         [ $form->select(foo => [1], {type => 'radio'}) ],
264         [ '<input id="foo_1" name="foo" type="radio" value="1">' ],
265         'input select'
266 );
267
268 is_deeply(
269         [
270                 $form->select(foo => [
271                         1, {value => 2, name => '', label => ''}, {value => 3, id => '', type => ''}
272                 ], {type => 'checkbox', label => {3 => 'test', 2 => 'ignore'}, value => '1'})
273         ],
274         [
275                 '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
276                 '<input id="foo_2" name="" type="checkbox" value="2">',
277                 '<label><input name="foo" value="3"> test</label>',
278         ],
279         'input selects'
280 );
281
282 is(
283         $form->radio(foo => 'test'),
284         '<label><input id="foo_1" name="foo" type="radio" value="1"> test</label>',
285         'radio method'
286 );
287
288 is(
289         $form->radio(foo => undef, 2),
290         '<input id="foo_2" name="foo" type="radio" value="2">',
291         'labelless radio'
292 );
293
294 is(
295         $form->radio(undef, {checked => 1}),
296         '<input checked type="radio">',
297         'simple radio button'
298 );
299
300 is_deeply(
301         [ $form->radio(undef, ['', '<">']) ],
302         [
303                 '<input type="radio" value="1">',
304                 '<label><input type="radio" value="2"> <"></label>',
305         ],
306         'multiple radios'
307 );
308
309 is_deeply(
310         [ $form->radio(foo => 'test', ['foo', ''], {value => '', id => ''}) ],
311         [
312                 '<label><input name="foo" type="radio" value="foo"> test</label>',
313                 '<label><input checked name="foo" type="radio" value=""> test</label>',
314         ],
315         'multiple radios with custom value'
316 );
317
318 is_deeply(
319         [ $form->radio(foo => ['', 0], [0, 1, '']) ],
320         [
321                 '<input id="foo_0" name="foo" type="radio" value="0">',
322                 '<label><input id="foo_1" name="foo" type="radio" value="1"> 0</label>',
323                 '<input id="foo_" name="foo" type="radio" value="">',
324         ],
325         'multiple radios with custom values'
326 );
327
328 # check
329
330 is(
331         $form->check('foo'),
332         '<input id="foo_1" name="foo" type="checkbox" value="1">',
333         'check method'
334 );
335
336 is(
337         $form->check(foo => '<">'),
338         '<label><input id="foo_1" name="foo" type="checkbox" value="1"> <"></label>',
339         'labeled checkbox'
340 );
341
342 is(
343         $form->check(foo => {label => 'test', value => undef}, {disabled => 1}),
344         '<label><input disabled id="foo" name="foo" type="checkbox"> test</label>',
345         'check parameters'
346 );
347
348 is_deeply(
349         [ $form->check(undef, '', 1) ],
350         [ '<input checked type="checkbox" value="1">' ],
351         'anonymous checkbox'
352 );
353
354 is_deeply(
355         [ $form->check(undef, ['', '<">']) ],
356         [
357                 '<input type="checkbox" value="1">',
358                 '<label><input type="checkbox" value="2"> <"></label>',
359         ],
360         'multiple checkboxes'
361 );
362
363 is_deeply(
364         [ $form->check(undef, [{}, undef], 1) ],
365         [
366                 '<input checked type="checkbox" value="1">',
367                 '<input checked type="checkbox" value="2">',
368         ],
369         'multiple checked checkboxes'
370 );
371
372 {
373         local $, = ' | ';
374         is(
375                 scalar $form->check(foo => [1, 0], {value => 0}),
376                 join(' | ',
377                         '<label><input id="foo_1" name="foo" type="checkbox" value="1"> 1</label>',
378                         '<label><input id="foo_2" name="foo" type="checkbox" value="2"> 0</label>',
379                 ),
380                 'merged checkboxes'
381         );
382 }
383
384 is_deeply(
385         [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ],
386         [
387                 '<input checked type="radio" value="4">',
388                 '<input type="checkbox" value="2">',
389                 '<input checked type="checkbox" value="3">',
390         ],
391         'various checkboxes'
392 );
393
394 #TODO
395