1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use strict; |
use strict; |
|
use Test::More tests => 47; |
|
|
use Test::Exception; |
|
4 |
use blib; |
use blib; |
5 |
|
|
6 |
use Data::Dump qw/dump/; |
use Test::More tests => 56; |
|
use Cwd qw/abs_path/; |
|
7 |
|
|
8 |
BEGIN { |
BEGIN { |
9 |
|
use_ok( 'WebPAC::Test' ); |
10 |
use_ok( 'WebPAC::Validate' ); |
use_ok( 'WebPAC::Validate' ); |
11 |
} |
} |
12 |
|
|
13 |
my $debug = shift @ARGV; |
ok(my $v = new WebPAC::Validate(%LOG), "new witout path"); |
14 |
|
|
15 |
ok(my $abs_path = abs_path($0), "abs_path"); |
ok( ! $v->{rules}, 'no path' ); |
|
$abs_path =~ s#/[^/]*$#/#; |
|
16 |
|
|
17 |
throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path"; |
ok($v = new WebPAC::Validate( |
|
|
|
|
ok(my $v = new WebPAC::Validate( |
|
18 |
path => "$abs_path/data/validate_test", |
path => "$abs_path/data/validate_test", |
19 |
debug => $debug, |
%LOG, |
20 |
), "new"); |
), "new with path"); |
21 |
|
|
22 |
ok($v->{rules}, "rules exist"); |
ok($v->{rules}, "rules exist"); |
23 |
|
|
28 |
'903' => [ 'a', 'b', 'c' ], |
'903' => [ 'a', 'b', 'c' ], |
29 |
'904' => [ 'a' ], |
'904' => [ 'a' ], |
30 |
'905' => [ 'a*' ], |
'905' => [ 'a*' ], |
31 |
|
'906' => [ '0' ], |
32 |
|
}, 'simple rules parsed'); |
33 |
|
|
34 |
|
diag dump( $v ) if ( $debug ); |
35 |
|
|
36 |
|
ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" ); |
37 |
|
|
38 |
|
diag dump( $v ) if ( $debug ); |
39 |
|
|
40 |
|
ok($v->{rules}, "rules exist"); |
41 |
|
|
42 |
|
is_deeply( $v->{rules}, { |
43 |
|
'900' => [ 'a', 'b', 'c', 'd' ], |
44 |
}, 'rules parsed'); |
}, 'rules parsed'); |
45 |
|
|
46 |
|
ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" ); |
47 |
|
|
48 |
|
is_deeply( $v->{rules}, { |
49 |
|
'900' => 1, |
50 |
|
'901' => [ 'a' ], |
51 |
|
'902' => [ 'b', 'c' ], |
52 |
|
'903' => [ 'a', 'b', 'c' ], |
53 |
|
'904' => [ 'a' ], |
54 |
|
'905' => [ 'a*' ], |
55 |
|
'906' => [ '0' ], |
56 |
|
}, 'rules'); |
57 |
|
|
58 |
|
ok($v->{rules}, "rules exist"); |
59 |
|
|
60 |
throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec"; |
throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec"; |
61 |
|
|
82 |
if (ref($tmp) eq 'HASH') { |
if (ref($tmp) eq 'HASH') { |
83 |
return $tmp; |
return $tmp; |
84 |
} else { |
} else { |
85 |
diag "explanation: $tmp"; |
diag "explanation: $tmp" if $debug; |
86 |
} |
} |
87 |
} |
} |
88 |
} else { |
} else { |
154 |
'905' => [ ] |
'905' => [ ] |
155 |
}); |
}); |
156 |
|
|
157 |
|
test_v({ |
158 |
|
'906' => [ ] |
159 |
|
}); |
160 |
|
|
161 |
|
test_v({ |
162 |
|
'906' => [ { '0' => 'foo' } ] |
163 |
|
}); |
164 |
|
|
165 |
my $expected_error = { |
my $expected_error = { |
166 |
900 => { not_repeatable => "probably bug in parsing input data" }, |
900 => { not_repeatable => "probably bug in parsing input data" }, |
167 |
901 => { missing_subfield => "a required" }, |
901 => { missing_subfield => "a required", "dump" => "baz" }, |
168 |
902 => { |
902 => { |
169 |
"dump" => "^a1^b1^b2", |
"dump" => "^a1^b1^b2", |
170 |
subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } }, |
subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } }, |
190 |
|
|
191 |
ok(my $r1 = $v->report, 'report'); |
ok(my $r1 = $v->report, 'report'); |
192 |
|
|
193 |
|
diag "report: $r1" if ( $debug ); |
194 |
|
|
195 |
is_deeply( |
is_deeply( |
196 |
test_v({ |
test_v({ |
197 |
'900' => 'foo', |
'900' => 'foo', |
203 |
}, undef), |
}, undef), |
204 |
$expected_error, 'validate with subfields'); |
$expected_error, 'validate with subfields'); |
205 |
|
|
|
|
|
206 |
ok(my $r2 = $v->report, 'report'); |
ok(my $r2 = $v->report, 'report'); |
207 |
|
|
208 |
cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields'); |
cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields'); |