XXX: check() id-related improvements + tests
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 3 Sep 2008 23:15:08 +0000 (23:15 +0000)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 15 Oct 2008 17:30:35 +0000 (17:30 +0000)
lib/HTML/Form/Simple.pm
t/html.t

index 58e8753..f96149f 100644 (file)
@@ -213,6 +213,8 @@ sub check {
                }
        }
        exists $rows->[$_]->{value} or $rows->[$_]->{value} = $_ + 1 for 0 .. $#$rows;
+       $rows->[0]->{id} = $attr->{id} || $rows->[0]->{name} || $attr->{name} || $name #XXX: //
+               if ref $label ne 'ARRAY' and defined $rows->[0] and not defined $rows->[0]->{id};
 
        $self->select($name, $rows, {%$attr, type => 'checkbox'});
 }
index 757b248..166ab11 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,7 +5,7 @@ use warnings;
 
 use Test::More;
 
-plan tests => 66;
+plan tests => 68;
 
 use_ok('HTML::Form::Simple');
 
@@ -402,13 +402,13 @@ is_deeply(
 
 is(
        $form->check('foo'),
-       '<input id="foo_1" name="foo" type="checkbox" value="1">',
+       '<input id="foo" name="foo" type="checkbox" value="1">',
        'check method'
 );
 
 is(
        $form->check(foo => '<">'),
-       '<label><input id="foo_1" name="foo" type="checkbox" value="1"> <"></label>',
+       '<label><input id="foo" name="foo" type="checkbox" value="1"> <"></label>',
        'labeled checkbox'
 );
 
@@ -424,13 +424,57 @@ is_deeply(
        'anonymous checkbox'
 );
 
+is(
+       $form->check(foo => [undef]),
+       '<input id="foo_1" name="foo" type="checkbox" value="1">',
+       'multipart check'
+);
+
+is_deeply(
+       [ $form->check(foo => [{value => undef}, undef]) ],
+       [
+               '<input id="foo" name="foo" type="checkbox">',
+               '<input id="foo_2" name="foo" type="checkbox" value="2">',
+       ],
+       'multiple checkboxes'
+);
+
+is(
+       $form->check(foo => [undef], {id => ''}),
+       '<input name="foo" type="checkbox" value="1">',
+       'idless checks'
+);
+
+is_deeply(
+       [ $form->check(
+               foo => [ {id => 'quux'}, {name => 'name'}, {value => 0}, {id => ''}, undef ], {id => 'bar'}
+       ) ],
+       [
+               '<input id="quux" name="foo" type="checkbox" value="1">',
+               '<input id="bar_2" name="name" type="checkbox" value="2">',
+               '<input id="bar_0" name="foo" type="checkbox" value="0">',
+               '<input name="foo" type="checkbox" value="4">',
+               '<input id="bar_5" name="foo" type="checkbox" value="5">',
+       ],
+       'check overrides'
+);
+
+is_deeply(
+       [ $form->check(foo => ['bar', {name => 'bar'}], {name => 'ignored'}) ],
+       [
+               '<label><input id="foo_1" name="foo" type="checkbox" value="1"> bar</label>',
+               '<input id="bar_2" name="bar" type="checkbox" value="2">',
+       ],
+       'checkbox names'
+);
+
 is_deeply(
        [ $form->check(undef, ['', '<">']) ],
        [
                '<input type="checkbox" value="1">',
                '<label><input type="checkbox" value="2"> <"></label>',
        ],
-       'multiple checkboxes'
+       'anonymous checkboxes'
 );
 
 is_deeply(