6 use Test::More tests => 361;
11 use_ok( 'WebPAC::Test' );
12 use_ok( 'WebPAC::Normalize' );
15 cmp_ok(_debug(1), '==', 1, '_debug level');
16 cmp_ok(_debug(0), '==', 0, '_debug level');
18 diag "debug level for $0 is $debug" if ($debug);
20 diag "debug level for WebPAC::Normalize is ", _debug( $debug - 2 );
54 'c' => 'New York University press',
71 'a' => 'xix, 202 str',
80 'e' => 'from Freud and Ferenczi to Michael balint',
81 'a' => 'Controversies in psychoanalytic method',
82 'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
83 'f' => 'by André E. Haynal'
85 '610' => [ 'povijest psihoanalize' ],
91 '320' => [ 'Kazalo' ],
94 '300' => [ 'Prijevod djela: ' ],
101 'key1' => { 1 => 1 },
102 'key2' => { 2 => 1 },
105 'key3' => { 3 => 1 },
106 'key4' => { 4 => 1 },
111 'key5' => { 5 => 1 },
112 'key6' => { 6 => 1 },
120 'key7' => { 7 => 1 },
121 'key8' => { 8 => 1 },
127 print dump( @_ ), ("-" x 78), "\n";
131 # how much of string evaled to display?
132 my $max_eval_output = 170;
135 my ($msg,$code) = @_;
137 my @l = split(/[\n\r]/, $code);
140 foreach my $i ( 0 .. $#l ) {
141 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
148 my $t = shift || die;
151 $eval_t =~ s/[\n\r\s]+/ /gs;
152 $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
153 $eval_t =~ s/\\/\\\\/gs;
157 ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
163 use WebPAC::Normalize;
165 ok(! _set_lookup( undef ), "set_lookup(undef)");
169 cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
170 cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
171 cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
172 diag "is_deeply checks\n";
173 is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
174 is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
175 is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
176 is_deeply( \[ rec('902') ], \[ '900' ] );
178 cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
180 # simple list manipulatons
181 cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
182 cmp_ok( join('-', prefix('', 'x', 'y') ), 'eq', 'x-y', 'prefix empty');
183 cmp_ok( join('-', prefix(0, 'x', 'y') ), 'eq', '0x-0y', 'prefix 0');
185 cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
186 cmp_ok( join('-', suffix('', 'x', 'y' ) ), 'eq', 'x-y', 'suffix empty');
187 cmp_ok( join('-', suffix(0, 'x', 'y' ) ), 'eq', 'x0-y0', 'suffix 0');
189 cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
190 cmp_ok( join('-', surround('', '', 'x','y','z') ), 'eq', 'x-y-z', 'surround empty');
191 cmp_ok( join('-', surround(0, 0, 'x','y','z') ), 'eq', '0x0-0y0-0z0', 'surround 0 0');
195 for my $i ( 0 .. 10 ) {
196 cmp_ok( count( @el ), '==', $i, "count($i)");
197 push @el, "element $i";
202 throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
204 ok(_set_load_row(sub {
205 my ($database,$input,$mfn) = @_;
206 diag "load_row( $database, $input, $mfn )" if ($debug);
207 cmp_ok( $#_, '==', 2, 'have 3 arguments');
208 ok($database, '_load_row database');
209 ok($input, '_load_row input');
210 ok($mfn, '_load_row mfn');
212 '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
215 }), '_set_load_row');
217 my @v = qw/foo bar baz aaa bbb ccc ddd/;
221 for my $i ( 0 .. $#v ) {
225 ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn");
230 save_into_lookup('db','input','key', sub { @v }),
231 '==', $size, "save_into_lookup $size values"
234 ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
235 diag "_get_lookup = ", dump($l) if ($debug);
241 diag "in show" if ($debug);
249 "lookup db/input/key");
251 push @accumulated, '900x-' . $mfn;
253 is_deeply(\@lookup, \@accumulated, "lookup db/input/key");
259 ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
260 diag "_get_lookup = ", dump($l) if ($debug);
266 foo => { 1000 => 1 },
267 bar => { 1000 => 1, 1001 => 1 },
268 baz => { 1000 => 1, 1001 => 1, 1002 => 1 },
269 aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 },
270 bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 },
271 ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 },
272 ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 },
280 diag "lookup_hash1 = ", dump($lookup_hash1) if ($debug);
281 ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1');
283 throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
285 ok(_set_load_row(sub {
286 my ($database,$input,$mfn) = @_;
287 diag "load_row( $database, $input, $mfn )";
288 cmp_ok( $#_, '==', 2, 'have 3 arguments');
289 ok($database, 'database');
293 }), '_set_load_row');
300 # 'db1','input1','key1',
304 # ), 'eq', 'found', 'lookup db1/input1/key1');
311 # 'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
313 # check join_with operations
315 sub test_join_with_2 {
323 'eq', $e, "join_with $a <1> $b = $e");
326 test_join_with_2('_','_','');
327 test_join_with_2('x','_','201x');
328 test_join_with_2('_','x','201x');
329 test_join_with_2('x','y','201x <1> 201y');
331 sub test_join_with_3 {
332 my ($a,$b,$c,$e) = @_;
335 join_with(" <1> ", rec('201',$a),
336 join_with(" <2> ", rec('201',$b),
340 'eq', $e, "join_with $a <1> $b <2> $c = $e");
343 test_join_with_3('_','_','_','');
344 test_join_with_3('x','_','_','201x');
345 test_join_with_3('_','x','_','201x');
346 test_join_with_3('_','_','x','201x');
347 test_join_with_3('x','y','_','201x <1> 201y');
348 test_join_with_3('x','_','y','201x <1> 201y');
349 test_join_with_3('_','x','y','201x <2> 201y');
350 test_join_with_3('x','_','y','201x <1> 201y');
351 test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
355 _set_lookup( $lookup_hash2 );
357 throws_ok { lookup() } qr/need/, 'empty lookup';
359 #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
361 #ok(! lookup('non-existent'), 'lookup non-existant' );
366 search_display('Title',
371 search_display('Who',
387 regex( 's/[^\\d]+//',
393 ok(my $ds = _get_ds(), "get_ds");
394 diag "ds = ", dump($ds) if ($debug);
401 ok($ds = _get_ds(), 'get_ds');
402 diag dump( $ds ) if ($debug);
404 ok( $ds && $ds->{something}, 'get_ds->something exists' );
405 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
406 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
412 test_s(qq{ search('something', '42'); });
413 test_s(qq{ search('empty', ''); });
414 test_check_ds('search');
417 test_s(qq{ display('something', '42'); });
418 test_s(qq{ display('empty', ''); });
419 test_check_ds('display');
422 test_s(qq{ search_display('something', '42'); });
423 test_s(qq{ search_display('empty', ''); });
424 test_check_ds('search');
425 test_check_ds('display');
428 test_s(qq{ sorted('something', '42'); });
429 test_s(qq{ sorted('empty', ''); });
430 test_check_ds('sorted');
433 my $n = read_file( "$abs_path/data/normalize.pl" );
435 #diag "normalize code:\n$n\n";
438 ok($ds = _get_ds(), "get_ds");
439 diag "ds = ", dump($ds) if ($debug);
447 my $rules = qq{ search('mixed', rec('200') ) };
452 ok($ds = _get_ds(), "get_ds");
455 'search' => [ '200a', '200b' ],
457 }, 'correct get_ds');
459 ok(my $ds2 = WebPAC::Normalize::data_structure(
462 ), 'data_structure');
463 is_deeply( $ds, $ds2, 'data_structure(s) same');
465 # wird and non-valid structure which is supported anyway
474 test_s(qq{ search('mixed', rec('200') ) });
475 ok($ds = _get_ds(), "get_ds");
478 'search' => [ '200a', '200-solo' ],
480 }, 'correct get_ds');
487 test_s(qq{ marc_indicators('900',1,2) });
488 test_s(qq{ marc('900','a', rec('200') ) });
490 ok($marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
491 diag dump( $marc ) if ($debug);
494 [ '900', 1, 2, 'a', '200a' ],
495 [ '900', 1, 2, 'a', '200-solo' ]
496 ], 'correct marc with indicators');
498 test_s(qq{ marc_indicators('900',' ',9) });
499 test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
501 ok($marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
502 diag dump( $marc ) if ($debug);
505 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
506 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
507 ], 'correct marc with repetable subfield');
510 # test magic re-ordering of input data
514 my ($msg, $rec, $rules, $struct) = @_;
519 foreach my $r (split(/;\s*$/, $rules)) {
520 $r =~ s/[\s\n\r]+/ /gs;
523 diag "rule: $r" if $debug;
527 ok(my $marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
528 diag dump( $marc ) if $debug;
529 diag "expects:\n", dump($struct) if ($debug > 1);
530 is_deeply( $marc, $struct, $msg );
534 'correct marc with repetable subfield',
548 marc_indicators('900',1 ,0);
549 marc('900','a', rec('200','a') );
550 marc('900','b', rec('200','b') );
551 marc('900','c', rec('200','c') );
554 [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
555 [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
556 [ '900', 1, 0, 'a', '200a-3' ],
562 'marc_repeatable_subfield',
578 marc_indicators('900',1 ,0);
579 marc_repeatable_subfield('900','a', rec('200','a') );
580 marc('900','b', rec('200','b') );
581 marc('900','c', rec('200','c') );
584 [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
585 [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
586 [ '900', 1, 0, 'c', '200c-3' ],
592 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
601 [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
606 'marc_compose with + subfields',
607 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
611 '+', prefix(" * ", rec(200,'c')),
613 '+', prefix(" : ", rec(200,'c')),
617 [ '900', ' ', ' ', 'a', 'foo ; bar * baz', 'b', '42 : baz' ]
625 my ($msg, $rec, $rule, $struct) = @_;
629 my $r = test_s( $rule );
630 diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
631 diag dump($struct) if ($debug);
632 is_deeply( $r, $struct, $msg );
638 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
639 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
644 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
645 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
649 'split_rec_on no part',
650 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
651 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
655 'split_rec_on no record',
657 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
662 'marc_compose+split_rec_on',
663 { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
666 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
668 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
682 cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
683 cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
684 ok(marc_leader(), 'marc_leader get');
685 diag "leader: ", dump(marc_leader()) if ($debug);
686 is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
696 'marc(001,rec(000))',
699 marc('001', rec('000') );
707 'marc_remove subfield',
708 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
710 marc('900', 'a', rec('200','a') );
711 marc('900', 'b', rec('200','b') );
712 marc_remove('900','b');
713 marc('900', 'b', rec('200','c') );
714 marc_remove('900','a');
717 [ '900', ' ', ' ', 'b', 'baz' ],
723 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
725 marc('900', 'a', rec('200','a') );
726 marc('900', 'b', rec('200','b') );
727 marc('901', 'b', rec('200','b') );
728 marc('901', 'c', rec('200','c') );
732 [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
736 test_s(qq{ marc_remove('*'); });
737 ok(! WebPAC::Normalize::MARC::_get_marc_fields(), 'marc_remove(*)');
741 { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
743 marc_leader('06',42);
745 marc('900', 'a', rec('200','a') );
746 marc('900', 'b', rec('200','b') );
749 marc_remove('900','b');
750 marc('900', 'b', rec('200','c') );
753 marc_remove('900','b');
754 marc('900', 'b', rec('200','d') );
757 marc_remove('900','b');
758 marc('900', 'b', rec('200','e') );
761 # this will return FIRST record
762 [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
766 cmp_ok( marc_count(), '==', 3, 'marc_count' );
769 foreach my $v ( qw/bar baz bing bong/ ) {
771 ok($marc = WebPAC::Normalize::MARC::_get_marc_fields( offset => $i ),
772 "_get_marc_fields( offset => $i )"
774 diag "marc $i = ", dump( $marc ) if ($debug);
776 [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
777 "MARC copy $i has $v",
779 is_deeply(WebPAC::Normalize::MARC::_get_marc_leader(), { '06' => 42, 11 => $i }, "_get_marc_leader copy $i");
784 'marc_original_order',
787 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
788 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
790 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
791 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
795 marc_original_order(900,200);
798 [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
799 [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
804 'rec1 skips subfields',
807 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
808 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
810 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
811 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
817 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
821 [ _pack_subfields_hash({
822 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
823 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
825 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
826 '_pack_subfields_hash( $h )'
830 _pack_subfields_hash({
831 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
832 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
835 '^aa1^bb1^aa2^bb2^cc1^cc2',
836 '_pack_subfields_hash( $h, 1 )'
841 marc_fixed('008', 0, 'abcdef');
842 marc_fixed('000', 5, '5');
843 marc_fixed('000', 10, 'A');
844 marc_fixed('000', 0, '0');
846 ok( my $m = WebPAC::Normalize::MARC::_get_marc_fields(), '_get_marc_fields');
848 is_deeply( WebPAC::Normalize::MARC::_get_marc_fields(),
856 test_s(qq{ isbn_13( '1558607013', '978-1558607019' ) });
857 test_s(qq{ isbn_10( '1558607013', '978-1558607019' ) });
860 [ isbn_13( '1558607013', '978-1558607019' ) ],
861 [ '978-1-55860-701-9', '978-1-55860-701-9', ],
865 [ isbn_10( '1558607013', '978-1558607019' ) ],
866 [ '1-55860-701-3', '1-55860-701-3' ],
873 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
874 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
875 i1 => '0', i2 => '1',
877 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
878 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
882 test_rule( 'frec', $rec, qq{ frec(200) }, [ 'a1' ] );
883 test_rule( 'frec', $rec, qq{ frec(200,'a') }, [ 'a1' ] );
884 test_rule( 'frec', $rec, qq{ frec(200,'b') }, [ 'b1' ] );
885 test_rule( 'frec', $rec, qq{ frec(200,'c') }, [ 'c1' ] );
886 test_rule( 'frec', $rec, qq{ frec(200,'i1') }, [ '0' ] );
887 test_rule( 'frec', $rec, qq{ frec(200,'i2') }, [ '1' ] );
889 $rec->{'900'} = $rec->{'200'};
890 foreach my $sf ( qw/a b c/ ) {
891 ok( frec_eq( '200' => $sf, '900' => $sf ), "frec_eq 200 == 900 $sf");
892 ok( ! frec_ne( '200' => $sf, '900' => $sf ), "! frec_ne 200 == 900 $sf");
895 foreach my $sf ( qw/a b/ ) {
896 ok( ! frec_eq( '200' => $sf, '200' => 'c' ), "! frec_eq 200 $sf == 200 c");
897 ok( frec_ne( '200' => $sf, '200' => 'c' ), "frec_ne 200 $sf == 200 c");
900 test_rule( 'rec(200,i1)', $rec, qq{ rec(200,'i1') }, [ '0' ] );
901 test_rule( 'rec(200,i2)', $rec, qq{ rec(200,'i2') }, [ '1' ] );
903 my $hash = { a => '[a]', 'b' => '[b]', subfields => [ 'a', 0, 'b', 0 ] };
904 is_deeply([ _pack_subfields_hash( $hash ) ], [ '[a]', '[b]' ], '_pack_subfields_hash' );
905 ok( $hash->{subfields}, 'subfields exist' );
906 cmp_ok( _pack_subfields_hash( $hash, 1 ), 'eq', '^a[a]^b[b]', '_pack_subfields_hash' );
907 ok( $hash->{subfields}, 'subfields exist' );
909 $rec = { 'arr' => [ 1, 2, 3 ] };
910 test_rule( 'rec_array', $rec, qq{ rec_array('arr') }, $rec->{arr} );
915 row( 'table', e => $_ ) foreach ( rec_array('arr') );
917 ok( my $rows = _get_ds->{_rows}->{table}, 'ds have _rows' );
919 foreach my $i ( 1 .. 3 ) {
920 cmp_ok( $rows->[ $i - 1 ]->{e}, '==', $i, "e $i" );
924 , { '900' => [{ a => 'Čev', b => 'ić' }] }
925 , qq{ join_with('', rec(900,'a'), 'apč', rec(900,'b') ) }
926 , [ "\x{10C}evap\x{10D}i\x{107}" ]