r8999@llin: dpavlin | 2005-11-20 22:46:22 +0100
[webpac2] / t / 3-normalize-xml.t
1 #!/usr/bin/perl -w
2
3 use Test::More tests => 74;
4 use Test::Exception;
5 use Cwd qw/abs_path/;
6 use blib;
7 use strict;
8 use Data::Dumper;
9
10 BEGIN {
11 use_ok( 'WebPAC::Normalize::XML' );
12 }
13
14 ok(my $abs_path = abs_path($0), "abs_path");
15 $abs_path =~ s#/[^/]*$#/#;
16 diag "abs_path: $abs_path";
17
18 throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";
19 throws_ok { new WebPAC::Normalize::XML( lookup => 'bar' ) } qr/pair/, "lookup without lookup_regex";
20
21 ok(my $n = new WebPAC::Normalize::XML( debug => 0 ), "new");
22
23 throws_ok { $n->open() } qr/tag/, "open without tag";
24 throws_ok { $n->open( tag => 'isis' ) } qr/xml_file/, "open without xml_file";
25 throws_ok { $n->open( tag => 'isis', xml_file => '/foo/bar/baz' ) } qr/file.*doesn't exist/, "open with invalid xml_file";
26 ok( $n->open(
27         tag => 'isis',
28         xml_file => "$abs_path/data/normalize.xml",
29 ), "open");
30
31 my $rec = {
32  '675' => [
33             {
34               'a' => '159.9'
35             }
36           ],
37  '210' => [
38             {
39               'c' => 'New York University press',
40               'a' => 'New York',
41               'd' => 'cop. 1988'
42             }
43           ],
44  '700' => [
45             {
46               'a' => 'Haynal',
47               'b' => 'AndrĂ©'
48             }
49           ],
50  '801' => [
51             'FFZG'
52           ],
53  '991' => [
54             '8302'
55           ],
56  '000' => [
57             1
58           ],
59  '702' => [
60             {
61               'a' => 'Holder',
62               'b' => 'Elizabeth'
63             }
64           ],
65  '215' => [
66             {
67               'c' => 'ilustr',
68               'a' => 'xix, 202 str',
69               'd' => '23cm'
70             }
71           ],
72  '990' => [
73             '2140',
74             '88',
75             'HAY'
76           ],
77  '200' => [
78             {
79               'e' => 'from Freud and Ferenczi to Michael balint',
80               'a' => 'Controversies in psychoanalytic method',
81               'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
82               'f' => 'by AndrĂ© E. Haynal'
83             }
84           ],
85  '610' => [
86             'povijest psihoanalize'
87           ],
88  '994' => [
89             {
90               'c' => '',
91               'a' => 'PS',
92               'b' => 'MG'
93             }
94           ],
95  '320' => [
96             'Kazalo'
97           ],
98  '101' => [
99             'ENG'
100           ],
101  '686' => [
102             '2140'
103           ],
104  '300' => [
105             'Prijevod djela: '
106           ]
107 };
108
109 foreach my $fld (keys %$rec) {
110         my $r = 0;
111         foreach my $item ($rec->{$fld}) {
112                 if (ref($item) eq 'HASH') {
113                         foreach my $sf (keys %$item) {
114                                 my $found = 0;
115                                 ok($n->get_data(\$rec, $fld, $sf, $r, \$found), "v${fld}^${sf}");
116                                 ok($found, "found");
117                         }
118                         my $found = 0;
119                         ok(! $n->get_data(\$rec, $fld, 'x', $r, \$found), "no v${fld}^x");
120                         ok(! $found, "not found");
121                 } else {
122                         my $found = 0;
123                         ok($n->get_data(\$rec, $fld, '', $r, \$found), "v${fld}");
124                         ok($found, "found");
125                 }
126         }
127         my $found = 0;
128         ok(! $n->get_data(\$rec, '999', '', $r, \$found), "no v${fld}");
129         ok(! $found, "not found");
130 }
131
132 ok(my $ds = $n->data_structure( $rec ), "data_structure");
133
134 diag Dumper($rec, $ds);
135