/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 653 - (show annotations)
Thu Sep 7 15:57:48 2006 UTC (17 years, 8 months ago) by dpavlin
File MIME type: application/x-troff
File size: 1747 byte(s)
added reset_errors and all_errors to validator (real reporter is still pending),
rewriten validator tests

1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More tests => 29;
5 use Test::Exception;
6 use blib;
7
8 use Data::Dump qw/dump/;
9 use Cwd qw/abs_path/;
10
11 BEGIN {
12 use_ok( 'WebPAC::Validate' );
13 }
14
15 my $debug = shift @ARGV;
16
17 ok(my $abs_path = abs_path($0), "abs_path");
18 $abs_path =~ s#/[^/]*$#/#;
19
20 throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path";
21
22 ok(my $v = new WebPAC::Validate(
23 path => "$abs_path/data/validate_test",
24 ), "new");
25
26 ok($v->{rules}, "rules exist");
27
28 is_deeply( $v->{rules}, {
29 '900' => 1,
30 '901' => [ 'a' ],
31 '902' => [ 'b', 'c' ],
32 '903' => [ 'a', 'b', 'c' ],
33 }, 'rules parsed');
34
35 throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec";
36
37 sub test_v {
38 my $row = shift || die "no row?";
39
40 my $d = dump( $row );
41
42 $row->{'000'} = [ 42 ];
43
44 $v->reset_errors;
45 my $e = $v->validate_errors( $row );
46
47 diag "validate $d\n",dump($e) if ($debug);
48
49 if (@_) {
50 my $tmp = $e;
51 while (@_) {
52 my $k = shift @_;
53 ok($tmp = $tmp->{$k}, "found $k");
54 }
55 diag "additional message: $tmp" if ($tmp);
56 } else {
57 ok(! $e, "validated $d");
58 diag "expected error: ", dump($e) if($e);
59 }
60
61 }
62
63 test_v({
64 '900' => 'foo'
65 }, qw/field 900 not_repeatable/);
66
67 test_v({
68 '900' => [ qw/foo bar baz/ ]
69 });
70
71 test_v({
72 '901' => [ qw/foo bar baz/ ]
73 }, qw/field 901 missing_subfield/);
74
75 test_v({
76 '901' => [ { 'a' => 42 } ]
77 });
78
79 test_v({
80 '901' => [ { 'b' => 42 } ]
81 }, qw/field 901 subfield b/);
82
83 test_v({
84 '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
85 });
86
87 test_v({
88 '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
89 }, qw/field 902 subfield a/);
90
91 test_v({
92 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
93 });
94
95 test_v({
96 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
97 }, qw/field 903 subfield d/);
98

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26