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