XXX: row method
[perl/html-form-simple.git] / t / html.t
index 8ab4806da4e9910cde6fd2c9deefc3b30cf2bf60..08f0e6ba8d9e9c4320366ff3ced51cfd86995317 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,13 +5,39 @@ use warnings;
 
 use Test::More;
 
-plan tests => 52;
+plan tests => 80;
 
 use_ok('HTML::Form::Simple');
 
 my $form = HTML::Form::Simple->new;
 ok($form, 'new form');
 
+# basics
+
+is(
+       $form->quote('<"test">'),
+       '&lt;&quot;test&quot;>',
+       'quote'
+);
+
+is(
+       $form->quote(\'<"test">'),
+       '<"test">',
+       'quote unquoted'
+);
+
+is(
+       $form->tag('foo'),
+       '<foo>',
+       'tag'
+);
+
+is(
+       $form->tag('a "' => {'b"' => 'c"'}),
+       '<a " b"="c&quot;">',
+       'tag attributes'
+);
+
 # form
 
 is(
@@ -58,6 +84,37 @@ is(
        'hidden options'
 );
 
+is_deeply(
+       [ $form->hidden(foo => [1, 0], {class => 'test'}) ],
+       [
+               '<input class="test" name="foo" type="hidden" value="1">',
+               '<input class="test" name="foo" type="hidden" value="0">',
+       ],
+       'hidden array'
+);
+
+is_deeply(
+       [ $form->hidden({2 => 0, 10 => 1}, {class => 'test'}) ],
+       [
+               '<input class="test" name="10" type="hidden" value="1">',
+               '<input class="test" name="2" type="hidden" value="0">',
+       ],
+       'hidden hash'
+);
+
+{
+       local $, = "\n";
+       is(
+               scalar $form->hidden({2 => [1, 0], 10 => 2}, {class => 'test'}),
+               join("\n",
+                       '<input class="test" name="10" type="hidden" value="2">',
+                       '<input class="test" name="2" type="hidden" value="1">',
+                       '<input class="test" name="2" type="hidden" value="0">',
+               ),
+               'scalar hiddens'
+       );
+}
+
 # submit
 
 is(
@@ -87,43 +144,43 @@ is(
 # input
 
 is(
-       $form->input,
+       $form->text,
        '<input type="text">',
        'empty input'
 );
 
 is(
-       $form->input(undef, undef, undef),
+       $form->text(undef, undef, undef),
        '<input type="text">',
        'explicit empty input'
 );
 
 is(
-       $form->input('test'),
+       $form->text('test'),
        '<input id="test" name="test" type="text">',
        'input with name'
 );
 
 is(
-       $form->input(undef, 'test'),
+       $form->text(undef, 'test'),
        '<input type="text" value="test">',
        'input with value'
 );
 
 is(
-       $form->input(undef, {value => 'test'}),
+       $form->text(undef, {value => 'test'}),
        '<input type="text" value="test">',
        'input with attribute value'
 );
 
 is(
-       $form->input({name => 'test', value => ''}),
+       $form->text({name => 'test', value => ''}),
        '<input id="test" name="test" type="text" value="">',
        'input with only attributes'
 );
 
 is(
-       $form->input('', '', {
+       $form->text('', '', {
                disabled => 0,
                something => undef,
                class => undef,
@@ -136,31 +193,31 @@ is(
 );
 
 is(
-       $form->input(undef, undef, {name => '0', value => '0'}),
+       $form->text(undef, undef, {name => '0', value => '0'}),
        '<input id="0" name="0" type="text" value="0">',
        'input overrides'
 );
 
 is(
-       $form->input('name', {id => ''}),
+       $form->text('name', {id => ''}),
        '<input name="name" type="text">',
        'input with id override'
 );
 
 is(
-       $form->input('<">', '<">', {id => '>"<'}),
+       $form->text('<">', '<">', {id => '>"<'}),
        '<input id=">&quot;&lt;" name="&lt;&quot;>" type="text" value="&lt;&quot;>">',
        'input quoting'
 );
 
 is(
-       $form->input(undef, {disabled => 'something'}),
+       $form->text(undef, {disabled => 'something'}),
        '<input disabled type="text">',
        'disabled input'
 );
 
 is(
-       $form->input({type => 'password'}),
+       $form->text({type => 'password'}),
        '<input type="password">',
        'password'
 );
@@ -168,19 +225,19 @@ is(
 # textarea
 
 is(
-       $form->input({rows => 0}),
+       $form->text({rows => 0}),
        '<textarea rows="0"></textarea>',
        'minimal textarea'
 );
 
 is(
-       $form->input(foo => 'bar', {cols => 42, rows => 1, disabled => 1}),
+       $form->text(foo => 'bar', {cols => 42, rows => 1, disabled => 1}),
        '<textarea disabled cols="42" id="foo" name="foo" rows="1">bar</textarea>',
        'textarea'
 );
 
 is(
-       $form->input(undef, qq{<foo>&bl'a"\n    .}, {cols => undef, rows => '<">'}),
+       $form->text(undef, qq{<foo>&bl'a"\n    .}, {cols => undef, rows => '<">'}),
        qq{<textarea rows="&lt;&quot;>">&lt;foo>&amp;bl'a&quot;\n    .</textarea>},
        'textarea quoting'
 );
@@ -281,14 +338,14 @@ is_deeply(
 
 is(
        $form->radio(foo => 'test'),
-       '<label><input id="foo_1" name="foo" type="radio" value="1"> test</label>',
+       '<input id="foo_test" name="foo" type="radio" value="test">',
        'radio method'
 );
 
 is(
-       $form->radio(foo => undef, 2),
-       '<input id="foo_2" name="foo" type="radio" value="2">',
-       'labelless radio'
+       $form->radio(foo => undef, 'test'),
+       '<label><input id="foo_1" name="foo" type="radio" value="1"> test</label>',
+       'labeled radio'
 );
 
 is(
@@ -299,15 +356,33 @@ is(
 
 is_deeply(
        [ $form->radio(undef, ['', '<">']) ],
+       [
+               '<input type="radio" value="">',
+               '<input type="radio" value="&lt;&quot;>">',
+       ],
+       'multiple radios'
+);
+
+is_deeply(
+       [ $form->radio(undef, undef, ['', '<">']) ],
        [
                '<input type="radio" value="1">',
                '<label><input type="radio" value="2"> <"></label>',
        ],
-       'multiple radios'
+       'multiple radio labels'
+);
+
+is_deeply(
+       [ $form->radio(undef, [0, 1], undef, 0) ],
+       [
+               '<input checked type="radio" value="0">',
+               '<input type="radio" value="1">',
+       ],
+       'radio default'
 );
 
 is_deeply(
-       [ $form->radio(foo => 'test', ['foo', ''], {value => '', id => ''}) ],
+       [ $form->radio(foo => ['foo', ''], 'test', {value => '', id => ''}) ],
        [
                '<label><input name="foo" type="radio" value="foo"> test</label>',
                '<label><input checked name="foo" type="radio" value=""> test</label>',
@@ -316,7 +391,7 @@ is_deeply(
 );
 
 is_deeply(
-       [ $form->radio(foo => ['', 0], [0, 1, '']) ],
+       [ $form->radio(foo => [0, 1, ''], ['', 0]) ],
        [
                '<input id="foo_0" name="foo" type="radio" value="0">',
                '<label><input id="foo_1" name="foo" type="radio" value="1"> 0</label>',
@@ -325,17 +400,50 @@ is_deeply(
        'multiple radios with custom values'
 );
 
+{
+       # make sure arguments aren't modified
+       my @args = (foo => [0, {value => 1}], [0, 1], {name => 0, value => 1});
+       my @orgs = (foo => [0, {value => 1}], [0, 1], {name => 0, value => 1});
+       my @output = (
+               '<label><input id="foo_0" name="foo" type="radio" value="0"> 0</label>',
+               '<label><input checked id="foo_1" name="foo" type="radio" value="1"> 1</label>',
+       );
+
+       is_deeply(
+               [ $form->radio(@args) ],
+               \@output,
+               'options var to radio'
+       );
+
+       is_deeply(
+               [ $form->check(@args) ],
+               [
+                       '<label><input checked id="foo_1" name="foo" type="checkbox" value="1"> 0</label>',
+                       '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
+               ],
+               'options var to check'
+       );
+
+       is(
+               scalar $form->radio(@args),
+               join('', @output),
+               'options var again to radio'
+       );
+
+       is_deeply(\@args, \@orgs, 'options var unmodified');
+}
+
 # check
 
 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'
 );
 
@@ -351,13 +459,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="foo_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(
@@ -391,5 +543,69 @@ is_deeply(
        'various checkboxes'
 );
 
+# row
+
+is(
+       $form->row(undef),
+       '<label></label> <input type="text">',
+       'empty row'
+);
+
+is(
+       $form->row('test'),
+       '<label for="test">test</label> <input id="test" name="test" type="text">',
+       'standard row'
+);
+
+is(
+       $form->row('test', {type => 'password'}, {for => '', label => '"blah"'}),
+       '<label>&quot;blah&quot;</label> <input id="test" name="test" type="password">',
+       'row options'
+);
+
+is(
+       $form->row('test', '"scalar"', {for => ''}),
+       '<label>test</label> &quot;scalar&quot;',
+       'row scalar'
+);
+
+# defaults
+
+my $defform = HTML::Form::Simple->new({foo => '<">', '' => 'empty', 0 => 0});
+ok($defform, 'form with defaults');
+
+is(
+       $defform->hidden(''),
+       '<input name="" type="hidden" value="empty">',
+       'hidden with default'
+);
+
+is(
+       $defform->hidden(undef),
+       '<input type="hidden">',
+       'nameless hidden'
+);
+
+is(
+       $defform->text('foo'),
+       '<input id="foo" name="foo" type="text" value="&lt;&quot;>">',
+       'input with default'
+);
+
+is(
+       $defform->text('foo', {value => 'custom'}),
+       '<input id="foo" name="foo" type="text" value="custom">',
+       'input with value and default'
+);
+
+is_deeply(
+       [ $defform->radio(0 => [1, 0]) ],
+       [
+               '<input id="0_1" name="0" type="radio" value="1">',
+               '<input checked id="0_0" name="0" type="radio" value="0">',
+       ],
+       'select with default'
+);
+
 #TODO