2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 81; |
use Test::More tests => 107; |
6 |
use Test::Exception; |
use Test::Exception; |
7 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
8 |
use blib; |
use blib; |
9 |
use File::Slurp; |
use File::Slurp; |
10 |
|
use Getopt::Long; |
|
use Data::Dump qw/dump/; |
|
|
my $debug = shift @ARGV; |
|
11 |
|
|
12 |
BEGIN { |
BEGIN { |
13 |
use_ok( 'WebPAC::Normalize' ); |
use_ok( 'WebPAC::Normalize' ); |
14 |
} |
} |
15 |
|
|
16 |
|
use Data::Dump qw/dump/; |
17 |
|
|
18 |
|
my $debug = 0; |
19 |
|
GetOptions( |
20 |
|
"debug+", \$debug |
21 |
|
); |
22 |
|
|
23 |
|
cmp_ok(_debug(1), '==', 1, '_debug level'); |
24 |
|
cmp_ok(_debug(0), '==', 0, '_debug level'); |
25 |
|
|
26 |
|
diag "debug level for $0 is $debug" if ($debug); |
27 |
|
if ($debug > 2) { |
28 |
|
diag "debug level for WebPAC::Normalize is ", _debug( $debug - 2 ); |
29 |
|
} |
30 |
|
|
31 |
ok(my $abs_path = abs_path($0), "abs_path"); |
ok(my $abs_path = abs_path($0), "abs_path"); |
32 |
$abs_path =~ s#/[^/]*$#/#; |
$abs_path =~ s#/[^/]*$#/#; |
33 |
diag "abs_path: $abs_path" if ($debug); |
diag "abs_path: $abs_path" if ($debug); |
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 $v = eval "$t"; |
154 |
ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t"); |
ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t"); |
155 |
|
$v; |
156 |
} |
} |
157 |
|
|
158 |
{ |
{ |
356 |
} |
} |
357 |
}, 'correct get_ds'); |
}, 'correct get_ds'); |
358 |
|
|
359 |
|
# |
360 |
# MARC |
# MARC |
361 |
|
# |
362 |
|
|
363 |
test_s(qq{ marc_indicators('900',1,2) }); |
test_s(qq{ marc_indicators('900',1,2) }); |
364 |
test_s(qq{ marc('900','a', rec('200') ) }); |
test_s(qq{ marc('900','a', rec('200') ) }); |
365 |
my @marc; |
my @marc; |
382 |
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
383 |
], 'correct marc with repetable subfield'); |
], 'correct marc with repetable subfield'); |
384 |
|
|
385 |
_clean_ds(); |
# |
386 |
_set_rec({ |
# test magic re-ordering of input data |
387 |
'200' => [{ |
# |
388 |
'a' => '200a-1', |
|
389 |
'b' => '200b-1', |
sub test_rec_rules { |
390 |
'c' => '200c-1', |
my ($msg, $rec, $rules, $struct) = @_; |
391 |
}, { |
|
392 |
'a' => '200a-2', |
_clean_ds(); |
393 |
'b' => '200b-2', |
_set_rec($rec); |
394 |
'c' => '200c-2', |
|
395 |
}, { |
foreach my $r (split(/;/, $rules)) { |
396 |
'a' => '200a-3', |
$r =~ s/[\s\n\r]+/ /gs; |
397 |
'c' => '200c-3', |
$r =~ s/^\s+//gs; |
398 |
}] |
$r =~ s/\s+$//gs; |
399 |
}); |
test_s($r) if ($r); |
400 |
test_s(qq{ marc_indicators('900',1 ,0) }); |
} |
|
test_s(qq{ marc_repeatable_subfield('900','a', rec('200','a') ) }); |
|
|
test_s(qq{ marc('900','b', rec('200','b') ) }); |
|
|
test_s(qq{ marc('900','c', rec('200','c') ) }); |
|
401 |
|
|
402 |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
ok(my @marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
403 |
diag dump( \@marc ) if ($debug); |
diag dump( \@marc ) if ($debug); |
404 |
|
diag "expects:\n", dump($struct) if ($debug > 1); |
405 |
|
is_deeply( \@marc, $struct, $msg ); |
406 |
|
} |
407 |
|
|
408 |
is_deeply( \@marc, [ |
test_rec_rules( |
409 |
[ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ], |
'correct marc with repetable subfield', |
410 |
[ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ], |
{ |
411 |
[ '900', 1, 0, 'c', '200c-3' ], |
'200' => [{ |
412 |
], 'correct marc with repetable subfield'); |
'a' => '200a-1', |
413 |
|
'b' => '200b-1', |
414 |
|
'c' => '200c-1', |
415 |
|
}, { |
416 |
|
'a' => '200a-2', |
417 |
|
'b' => '200b-2', |
418 |
|
}, { |
419 |
|
'a' => '200a-3', |
420 |
|
}], |
421 |
|
}, |
422 |
|
qq{ |
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') ); |
427 |
|
}, |
428 |
|
[ |
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' ], |
432 |
|
], |
433 |
|
); |
434 |
|
|
435 |
|
|
436 |
|
test_rec_rules( |
437 |
|
'marc_repeatable_subfield', |
438 |
|
{ |
439 |
|
'200' => [{ |
440 |
|
'a' => '200a-1', |
441 |
|
'b' => '200b-1', |
442 |
|
'c' => '200c-1', |
443 |
|
}, { |
444 |
|
'a' => '200a-2', |
445 |
|
'b' => '200b-2', |
446 |
|
'c' => '200c-2', |
447 |
|
}, { |
448 |
|
'a' => '200a-3', |
449 |
|
'c' => '200c-3', |
450 |
|
}], |
451 |
|
}, |
452 |
|
qq{ |
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') ); |
457 |
|
}, |
458 |
|
[ |
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' ], |
462 |
|
], |
463 |
|
); |
464 |
|
|
465 |
|
test_rec_rules( |
466 |
|
'marc_compose', |
467 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
468 |
|
qq{ |
469 |
|
marc_compose('900', |
470 |
|
'c', rec(200,'b'), |
471 |
|
'b', rec(200,'a'), |
472 |
|
'a', rec(200,'c'), |
473 |
|
); |
474 |
|
}, |
475 |
|
[ |
476 |
|
[ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ] |
477 |
|
], |
478 |
|
); |
479 |
|
|
480 |
|
# |
481 |
|
# test rules |
482 |
|
# |
483 |
|
sub test_rule { |
484 |
|
my ($msg, $rec, $rule, $struct) = @_; |
485 |
|
_clean_ds(); |
486 |
|
_set_rec( $rec ); |
487 |
|
$rule =~ s/\\/\\/gs; |
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 ); |
492 |
|
} |
493 |
|
|
494 |
|
# test split_rec_on |
495 |
|
test_rule( |
496 |
|
'split_rec_on', |
497 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
498 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) }, |
499 |
|
'foo', |
500 |
|
); |
501 |
|
test_rule( |
502 |
|
'split_rec_on', |
503 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
504 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) }, |
505 |
|
'bar', |
506 |
|
); |
507 |
|
test_rule( |
508 |
|
'split_rec_on no part', |
509 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
510 |
|
q! split_rec_on('200','a', qr/\\s*;\\s*/) !, |
511 |
|
[ 'foo', 'bar' ], |
512 |
|
); |
513 |
|
test_rule( |
514 |
|
'split_rec_on no record', |
515 |
|
{}, |
516 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/) }, |
517 |
|
'', |
518 |
|
); |
519 |
|
|
520 |
|
test_rec_rules( |
521 |
|
'marc_compose+split_rec_on', |
522 |
|
{ '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] }, |
523 |
|
qq{ |
524 |
|
marc_compose('900', |
525 |
|
'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1), |
526 |
|
'c', rec(200,'c'), |
527 |
|
'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2), |
528 |
|
'b', rec(200,'b'), |
529 |
|
); |
530 |
|
}, |
531 |
|
[ |
532 |
|
[ '900', ' ', ' ', |
533 |
|
'a', 'foo', |
534 |
|
'c', 'baz', |
535 |
|
'a', 'bar', |
536 |
|
'b', 42, |
537 |
|
] |
538 |
|
], |
539 |
|
); |
540 |
|
|
541 |
|
cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader'); |
542 |
|
cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader'); |
543 |
|
ok(marc_leader(), 'marc_leader get'); |
544 |
|
diag "leader: ", dump(marc_leader()) if ($debug); |
545 |
|
is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full"); |
546 |
} |
} |
547 |
|
|