5 use Test::More tests => 157;
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;
155 ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
161 use WebPAC::Normalize;
163 ok(! _set_lookup( undef ), "set_lookup(undef)");
167 cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
168 cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
169 cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
170 diag "is_deeply checks\n";
171 is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
172 is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
173 is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
174 is_deeply( \[ rec('902') ], \[ '900' ] );
176 cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
178 # simple list manipulatons
179 cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
180 cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
181 cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
184 _set_lookup( $lookup1 );
194 'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
196 # check join_with operations
198 sub test_join_with_2 {
206 'eq', $e, "join_with $a <1> $b = $e");
209 test_join_with_2('_','_','');
210 test_join_with_2('x','_','201x');
211 test_join_with_2('_','x','201x');
212 test_join_with_2('x','y','201x <1> 201y');
214 sub test_join_with_3 {
215 my ($a,$b,$c,$e) = @_;
218 join_with(" <1> ", rec('201',$a),
219 join_with(" <2> ", rec('201',$b),
223 'eq', $e, "join_with $a <1> $b <2> $c = $e");
226 test_join_with_3('_','_','_','');
227 test_join_with_3('x','_','_','201x');
228 test_join_with_3('_','x','_','201x');
229 test_join_with_3('_','_','x','201x');
230 test_join_with_3('x','y','_','201x <1> 201y');
231 test_join_with_3('x','_','y','201x <1> 201y');
232 test_join_with_3('_','x','y','201x <2> 201y');
233 test_join_with_3('x','_','y','201x <1> 201y');
234 test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
238 _set_lookup( $lookup2 );
240 is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
242 ok(! lookup('non-existent'), 'lookup non-existant' );
268 regex( 's/[^\\d]+//',
274 ok(my $ds = _get_ds(), "get_ds");
275 diag "ds = ", dump($ds) if ($debug);
282 ok($ds = _get_ds(), 'get_ds');
283 diag dump( $ds ) if ($debug);
285 ok( $ds && $ds->{something}, 'get_ds->something exists' );
286 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
287 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
293 test_s(qq{ search('something', '42'); });
294 test_s(qq{ search('empty', ''); });
295 test_check_ds('search');
298 test_s(qq{ display('something', '42'); });
299 test_s(qq{ display('empty', ''); });
300 test_check_ds('display');
303 test_s(qq{ tag('something', '42'); });
304 test_s(qq{ tag('empty', ''); });
305 test_check_ds('search');
306 test_check_ds('display');
309 my $n = read_file( "$abs_path/data/normalize.pl" );
311 #diag "normalize code:\n$n\n";
314 ok($ds = _get_ds(), "get_ds");
315 diag "ds = ", dump($ds) if ($debug);
323 my $rules = qq{ search('mixed', rec('200') ) };
328 ok($ds = _get_ds(), "get_ds");
331 'search' => [ '200a', '200b' ],
334 }, 'correct get_ds');
336 ok(my $ds2 = WebPAC::Normalize::data_structure(
339 ), 'data_structure');
340 is_deeply( $ds, $ds2, 'data_structure(s) same');
342 # wird and non-valid structure which is supported anyway
351 test_s(qq{ search('mixed', rec('200') ) });
352 ok($ds = _get_ds(), "get_ds");
355 'search' => [ '200a', '200-solo' ],
358 }, 'correct get_ds');
365 test_s(qq{ marc_indicators('900',1,2) });
366 test_s(qq{ marc('900','a', rec('200') ) });
368 ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
369 diag dump( $marc ) if ($debug);
372 [ '900', 1, 2, 'a', '200a' ],
373 [ '900', 1, 2, 'a', '200-solo' ]
374 ], 'correct marc with indicators');
376 test_s(qq{ marc_indicators('900',' ',9) });
377 test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
379 ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
380 diag dump( $marc ) if ($debug);
383 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
384 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
385 ], 'correct marc with repetable subfield');
388 # test magic re-ordering of input data
392 my ($msg, $rec, $rules, $struct) = @_;
397 foreach my $r (split(/;/, $rules)) {
398 $r =~ s/[\s\n\r]+/ /gs;
404 ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
405 diag dump( $marc ) if ($debug);
406 diag "expects:\n", dump($struct) if ($debug > 1);
407 is_deeply( $marc, $struct, $msg );
411 'correct marc with repetable subfield',
425 marc_indicators('900',1 ,0);
426 marc('900','a', rec('200','a') );
427 marc('900','b', rec('200','b') );
428 marc('900','c', rec('200','c') );
431 [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
432 [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
433 [ '900', 1, 0, 'a', '200a-3' ],
439 'marc_repeatable_subfield',
455 marc_indicators('900',1 ,0);
456 marc_repeatable_subfield('900','a', rec('200','a') );
457 marc('900','b', rec('200','b') );
458 marc('900','c', rec('200','c') );
461 [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
462 [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
463 [ '900', 1, 0, 'c', '200c-3' ],
469 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
478 [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
483 'marc_compose with + subfields',
484 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
488 '+', prefix(" * ", rec(200,'c')),
490 '+', prefix(" : ", rec(200,'c')),
494 [ '900', ' ', ' ', 'a', 'foo ; bar * baz', 'b', '42 : baz' ]
502 my ($msg, $rec, $rule, $struct) = @_;
506 my $r = test_s( $rule );
507 diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
508 diag dump($struct) if ($debug);
509 is_deeply( $r, $struct, $msg );
515 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
516 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
521 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
522 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
526 'split_rec_on no part',
527 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
528 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
532 'split_rec_on no record',
534 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
539 'marc_compose+split_rec_on',
540 { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
543 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
545 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
559 cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
560 cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
561 ok(marc_leader(), 'marc_leader get');
562 diag "leader: ", dump(marc_leader()) if ($debug);
563 is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
573 'marc(001,rec(000))',
576 marc('001', rec('000') );
584 'marc_remove subfield',
585 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
587 marc('900', 'a', rec('200','a') );
588 marc('900', 'b', rec('200','b') );
589 marc_remove('900','b');
590 marc('900', 'b', rec('200','c') );
591 marc_remove('900','a');
594 [ '900', ' ', ' ', 'b', 'baz' ],
600 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
602 marc('900', 'a', rec('200','a') );
603 marc('900', 'b', rec('200','b') );
604 marc('901', 'b', rec('200','b') );
605 marc('901', 'c', rec('200','c') );
609 [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
614 { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
616 marc('900', 'a', rec('200','a') );
617 marc('900', 'b', rec('200','b') );
619 marc_remove('900','b');
620 marc('900', 'b', rec('200','c') );
622 marc_remove('900','b');
623 marc('900', 'b', rec('200','d') );
625 marc_remove('900','b');
626 marc('900', 'b', rec('200','e') );
629 # this will return FIRST record
630 [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
635 foreach my $v ( qw/bar baz bing bong/ ) {
637 ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
638 "_get_marc_fields( offset => $i )"
640 diag "marc $i = ", dump( $marc ) if ($debug);
642 [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
643 "MARC copy $i has $v",
649 'marc_original_order',
652 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
653 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
655 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
656 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
660 marc_original_order(900,200);
663 [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
664 [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
669 'rec1 skips subfields',
672 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
673 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
675 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
676 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
682 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
686 [ _pack_subfields_hash({
687 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
688 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
690 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
691 '_pack_subfields_hash( $h )'
695 _pack_subfields_hash({
696 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
697 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
700 '^aa1^bb1^aa2^bb2^cc1^cc2',
701 '_pack_subfields_hash( $h, 1 )'