d12d6ef8127ee73a16e2ba4c837dcab0b3616ccc
[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 => 43;
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('', '', {disabled => 0, something => undef, class => undef, style => ''}),
123         '<input name="" type="text" value="">',
124         'input with empty attributes'
125 );
126
127 is(
128         $form->input(undef, undef, {name => 'override', value => 'override'}),
129         '<input type="text">',
130         'ignore input overrides'
131 );
132
133 is(
134         $form->input('name', {id => ''}),
135         '<input name="name" type="text">',
136         'input with id override'
137 );
138
139 is(
140         $form->input('<">', '<">', {id => '>"<'}),
141         '<input id=">&quot;&lt;" name="&lt;&quot;>" type="text" value="&lt;&quot;>">',
142         'input quoting'
143 );
144
145 is(
146         $form->input(undef, {disabled => 'something'}),
147         '<input disabled type="text">',
148         'disabled input'
149 );
150
151 is(
152         $form->input({type => 'password'}),
153         '<input type="password">',
154         'password'
155 );
156
157 # textarea
158
159 is(
160         $form->input({rows => 0}),
161         '<textarea rows="0"></textarea>',
162         'minimal textarea'
163 );
164
165 is(
166         $form->input(foo => 'bar', {cols => 42, rows => 1, disabled => 1}),
167         '<textarea disabled cols="42" id="foo" name="foo" rows="1">bar</textarea>',
168         'textarea'
169 );
170
171 is(
172         $form->input(undef, qq{<foo>&bl'a"\n    .}, {cols => undef, rows => '<">'}),
173         qq{<textarea rows="&lt;&quot;>">&lt;foo>&amp;bl'a&quot;\n    .</textarea>},
174         'textarea quoting'
175 );
176
177 # select
178
179 is_deeply(
180         [ $form->select ],
181         [ qw(<select> </select>) ], # malformed html: at least 1 option required
182         'empty select'
183 );
184
185 is_deeply(
186         [ $form->select(undef, [], '', {name => '', class => ''}) ],
187         [ qw(<select> </select>) ],
188         'explicit empty select'
189 );
190
191 is_deeply(
192         [ $form->select(undef, [undef]) ],
193         [ qw(<select> <option> </select>) ],
194         'minimal select'
195 );
196
197 is_deeply(
198         [ $form->select(foo => [1, 2]) ],
199         [
200                 '<select id="foo" name="foo">',
201                 '<option value="1">', '<option value="2">',
202                 '</select>'
203         ],
204         'select contents'
205 );
206
207 is_deeply(
208         [ $form->select(foo => [1, 2], 3) ],
209         [
210                 '<select id="foo" name="foo">',
211                 '<option value="1">', '<option value="2">',
212                 '</select>'
213         ],
214         'select invalid default'
215 );
216
217 is_deeply(
218         [ $form->select(undef, [1, 2], 2) ],
219         [
220                 '<select>',
221                 '<option value="1">', '<option selected value="2">',
222                 '</select>'
223         ],
224         'select default'
225 );
226
227 is_deeply(
228         [
229                 $form->select(foo => [
230                         '<">', {value => 2, disabled => 1, selected => 0, class => 1}, {selected => 1}
231                 ], '2', {id => '', class => 'a <b', value => 1})
232         ],
233         [
234                 '<select class="a &lt;b" name="foo">',
235                 '<option value="&lt;&quot;>">',
236                 '<option selected disabled class="1" value="2">',
237                 '<option selected>',
238                 '</select>'
239         ],
240         'complex select'
241 );
242
243 is(
244         scalar $form->select(foo => [1, 2]),
245         '<select id="foo" name="foo"><option value="1"><option value="2"></select>',
246         'select scalar'
247 );
248
249 # radio
250
251 is_deeply(
252         [ $form->select(foo => [1], {type => 'radio'}) ],
253         [ '<input id="foo_1" name="foo" type="radio" value="1">' ],
254         'input select'
255 );
256
257 is_deeply(
258         [
259                 $form->select(foo => [
260                         1, {value => 2, name => '', label => ''}, {value => 3, id => '', type => ''}
261                 ], {type => 'checkbox', label => {3 => 'test', 2 => 'ignore'}, value => '1'})
262         ],
263         [
264                 '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
265                 '<input id="foo_2" name="" type="checkbox" value="2">',
266                 '<label><input name="foo" value="3"> test</label>',
267         ],
268         'input selects'
269 );
270
271 is(
272         $form->radio(foo => 1),
273         '<input id="foo_1" name="foo" type="radio" value="1">',
274         'radio method'
275 );
276
277 is(
278         $form->radio(undef, {checked => 1}),
279         '<input checked type="radio">',
280         'simple radio button'
281 );
282
283 is(
284         $form->check('foo'),
285         '<input id="foo_1" name="foo" type="checkbox" value="1">',
286         'check method'
287 );
288
289 is(
290         $form->check(foo => 'test', {value => undef}),
291         '<label><input id="foo" name="foo" type="checkbox"> test</label>',
292         'check parameters'
293 );
294
295 is(
296         $form->check(undef, '', 1),
297         '<input checked type="checkbox" value="1">',
298         'simple checkbox'
299 );
300
301 TODO: {
302         local $TODO = 'shorthand';
303 is_deeply(
304         [ $form->check(undef, ['', '<">']) ],
305         [
306                 '<input checked type="checkbox" value="1">',
307                 '<label><input checked type="checkbox" value="2"> &lt;&quot;></label>',
308         ],
309         'multiple checkboxes'
310 );
311 };
312
313 #TODO
314