2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 69; |
use Test::More tests => 89; |
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 |
|
|
269 |
}); |
}); |
270 |
|
|
271 |
ok(my $ds = _get_ds(), "get_ds"); |
ok(my $ds = _get_ds(), "get_ds"); |
272 |
diag "ds = ", Dumper($ds) if ($debug); |
diag "ds = ", dump($ds) if ($debug); |
273 |
|
|
274 |
|
|
275 |
sub test_check_ds { |
sub test_check_ds { |
277 |
my $t = shift; |
my $t = shift; |
278 |
|
|
279 |
ok($ds = _get_ds(), 'get_ds'); |
ok($ds = _get_ds(), 'get_ds'); |
280 |
diag Dumper( $ds ) if ($debug); |
diag dump( $ds ) if ($debug); |
281 |
|
|
282 |
ok( $ds && $ds->{something}, 'get_ds->something exists' ); |
ok( $ds && $ds->{something}, 'get_ds->something exists' ); |
283 |
ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t); |
ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t); |
309 |
test_s( $n ); |
test_s( $n ); |
310 |
|
|
311 |
ok($ds = _get_ds(), "get_ds"); |
ok($ds = _get_ds(), "get_ds"); |
312 |
diag "ds = ", Dumper($ds) if ($debug); |
diag "ds = ", dump($ds) if ($debug); |
313 |
|
|
314 |
my $rec = { |
my $rec = { |
315 |
'200' => [{ |
'200' => [{ |
354 |
} |
} |
355 |
}, 'correct get_ds'); |
}, 'correct get_ds'); |
356 |
|
|
357 |
|
# |
358 |
# MARC |
# MARC |
359 |
test_s(qq{ marc21('900','a', rec('200') ) }); |
# |
360 |
|
|
361 |
|
test_s(qq{ marc_indicators('900',1,2) }); |
362 |
|
test_s(qq{ marc('900','a', rec('200') ) }); |
363 |
my @marc; |
my @marc; |
364 |
ok(@marc = WebPAC::Normalize::_get_marc21_fields(), "_get_marc21_fields"); |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
365 |
diag Dumper(\@marc); |
diag dump( \@marc ) if ($debug); |
366 |
|
|
367 |
|
is_deeply( \@marc, [ |
368 |
|
[ '900', 1, 2, 'a', '200a' ], |
369 |
|
[ '900', 1, 2, 'a', '200-solo' ] |
370 |
|
], 'correct marc with indicators'); |
371 |
|
|
372 |
|
test_s(qq{ marc_indicators('900',' ',9) }); |
373 |
|
test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) }); |
374 |
|
|
375 |
|
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
376 |
|
diag dump( \@marc ) if ($debug); |
377 |
|
|
378 |
|
is_deeply( \@marc, [ |
379 |
|
[ '900', 1, 2, 'a', '200a', 'a', '200-solo' ], |
380 |
|
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
381 |
|
], 'correct marc with repetable subfield'); |
382 |
|
|
383 |
|
# |
384 |
|
# test magic re-ordering of input data |
385 |
|
# |
386 |
|
|
387 |
|
sub test_rec_rules { |
388 |
|
my ($msg, $rec, $rules, $struct) = @_; |
389 |
|
|
390 |
|
_clean_ds(); |
391 |
|
_set_rec($rec); |
392 |
|
|
393 |
|
foreach my $r (split(/;/, $rules)) { |
394 |
|
$r =~ s/[\s\n\r]+/ /gs; |
395 |
|
$r =~ s/^\s+//gs; |
396 |
|
$r =~ s/\s+$//gs; |
397 |
|
test_s($r) if ($r); |
398 |
|
} |
399 |
|
|
400 |
|
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
401 |
|
diag dump( \@marc ) if ($debug); |
402 |
|
|
403 |
|
is_deeply( \@marc, $struct, $msg ); |
404 |
|
} |
405 |
|
|
406 |
|
test_rec_rules( |
407 |
|
'correct marc with repetable subfield', |
408 |
|
{ |
409 |
|
'200' => [{ |
410 |
|
'a' => '200a-1', |
411 |
|
'b' => '200b-1', |
412 |
|
'c' => '200c-1', |
413 |
|
}, { |
414 |
|
'a' => '200a-2', |
415 |
|
'b' => '200b-2', |
416 |
|
}, { |
417 |
|
'a' => '200a-3', |
418 |
|
}], |
419 |
|
}, |
420 |
|
qq{ |
421 |
|
marc_indicators('900',1 ,0); |
422 |
|
marc('900','a', rec('200','a') ); |
423 |
|
marc('900','b', rec('200','b') ); |
424 |
|
marc('900','c', rec('200','c') ); |
425 |
|
}, |
426 |
|
[ |
427 |
|
[ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ], |
428 |
|
[ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ], |
429 |
|
[ '900', 1, 0, 'a', '200a-3' ], |
430 |
|
], |
431 |
|
); |
432 |
|
|
433 |
|
|
434 |
|
test_rec_rules( |
435 |
|
'marc_repeatable_subfield', |
436 |
|
{ |
437 |
|
'200' => [{ |
438 |
|
'a' => '200a-1', |
439 |
|
'b' => '200b-1', |
440 |
|
'c' => '200c-1', |
441 |
|
}, { |
442 |
|
'a' => '200a-2', |
443 |
|
'b' => '200b-2', |
444 |
|
'c' => '200c-2', |
445 |
|
}, { |
446 |
|
'a' => '200a-3', |
447 |
|
'c' => '200c-3', |
448 |
|
}], |
449 |
|
}, |
450 |
|
qq{ |
451 |
|
marc_indicators('900',1 ,0); |
452 |
|
marc_repeatable_subfield('900','a', rec('200','a') ); |
453 |
|
marc('900','b', rec('200','b') ); |
454 |
|
marc('900','c', rec('200','c') ); |
455 |
|
}, |
456 |
|
[ |
457 |
|
[ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ], |
458 |
|
[ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ], |
459 |
|
[ '900', 1, 0, 'c', '200c-3' ], |
460 |
|
], |
461 |
|
); |
462 |
} |
} |
463 |
|
|