10 use_ok('HTML::Form::Simple');
12 my $form = HTML::Form::Simple->new;
13 ok($form, 'new form');
18 $form->quote('<"test">'),
19 '<"test">',
24 $form->quote(\'<"test">'),
36 $form->tag('a "' => {'b"' => 'c"'}),
50 $form->start({method => 'get', action => '', ignore => undef}),
51 '<form action="" method="get">',
52 'start with attributes'
56 eval { $form->start('should be a hashref') },
71 '<input type="hidden">',
76 $form->hidden(foo => 'bar'),
77 '<input name="foo" type="hidden" value="bar">',
82 $form->hidden(undef, undef, {value => 'bar', name => 'foo', id => 'foo'}),
83 '<input id="foo" name="foo" type="hidden" value="bar">',
88 [ $form->hidden(foo => [1, 0], {class => 'test'}) ],
90 '<input class="test" name="foo" type="hidden" value="1">',
91 '<input class="test" name="foo" type="hidden" value="0">',
97 [ $form->hidden({2 => 0, 10 => 1}, {class => 'test'}) ],
99 '<input class="test" name="10" type="hidden" value="1">',
100 '<input class="test" name="2" type="hidden" value="0">',
108 scalar $form->hidden({2 => [1, 0], 10 => 2}, {class => 'test'}),
110 '<input class="test" name="10" type="hidden" value="2">',
111 '<input class="test" name="2" type="hidden" value="1">',
112 '<input class="test" name="2" type="hidden" value="0">',
122 '<input type="submit">',
127 $form->submit('<OK>'),
128 '<input type="submit" value="<OK>">',
133 $form->submit({disabled => 1, id => 'test', type => 'button'}),
134 '<input disabled id="test" type="button">',
139 $form->submit('<OK>', {type => '', value => 'override', id => ''}),
140 '<input value="<OK>">',
148 '<input type="text">',
153 $form->text(undef, undef, undef),
154 '<input type="text">',
155 'explicit empty input'
160 '<input id="test" name="test" type="text">',
165 $form->text(undef, 'test'),
166 '<input type="text" value="test">',
171 $form->text(undef, {value => 'test'}),
172 '<input type="text" value="test">',
173 'input with attribute value'
177 $form->text({name => 'test', value => ''}),
178 '<input id="test" name="test" type="text" value="">',
179 'input with only attributes'
183 $form->text('', '', {
189 value => 'overrides',
191 '<input name="" type="text" value="">',
192 'input with empty attributes'
196 $form->text(undef, undef, {name => '0', value => '0'}),
197 '<input id="0" name="0" type="text" value="0">',
202 $form->text('name', {id => ''}),
203 '<input name="name" type="text">',
204 'input with id override'
208 $form->text('<">', '<">', {id => '>"<'}),
209 '<input id=">"<" name="<">" type="text" value="<">">',
214 $form->text(undef, {disabled => 'something'}),
215 '<input disabled type="text">',
220 $form->text({type => 'password'}),
221 '<input type="password">',
228 $form->text({rows => 0}),
229 '<textarea rows="0"></textarea>',
234 $form->text(foo => 'bar', {cols => 42, rows => 1, disabled => 1}),
235 '<textarea disabled cols="42" id="foo" name="foo" rows="1">bar</textarea>',
240 $form->text(undef, qq{<foo>&bl'a"\n .}, {cols => undef, rows => '<">'}),
241 qq{<textarea rows="<">"><foo>&bl'a"\n .</textarea>},
249 [ qw(<select> </select>) ], # malformed html: at least 1 option required
254 [ $form->select(undef, [], '', {name => '', class => ''}) ],
255 [ qw(<select> </select>) ],
256 'explicit empty select'
260 [ $form->select(undef, [undef]) ],
261 [ qw(<select> <option> </select>) ],
266 [ $form->select(foo => [1, 2]) ],
268 '<select id="foo" name="foo">',
269 '<option value="1">', '<option value="2">',
276 [ $form->select(foo => [1, 2], 3) ],
278 '<select id="foo" name="foo">',
279 '<option value="1">', '<option value="2">',
282 'select invalid default'
286 [ $form->select(undef, [1, 2], 2) ],
289 '<option value="1">', '<option selected value="2">',
297 $form->select(foo => [
298 '<">', {value => 2, disabled => 1, selected => 0, class => 1}, {selected => 1}
299 ], '2', {id => '', class => 'a <b', value => 1})
302 '<select class="a <b" name="foo">',
303 '<option value="<">">',
304 '<option selected disabled class="1" value="2">',
312 scalar $form->select(foo => [1, 2]),
313 '<select id="foo" name="foo"><option value="1"><option value="2"></select>',
320 [ $form->select(foo => [1], {type => 'radio'}) ],
321 [ '<input id="foo_1" name="foo" type="radio" value="1">' ],
327 $form->select(foo => [
328 1, {value => 2, name => '', label => ''}, {value => 3, id => '', type => ''}
329 ], {type => 'checkbox', label => {3 => 'test', 2 => 'ignore'}, value => '1'})
332 '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
333 '<input id="foo_2" name="" type="checkbox" value="2">',
334 '<label><input name="foo" value="3"> test</label>',
340 $form->radio(foo => 'test'),
341 '<input id="foo_test" name="foo" type="radio" value="test">',
346 $form->radio(foo => undef, 'test'),
347 '<label><input id="foo_1" name="foo" type="radio" value="1"> test</label>',
352 $form->radio(undef, {checked => 1}),
353 '<input checked type="radio">',
354 'simple radio button'
358 [ $form->radio(undef, ['', '<">']) ],
360 '<input type="radio" value="">',
361 '<input type="radio" value="<">">',
367 [ $form->radio(undef, undef, ['', '<">']) ],
369 '<input type="radio" value="1">',
370 '<label><input type="radio" value="2"> <"></label>',
372 'multiple radio labels'
376 [ $form->radio(undef, [0, 1], undef, 0) ],
378 '<input checked type="radio" value="0">',
379 '<input type="radio" value="1">',
385 [ $form->radio(foo => ['foo', ''], 'test', {value => '', id => ''}) ],
387 '<label><input name="foo" type="radio" value="foo"> test</label>',
388 '<label><input checked name="foo" type="radio" value=""> test</label>',
390 'multiple radios with custom value'
394 [ $form->radio(foo => [0, 1, ''], ['', 0]) ],
396 '<input id="foo_0" name="foo" type="radio" value="0">',
397 '<label><input id="foo_1" name="foo" type="radio" value="1"> 0</label>',
398 '<input id="foo_" name="foo" type="radio" value="">',
400 'multiple radios with custom values'
404 # make sure arguments aren't modified
405 my @args = (foo => [0, {value => 1}], [0, 1], {name => 0, value => 1});
406 my @orgs = (foo => [0, {value => 1}], [0, 1], {name => 0, value => 1});
408 '<label><input id="foo_0" name="foo" type="radio" value="0"> 0</label>',
409 '<label><input checked id="foo_1" name="foo" type="radio" value="1"> 1</label>',
413 [ $form->radio(@args) ],
415 'options var to radio'
419 [ $form->check(@args) ],
421 '<label><input checked id="foo_1" name="foo" type="checkbox" value="1"> 0</label>',
422 '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
424 'options var to check'
428 scalar $form->radio(@args),
430 'options var again to radio'
433 is_deeply(\@args, \@orgs, 'options var unmodified');
440 '<input id="foo" name="foo" type="checkbox" value="1">',
445 $form->check(foo => '<">'),
446 '<label><input id="foo" name="foo" type="checkbox" value="1"> <"></label>',
451 $form->check(foo => {label => 'test', value => undef}, {disabled => 1}),
452 '<label><input disabled id="foo" name="foo" type="checkbox"> test</label>',
457 [ $form->check(undef, '', 1) ],
458 [ '<input checked type="checkbox" value="1">' ],
463 $form->check(foo => [undef]),
464 '<input id="foo_1" name="foo" type="checkbox" value="1">',
469 [ $form->check(foo => [{value => undef}, undef]) ],
471 '<input id="foo" name="foo" type="checkbox">',
472 '<input id="foo_2" name="foo" type="checkbox" value="2">',
474 'multiple checkboxes'
478 $form->check(foo => [undef], {id => ''}),
479 '<input name="foo" type="checkbox" value="1">',
485 foo => [ {id => 'quux'}, {name => 'name'}, {value => 0}, {id => ''}, undef ], {id => 'bar'}
488 '<input id="quux" name="foo" type="checkbox" value="1">',
489 '<input id="bar_2" name="name" type="checkbox" value="2">',
490 '<input id="bar_0" name="foo" type="checkbox" value="0">',
491 '<input name="foo" type="checkbox" value="4">',
492 '<input id="bar_5" name="foo" type="checkbox" value="5">',
498 [ $form->check(foo => ['bar', {name => 'bar'}], {name => 'ignored'}) ],
500 '<label><input id="foo_1" name="foo" type="checkbox" value="1"> bar</label>',
501 '<input id="foo_2" name="bar" type="checkbox" value="2">',
507 [ $form->check(undef, ['', '<">']) ],
509 '<input type="checkbox" value="1">',
510 '<label><input type="checkbox" value="2"> <"></label>',
512 'anonymous checkboxes'
516 [ $form->check(undef, [{}, undef], 1) ],
518 '<input checked type="checkbox" value="1">',
519 '<input checked type="checkbox" value="2">',
521 'multiple checked checkboxes'
527 scalar $form->check(foo => [1, 0], {value => 0}),
529 '<label><input id="foo_1" name="foo" type="checkbox" value="1"> 1</label>',
530 '<label><input id="foo_2" name="foo" type="checkbox" value="2"> 0</label>',
537 [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ],
539 '<input checked type="radio" value="4">',
540 '<input type="checkbox" value="2">',
541 '<input checked type="checkbox" value="3">',
548 my $defform = HTML::Form::Simple->new({foo => '<">', '' => 'empty', 0 => 0});
549 ok($defform, 'form with defaults');
552 $defform->hidden(''),
553 '<input name="" type="hidden" value="empty">',
554 'hidden with default'
558 $defform->hidden(undef),
559 '<input type="hidden">',
564 $defform->text('foo'),
565 '<input id="foo" name="foo" type="text" value="<">">',
570 $defform->text('foo', {value => 'custom'}),
571 '<input id="foo" name="foo" type="text" value="custom">',
572 'input with value and default'
576 [ $defform->radio(0 => [1, 0]) ],
578 '<input id="0_1" name="0" type="radio" value="1">',
579 '<input checked id="0_0" name="0" type="radio" value="0">',
581 'select with default'