/[webpac2]/trunk/t/1-validate.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/t/1-validate.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 514 by dpavlin, Tue May 16 13:38:09 2006 UTC revision 666 by dpavlin, Mon Sep 11 12:32:51 2006 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  use strict;
4  use Test::More tests => 5;  use Test::More tests => 51;
5  use Test::Exception;  use Test::Exception;
6  use blib;  use blib;
7    
8  use Data::Dumper;  use Data::Dump qw/dump/;
9  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
10    
11  BEGIN {  BEGIN {
# Line 21  throws_ok { new WebPAC::Validate( no_log Line 21  throws_ok { new WebPAC::Validate( no_log
21    
22  ok(my $v = new WebPAC::Validate(  ok(my $v = new WebPAC::Validate(
23          path => "$abs_path/data/validate_test",          path => "$abs_path/data/validate_test",
24            debug => $debug,
25  ), "new");  ), "new");
26    
27  ok($v->{rules}, "rules exist");  ok($v->{rules}, "rules exist");
28    
29    is_deeply( $v->{rules}, {
30            '900' => 1,
31            '901' => [ 'a' ],
32            '902' => [ 'b', 'c' ],
33            '903' => [ 'a', 'b', 'c' ],
34            '904' => [ 'a' ],
35            '905' => [ 'a*' ],
36    }, 'rules parsed');
37    
38    
39    
40    throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec";
41    
42    sub test_v {
43            my $row = shift || die "no row?";
44    
45            my $d = dump( $row );
46    
47            $row->{'000'} = [ 42 ];
48    
49            $v->reset_errors;
50            my $e = $v->validate_errors( $row );
51    
52            diag "validate $d\n",dump($e) if ($debug);
53    
54            if (@_) {
55                    my $tmp = $e;
56                    while (@_) {
57                            my $k = shift @_;
58                            ok($tmp = $tmp->{$k}, "found $k");
59                    }
60                    diag "tmp: ",dump($tmp) if ($debug);
61                    if ($tmp) {
62                            if (ref($tmp) eq 'HASH') {
63                                    return $tmp;
64                            } else {
65                                    diag "explanation: $tmp";
66                            }
67                    }
68            } else {
69                    ok(! $e, "validated $d");
70                    diag "expected error: ", dump($e) if($e);
71            }
72    
73    }
74    
75    test_v({
76            '900' => 'foo'
77    }, qw/900 not_repeatable/);
78    
79    test_v({
80            '900' => [ qw/foo bar baz/ ]
81    });
82    
83    test_v({
84            '901' => [ qw/foo bar baz/ ]
85    }, qw/901 missing_subfield/);
86    
87    test_v({
88            '901' => [ { 'a' => 42 } ]
89    });
90    
91    test_v({
92            '901' => [ { 'b' => 42 } ]
93    }, qw/901 subfield extra b/);
94    
95    test_v({
96            '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
97    });
98    
99    test_v({
100            '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
101    }, qw/902 subfield extra a/);
102    
103    test_v({
104            '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
105    });
106    
107    test_v({
108            '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
109    }, qw/903 subfield extra d/);
110    
111    is_deeply(
112    
113    test_v({
114            '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
115    }, qw/903 subfield extra/),
116    { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
117    
118    test_v({
119            '904' => [ { 'a' => 1, } ]
120    });
121    
122    test_v({
123            '904' => [ { 'b' => 1 } ]
124    }, qw/904 subfield extra b/);
125    
126    test_v({
127            '904' => [ { 'a' => [ 1,2 ] } ]
128    }, qw/904 subfield extra_repeatable a/);
129    
130    test_v({
131            '905' => [ { 'a' => [ 1,2 ] } ]
132    });
133    
134    test_v({
135            '905' => [ ]
136    });
137    
138    test_v({
139            '900' => 'foo',
140            '901' => [ qw/foo bar baz/ ],
141            '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
142            '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
143            '904' => [ { 'b' => 1 } ],
144            '905' => [ { 'a' => 1 } ],
145    });
146    
147    diag "errors: ",dump( $v->all_errors );
148    
149    diag "report: ", $v->report;

Legend:
Removed from v.514  
changed lines
  Added in v.666

  ViewVC Help
Powered by ViewVC 1.1.26