XXX: multiple radio values
[perl/html-form-simple.git] / t / html.t
index 0535da9294a35f8f58143a5bcdbf3c713c04aa59..b7568b079cfa9ea14bfc9d0300c9d5d78009e008 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,7 +5,7 @@ use warnings;
 
 use Test::More;
 
-plan tests => 31;
+plan tests => 47;
 
 use_ok('HTML::Form::Simple');
 
@@ -119,15 +119,22 @@ is(
 );
 
 is(
-       $form->input('', '', {disabled => 0, something => undef}),
+       $form->input('', '', {
+               disabled => 0,
+               something => undef,
+               class => undef,
+               style => '',
+               name => 'ignore',
+               value => 'overrides',
+       }),
        '<input name="" type="text" value="">',
        'input with empty attributes'
 );
 
 is(
-       $form->input(undef, undef, {name => 'override', value => 'override'}),
-       '<input type="text">',
-       'ignore input overrides'
+       $form->input(undef, undef, {name => '0', value => '0'}),
+       '<input id="0" name="0" type="text" value="0">',
+       'input overrides'
 );
 
 is(
@@ -176,29 +183,175 @@ is(
 
 # select
 
-is(
-       $form->select,
-       '<select></select>', # malformed html: at least 1 option required
+is_deeply(
+       [ $form->select ],
+       [ qw(<select> </select>) ], # malformed html: at least 1 option required
        'empty select'
 );
 
-is(
-       $form->select(undef, [], '', {name => ''}),
-       '<select></select>',
+is_deeply(
+       [ $form->select(undef, [], '', {name => '', class => ''}) ],
+       [ qw(<select> </select>) ],
        'explicit empty select'
 );
 
-is(
-       $form->select(undef, [undef]),
-       '<select><option></select>',
+is_deeply(
+       [ $form->select(undef, [undef]) ],
+       [ qw(<select> <option> </select>) ],
        'minimal select'
 );
 
-is(
-       $form->select(foo => [1..2]),
-       '<select name="foo"><option value="1"><option value="2"></select>',
+is_deeply(
+       [ $form->select(foo => [1, 2]) ],
+       [
+               '<select id="foo" name="foo">',
+               '<option value="1">', '<option value="2">',
+               '</select>'
+       ],
        'select contents'
 );
 
+is_deeply(
+       [ $form->select(foo => [1, 2], 3) ],
+       [
+               '<select id="foo" name="foo">',
+               '<option value="1">', '<option value="2">',
+               '</select>'
+       ],
+       'select invalid default'
+);
+
+is_deeply(
+       [ $form->select(undef, [1, 2], 2) ],
+       [
+               '<select>',
+               '<option value="1">', '<option selected value="2">',
+               '</select>'
+       ],
+       'select default'
+);
+
+is_deeply(
+       [
+               $form->select(foo => [
+                       '<">', {value => 2, disabled => 1, selected => 0, class => 1}, {selected => 1}
+               ], '2', {id => '', class => 'a <b', value => 1})
+       ],
+       [
+               '<select class="a &lt;b" name="foo">',
+               '<option value="&lt;&quot;>">',
+               '<option selected disabled class="1" value="2">',
+               '<option selected>',
+               '</select>'
+       ],
+       'complex select'
+);
+
+is(
+       scalar $form->select(foo => [1, 2]),
+       '<select id="foo" name="foo"><option value="1"><option value="2"></select>',
+       'select scalar'
+);
+
+# radio
+
+is_deeply(
+       [ $form->select(foo => [1], {type => 'radio'}) ],
+       [ '<input id="foo_1" name="foo" type="radio" value="1">' ],
+       'input select'
+);
+
+is_deeply(
+       [
+               $form->select(foo => [
+                       1, {value => 2, name => '', label => ''}, {value => 3, id => '', type => ''}
+               ], {type => 'checkbox', label => {3 => 'test', 2 => 'ignore'}, value => '1'})
+       ],
+       [
+               '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
+               '<input id="foo_2" name="" type="checkbox" value="2">',
+               '<label><input name="foo" value="3"> test</label>',
+       ],
+       'input selects'
+);
+
+is(
+       $form->radio(foo => 'test'),
+       '<label><input id="foo_1" name="foo" type="radio" value="1"> test</label>',
+       'radio method'
+);
+
+is(
+       $form->radio(foo => undef, [2]), #TODO
+       '<input id="foo_2" name="foo" type="radio" value="2">',
+       'labelless radio'
+);
+
+is(
+       $form->radio(undef, {checked => 1}),
+       '<input checked type="radio">',
+       'simple radio button'
+);
+
+is_deeply(
+       [ $form->radio(undef, ['', '<">']) ],
+       [
+               '<input type="radio" value="1">',
+               '<label><input type="radio" value="2"> <"></label>',
+       ],
+       'multiple radios'
+);
+
+is_deeply(
+       [ $form->radio(foo => 'test', ['foo', ''], {value => '', id => ''}) ],
+       [
+               '<label><input name="foo" type="radio" value="foo"> test</label>',
+               '<label><input checked name="foo" type="radio" value=""> test</label>',
+       ],
+       'multiple radios with custom value'
+);
+
+is_deeply(
+       [ $form->radio(foo => ['', 0], [0, 1, '']) ],
+       [
+               '<input id="foo_0" name="foo" type="radio" value="0">',
+               '<label><input id="foo_1" name="foo" type="radio" value="1"> 0</label>',
+               '<input id="foo_" name="foo" type="radio" value="">',
+       ],
+       'multiple radios with custom values'
+);
+
+# check
+
+is(
+       $form->check('foo'),
+       '<input id="foo_1" name="foo" type="checkbox" value="1">',
+       'check method'
+);
+
+is(
+       $form->check(foo => 'test', {value => undef}),
+       '<label><input id="foo" name="foo" type="checkbox"> test</label>',
+       'check parameters'
+);
+
+is(
+       $form->check(undef, '', 1),
+       '<input checked type="checkbox" value="1">',
+       'simple checkbox'
+);
+
+TODO: {
+       local $TODO = 'shorthand';
+is_deeply(
+       [ $form->check(undef, ['', '<">']) ],
+       [
+               '<input checked type="checkbox" value="1">',
+               '<label><input checked type="checkbox" value="2"> &lt;&quot;></label>',
+       ],
+       'multiple checkboxes'
+);
+};
+
 #TODO