diff --git a/t/Form/Password.t b/t/Form/Password.t index a12a7901f..c65c3f860 100644 --- a/t/Form/Password.t +++ b/t/Form/Password.t @@ -69,8 +69,6 @@ is(scalar @inputs, 1, 'The form has 1 input'); my $input = $inputs[0]; use Data::Dumper; -diag(Dumper $input); -diag($html); is($input->name, 'TestText', 'Checking input name'); is($input->type, $formType, 'Checking input type'); is($input->value, 'Some text in here', 'Checking default value'); diff --git a/t/Form/SelectList.t b/t/Form/SelectList.t index 16ddd40f8..6722678c5 100644 --- a/t/Form/SelectList.t +++ b/t/Form/SelectList.t @@ -34,14 +34,16 @@ my %testBlock; tie %testBlock, 'Tie::IxHash'; %testBlock = ( - #INT1 => [ '-123456', 'EQUAL', 'valid, negative integer'], - #INT2 => [ '+123456', 0, 'reject explicitly positive integer'], + LIST1 => [ [qw/a/], 'a', 'single element array, scalar', 'SCALAR'], + LIST2 => [ [qw/a/], 'EQUAL', 'single element array, array', 'ARRAY'], + LIST3 => [ [qw/a b c/], "a\nb\nc", 'multi element array, scalar', 'SCALAR'], + LIST4 => [ [qw/a b c/], 'EQUAL', 'multi element array, array', 'ARRAY'], ); my $formClass = 'WebGUI::Form::SelectList'; my $formType = 'SelectList'; -my $numTests = 5 + scalar keys %testBlock; +my $numTests = 11 + scalar keys %testBlock; diag("Planning on running $numTests tests\n"); @@ -53,40 +55,91 @@ my $html = join "\n", $header, $formClass->new($session, { name => 'ListMultiple', - options => { a=>'a', b=>'b', c=>'c', d=>'d', e=>'e' }, - value => [ qw(a c e)], + options => { a=>'aa', b=>'bb', c=>'cc', d=>'dd', e=>'ee', ''=>'Empty' }, + value => [ qw(a c e), ''], sortByValue => 1, })->toHtml, $footer; -diag($html); - my @forms = HTML::Form->parse($html, 'http://www.webgui.org'); ##Test Form Generation is(scalar @forms, 1, '1 form was parsed'); -#use Data::Dumper; -#diag(Dumper \@forms); - my $form = $forms[0]; -my @inputs = $form->inputs; -is(scalar @inputs, 5, 'The form has 5 inputs'); - -#Basic tests - #use Data::Dumper; #diag(Dumper $form); +my @inputs = $form->inputs; +is(scalar @inputs, 6, 'The form has 6 inputs'); + +#Basic tests my @options = $form->find_input('ListMultiple'); is( scalar(grep {$_->type ne 'option'} @options), 0, 'All inputs are of type option'); -my @names = map { $_->name } @options; -cmp_bag( [@names], [ ('ListMultiple')x5 ], 'correct number of names and names'); +is( scalar(grep {$_->{multiple} ne '1'} @options), 0, 'All inputs have multiple'); -cmp_bag([ $form->param('ListMultiple') ], [qw(a c e)], 'preselected values'); +my @names = map { $_->name } @options; +cmp_deeply( [@names], bag(('ListMultiple')x6), 'correct number of names and names'); + +cmp_set([ $form->param('ListMultiple') ], [qw(a c e), ''], 'preselected values in order'); my @values = map { $_->possible_values } @options; -#cmp_bag([ @values ], [qw(a b c d e), ('')x5], 'list of all options'); +cmp_bag([ @values ], [qw(a b c d e), '', (undef)x6], 'list of all options'); + +my @value_names = map { $_->value_names } @options; +cmp_bag([ @value_names ], [qw(aa bb cc dd ee Empty), ('off')x6], 'list of all displayed value names'); + +$html = join "\n", + $header, + $formClass->new($session, { + name => 'ListMultiple', + options => { a=>'aa', b=>'bb', c=>'cc', d=>'dd', e=>'ee' }, + defaultValue => [ qw(a b c) ], + sortByValue => 1, + })->toHtml, + $footer; + +@forms = HTML::Form->parse($html, 'http://www.webgui.org'); +$form = $forms[0]; + +cmp_bag([ $form->param('ListMultiple') ], [qw(a b c)], 'defaultValue used if value is blank'); + + +$html = join "\n", + $header, + $formClass->new($session, { + name => 'ListMultiple', + options => { a=>'aa', b=>'bb', c=>'cc', d=>'dd', e=>'ee' }, + value => [ qw(d e) ], + defaultValue => [ qw(a b c) ], + sortByValue => 1, + })->toHtml, + $footer; + +@forms = HTML::Form->parse($html, 'http://www.webgui.org'); +$form = $forms[0]; + +cmp_bag([ $form->param('ListMultiple') ], [qw(d e)], 'defaultValue ignored if value is present'); + + +$html = join "\n", + $header, + $formClass->new($session, { + name => 'ListMultiple', + options => { a=>'aa', b=>'bb', c=>'cc', d=>'dd', e=>'ee' }, + value => [ qw(a b c d e) ], + sortByValue => 1, + })->toHtml, + $footer; + +@forms = HTML::Form->parse($html, 'http://www.webgui.org'); +$form = $forms[0]; + +cmp_set([ $form->param('ListMultiple') ], [qw(a b c d e)], 'sorted value check'); + +##Test Form Output parsing + +WebGUI::Form_Checking::auto_check($session, $formType, %testBlock); diff --git a/t/lib/WebGUI/Form_Checking.pm b/t/lib/WebGUI/Form_Checking.pm index 35e490c0e..adea59048 100644 --- a/t/lib/WebGUI/Form_Checking.pm +++ b/t/lib/WebGUI/Form_Checking.pm @@ -2,6 +2,7 @@ package WebGUI::Form_Checking; use Test::MockObject; use Test::More; +use Test::Deep; sub auto_check { my ($session, $formType, %testBlock) = @_; @@ -11,17 +12,29 @@ sub auto_check { $request->mock('body', sub { my ($self, $value) = @_; - return $testBlock{$value}->[0] if (exists $testBlock{$value}); - return; + return unless exists $testBlock{$value}; + if (ref $testBlock{$value}->[0] eq "ARRAY") { + return @{ $testBlock{$value}->[0] }; + } + else { + return $testBlock{$value}->[0]; + } } ); $session->{_request} = $request; foreach my $key (keys %testBlock) { - my ($testValue, $expected, $comment) = @{ $testBlock{$key} }; - my $value = $session->form->get($key, $formType); - is($value, ($expected eq 'EQUAL' ? $testValue : $expected), $comment); + my ($testValue, $expected, $comment, $dataType) = @{ $testBlock{$key} }; + $dataType ||= 'SCALAR'; + if ($dataType eq 'SCALAR') { + my $value = $session->form->get($key, $formType); + is($value, ($expected eq 'EQUAL' ? $testValue : $expected), $comment); + } + elsif ($dataType eq 'ARRAY') { + my @value = $session->form->get($key, $formType); + cmp_bag(\@value, ($expected eq 'EQUAL' ? $testValue : $expected), $comment); + } } $session->{_request} = $origSessionRequest;