X-Git-Url: http://git.shiar.nl/perl/html-form-simple.git/blobdiff_plain/ac5c326058425050aa8f7ba5036f2f3bc0695350..47c6a0e47426e447523d4697b2fb596366f7f9d4:/t/html.t diff --git a/t/html.t b/t/html.t index e023997..08f0e6b 100644 --- a/t/html.t +++ b/t/html.t @@ -5,13 +5,39 @@ use warnings; use Test::More; -plan tests => 31; +plan tests => 80; use_ok('HTML::Form::Simple'); my $form = HTML::Form::Simple->new; ok($form, 'new form'); +# basics + +is( + $form->quote('<"test">'), + '<"test">', + 'quote' +); + +is( + $form->quote(\'<"test">'), + '<"test">', + 'quote unquoted' +); + +is( + $form->tag('foo'), + '', + 'tag' +); + +is( + $form->tag('a "' => {'b"' => 'c"'}), + '', + 'tag attributes' +); + # form is( @@ -52,7 +78,42 @@ is( 'hidden' ); -#TODO: hidden options +is( + $form->hidden(undef, undef, {value => 'bar', name => 'foo', id => 'foo'}), + '', + 'hidden options' +); + +is_deeply( + [ $form->hidden(foo => [1, 0], {class => 'test'}) ], + [ + '', + '', + ], + 'hidden array' +); + +is_deeply( + [ $form->hidden({2 => 0, 10 => 1}, {class => 'test'}) ], + [ + '', + '', + ], + 'hidden hash' +); + +{ + local $, = "\n"; + is( + scalar $form->hidden({2 => [1, 0], 10 => 2}, {class => 'test'}), + join("\n", + '', + '', + '', + ), + 'scalar hiddens' + ); +} # submit @@ -83,73 +144,80 @@ is( # input is( - $form->input, + $form->text, '', 'empty input' ); is( - $form->input(undef, undef, undef), + $form->text(undef, undef, undef), '', 'explicit empty input' ); is( - $form->input('test'), + $form->text('test'), '', 'input with name' ); is( - $form->input(undef, 'test'), + $form->text(undef, 'test'), '', 'input with value' ); is( - $form->input(undef, {value => 'test'}), + $form->text(undef, {value => 'test'}), '', 'input with attribute value' ); is( - $form->input({name => 'test', value => ''}), + $form->text({name => 'test', value => ''}), '', 'input with only attributes' ); is( - $form->input('', '', {disabled => 0, something => undef, class => undef, style => ''}), + $form->text('', '', { + disabled => 0, + something => undef, + class => undef, + style => '', + name => 'ignore', + value => 'overrides', + }), '', 'input with empty attributes' ); is( - $form->input(undef, undef, {name => 'override', value => 'override'}), - '', - 'ignore input overrides' + $form->text(undef, undef, {name => '0', value => '0'}), + '', + 'input overrides' ); is( - $form->input('name', {id => ''}), + $form->text('name', {id => ''}), '', 'input with id override' ); is( - $form->input('<">', '<">', {id => '>"<'}), + $form->text('<">', '<">', {id => '>"<'}), '', 'input quoting' ); is( - $form->input(undef, {disabled => 'something'}), + $form->text(undef, {disabled => 'something'}), '', 'disabled input' ); is( - $form->input({type => 'password'}), + $form->text({type => 'password'}), '', 'password' ); @@ -157,48 +225,387 @@ is( # textarea is( - $form->input({rows => 0}), + $form->text({rows => 0}), '', 'minimal textarea' ); is( - $form->input(foo => 'bar', {cols => 42, rows => 1, disabled => 1}), + $form->text(foo => 'bar', {cols => 42, rows => 1, disabled => 1}), '', 'textarea' ); is( - $form->input(undef, qq{&bl'a"\n .}, {cols => undef, rows => '<">'}), + $form->text(undef, qq{&bl'a"\n .}, {cols => undef, rows => '<">'}), qq{}, 'textarea quoting' ); # select -is( - $form->select, - '', # malformed html: at least 1 option required +is_deeply( + [ $form->select ], + [ qw() ], # malformed html: at least 1 option required 'empty select' ); -is( - $form->select(undef, [], '', {name => ''}), - '', +is_deeply( + [ $form->select(undef, [], '', {name => '', class => ''}) ], + [ qw() ], 'explicit empty select' ); -is( - $form->select(undef, [undef]), - '', +is_deeply( + [ $form->select(undef, [undef]) ], + [ qw() ], 'minimal select' ); -is( - $form->select(foo => [1..2]), - '', +is_deeply( + [ $form->select(foo => [1, 2]) ], + [ + '' + ], 'select contents' ); +is_deeply( + [ $form->select(foo => [1, 2], 3) ], + [ + '' + ], + 'select invalid default' +); + +is_deeply( + [ $form->select(undef, [1, 2], 2) ], + [ + '' + ], + 'select default' +); + +is_deeply( + [ + $form->select(foo => [ + '<">', {value => 2, disabled => 1, selected => 0, class => 1}, {selected => 1} + ], '2', {id => '', class => 'a 1}) + ], + [ + '' + ], + 'complex select' +); + +is( + scalar $form->select(foo => [1, 2]), + '', + 'select scalar' +); + +# radio + +is_deeply( + [ $form->select(foo => [1], {type => 'radio'}) ], + [ '' ], + '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 selects' +); + +is( + $form->radio(foo => 'test'), + '', + 'radio method' +); + +is( + $form->radio(foo => undef, 'test'), + '', + 'labeled radio' +); + +is( + $form->radio(undef, {checked => 1}), + '', + 'simple radio button' +); + +is_deeply( + [ $form->radio(undef, ['', '<">']) ], + [ + '', + '', + ], + 'multiple radios' +); + +is_deeply( + [ $form->radio(undef, undef, ['', '<">']) ], + [ + '', + '', + ], + 'multiple radio labels' +); + +is_deeply( + [ $form->radio(undef, [0, 1], undef, 0) ], + [ + '', + '', + ], + 'radio default' +); + +is_deeply( + [ $form->radio(foo => ['foo', ''], 'test', {value => '', id => ''}) ], + [ + '', + '', + ], + 'multiple radios with custom value' +); + +is_deeply( + [ $form->radio(foo => [0, 1, ''], ['', 0]) ], + [ + '', + '', + '', + ], + '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 = ( + '', + '', + ); + + is_deeply( + [ $form->radio(@args) ], + \@output, + 'options var to radio' + ); + + is_deeply( + [ $form->check(@args) ], + [ + '', + '', + ], + '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'), + '', + 'check method' +); + +is( + $form->check(foo => '<">'), + '', + 'labeled checkbox' +); + +is( + $form->check(foo => {label => 'test', value => undef}, {disabled => 1}), + '', + 'check parameters' +); + +is_deeply( + [ $form->check(undef, '', 1) ], + [ '' ], + 'anonymous checkbox' +); + +is( + $form->check(foo => [undef]), + '', + 'multipart check' +); + +is_deeply( + [ $form->check(foo => [{value => undef}, undef]) ], + [ + '', + '', + ], + 'multiple checkboxes' +); + +is( + $form->check(foo => [undef], {id => ''}), + '', + 'idless checks' +); + +is_deeply( + [ $form->check( + foo => [ {id => 'quux'}, {name => 'name'}, {value => 0}, {id => ''}, undef ], {id => 'bar'} + ) ], + [ + '', + '', + '', + '', + '', + ], + 'check overrides' +); + +is_deeply( + [ $form->check(foo => ['bar', {name => 'bar'}], {name => 'ignored'}) ], + [ + '', + '', + ], + 'checkbox names' +); + +is_deeply( + [ $form->check(undef, ['', '<">']) ], + [ + '', + '', + ], + 'anonymous checkboxes' +); + +is_deeply( + [ $form->check(undef, [{}, undef], 1) ], + [ + '', + '', + ], + 'multiple checked checkboxes' +); + +{ + local $, = ' | '; + is( + scalar $form->check(foo => [1, 0], {value => 0}), + join(' | ', + '', + '', + ), + 'merged checkboxes' + ); +} + +is_deeply( + [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ], + [ + '', + '', + '', + ], + 'various checkboxes' +); + +# row + +is( + $form->row(undef), + ' ', + 'empty row' +); + +is( + $form->row('test'), + ' ', + 'standard row' +); + +is( + $form->row('test', {type => 'password'}, {for => '', label => '"blah"'}), + ' ', + 'row options' +); + +is( + $form->row('test', '"scalar"', {for => ''}), + ' "scalar"', + 'row scalar' +); + +# defaults + +my $defform = HTML::Form::Simple->new({foo => '<">', '' => 'empty', 0 => 0}); +ok($defform, 'form with defaults'); + +is( + $defform->hidden(''), + '', + 'hidden with default' +); + +is( + $defform->hidden(undef), + '', + 'nameless hidden' +); + +is( + $defform->text('foo'), + '', + 'input with default' +); + +is( + $defform->text('foo', {value => 'custom'}), + '', + 'input with value and default' +); + +is_deeply( + [ $defform->radio(0 => [1, 0]) ], + [ + '', + '', + ], + 'select with default' +); + #TODO