2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 89; |
use Test::More tests => 147; |
6 |
use Test::Exception; |
use Test::Exception; |
7 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
8 |
use blib; |
use blib; |
148 |
my $eval_t = $t; |
my $eval_t = $t; |
149 |
$eval_t =~ s/[\n\r\s]+/ /gs; |
$eval_t =~ s/[\n\r\s]+/ /gs; |
150 |
$eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output); |
$eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output); |
151 |
|
$eval_t =~ s/\\/\\\\/gs; |
152 |
|
|
153 |
eval "$t"; |
my @__ret; |
154 |
ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t"); |
eval "\@__ret = $t"; |
155 |
|
ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret)); |
156 |
|
return \@__ret; |
157 |
} |
} |
158 |
|
|
159 |
{ |
{ |
360 |
# |
# |
361 |
# MARC |
# MARC |
362 |
# |
# |
363 |
|
_debug( 4 ); |
364 |
|
|
365 |
test_s(qq{ marc_indicators('900',1,2) }); |
test_s(qq{ marc_indicators('900',1,2) }); |
366 |
test_s(qq{ marc('900','a', rec('200') ) }); |
test_s(qq{ marc('900','a', rec('200') ) }); |
367 |
my @marc; |
my $marc; |
368 |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
369 |
diag dump( \@marc ) if ($debug); |
diag dump( $marc ) if ($debug); |
370 |
|
|
371 |
is_deeply( \@marc, [ |
is_deeply( $marc, [ |
372 |
[ '900', 1, 2, 'a', '200a' ], |
[ '900', 1, 2, 'a', '200a' ], |
373 |
[ '900', 1, 2, 'a', '200-solo' ] |
[ '900', 1, 2, 'a', '200-solo' ] |
374 |
], 'correct marc with indicators'); |
], 'correct marc with indicators'); |
376 |
test_s(qq{ marc_indicators('900',' ',9) }); |
test_s(qq{ marc_indicators('900',' ',9) }); |
377 |
test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) }); |
test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) }); |
378 |
|
|
379 |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
380 |
diag dump( \@marc ) if ($debug); |
diag dump( $marc ) if ($debug); |
381 |
|
|
382 |
is_deeply( \@marc, [ |
is_deeply( $marc, [ |
383 |
[ '900', 1, 2, 'a', '200a', 'a', '200-solo' ], |
[ '900', 1, 2, 'a', '200a', 'a', '200-solo' ], |
384 |
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
385 |
], 'correct marc with repetable subfield'); |
], 'correct marc with repetable subfield'); |
401 |
test_s($r) if ($r); |
test_s($r) if ($r); |
402 |
} |
} |
403 |
|
|
404 |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
405 |
diag dump( \@marc ) if ($debug); |
diag dump( $marc ) if ($debug); |
406 |
|
diag "expects:\n", dump($struct) if ($debug > 1); |
407 |
is_deeply( \@marc, $struct, $msg ); |
is_deeply( $marc, $struct, $msg ); |
408 |
} |
} |
409 |
|
|
410 |
test_rec_rules( |
test_rec_rules( |
463 |
[ '900', 1, 0, 'c', '200c-3' ], |
[ '900', 1, 0, 'c', '200c-3' ], |
464 |
], |
], |
465 |
); |
); |
466 |
|
|
467 |
|
test_rec_rules( |
468 |
|
'marc_compose', |
469 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
470 |
|
qq{ |
471 |
|
marc_compose('900', |
472 |
|
'c', rec(200,'b'), |
473 |
|
'b', rec(200,'a'), |
474 |
|
'a', rec(200,'c'), |
475 |
|
); |
476 |
|
}, |
477 |
|
[ |
478 |
|
[ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ] |
479 |
|
], |
480 |
|
); |
481 |
|
|
482 |
|
# |
483 |
|
# test rules |
484 |
|
# |
485 |
|
sub test_rule { |
486 |
|
my ($msg, $rec, $rule, $struct) = @_; |
487 |
|
_clean_ds(); |
488 |
|
_set_rec( $rec ); |
489 |
|
$rule =~ s/\\/\\/gs; |
490 |
|
my $r = test_s( $rule ); |
491 |
|
diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1); |
492 |
|
diag dump($struct) if ($debug); |
493 |
|
is_deeply( $r, $struct, $msg ); |
494 |
|
} |
495 |
|
|
496 |
|
# test split_rec_on |
497 |
|
test_rule( |
498 |
|
'split_rec_on', |
499 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
500 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) }, |
501 |
|
[ 'foo' ], |
502 |
|
); |
503 |
|
test_rule( |
504 |
|
'split_rec_on', |
505 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
506 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) }, |
507 |
|
[ 'bar' ], |
508 |
|
); |
509 |
|
test_rule( |
510 |
|
'split_rec_on no part', |
511 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
512 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/) }, |
513 |
|
[ 'foo', 'bar' ], |
514 |
|
); |
515 |
|
test_rule( |
516 |
|
'split_rec_on no record', |
517 |
|
{}, |
518 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/) }, |
519 |
|
[ '' ], |
520 |
|
); |
521 |
|
|
522 |
|
test_rec_rules( |
523 |
|
'marc_compose+split_rec_on', |
524 |
|
{ '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] }, |
525 |
|
qq{ |
526 |
|
marc_compose('900', |
527 |
|
'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1), |
528 |
|
'c', rec(200,'c'), |
529 |
|
'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2), |
530 |
|
'b', rec(200,'b'), |
531 |
|
); |
532 |
|
}, |
533 |
|
[ |
534 |
|
[ '900', ' ', ' ', |
535 |
|
'a', 'foo', |
536 |
|
'c', 'baz', |
537 |
|
'a', 'bar', |
538 |
|
'b', 42, |
539 |
|
] |
540 |
|
], |
541 |
|
); |
542 |
|
|
543 |
|
cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader'); |
544 |
|
cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader'); |
545 |
|
ok(marc_leader(), 'marc_leader get'); |
546 |
|
diag "leader: ", dump(marc_leader()) if ($debug); |
547 |
|
is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full"); |
548 |
|
|
549 |
|
test_rule( |
550 |
|
'rec1(000)', |
551 |
|
{ '000' => [ 42 ]}, |
552 |
|
qq{ rec('000') }, |
553 |
|
[ 42 ], |
554 |
|
); |
555 |
|
|
556 |
|
test_rec_rules( |
557 |
|
'marc(001,rec(000))', |
558 |
|
{ '000' => [ 42 ]}, |
559 |
|
qq{ |
560 |
|
marc('001', rec('000') ); |
561 |
|
}, |
562 |
|
[ |
563 |
|
[ '001', 42, ] |
564 |
|
], |
565 |
|
); |
566 |
|
|
567 |
|
test_rec_rules( |
568 |
|
'marc_remove subfield', |
569 |
|
{ '200' => [{ a => 42, b => 'bar', c => 'baz' }] }, |
570 |
|
qq{ |
571 |
|
marc('900', 'a', rec('200','a') ); |
572 |
|
marc('900', 'b', rec('200','b') ); |
573 |
|
marc_remove('900','b'); |
574 |
|
marc('900', 'b', rec('200','c') ); |
575 |
|
marc_remove('900','a'); |
576 |
|
}, |
577 |
|
[ |
578 |
|
[ '900', ' ', ' ', 'b', 'baz' ], |
579 |
|
], |
580 |
|
); |
581 |
|
|
582 |
|
test_rec_rules( |
583 |
|
'marc_remove field', |
584 |
|
{ '200' => [{ a => 42, b => 'bar', c => 'baz' }] }, |
585 |
|
qq{ |
586 |
|
marc('900', 'a', rec('200','a') ); |
587 |
|
marc('900', 'b', rec('200','b') ); |
588 |
|
marc('901', 'b', rec('200','b') ); |
589 |
|
marc('901', 'c', rec('200','c') ); |
590 |
|
marc_remove('900'); |
591 |
|
}, |
592 |
|
[ |
593 |
|
[ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ], |
594 |
|
], |
595 |
|
); |
596 |
|
test_rec_rules( |
597 |
|
'marc_duplicate', |
598 |
|
{ '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] }, |
599 |
|
qq{ |
600 |
|
marc('900', 'a', rec('200','a') ); |
601 |
|
marc('900', 'b', rec('200','b') ); |
602 |
|
marc_duplicate; |
603 |
|
marc_remove('900','b'); |
604 |
|
marc('900', 'b', rec('200','c') ); |
605 |
|
marc_duplicate; |
606 |
|
marc_remove('900','b'); |
607 |
|
marc('900', 'b', rec('200','d') ); |
608 |
|
marc_duplicate; |
609 |
|
marc_remove('900','b'); |
610 |
|
marc('900', 'b', rec('200','e') ); |
611 |
|
}, |
612 |
|
[ |
613 |
|
# this will return FIRST record |
614 |
|
[ '900', ' ', ' ', 'a', 42, 'b', 'bar' ], |
615 |
|
], |
616 |
|
); |
617 |
|
|
618 |
|
my $i = 0; |
619 |
|
foreach my $v ( qw/bar baz bing bong/ ) { |
620 |
|
|
621 |
|
ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ), |
622 |
|
"_get_marc_fields( offset => $i )" |
623 |
|
); |
624 |
|
diag "marc $i = ", dump( $marc ) if ($debug); |
625 |
|
is_deeply( $marc, |
626 |
|
[ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ], |
627 |
|
"MARC copy $i has $v", |
628 |
|
); |
629 |
|
$i++; |
630 |
|
} |
631 |
} |
} |
632 |
|
|