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