#!/usr/bin/perl -w
use strict;
-use Test::More tests => 47;
-use Test::Exception;
-use blib;
+use lib 'lib';
-use Data::Dump qw/dump/;
-use Cwd qw/abs_path/;
+use Test::More tests => 56;
BEGIN {
+use_ok( 'WebPAC::Test' );
use_ok( 'WebPAC::Validate' );
}
-my $debug = shift @ARGV;
+ok(my $v = new WebPAC::Validate(%LOG), "new witout path");
-ok(my $abs_path = abs_path($0), "abs_path");
-$abs_path =~ s#/[^/]*$#/#;
+ok( ! $v->{rules}, 'no path' );
-throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path";
-
-ok(my $v = new WebPAC::Validate(
+ok($v = new WebPAC::Validate(
path => "$abs_path/data/validate_test",
- debug => $debug,
-), "new");
+ %LOG,
+), "new with path");
ok($v->{rules}, "rules exist");
'903' => [ 'a', 'b', 'c' ],
'904' => [ 'a' ],
'905' => [ 'a*' ],
+ '906' => [ '0' ],
+}, 'simple rules parsed');
+
+diag dump( $v ) if ( $debug );
+
+ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" );
+
+diag dump( $v ) if ( $debug );
+
+ok($v->{rules}, "rules exist");
+
+is_deeply( $v->{rules}, {
+ '900' => [ 'a', 'b', 'c', 'd' ],
}, 'rules parsed');
+ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" );
+is_deeply( $v->{rules}, {
+ '900' => 1,
+ '901' => [ 'a' ],
+ '902' => [ 'b', 'c' ],
+ '903' => [ 'a', 'b', 'c' ],
+ '904' => [ 'a' ],
+ '905' => [ 'a*' ],
+ '906' => [ '0' ],
+}, 'rules');
+
+ok($v->{rules}, "rules exist");
throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
$row->{'000'} = [ 42 ];
- $v->reset_errors;
+ $v->reset;
my $e = $v->validate_rec( $row );
diag "validate $d\n",dump($e) if ($debug);
if (ref($tmp) eq 'HASH') {
return $tmp;
} else {
- diag "explanation: $tmp";
+ diag "explanation: $tmp" if $debug;
}
}
} else {
'905' => [ ]
});
+test_v({
+ '906' => [ ]
+});
+
+test_v({
+ '906' => [ { '0' => 'foo' } ]
+});
+
my $expected_error = {
900 => { not_repeatable => "probably bug in parsing input data" },
- 901 => { missing_subfield => "a required" },
+ 901 => { missing_subfield => "a required", "dump" => "baz" },
902 => {
"dump" => "^a1^b1^b2",
subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
ok(my $r1 = $v->report, 'report');
+diag "report: $r1" if ( $debug );
+
is_deeply(
test_v({
'900' => 'foo',
}, undef),
$expected_error, 'validate with subfields');
-
ok(my $r2 = $v->report, 'report');
cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');