X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=t%2F1_validate_11_no_extra.t;h=226cdf61c2ca304a5dac6811719dc58a0049bec5;hb=f30b8252fcf71659a2fd3b5895e009ff8e39299d;hp=46ca0e6b242488dcb9f95a0e0d8341b553b6708b;hpb=85070b46d0a93ddbeef07341421adb8389a55418;p=chaz%2Fp5-CGI-Ex diff --git a/t/1_validate_11_no_extra.t b/t/1_validate_11_no_extra.t index 46ca0e6..226cdf6 100644 --- a/t/1_validate_11_no_extra.t +++ b/t/1_validate_11_no_extra.t @@ -1,148 +1,77 @@ # -*- Mode: Perl; -*- -use strict; +=head1 NAME + +1_validate_11_no_extra.t - Test CGI::Ex::Validate's ability to not allow extra form fields -$^W = 1; +=cut -### determine number of tests -seek(DATA,0,0); -my $prog = join "", ; -my @tests = ($prog =~ /&print_ok\(/g); -my $tests = @tests; -print "1..$tests\n"; +use strict; +use Test::More tests => 13; -require CGI::Ex::Validate; +use_ok('CGI::Ex::Validate'); -my ($N, $v, $e, $ok) = (0); +my ($v, $e); -sub validate { - return scalar &CGI::Ex::Validate::validate(@_); -} -sub print_ok { - my $ok = shift; - $N ++; - warn "Test failed at line ".(caller)[2]."\n" if ! $ok; - print "" . ($ok ? "" : "not ") . "ok $N\n"; -} -&print_ok(1); +sub validate { CGI::Ex::Validate::validate(@_) } ###----------------------------------------------------------------### ### test single group for extra fields -$v = [ -{ - 'general no_extra_fields' => 'all', +$v = { + 'group no_extra_fields' => 1, foo => {max_len => 10}, -}, -]; +}; -$e = &validate({}, $v); -&print_ok(! $e); +$e = validate({}, $v); +ok(! $e); -$e = &validate({foo => "foo"}, $v); -&print_ok(! $e); +$e = validate({foo => "foo"}, $v); +ok(! $e); -$e = &validate({foo => "foo", bar => "bar"}, $v); -&print_ok($e); +$e = validate({foo => "foo", bar => "bar"}, $v); +ok($e); -$e = &validate({bar => "bar"}, $v); -&print_ok($e); +$e = validate({bar => "bar"}, $v); +ok($e); ### test on failed validate if -$v = [ -{ - 'general no_extra_fields' => 'all', +$v = { + 'group no_extra_fields' => 1, 'group validate_if' => 'baz', foo => {max_len => 10}, -}, -]; +}; -$e = &validate({}, $v); -&print_ok(! $e); +$e = validate({}, $v); +ok(! $e); -$e = &validate({foo => "foo"}, $v); -&print_ok(! $e); +$e = validate({foo => "foo"}, $v); +ok(! $e); -$e = &validate({foo => "foo", bar => "bar"}, $v); -&print_ok(! $e); +$e = validate({foo => "foo", bar => "bar"}, $v); +ok(! $e); -$e = &validate({bar => "bar"}, $v); -&print_ok(! $e); +$e = validate({bar => "bar"}, $v); +ok(! $e); ### test on successful validate if -$v = [ -{ - 'general no_extra_fields' => 'all', - 'group validate_if' => 'baz', - foo => {max_len => 10}, - baz => {max_len => 10}, -}, -]; - -$e = &validate({baz => 1}, $v); -&print_ok(! $e); - -$e = &validate({baz => 1, foo => "foo"}, $v); -&print_ok(! $e); - -$e = &validate({baz => 1, foo => "foo", bar => "bar"}, $v); -&print_ok($e); - -$e = &validate({baz => 1, bar => "bar"}, $v); -&print_ok($e); - -### test on multiple groups, some with validate if -$v = [ -{ - 'general no_extra_fields' => 'all', - 'group validate_if' => 'baz', - foo => {max_len => 10}, - baz => {max_len => 10}, -}, -{ - 'group validate_if' => 'hem', - haw => {max_len => 10}, -}, -]; - -$e = &validate({haw => 1, baz => 1}, $v); -&print_ok(! $e); - -$e = &validate({haw => 1, baz => 1, foo => "foo"}, $v); -&print_ok(! $e); - -$e = &validate({haw => 1, baz => 1, foo => "foo", bar => "bar"}, $v); -&print_ok($e); - -$e = &validate({haw => 1, baz => 1, bar => "bar"}, $v); -&print_ok($e); - - -### test on multiple groups, some with validate if -$v = [ -{ - 'general no_extra_fields' => 'used', +$v = { + 'group no_extra_fields' => 1, 'group validate_if' => 'baz', foo => {max_len => 10}, baz => {max_len => 10}, -}, -{ - 'group validate_if' => 'hem', - haw => {max_len => 10}, -}, -]; +}; -$e = &validate({haw => 1, baz => 1}, $v); -&print_ok($e); +$e = validate({baz => 1}, $v); +ok(! $e); -$e = &validate({haw => 1, baz => 1, foo => "foo"}, $v); -&print_ok($e); +$e = validate({baz => 1, foo => "foo"}, $v); +ok(! $e); -$e = &validate({haw => 1, baz => 1, foo => "foo", bar => "bar"}, $v); -&print_ok($e); +$e = validate({baz => 1, foo => "foo", bar => "bar"}, $v); +ok($e); -$e = &validate({haw => 1, baz => 1, bar => "bar"}, $v); -&print_ok($e); +$e = validate({baz => 1, bar => "bar"}, $v); +ok($e); -__DATA__