1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use Test::More tests => 13; |
5 |
use Test::Exception; |
6 |
use blib; |
7 |
|
8 |
use Data::Dump qw/dump/; |
9 |
use Cwd qw/abs_path/; |
10 |
use File::Temp qw/ :POSIX /; |
11 |
|
12 |
BEGIN { |
13 |
use_ok( 'WebPAC::Validate' ); |
14 |
} |
15 |
|
16 |
my $debug = shift @ARGV; |
17 |
|
18 |
ok(my $abs_path = abs_path($0), "abs_path"); |
19 |
$abs_path =~ s#/[^/]*$#/#; |
20 |
|
21 |
my $delimiters_path = tmpnam(); |
22 |
|
23 |
ok(my $v = new WebPAC::Validate( |
24 |
path => "$abs_path/data/validate_test", |
25 |
delimiters => [ ' : ', ' ; ', ' / ', ' \. ', ' = ' ], |
26 |
delimiters_path => $delimiters_path, |
27 |
debug => $debug, |
28 |
), "new"); |
29 |
|
30 |
diag "rules = ", dump( $v->{rules} ) if ( $debug ); |
31 |
|
32 |
$v->{rules} = {}; |
33 |
|
34 |
sub test_v { |
35 |
my $row = shift || die "no row?"; |
36 |
|
37 |
my $d = dump( $row ); |
38 |
|
39 |
$row->{'000'} = [ 42 ]; |
40 |
|
41 |
$v->reset; |
42 |
my $e = $v->validate_rec( $row ); |
43 |
|
44 |
diag ">> validate $d\n",dump($e) if ($debug); |
45 |
} |
46 |
|
47 |
test_v({ |
48 |
'900' => [ |
49 |
{ 'a' => 'a : aa = aaa : a ; a', 'b' => 'b ; b ; b ; b ; a : / z . z . ' }, |
50 |
{ 'a' => 'a : a : a', 'c' => [ 'a : b ; c', 'a : b ; c / d' ] }, |
51 |
{ 'a' => 'a : b / c' }, |
52 |
{ 'a' => 'a : b / c' }, |
53 |
], |
54 |
'901' => [ |
55 |
{ 'a' => 'a : b / c', 'b' => 'foo' }, |
56 |
{ 'a' => 'a : b / c', 'b' => 'foo' }, |
57 |
], |
58 |
}); |
59 |
|
60 |
diag dump($v->{_delimiters_templates}) if ( $debug ); |
61 |
|
62 |
is_deeply( $v->{_delimiters_templates}, { |
63 |
900 => { |
64 |
"^a : / " => 2, |
65 |
"^a : : ^c : ; ^c : ; / " => 1, |
66 |
"^a : = : ; ^b ; ; ; ; : . . " => 1, |
67 |
}, |
68 |
901 => { "^a : / ^b" => 2 }, |
69 |
}, 'just subfields and delimiters'); |
70 |
|
71 |
ok(my $dt = $v->delimiters_templates, 'delimiters_template'); |
72 |
diag $dt if ( $debug ); |
73 |
|
74 |
ok($dt = $v->delimiters_templates( report => 1 ), 'delimiters_template report'); |
75 |
diag $dt if ( $debug ); |
76 |
|
77 |
$v->{_validate_delimiters_templates} = { |
78 |
900 => { '^a : ^b' => 1 }, |
79 |
}; |
80 |
|
81 |
test_v({ |
82 |
'900' => [ |
83 |
{ 'a' => 'foo : bar', 'b' => 'baz' }, |
84 |
{ 'a' => 'foo', 'b' => 'baz' }, |
85 |
{ 'a' => 'foo' }, |
86 |
{ 'a' => 'foo : bar' }, |
87 |
], |
88 |
}); |
89 |
|
90 |
ok (my $e = $v->report, 'report'); |
91 |
|
92 |
diag $e if ( $debug ); |
93 |
|
94 |
ok( $v->save_delimiters_templates, 'save_delimiters_templates' ); |
95 |
|
96 |
ok( -s $delimiters_path, "$delimiters_path " . ( -s $delimiters_path ) . " bytes" ); |
97 |
|
98 |
ok( $v->save_delimiters_templates, 'save_delimiters_templates to existing file' ); |
99 |
|
100 |
ok( -s $delimiters_path . '.new' , "new file created" ); |
101 |
|
102 |
ok(my $v2 = new WebPAC::Validate( |
103 |
delimiters => [ ' : ', ' ; ', ' / ', ' \. ', ' = ' ], |
104 |
delimiters_path => $delimiters_path, |
105 |
debug => $debug, |
106 |
), "new"); |
107 |
|
108 |
is_deeply( $v->{_accumulated_delimiters_templates}, $v2->{_validate_delimiters_templates}, 'save/load ok'); |
109 |
|
110 |
diag dump( $v, $v2 ) if ( $debug ); |