1 |
dpavlin |
460 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
|
5 |
dpavlin |
490 |
use Test::More tests => 64; |
6 |
dpavlin |
460 |
use Test::Exception; |
7 |
|
|
use Cwd qw/abs_path/; |
8 |
|
|
use blib; |
9 |
|
|
use File::Slurp; |
10 |
|
|
|
11 |
|
|
use Data::Dumper; |
12 |
dpavlin |
472 |
my $debug = shift @ARGV; |
13 |
dpavlin |
460 |
|
14 |
|
|
BEGIN { |
15 |
|
|
use_ok( 'WebPAC::Normalize::Set' ); |
16 |
|
|
} |
17 |
|
|
|
18 |
|
|
ok(my $abs_path = abs_path($0), "abs_path"); |
19 |
dpavlin |
468 |
$abs_path =~ s#/[^/]*$#/#; |
20 |
dpavlin |
464 |
diag "abs_path: $abs_path" if ($debug); |
21 |
dpavlin |
460 |
|
22 |
|
|
#throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup"; |
23 |
|
|
|
24 |
dpavlin |
464 |
my $rec1 = { |
25 |
|
|
'200' => [{ |
26 |
|
|
'a' => '200a', |
27 |
|
|
'b' => '200b', |
28 |
|
|
},{ |
29 |
|
|
'c' => '200c', |
30 |
|
|
'd' => '200d', |
31 |
|
|
},{ |
32 |
|
|
'a' => '200a*2', |
33 |
|
|
'd' => '200d*2', |
34 |
|
|
}], |
35 |
|
|
'201' => [{ |
36 |
|
|
'x' => '201x', |
37 |
|
|
'y' => '201y', |
38 |
|
|
}], |
39 |
|
|
'900' => [ |
40 |
|
|
'900-no_subfield' |
41 |
|
|
], |
42 |
|
|
'901' => [{ |
43 |
|
|
'a' => '900a', |
44 |
|
|
}], |
45 |
|
|
'902' => [{ |
46 |
|
|
'z' => '900', |
47 |
|
|
}], |
48 |
|
|
}; |
49 |
|
|
|
50 |
|
|
my $rec2 = { |
51 |
dpavlin |
460 |
'675' => [ { |
52 |
|
|
'a' => '159.9' |
53 |
|
|
} ], |
54 |
|
|
'210' => [ { |
55 |
|
|
'c' => 'New York University press', |
56 |
|
|
'a' => 'New York', |
57 |
|
|
'd' => 'cop. 1988' |
58 |
|
|
} ], |
59 |
|
|
'700' => [ { |
60 |
|
|
'a' => 'Haynal', |
61 |
|
|
'b' => 'André' |
62 |
|
|
} ], |
63 |
|
|
'801' => [ 'FFZG' ], |
64 |
|
|
'991' => [ '8302' ], |
65 |
|
|
'000' => [ 1 ], |
66 |
|
|
'702' => [ { |
67 |
|
|
'a' => 'Holder', |
68 |
|
|
'b' => 'Elizabeth' |
69 |
|
|
} ], |
70 |
|
|
'215' => [ { |
71 |
|
|
'c' => 'ilustr', |
72 |
|
|
'a' => 'xix, 202 str', |
73 |
|
|
'd' => '23cm' |
74 |
|
|
} ], |
75 |
|
|
'990' => [ |
76 |
|
|
'2140', |
77 |
|
|
'88', |
78 |
|
|
'HAY' |
79 |
|
|
], |
80 |
|
|
'200' => [ { |
81 |
|
|
'e' => 'from Freud and Ferenczi to Michael balint', |
82 |
|
|
'a' => 'Controversies in psychoanalytic method', |
83 |
|
|
'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern', |
84 |
|
|
'f' => 'by André E. Haynal' |
85 |
|
|
} ], |
86 |
|
|
'610' => [ 'povijest psihoanalize' ], |
87 |
|
|
'994' => [ { |
88 |
|
|
'c' => '', |
89 |
|
|
'a' => 'PS', |
90 |
|
|
'b' => 'MG' |
91 |
|
|
} ], |
92 |
|
|
'320' => [ 'Kazalo' ], |
93 |
|
|
'101' => [ 'ENG' ], |
94 |
|
|
'686' => [ '2140' ], |
95 |
|
|
'300' => [ 'Prijevod djela: ' ], |
96 |
|
|
}; |
97 |
|
|
|
98 |
|
|
|
99 |
dpavlin |
464 |
my $lookup1 = { |
100 |
dpavlin |
460 |
'00900' => [ |
101 |
dpavlin |
464 |
'lookup 1', |
102 |
|
|
'lookup 2', |
103 |
dpavlin |
460 |
], |
104 |
|
|
}; |
105 |
|
|
|
106 |
dpavlin |
464 |
my $lookup2 = { |
107 |
|
|
'00900' => 'lookup', |
108 |
|
|
}; |
109 |
dpavlin |
460 |
|
110 |
dpavlin |
464 |
|
111 |
dpavlin |
460 |
sub test { |
112 |
|
|
print Dumper( @_ ), ("-" x 78), "\n"; |
113 |
|
|
ok( defined(@_) ); |
114 |
|
|
} |
115 |
|
|
|
116 |
dpavlin |
471 |
# how much of string evaled to display? |
117 |
|
|
my $max_eval_output = 170; |
118 |
|
|
|
119 |
|
|
sub dump_error { |
120 |
|
|
my ($msg,$code) = @_; |
121 |
|
|
|
122 |
|
|
my @l = split(/[\n\r]/, $code); |
123 |
|
|
my $out = "$msg\n"; |
124 |
|
|
|
125 |
|
|
foreach my $i ( 0 .. $#l ) { |
126 |
|
|
$out .= sprintf("%2d: %s\n", $i, $l[$i]); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
return $out; |
130 |
|
|
} |
131 |
|
|
|
132 |
dpavlin |
460 |
sub test_s { |
133 |
|
|
my $t = shift || die; |
134 |
dpavlin |
471 |
|
135 |
|
|
my $eval_t = $t; |
136 |
|
|
$eval_t =~ s/[\n\r\s]+/ /gs; |
137 |
|
|
$eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output); |
138 |
|
|
|
139 |
|
|
eval "$t"; |
140 |
|
|
ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t"); |
141 |
dpavlin |
460 |
} |
142 |
|
|
|
143 |
|
|
{ |
144 |
|
|
no strict 'subs'; |
145 |
|
|
use WebPAC::Normalize::Set; |
146 |
|
|
|
147 |
dpavlin |
476 |
ok(! set_lookup( undef ), "set_lookup(undef)"); |
148 |
|
|
|
149 |
dpavlin |
464 |
set_rec( $rec1 ); |
150 |
dpavlin |
460 |
|
151 |
dpavlin |
471 |
cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' ); |
152 |
|
|
cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' ); |
153 |
|
|
cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' ); |
154 |
|
|
diag "is_deeply checks\n"; |
155 |
dpavlin |
464 |
is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] ); |
156 |
|
|
is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]); |
157 |
|
|
is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]); |
158 |
|
|
is_deeply( \[ rec('902') ], \[ '900' ] ); |
159 |
|
|
|
160 |
dpavlin |
471 |
cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' ); |
161 |
|
|
|
162 |
dpavlin |
464 |
set_lookup( $lookup1 ); |
163 |
|
|
|
164 |
|
|
cmp_ok( |
165 |
dpavlin |
460 |
join_with(" i ", |
166 |
|
|
lookup( |
167 |
|
|
regex( 's/^/00/', |
168 |
dpavlin |
464 |
rec2('902','z') |
169 |
dpavlin |
460 |
) |
170 |
|
|
) |
171 |
dpavlin |
464 |
), |
172 |
dpavlin |
471 |
'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2'); |
173 |
dpavlin |
460 |
|
174 |
dpavlin |
485 |
# check join_with operations |
175 |
|
|
|
176 |
|
|
sub test_join_with_2 { |
177 |
|
|
my ($a,$b,$e) = @_; |
178 |
|
|
|
179 |
|
|
cmp_ok( |
180 |
|
|
join_with(" <1> ", |
181 |
|
|
rec('201',$a), |
182 |
|
|
rec('201',$b), |
183 |
|
|
), |
184 |
|
|
'eq', $e, "join_with $a <1> $b = $e"); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
test_join_with_2('_','_',''); |
188 |
|
|
test_join_with_2('x','_','201x'); |
189 |
|
|
test_join_with_2('_','x','201x'); |
190 |
|
|
test_join_with_2('x','y','201x <1> 201y'); |
191 |
|
|
|
192 |
|
|
sub test_join_with_3 { |
193 |
|
|
my ($a,$b,$c,$e) = @_; |
194 |
|
|
|
195 |
|
|
cmp_ok( |
196 |
|
|
join_with(" <1> ", rec('201',$a), |
197 |
|
|
join_with(" <2> ", rec('201',$b), |
198 |
|
|
rec('201',$c), |
199 |
|
|
) |
200 |
|
|
), |
201 |
|
|
'eq', $e, "join_with $a <1> $b <2> $c = $e"); |
202 |
|
|
}; |
203 |
|
|
|
204 |
|
|
test_join_with_3('_','_','_',''); |
205 |
|
|
test_join_with_3('x','_','_','201x'); |
206 |
|
|
test_join_with_3('_','x','_','201x'); |
207 |
|
|
test_join_with_3('_','_','x','201x'); |
208 |
|
|
test_join_with_3('x','y','_','201x <1> 201y'); |
209 |
|
|
test_join_with_3('x','_','y','201x <1> 201y'); |
210 |
|
|
test_join_with_3('_','x','y','201x <2> 201y'); |
211 |
|
|
test_join_with_3('x','_','y','201x <1> 201y'); |
212 |
|
|
test_join_with_3('x','y','x','201x <1> 201y <2> 201x'); |
213 |
|
|
|
214 |
|
|
# test lookups |
215 |
|
|
|
216 |
dpavlin |
464 |
set_lookup( $lookup2 ); |
217 |
|
|
|
218 |
dpavlin |
471 |
is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' ); |
219 |
dpavlin |
464 |
|
220 |
dpavlin |
471 |
ok(! lookup('non-existent'), 'lookup non-existant' ); |
221 |
dpavlin |
464 |
|
222 |
|
|
set_rec( $rec2 ); |
223 |
|
|
|
224 |
dpavlin |
460 |
test_s(qq{ |
225 |
|
|
tag('Title', |
226 |
|
|
rec('200','a') |
227 |
|
|
); |
228 |
|
|
}); |
229 |
|
|
test_s(qq{ |
230 |
|
|
tag('Who', |
231 |
|
|
join_with(" ", |
232 |
|
|
rec('702','a'), |
233 |
|
|
rec('702','b') |
234 |
|
|
) |
235 |
|
|
); |
236 |
|
|
}); |
237 |
|
|
|
238 |
|
|
test_s(qq{ |
239 |
|
|
display('Publisher', |
240 |
|
|
rec('210','c') |
241 |
|
|
) |
242 |
|
|
}); |
243 |
|
|
|
244 |
|
|
test_s(qq{ |
245 |
|
|
search('Year', |
246 |
|
|
regex( 's/[^\\d]+//', |
247 |
|
|
rec('210','d') |
248 |
|
|
) |
249 |
|
|
) |
250 |
|
|
}); |
251 |
|
|
|
252 |
|
|
ok(my $ds = get_ds(), "get_ds"); |
253 |
dpavlin |
464 |
diag "ds = ", Dumper($ds) if ($debug); |
254 |
dpavlin |
460 |
|
255 |
dpavlin |
472 |
|
256 |
|
|
sub test_check_ds { |
257 |
|
|
|
258 |
|
|
my $t = shift; |
259 |
|
|
|
260 |
|
|
ok($ds = get_ds(), 'get_ds'); |
261 |
|
|
diag Dumper( $ds ) if ($debug); |
262 |
|
|
|
263 |
|
|
ok( $ds && $ds->{something}, 'get_ds->something exists' ); |
264 |
|
|
ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t); |
265 |
|
|
ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' ); |
266 |
|
|
|
267 |
|
|
return $ds; |
268 |
|
|
} |
269 |
|
|
|
270 |
dpavlin |
461 |
clean_ds(); |
271 |
dpavlin |
471 |
test_s(qq{ search('something', '42'); }); |
272 |
|
|
test_s(qq{ search('empty', ''); }); |
273 |
dpavlin |
472 |
test_check_ds('search'); |
274 |
dpavlin |
471 |
|
275 |
dpavlin |
472 |
clean_ds(); |
276 |
|
|
test_s(qq{ display('something', '42'); }); |
277 |
|
|
test_s(qq{ display('empty', ''); }); |
278 |
|
|
test_check_ds('display'); |
279 |
dpavlin |
471 |
|
280 |
dpavlin |
472 |
clean_ds(); |
281 |
|
|
test_s(qq{ tag('something', '42'); }); |
282 |
|
|
test_s(qq{ tag('empty', ''); }); |
283 |
|
|
test_check_ds('search'); |
284 |
|
|
test_check_ds('display'); |
285 |
dpavlin |
471 |
|
286 |
|
|
clean_ds(); |
287 |
dpavlin |
468 |
my $n = read_file( "$abs_path/data/normalize.pl" ); |
288 |
dpavlin |
461 |
$n .= "\n1;\n"; |
289 |
dpavlin |
460 |
#diag "normalize code:\n$n\n"; |
290 |
dpavlin |
461 |
test_s( $n ); |
291 |
|
|
|
292 |
|
|
ok($ds = get_ds(), "get_ds"); |
293 |
dpavlin |
464 |
diag "ds = ", Dumper($ds) if ($debug); |
294 |
dpavlin |
490 |
|
295 |
|
|
my $rec = { |
296 |
|
|
'200' => [{ |
297 |
|
|
'a' => '200a', |
298 |
|
|
'b' => '200b', |
299 |
|
|
}], |
300 |
|
|
}; |
301 |
|
|
my $rules = qq{ search('mixed', rec('200') ) }; |
302 |
|
|
|
303 |
|
|
clean_ds(); |
304 |
|
|
set_rec( $rec ); |
305 |
|
|
test_s( $rules ); |
306 |
|
|
ok($ds = get_ds(), "get_ds"); |
307 |
|
|
is_deeply( $ds, { |
308 |
|
|
'mixed' => { |
309 |
|
|
'search' => [ '200a', '200b' ], |
310 |
|
|
'tag' => 'mixed' |
311 |
|
|
} |
312 |
|
|
}, 'correct get_ds'); |
313 |
|
|
|
314 |
|
|
ok(my $ds2 = WebPAC::Normalize::Set::data_structure( |
315 |
|
|
row => $rec, |
316 |
|
|
rules => $rules, |
317 |
|
|
), 'data_structure'); |
318 |
|
|
is_deeply( $ds, $ds2, 'data_structure(s) same'); |
319 |
|
|
|
320 |
|
|
# wird and non-valid structure which is supported anyway |
321 |
|
|
clean_ds(); |
322 |
|
|
set_rec({ |
323 |
|
|
'200' => [{ |
324 |
|
|
'a' => '200a', |
325 |
|
|
}, |
326 |
|
|
'200-solo' |
327 |
|
|
] |
328 |
|
|
}); |
329 |
|
|
test_s(qq{ search('mixed', rec('200') ) }); |
330 |
|
|
ok($ds = get_ds(), "get_ds"); |
331 |
|
|
is_deeply( $ds, { |
332 |
|
|
'mixed' => { |
333 |
|
|
'search' => [ '200a', '200-solo' ], |
334 |
|
|
'tag' => 'mixed' |
335 |
|
|
} |
336 |
|
|
}, 'correct get_ds'); |
337 |
|
|
|
338 |
dpavlin |
460 |
} |
339 |
|
|
|