do not quote scalar refs to pass literal html
[perl/html-form-simple.git] / t / html.t
index 2fea804b2353d89b0eeb9d07dc09b83047150aef..a82c916d0b5fbecf87531d0f5538bf539b4a2c8a 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,13 +5,39 @@ use warnings;
 
 use Test::More;
 
-plan tests => 55;
+plan tests => 76;
 
 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(
@@ -312,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(
@@ -330,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(foo => 'test', ['foo', ''], {value => '', id => ''}) ],
+       [ $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 => ['foo', ''], 'test', {value => '', id => ''}) ],
        [
                '<label><input name="foo" type="radio" value="foo"> test</label>',
                '<label><input checked name="foo" type="radio" value=""> test</label>',
@@ -347,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>',
@@ -356,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'
 );
 
@@ -382,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(
@@ -422,5 +543,43 @@ is_deeply(
        'various checkboxes'
 );
 
+# 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