/[webpac2]/trunk/t/3-normalize.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/t/3-normalize.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 538 by dpavlin, Thu Jun 29 15:29:19 2006 UTC revision 641 by dpavlin, Wed Sep 6 20:54:47 2006 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 67;  use Test::More tests => 157;
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);
# Line 109  my $lookup2 = { Line 122  my $lookup2 = {
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    
# Line 135  sub test_s { Line 148  sub test_s {
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 @__ret;
154          ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t");          eval "\@__ret = $t";
155            ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
156            return \@__ret;
157  }  }
158    
159  {  {
# Line 256  sub test_s { Line 272  sub test_s {
272          });          });
273    
274          ok(my $ds = _get_ds(), "get_ds");          ok(my $ds = _get_ds(), "get_ds");
275          diag "ds = ", Dumper($ds) if ($debug);          diag "ds = ", dump($ds) if ($debug);
276    
277    
278          sub test_check_ds {          sub test_check_ds {
# Line 264  sub test_s { Line 280  sub test_s {
280                  my $t = shift;                  my $t = shift;
281    
282                  ok($ds = _get_ds(), 'get_ds');                  ok($ds = _get_ds(), 'get_ds');
283                  diag Dumper( $ds ) if ($debug);                  diag dump( $ds ) if ($debug);
284    
285                  ok( $ds && $ds->{something}, 'get_ds->something exists' );                  ok( $ds && $ds->{something}, 'get_ds->something exists' );
286                  ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);                  ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
# Line 296  sub test_s { Line 312  sub test_s {
312          test_s( $n );          test_s( $n );
313    
314          ok($ds = _get_ds(), "get_ds");          ok($ds = _get_ds(), "get_ds");
315          diag "ds = ", Dumper($ds) if ($debug);          diag "ds = ", dump($ds) if ($debug);
316    
317          my $rec = {          my $rec = {
318                  '200' => [{                  '200' => [{
# Line 341  sub test_s { Line 357  sub test_s {
357                  }                  }
358          }, 'correct get_ds');          }, 'correct get_ds');
359    
360            #
361            # MARC
362            #
363            _debug( 4 );
364    
365            test_s(qq{ marc_indicators('900',1,2) });
366            test_s(qq{ marc('900','a', rec('200') ) });
367            my $marc;
368            ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
369            diag dump( $marc ) if ($debug);
370    
371            is_deeply( $marc, [
372                    [ '900', 1, 2, 'a', '200a' ],
373                    [ '900', 1, 2, 'a', '200-solo' ]
374            ], 'correct marc with indicators');
375    
376            test_s(qq{ marc_indicators('900',' ',9) });
377            test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
378    
379            ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
380            diag dump( $marc ) if ($debug);
381    
382            is_deeply( $marc, [
383                    [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
384                    [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
385            ], 'correct marc with repetable subfield');
386    
387            #
388            # test magic re-ordering of input data
389            #
390    
391            sub test_rec_rules {
392                    my ($msg, $rec, $rules, $struct) = @_;
393    
394                    _clean_ds();
395                    _set_rec($rec);
396    
397                    foreach my $r (split(/;/, $rules)) {
398                            $r =~ s/[\s\n\r]+/ /gs;
399                            $r =~ s/^\s+//gs;
400                            $r =~ s/\s+$//gs;
401                            test_s($r) if ($r);
402                    }
403    
404                    ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
405                    diag dump( $marc ) if ($debug);
406                    diag "expects:\n", dump($struct) if ($debug > 1);
407                    is_deeply( $marc, $struct, $msg );
408            }
409    
410            test_rec_rules(
411                    'correct marc with repetable subfield',
412                    {
413                            '200' => [{
414                                    'a' => '200a-1',
415                                    'b' => '200b-1',
416                                    'c' => '200c-1',
417                            }, {
418                                    'a' => '200a-2',
419                                    'b' => '200b-2',
420                            }, {
421                                    'a' => '200a-3',
422                            }],
423                    },
424                    qq{
425                            marc_indicators('900',1 ,0);
426                            marc('900','a', rec('200','a') );
427                            marc('900','b', rec('200','b') );
428                            marc('900','c', rec('200','c') );
429                    },
430                    [
431                            [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
432                            [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
433                            [ '900', 1, 0, 'a', '200a-3' ],
434                    ],
435            );
436    
437    
438            test_rec_rules(
439                    'marc_repeatable_subfield',
440                    {
441                            '200' => [{
442                                    'a' => '200a-1',
443                                    'b' => '200b-1',
444                                    'c' => '200c-1',
445                            }, {
446                                    'a' => '200a-2',
447                                    'b' => '200b-2',
448                                    'c' => '200c-2',
449                            }, {
450                                    'a' => '200a-3',
451                                    'c' => '200c-3',
452                            }],
453                    },
454                    qq{
455                            marc_indicators('900',1 ,0);
456                            marc_repeatable_subfield('900','a', rec('200','a') );
457                            marc('900','b', rec('200','b') );
458                            marc('900','c', rec('200','c') );
459                    },
460                    [
461                            [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
462                            [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
463                            [ '900', 1, 0, 'c', '200c-3' ],
464                    ],
465            );
466    
467            test_rec_rules(
468                    'marc_compose',
469                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
470                    qq{
471                            marc_compose('900',
472                                    'c', rec(200,'b'),
473                                    'b', rec(200,'a'),
474                                    'a', rec(200,'c'),
475                            );
476                    },
477                    [
478                            [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
479                    ],
480            );
481    
482            test_rec_rules(
483                    'marc_compose with + subfields',
484                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
485                    qq{
486                            marc_compose('900',
487                                    'a', rec(200,'a'),
488                                    '+', prefix(" * ", rec(200,'c')),
489                                    'b', rec(200,'b'),
490                                    '+', prefix(" : ", rec(200,'c')),
491                            );
492                    },
493                    [
494                            [ '900', ' ', ' ', 'a', 'foo ; bar * baz', 'b', '42 : baz' ]
495                    ],
496            );
497    
498            #
499            # test rules
500            #
501            sub test_rule {
502                    my ($msg, $rec, $rule, $struct) = @_;
503                    _clean_ds();
504                    _set_rec( $rec );
505                    $rule =~ s/\\/\\/gs;
506                    my $r = test_s( $rule );
507                    diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
508                    diag dump($struct) if ($debug);
509                    is_deeply( $r, $struct, $msg );
510            }
511    
512            # test split_rec_on
513            test_rule(
514                    'split_rec_on',
515                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
516                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
517                    [ 'foo' ],
518            );
519            test_rule(
520                    'split_rec_on',
521                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
522                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
523                    [ 'bar' ],
524            );
525            test_rule(
526                    'split_rec_on no part',
527                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
528                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
529                    [ 'foo', 'bar' ],
530            );
531            test_rule(
532                    'split_rec_on no record',
533                    {},
534                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
535                    [ '' ],
536            );
537    
538            test_rec_rules(
539                    'marc_compose+split_rec_on',
540                    { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
541                    qq{
542                            marc_compose('900',
543                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
544                                    'c', rec(200,'c'),
545                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
546                                    'b', rec(200,'b'),
547                            );
548                    },
549                    [
550                            [ '900', ' ', ' ',
551                                    'a', 'foo',
552                                    'c', 'baz',
553                                    'a', 'bar',
554                                    'b', 42,
555                            ]
556                    ],
557            );
558    
559            cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
560            cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
561            ok(marc_leader(), 'marc_leader get');
562            diag "leader: ", dump(marc_leader()) if ($debug);
563            is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
564    
565            test_rule(
566                    'rec1(000)',
567                    { '000' => [ 42 ]},
568                    qq{ rec('000') },
569                    [ 42 ],
570            );
571    
572            test_rec_rules(
573                    'marc(001,rec(000))',
574                    { '000' => [ 42 ]},
575                    qq{
576                            marc('001', rec('000') );
577                    },
578                    [
579                            [ '001', 42, ]
580                    ],
581            );
582    
583            test_rec_rules(
584                    'marc_remove subfield',
585                    { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
586                    qq{
587                            marc('900', 'a', rec('200','a') );
588                            marc('900', 'b', rec('200','b') );
589                            marc_remove('900','b');
590                            marc('900', 'b', rec('200','c') );
591                            marc_remove('900','a');
592                    },
593                    [
594                            [ '900', ' ', ' ', 'b', 'baz' ],
595                    ],
596            );
597    
598            test_rec_rules(
599                    'marc_remove field',
600                    { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
601                    qq{
602                            marc('900', 'a', rec('200','a') );
603                            marc('900', 'b', rec('200','b') );
604                            marc('901', 'b', rec('200','b') );
605                            marc('901', 'c', rec('200','c') );
606                            marc_remove('900');
607                    },
608                    [
609                            [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
610                    ],
611            );
612            test_rec_rules(
613                    'marc_duplicate',
614                    { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
615                    qq{
616                            marc('900', 'a', rec('200','a') );
617                            marc('900', 'b', rec('200','b') );
618                            marc_duplicate;
619                            marc_remove('900','b');
620                            marc('900', 'b', rec('200','c') );
621                            marc_duplicate;
622                            marc_remove('900','b');
623                            marc('900', 'b', rec('200','d') );
624                            marc_duplicate;
625                            marc_remove('900','b');
626                            marc('900', 'b', rec('200','e') );
627                    },
628                    [
629                            # this will return FIRST record
630                            [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
631                    ],
632            );
633    
634            my $i = 0;
635            foreach my $v ( qw/bar baz bing bong/ ) {
636    
637                    ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
638                            "_get_marc_fields( offset => $i )"
639                    );
640                    diag "marc $i = ", dump( $marc ) if ($debug);
641                    is_deeply( $marc,
642                            [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
643                            "MARC copy $i has $v",
644                    );
645                    $i++;
646            }
647    
648            test_rec_rules(
649                    'marc_original_order',
650                    {
651                            '200' => [ {
652                                    a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
653                                    subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
654                            }, {
655                                    a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
656                                    subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
657                            } ],
658                    },
659                    qq{
660                            marc_original_order(900,200);
661                    },
662                    [
663                            [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
664                            [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
665                    ],
666            );
667    
668            test_rule(
669                    'rec1 skips subfields',
670                    {
671                            '200' => [ {
672                                    a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
673                                    subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
674                            }, {
675                                    a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
676                                    subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
677                            } ],
678                    },
679                    qq{
680                            rec1(200);
681                    },
682                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
683            );
684    
685            is_deeply(
686                    [ _pack_subfields_hash({
687                            a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
688                            subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
689                    }) ],
690                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
691                    '_pack_subfields_hash( $h )'
692            );
693    
694            is_deeply(
695                    [ _pack_subfields_hash({
696                            a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
697                            subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
698                    }, 1) ],
699                    ['^a','a1', '^b','b1', '^a','a2', '^b','b2', '^c','c1', '^c','c2'],
700                    '_pack_subfields_hash( $h, 1 )'
701            );
702  }  }
703    

Legend:
Removed from v.538  
changed lines
  Added in v.641

  ViewVC Help
Powered by ViewVC 1.1.26