5 use Test::More tests => 309;
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);
66 'c' => 'New York University press',
83 'a' => 'xix, 202 str',
92 'e' => 'from Freud and Ferenczi to Michael balint',
93 'a' => 'Controversies in psychoanalytic method',
94 'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
95 'f' => 'by André E. Haynal'
97 '610' => [ 'povijest psihoanalize' ],
103 '320' => [ 'Kazalo' ],
106 '300' => [ 'Prijevod djela: ' ],
113 'key1' => { 1 => 1 },
114 'key2' => { 2 => 1 },
117 'key3' => { 3 => 1 },
118 'key4' => { 4 => 1 },
123 'key5' => { 5 => 1 },
124 'key6' => { 6 => 1 },
132 'key7' => { 7 => 1 },
133 'key8' => { 8 => 1 },
139 print dump( @_ ), ("-" x 78), "\n";
143 # how much of string evaled to display?
144 my $max_eval_output = 170;
147 my ($msg,$code) = @_;
149 my @l = split(/[\n\r]/, $code);
152 foreach my $i ( 0 .. $#l ) {
153 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
160 my $t = shift || die;
163 $eval_t =~ s/[\n\r\s]+/ /gs;
164 $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
165 $eval_t =~ s/\\/\\\\/gs;
169 ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
175 use WebPAC::Normalize;
177 ok(! _set_lookup( undef ), "set_lookup(undef)");
181 cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
182 cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
183 cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
184 diag "is_deeply checks\n";
185 is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
186 is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
187 is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
188 is_deeply( \[ rec('902') ], \[ '900' ] );
190 cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
192 # simple list manipulatons
193 cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
194 cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
195 cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
199 throws_ok { _set_load_ds() } qr/CODE/, 'empty _set_load_ds()';
201 ok(_set_load_ds(sub {
202 my ($database,$input,$mfn) = @_;
203 diag "load_ds( $database, $input, $mfn )";
204 cmp_ok( $#_, '==', 2, 'have 3 arguments');
205 ok($database, '_load_ds database');
206 ok($input, '_load_ds input');
207 ok($mfn, '_load_ds mfn');
209 '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
214 my @v = qw/foo bar baz aaa bbb ccc ddd/;
218 for my $i ( 0 .. $#v ) {
222 ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn");
227 save_into_lookup('db','input','key', sub { @v }),
228 '==', $size, "save_into_lookup $size values"
231 ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
232 diag "_get_lookup = ", dump($l);
236 ok(my @lookup = lookup(
246 "lookup db/input/key");
248 push @accumulated, '900x-' . $mfn;
250 is_deeply(\@lookup, \@accumulated, "lookup db/input/key");
256 ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
257 diag "_get_lookup = ", dump($l);
263 foo => { 1000 => 1 },
264 bar => { 1000 => 1, 1001 => 1 },
265 baz => { 1000 => 1, 1001 => 1, 1002 => 1 },
266 aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 },
267 bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 },
268 ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 },
269 ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 },
277 diag "lookup_hash1 = ", dump($lookup_hash1);
278 ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1');
280 throws_ok { _set_load_ds() } qr/CODE/, 'empty _set_load_ds()';
282 ok(_set_load_ds(sub {
283 my ($database,$input,$mfn) = @_;
284 diag "load_ds( $database, $input, $mfn )";
285 cmp_ok( $#_, '==', 2, 'have 3 arguments');
286 ok($database, 'database');
297 # 'db1','input1','key1',
301 # ), 'eq', 'found', 'lookup db1/input1/key1');
308 # 'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
310 # check join_with operations
312 sub test_join_with_2 {
320 'eq', $e, "join_with $a <1> $b = $e");
323 test_join_with_2('_','_','');
324 test_join_with_2('x','_','201x');
325 test_join_with_2('_','x','201x');
326 test_join_with_2('x','y','201x <1> 201y');
328 sub test_join_with_3 {
329 my ($a,$b,$c,$e) = @_;
332 join_with(" <1> ", rec('201',$a),
333 join_with(" <2> ", rec('201',$b),
337 'eq', $e, "join_with $a <1> $b <2> $c = $e");
340 test_join_with_3('_','_','_','');
341 test_join_with_3('x','_','_','201x');
342 test_join_with_3('_','x','_','201x');
343 test_join_with_3('_','_','x','201x');
344 test_join_with_3('x','y','_','201x <1> 201y');
345 test_join_with_3('x','_','y','201x <1> 201y');
346 test_join_with_3('_','x','y','201x <2> 201y');
347 test_join_with_3('x','_','y','201x <1> 201y');
348 test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
352 _set_lookup( $lookup_hash2 );
354 throws_ok { lookup() } qr/need/, 'empty lookup';
356 #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
358 #ok(! lookup('non-existent'), 'lookup non-existant' );
384 regex( 's/[^\\d]+//',
390 ok(my $ds = _get_ds(), "get_ds");
391 diag "ds = ", dump($ds) if ($debug);
398 ok($ds = _get_ds(), 'get_ds');
399 diag dump( $ds ) if ($debug);
401 ok( $ds && $ds->{something}, 'get_ds->something exists' );
402 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
403 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
409 test_s(qq{ search('something', '42'); });
410 test_s(qq{ search('empty', ''); });
411 test_check_ds('search');
414 test_s(qq{ display('something', '42'); });
415 test_s(qq{ display('empty', ''); });
416 test_check_ds('display');
419 test_s(qq{ tag('something', '42'); });
420 test_s(qq{ tag('empty', ''); });
421 test_check_ds('search');
422 test_check_ds('display');
425 my $n = read_file( "$abs_path/data/normalize.pl" );
427 #diag "normalize code:\n$n\n";
430 ok($ds = _get_ds(), "get_ds");
431 diag "ds = ", dump($ds) if ($debug);
439 my $rules = qq{ search('mixed', rec('200') ) };
444 ok($ds = _get_ds(), "get_ds");
447 'search' => [ '200a', '200b' ],
450 }, 'correct get_ds');
452 ok(my $ds2 = WebPAC::Normalize::data_structure(
455 ), 'data_structure');
456 is_deeply( $ds, $ds2, 'data_structure(s) same');
458 # wird and non-valid structure which is supported anyway
467 test_s(qq{ search('mixed', rec('200') ) });
468 ok($ds = _get_ds(), "get_ds");
471 'search' => [ '200a', '200-solo' ],
474 }, 'correct get_ds');
481 test_s(qq{ marc_indicators('900',1,2) });
482 test_s(qq{ marc('900','a', rec('200') ) });
484 ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
485 diag dump( $marc ) if ($debug);
488 [ '900', 1, 2, 'a', '200a' ],
489 [ '900', 1, 2, 'a', '200-solo' ]
490 ], 'correct marc with indicators');
492 test_s(qq{ marc_indicators('900',' ',9) });
493 test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
495 ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
496 diag dump( $marc ) if ($debug);
499 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
500 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
501 ], 'correct marc with repetable subfield');
504 # test magic re-ordering of input data
508 my ($msg, $rec, $rules, $struct) = @_;
513 foreach my $r (split(/;/, $rules)) {
514 $r =~ s/[\s\n\r]+/ /gs;
520 ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
521 diag dump( $marc ) if ($debug);
522 diag "expects:\n", dump($struct) if ($debug > 1);
523 is_deeply( $marc, $struct, $msg );
527 'correct marc with repetable subfield',
541 marc_indicators('900',1 ,0);
542 marc('900','a', rec('200','a') );
543 marc('900','b', rec('200','b') );
544 marc('900','c', rec('200','c') );
547 [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
548 [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
549 [ '900', 1, 0, 'a', '200a-3' ],
555 'marc_repeatable_subfield',
571 marc_indicators('900',1 ,0);
572 marc_repeatable_subfield('900','a', rec('200','a') );
573 marc('900','b', rec('200','b') );
574 marc('900','c', rec('200','c') );
577 [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
578 [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
579 [ '900', 1, 0, 'c', '200c-3' ],
585 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
594 [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
599 'marc_compose with + subfields',
600 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
604 '+', prefix(" * ", rec(200,'c')),
606 '+', prefix(" : ", rec(200,'c')),
610 [ '900', ' ', ' ', 'a', 'foo ; bar * baz', 'b', '42 : baz' ]
618 my ($msg, $rec, $rule, $struct) = @_;
622 my $r = test_s( $rule );
623 diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
624 diag dump($struct) if ($debug);
625 is_deeply( $r, $struct, $msg );
631 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
632 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
637 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
638 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
642 'split_rec_on no part',
643 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
644 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
648 'split_rec_on no record',
650 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
655 'marc_compose+split_rec_on',
656 { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
659 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
661 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
675 cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
676 cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
677 ok(marc_leader(), 'marc_leader get');
678 diag "leader: ", dump(marc_leader()) if ($debug);
679 is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
689 'marc(001,rec(000))',
692 marc('001', rec('000') );
700 'marc_remove subfield',
701 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
703 marc('900', 'a', rec('200','a') );
704 marc('900', 'b', rec('200','b') );
705 marc_remove('900','b');
706 marc('900', 'b', rec('200','c') );
707 marc_remove('900','a');
710 [ '900', ' ', ' ', 'b', 'baz' ],
716 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
718 marc('900', 'a', rec('200','a') );
719 marc('900', 'b', rec('200','b') );
720 marc('901', 'b', rec('200','b') );
721 marc('901', 'c', rec('200','c') );
725 [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
730 { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
732 marc('900', 'a', rec('200','a') );
733 marc('900', 'b', rec('200','b') );
735 marc_remove('900','b');
736 marc('900', 'b', rec('200','c') );
738 marc_remove('900','b');
739 marc('900', 'b', rec('200','d') );
741 marc_remove('900','b');
742 marc('900', 'b', rec('200','e') );
745 # this will return FIRST record
746 [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
751 foreach my $v ( qw/bar baz bing bong/ ) {
753 ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
754 "_get_marc_fields( offset => $i )"
756 diag "marc $i = ", dump( $marc ) if ($debug);
758 [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
759 "MARC copy $i has $v",
765 'marc_original_order',
768 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
769 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
771 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
772 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
776 marc_original_order(900,200);
779 [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
780 [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
785 'rec1 skips subfields',
788 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
789 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
791 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
792 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
798 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
802 [ _pack_subfields_hash({
803 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
804 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
806 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
807 '_pack_subfields_hash( $h )'
811 _pack_subfields_hash({
812 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
813 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
816 '^aa1^bb1^aa2^bb2^cc1^cc2',
817 '_pack_subfields_hash( $h, 1 )'