use Test::More;
-plan tests => 31;
+plan tests => 55;
use_ok('HTML::Form::Simple');
'hidden'
);
-#TODO: hidden options
+is(
+ $form->hidden(undef, undef, {value => 'bar', name => 'foo', id => 'foo'}),
+ '<input id="foo" name="foo" type="hidden" value="bar">',
+ '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
# 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('', '', {disabled => 0, something => undef}),
+ $form->text('', '', {
+ 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->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=">"<" name="<">" type="text" value="<">">',
'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'
);
# 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="<">"><foo>&bl'a"\n .</textarea>},
'textarea quoting'
);
# 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 <b" name="foo">',
+ '<option value="<">">',
+ '<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),
+ '<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 => '<">'),
+ '<label><input id="foo_1" name="foo" type="checkbox" value="1"> <"></label>',
+ 'labeled checkbox'
+);
+
+is(
+ $form->check(foo => {label => 'test', value => undef}, {disabled => 1}),
+ '<label><input disabled id="foo" name="foo" type="checkbox"> test</label>',
+ 'check parameters'
+);
+
+is_deeply(
+ [ $form->check(undef, '', 1) ],
+ [ '<input checked type="checkbox" value="1">' ],
+ 'anonymous checkbox'
+);
+
+is_deeply(
+ [ $form->check(undef, ['', '<">']) ],
+ [
+ '<input type="checkbox" value="1">',
+ '<label><input type="checkbox" value="2"> <"></label>',
+ ],
+ 'multiple checkboxes'
+);
+
+is_deeply(
+ [ $form->check(undef, [{}, undef], 1) ],
+ [
+ '<input checked type="checkbox" value="1">',
+ '<input checked type="checkbox" value="2">',
+ ],
+ 'multiple checked checkboxes'
+);
+
+{
+ local $, = ' | ';
+ is(
+ scalar $form->check(foo => [1, 0], {value => 0}),
+ join(' | ',
+ '<label><input id="foo_1" name="foo" type="checkbox" value="1"> 1</label>',
+ '<label><input id="foo_2" name="foo" type="checkbox" value="2"> 0</label>',
+ ),
+ 'merged checkboxes'
+ );
+}
+
+is_deeply(
+ [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ],
+ [
+ '<input checked type="radio" value="4">',
+ '<input type="checkbox" value="2">',
+ '<input checked type="checkbox" value="3">',
+ ],
+ 'various checkboxes'
+);
+
#TODO