2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 69; |
use Test::More tests => 99; |
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::Dumper; |
|
|
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); |
122 |
|
|
123 |
|
|
124 |
sub test { |
sub test { |
125 |
print Dumper( @_ ), ("-" x 78), "\n"; |
print dump( @_ ), ("-" x 78), "\n"; |
126 |
ok( defined(@_) ); |
ok( defined(@_) ); |
127 |
} |
} |
128 |
|
|
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 |
{ |
{ |
271 |
}); |
}); |
272 |
|
|
273 |
ok(my $ds = _get_ds(), "get_ds"); |
ok(my $ds = _get_ds(), "get_ds"); |
274 |
diag "ds = ", Dumper($ds) if ($debug); |
diag "ds = ", dump($ds) if ($debug); |
275 |
|
|
276 |
|
|
277 |
sub test_check_ds { |
sub test_check_ds { |
279 |
my $t = shift; |
my $t = shift; |
280 |
|
|
281 |
ok($ds = _get_ds(), 'get_ds'); |
ok($ds = _get_ds(), 'get_ds'); |
282 |
diag Dumper( $ds ) if ($debug); |
diag dump( $ds ) if ($debug); |
283 |
|
|
284 |
ok( $ds && $ds->{something}, 'get_ds->something exists' ); |
ok( $ds && $ds->{something}, 'get_ds->something exists' ); |
285 |
ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t); |
ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t); |
311 |
test_s( $n ); |
test_s( $n ); |
312 |
|
|
313 |
ok($ds = _get_ds(), "get_ds"); |
ok($ds = _get_ds(), "get_ds"); |
314 |
diag "ds = ", Dumper($ds) if ($debug); |
diag "ds = ", dump($ds) if ($debug); |
315 |
|
|
316 |
my $rec = { |
my $rec = { |
317 |
'200' => [{ |
'200' => [{ |
356 |
} |
} |
357 |
}, 'correct get_ds'); |
}, 'correct get_ds'); |
358 |
|
|
359 |
|
# |
360 |
# MARC |
# MARC |
361 |
test_s(qq{ marc21('900','a', rec('200') ) }); |
# |
362 |
|
|
363 |
|
test_s(qq{ marc_indicators('900',1,2) }); |
364 |
|
test_s(qq{ marc('900','a', rec('200') ) }); |
365 |
my @marc; |
my @marc; |
366 |
ok(@marc = WebPAC::Normalize::_get_marc21_fields(), "_get_marc21_fields"); |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
367 |
diag Dumper(\@marc); |
diag dump( \@marc ) if ($debug); |
368 |
|
|
369 |
|
is_deeply( \@marc, [ |
370 |
|
[ '900', 1, 2, 'a', '200a' ], |
371 |
|
[ '900', 1, 2, 'a', '200-solo' ] |
372 |
|
], 'correct marc with indicators'); |
373 |
|
|
374 |
|
test_s(qq{ marc_indicators('900',' ',9) }); |
375 |
|
test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) }); |
376 |
|
|
377 |
|
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
378 |
|
diag dump( \@marc ) if ($debug); |
379 |
|
|
380 |
|
is_deeply( \@marc, [ |
381 |
|
[ '900', 1, 2, 'a', '200a', 'a', '200-solo' ], |
382 |
|
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
383 |
|
], 'correct marc with repetable subfield'); |
384 |
|
|
385 |
|
# |
386 |
|
# test magic re-ordering of input data |
387 |
|
# |
388 |
|
|
389 |
|
sub test_rec_rules { |
390 |
|
my ($msg, $rec, $rules, $struct) = @_; |
391 |
|
|
392 |
|
_clean_ds(); |
393 |
|
_set_rec($rec); |
394 |
|
|
395 |
|
foreach my $r (split(/;/, $rules)) { |
396 |
|
$r =~ s/[\s\n\r]+/ /gs; |
397 |
|
$r =~ s/^\s+//gs; |
398 |
|
$r =~ s/\s+$//gs; |
399 |
|
test_s($r) if ($r); |
400 |
|
} |
401 |
|
|
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 ); |
406 |
|
} |
407 |
|
|
408 |
|
test_rec_rules( |
409 |
|
'correct marc with repetable subfield', |
410 |
|
{ |
411 |
|
'200' => [{ |
412 |
|
'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 |
|
|
508 |
|
test_rec_rules( |
509 |
|
'marc_compose+split_rec_on', |
510 |
|
{ '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] }, |
511 |
|
qq{ |
512 |
|
marc_compose('900', |
513 |
|
'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1), |
514 |
|
'c', rec(200,'c'), |
515 |
|
'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2), |
516 |
|
'b', rec(200,'b'), |
517 |
|
); |
518 |
|
}, |
519 |
|
[ |
520 |
|
[ '900', ' ', ' ', |
521 |
|
'a', 'foo', |
522 |
|
'c', 'baz', |
523 |
|
'a', 'bar', |
524 |
|
'b', 42, |
525 |
|
] |
526 |
|
], |
527 |
|
); |
528 |
} |
} |
529 |
|
|