5 use Test::More tests => 103;
13 use_ok( 'WebPAC::Normalize' );
16 use Data::Dump qw/dump/;
23 cmp_ok(_debug(1), '==', 1, '_debug level');
24 cmp_ok(_debug(0), '==', 0, '_debug level');
26 diag "debug level for $0 is $debug" if ($debug);
28 diag "debug level for WebPAC::Normalize is ", _debug( $debug - 2 );
31 ok(my $abs_path = abs_path($0), "abs_path");
32 $abs_path =~ s#/[^/]*$#/#;
33 diag "abs_path: $abs_path" if ($debug);
35 #throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";
68 'c' => 'New York University press',
85 'a' => 'xix, 202 str',
94 'e' => 'from Freud and Ferenczi to Michael balint',
95 'a' => 'Controversies in psychoanalytic method',
96 'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
97 'f' => 'by André E. Haynal'
99 '610' => [ 'povijest psihoanalize' ],
105 '320' => [ 'Kazalo' ],
108 '300' => [ 'Prijevod djela: ' ],
125 print dump( @_ ), ("-" x 78), "\n";
129 # how much of string evaled to display?
130 my $max_eval_output = 170;
133 my ($msg,$code) = @_;
135 my @l = split(/[\n\r]/, $code);
138 foreach my $i ( 0 .. $#l ) {
139 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
146 my $t = shift || die;
149 $eval_t =~ s/[\n\r\s]+/ /gs;
150 $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
151 $eval_t =~ s/\\/\\\\/gs;
154 ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t");
160 use WebPAC::Normalize;
162 ok(! _set_lookup( undef ), "set_lookup(undef)");
166 cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
167 cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
168 cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
169 diag "is_deeply checks\n";
170 is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
171 is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
172 is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
173 is_deeply( \[ rec('902') ], \[ '900' ] );
175 cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
177 # simple list manipulatons
178 cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
179 cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
180 cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
183 _set_lookup( $lookup1 );
193 'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
195 # check join_with operations
197 sub test_join_with_2 {
205 'eq', $e, "join_with $a <1> $b = $e");
208 test_join_with_2('_','_','');
209 test_join_with_2('x','_','201x');
210 test_join_with_2('_','x','201x');
211 test_join_with_2('x','y','201x <1> 201y');
213 sub test_join_with_3 {
214 my ($a,$b,$c,$e) = @_;
217 join_with(" <1> ", rec('201',$a),
218 join_with(" <2> ", rec('201',$b),
222 'eq', $e, "join_with $a <1> $b <2> $c = $e");
225 test_join_with_3('_','_','_','');
226 test_join_with_3('x','_','_','201x');
227 test_join_with_3('_','x','_','201x');
228 test_join_with_3('_','_','x','201x');
229 test_join_with_3('x','y','_','201x <1> 201y');
230 test_join_with_3('x','_','y','201x <1> 201y');
231 test_join_with_3('_','x','y','201x <2> 201y');
232 test_join_with_3('x','_','y','201x <1> 201y');
233 test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
237 _set_lookup( $lookup2 );
239 is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
241 ok(! lookup('non-existent'), 'lookup non-existant' );
267 regex( 's/[^\\d]+//',
273 ok(my $ds = _get_ds(), "get_ds");
274 diag "ds = ", dump($ds) if ($debug);
281 ok($ds = _get_ds(), 'get_ds');
282 diag dump( $ds ) if ($debug);
284 ok( $ds && $ds->{something}, 'get_ds->something exists' );
285 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
286 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
292 test_s(qq{ search('something', '42'); });
293 test_s(qq{ search('empty', ''); });
294 test_check_ds('search');
297 test_s(qq{ display('something', '42'); });
298 test_s(qq{ display('empty', ''); });
299 test_check_ds('display');
302 test_s(qq{ tag('something', '42'); });
303 test_s(qq{ tag('empty', ''); });
304 test_check_ds('search');
305 test_check_ds('display');
308 my $n = read_file( "$abs_path/data/normalize.pl" );
310 #diag "normalize code:\n$n\n";
313 ok($ds = _get_ds(), "get_ds");
314 diag "ds = ", dump($ds) if ($debug);
322 my $rules = qq{ search('mixed', rec('200') ) };
327 ok($ds = _get_ds(), "get_ds");
330 'search' => [ '200a', '200b' ],
333 }, 'correct get_ds');
335 ok(my $ds2 = WebPAC::Normalize::data_structure(
338 ), 'data_structure');
339 is_deeply( $ds, $ds2, 'data_structure(s) same');
341 # wird and non-valid structure which is supported anyway
350 test_s(qq{ search('mixed', rec('200') ) });
351 ok($ds = _get_ds(), "get_ds");
354 'search' => [ '200a', '200-solo' ],
357 }, 'correct get_ds');
363 test_s(qq{ marc_indicators('900',1,2) });
364 test_s(qq{ marc('900','a', rec('200') ) });
366 ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
367 diag dump( \@marc ) if ($debug);
370 [ '900', 1, 2, 'a', '200a' ],
371 [ '900', 1, 2, 'a', '200-solo' ]
372 ], 'correct marc with indicators');
374 test_s(qq{ marc_indicators('900',' ',9) });
375 test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
377 ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
378 diag dump( \@marc ) if ($debug);
381 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
382 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
383 ], 'correct marc with repetable subfield');
386 # test magic re-ordering of input data
390 my ($msg, $rec, $rules, $struct) = @_;
395 foreach my $r (split(/;/, $rules)) {
396 $r =~ s/[\s\n\r]+/ /gs;
402 ok(my @marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
403 diag dump( \@marc ) if ($debug);
404 diag "expects:\n", dump($struct) if ($debug > 1);
405 is_deeply( \@marc, $struct, $msg );
409 'correct marc with repetable subfield',
423 marc_indicators('900',1 ,0);
424 marc('900','a', rec('200','a') );
425 marc('900','b', rec('200','b') );
426 marc('900','c', rec('200','c') );
429 [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
430 [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
431 [ '900', 1, 0, 'a', '200a-3' ],
437 'marc_repeatable_subfield',
453 marc_indicators('900',1 ,0);
454 marc_repeatable_subfield('900','a', rec('200','a') );
455 marc('900','b', rec('200','b') );
456 marc('900','c', rec('200','c') );
459 [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
460 [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
461 [ '900', 1, 0, 'c', '200c-3' ],
467 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
476 [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
484 my ($msg, $rec, $rule, $struct) = @_;
488 my $r = test_s( $rule );
489 diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
490 diag dump($struct) if ($debug);
491 is_deeply( $r, $struct, $msg );
497 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
498 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
503 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
504 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
509 'marc_compose+split_rec_on',
510 { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
513 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
515 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
529 cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
530 cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
531 ok(marc_leader(), 'marc_leader get');
532 diag "leader: ", dump(marc_leader()) if ($debug);
533 is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");