/[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 536 by dpavlin, Mon Jun 26 16:39:51 2006 UTC revision 574 by dpavlin, Mon Jul 3 21:08:07 2006 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 67;  use Test::More tests => 140;
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  {  {
160          no strict 'subs';          no strict 'subs';
161          use WebPAC::Normalize;          use WebPAC::Normalize;
162    
163          ok(! set_lookup( undef ), "set_lookup(undef)");          ok(! _set_lookup( undef ), "set_lookup(undef)");
164    
165          set_rec( $rec1 );          _set_rec( $rec1 );
166    
167          cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );          cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
168          cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );          cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
# Line 165  sub test_s { Line 181  sub test_s {
181          cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');          cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
182    
183    
184          set_lookup( $lookup1 );          _set_lookup( $lookup1 );
185                    
186          cmp_ok(          cmp_ok(
187                  join_with(" i ",                  join_with(" i ",
# Line 219  sub test_s { Line 235  sub test_s {
235    
236          # test lookups          # test lookups
237    
238          set_lookup( $lookup2 );          _set_lookup( $lookup2 );
239    
240          is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );          is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
241    
242          ok(! lookup('non-existent'), 'lookup non-existant' );          ok(! lookup('non-existent'), 'lookup non-existant' );
243    
244          set_rec( $rec2 );          _set_rec( $rec2 );
245    
246          test_s(qq{          test_s(qq{
247                  tag('Title',                  tag('Title',
# Line 255  sub test_s { Line 271  sub test_s {
271                  )                  )
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 {
279    
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 273  sub test_s { Line 289  sub test_s {
289                  return $ds;                  return $ds;
290          }          }
291    
292          clean_ds();          _clean_ds();
293          test_s(qq{ search('something', '42'); });          test_s(qq{ search('something', '42'); });
294          test_s(qq{ search('empty', ''); });          test_s(qq{ search('empty', ''); });
295          test_check_ds('search');          test_check_ds('search');
296    
297          clean_ds();          _clean_ds();
298          test_s(qq{ display('something', '42'); });          test_s(qq{ display('something', '42'); });
299          test_s(qq{ display('empty', ''); });          test_s(qq{ display('empty', ''); });
300          test_check_ds('display');          test_check_ds('display');
301    
302          clean_ds();          _clean_ds();
303          test_s(qq{ tag('something', '42'); });          test_s(qq{ tag('something', '42'); });
304          test_s(qq{ tag('empty', ''); });          test_s(qq{ tag('empty', ''); });
305          test_check_ds('search');          test_check_ds('search');
306          test_check_ds('display');          test_check_ds('display');
307    
308          clean_ds();          _clean_ds();
309          my $n = read_file( "$abs_path/data/normalize.pl" );          my $n = read_file( "$abs_path/data/normalize.pl" );
310          $n .= "\n1;\n";          $n .= "\n1;\n";
311          #diag "normalize code:\n$n\n";          #diag "normalize code:\n$n\n";
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 306  sub test_s { Line 322  sub test_s {
322          };          };
323          my $rules = qq{ search('mixed', rec('200') ) };          my $rules = qq{ search('mixed', rec('200') ) };
324                    
325          clean_ds();          _clean_ds();
326          set_rec( $rec );          _set_rec( $rec );
327          test_s( $rules );          test_s( $rules );
328          ok($ds = get_ds(), "get_ds");          ok($ds = _get_ds(), "get_ds");
329          is_deeply( $ds, {          is_deeply( $ds, {
330                  'mixed' => {                  'mixed' => {
331                          'search' => [ '200a', '200b' ],                          'search' => [ '200a', '200b' ],
# Line 324  sub test_s { Line 340  sub test_s {
340          is_deeply( $ds, $ds2, 'data_structure(s) same');          is_deeply( $ds, $ds2, 'data_structure(s) same');
341    
342          # wird and non-valid structure which is supported anyway          # wird and non-valid structure which is supported anyway
343          clean_ds();          _clean_ds();
344          set_rec({          _set_rec({
345                  '200' => [{                  '200' => [{
346                          'a' => '200a',                          'a' => '200a',
347                  },                  },
# Line 333  sub test_s { Line 349  sub test_s {
349                  ]                  ]
350          });          });
351          test_s(qq{ search('mixed', rec('200') ) });          test_s(qq{ search('mixed', rec('200') ) });
352          ok($ds = get_ds(), "get_ds");          ok($ds = _get_ds(), "get_ds");
353          is_deeply( $ds, {          is_deeply( $ds, {
354                  'mixed' => {                  'mixed' => {
355                          'search' => [ '200a', '200-solo' ],                          'search' => [ '200a', '200-solo' ],
# 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            #
483            # test rules
484            #
485            sub test_rule {
486                    my ($msg, $rec, $rule, $struct) = @_;
487                    _clean_ds();
488                    _set_rec( $rec );
489                    $rule =~ s/\\/\\/gs;
490                    my $r = test_s( $rule );
491                    diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
492                    diag dump($struct) if ($debug);
493                    is_deeply( $r, $struct, $msg );
494            }
495    
496            # test split_rec_on
497            test_rule(
498                    'split_rec_on',
499                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
500                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
501                    [ 'foo' ],
502            );
503            test_rule(
504                    'split_rec_on',
505                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
506                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
507                    [ 'bar' ],
508            );
509            test_rule(
510                    'split_rec_on no part',
511                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
512                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
513                    [ 'foo', 'bar' ],
514            );
515            test_rule(
516                    'split_rec_on no record',
517                    {},
518                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
519                    [ '' ],
520            );
521    
522            test_rec_rules(
523                    'marc_compose+split_rec_on',
524                    { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
525                    qq{
526                            marc_compose('900',
527                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
528                                    'c', rec(200,'c'),
529                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
530                                    'b', rec(200,'b'),
531                            );
532                    },
533                    [
534                            [ '900', ' ', ' ',
535                                    'a', 'foo',
536                                    'c', 'baz',
537                                    'a', 'bar',
538                                    'b', 42,
539                            ]
540                    ],
541            );
542    
543            cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
544            cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
545            ok(marc_leader(), 'marc_leader get');
546            diag "leader: ", dump(marc_leader()) if ($debug);
547            is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
548    
549            test_rule(
550                    'rec1(000)',
551                    { '000' => [ 42 ]},
552                    qq{ rec('000') },
553                    [ 42 ],
554            );
555    
556            test_rec_rules(
557                    'marc(001,rec(000))',
558                    { '000' => [ 42 ]},
559                    qq{
560                            marc('001', rec('000') );
561                    },
562                    [
563                            [ '001', 42, ]
564                    ],
565            );
566    
567            test_rec_rules(
568                    'marc_remove',
569                    { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
570                    qq{
571                            marc('900', 'a', rec('200','a') );
572                            marc('900', 'b', rec('200','b') );
573                            marc_remove('900','b');
574                            marc('900', 'b', rec('200','c') );
575                            marc_remove('900','a');
576                    },
577                    [
578                            [ '900', ' ', ' ', 'b', 'baz' ],
579                    ],
580            );
581    
582            test_rec_rules(
583                    'marc_duplicate',
584                    { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
585                    qq{
586                            marc('900', 'a', rec('200','a') );
587                            marc('900', 'b', rec('200','b') );
588                            marc_duplicate;
589                            marc_remove('900','b');
590                            marc('900', 'b', rec('200','c') );
591                            marc_duplicate;
592                            marc_remove('900','b');
593                            marc('900', 'b', rec('200','d') );
594                            marc_duplicate;
595                            marc_remove('900','b');
596                            marc('900', 'b', rec('200','e') );
597                    },
598                    [
599                            # this will return FIRST record
600                            [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
601                    ],
602            );
603    
604            my $i = 0;
605            foreach my $v ( qw/bar baz bing bong/ ) {
606    
607                    ok(@marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
608                            "_get_marc_fields( offset => $i )"
609                    );
610                    diag "marc $i = ", dump( @marc ) if ($debug);
611                    is_deeply( \@marc,
612                            [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
613                            "MARC copy $i has $v",
614                    );
615                    $i++;
616            }
617  }  }
618    

Legend:
Removed from v.536  
changed lines
  Added in v.574

  ViewVC Help
Powered by ViewVC 1.1.26