/[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 550 by dpavlin, Fri Jun 30 18:48:33 2006 UTC revision 571 by dpavlin, Mon Jul 3 14:30:22 2006 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 81;  use Test::More tests => 112;
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::Dump qw/dump/;  
 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 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 341  sub test_s { Line 357  sub test_s {
357                  }                  }
358          }, 'correct get_ds');          }, 'correct get_ds');
359    
360            #
361          # MARC          # MARC
362            #
363    
364          test_s(qq{ marc_indicators('900',1,2) });          test_s(qq{ marc_indicators('900',1,2) });
365          test_s(qq{ marc('900','a', rec('200') ) });          test_s(qq{ marc('900','a', rec('200') ) });
366          my @marc;          my @marc;
# Line 364  sub test_s { Line 383  sub test_s {
383                  [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]                  [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
384          ], 'correct marc with repetable subfield');          ], 'correct marc with repetable subfield');
385    
386          _clean_ds();          #
387          _set_rec({          # test magic re-ordering of input data
388                  '200' => [{          #
389                          'a' => '200a-1',  
390                          'b' => '200b-1',          sub test_rec_rules {
391                          'c' => '200c-1',                  my ($msg, $rec, $rules, $struct) = @_;
392                  }, {  
393                          'a' => '200a-2',                  _clean_ds();
394                          'b' => '200b-2',                  _set_rec($rec);
395                          'c' => '200c-2',  
396                  }, {                  foreach my $r (split(/;/, $rules)) {
397                          'a' => '200a-3',                          $r =~ s/[\s\n\r]+/ /gs;
398                          'c' => '200c-3',                          $r =~ s/^\s+//gs;
399                  }]                          $r =~ s/\s+$//gs;
400          });                          test_s($r) if ($r);
401          test_s(qq{ marc_indicators('900',1 ,0) });                  }
         test_s(qq{ marc_repeatable_subfield('900','a', rec('200','a') ) });  
         test_s(qq{ marc('900','b', rec('200','b') ) });  
         test_s(qq{ marc('900','c', rec('200','c') ) });  
402    
403          ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");                  ok(my @marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
404          diag dump( \@marc ) if ($debug);                  diag dump( \@marc ) if ($debug);
405                    diag "expects:\n", dump($struct) if ($debug > 1);
406                    is_deeply( \@marc, $struct, $msg );
407            }
408    
409          is_deeply( \@marc, [          test_rec_rules(
410                  [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],                  'correct marc with repetable subfield',
411                  [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],                  {
412                  [ '900', 1, 0, 'c', '200c-3' ],                          '200' => [{
413          ], 'correct marc with repetable subfield');                                  'a' => '200a-1',
414                                    'b' => '200b-1',
415                                    'c' => '200c-1',
416                            }, {
417                                    'a' => '200a-2',
418                                    'b' => '200b-2',
419                            }, {
420                                    'a' => '200a-3',
421                            }],
422                    },
423                    qq{
424                            marc_indicators('900',1 ,0);
425                            marc('900','a', rec('200','a') );
426                            marc('900','b', rec('200','b') );
427                            marc('900','c', rec('200','c') );
428                    },
429                    [
430                            [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
431                            [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
432                            [ '900', 1, 0, 'a', '200a-3' ],
433                    ],
434            );
435    
436    
437            test_rec_rules(
438                    'marc_repeatable_subfield',
439                    {
440                            '200' => [{
441                                    'a' => '200a-1',
442                                    'b' => '200b-1',
443                                    'c' => '200c-1',
444                            }, {
445                                    'a' => '200a-2',
446                                    'b' => '200b-2',
447                                    'c' => '200c-2',
448                            }, {
449                                    'a' => '200a-3',
450                                    'c' => '200c-3',
451                            }],
452                    },
453                    qq{
454                            marc_indicators('900',1 ,0);
455                            marc_repeatable_subfield('900','a', rec('200','a') );
456                            marc('900','b', rec('200','b') );
457                            marc('900','c', rec('200','c') );
458                    },
459                    [
460                            [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
461                            [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
462                            [ '900', 1, 0, 'c', '200c-3' ],
463                    ],
464            );
465    
466            test_rec_rules(
467                    'marc_compose',
468                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
469                    qq{
470                            marc_compose('900',
471                                    'c', rec(200,'b'),
472                                    'b', rec(200,'a'),
473                                    'a', rec(200,'c'),
474                            );
475                    },
476                    [
477                            [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
478                    ],
479            );
480    
481            #
482            # test rules
483            #
484            sub test_rule {
485                    my ($msg, $rec, $rule, $struct) = @_;
486                    _clean_ds();
487                    _set_rec( $rec );
488                    $rule =~ s/\\/\\/gs;
489                    my $r = test_s( $rule );
490                    diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
491                    diag dump($struct) if ($debug);
492                    is_deeply( $r, $struct, $msg );
493            }
494    
495            # test split_rec_on
496            test_rule(
497                    'split_rec_on',
498                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
499                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
500                    [ 'foo' ],
501            );
502            test_rule(
503                    'split_rec_on',
504                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
505                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
506                    [ 'bar' ],
507            );
508            test_rule(
509                    'split_rec_on no part',
510                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
511                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
512                    [ 'foo', 'bar' ],
513            );
514            test_rule(
515                    'split_rec_on no record',
516                    {},
517                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
518                    [ '' ],
519            );
520    
521            test_rec_rules(
522                    'marc_compose+split_rec_on',
523                    { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
524                    qq{
525                            marc_compose('900',
526                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
527                                    'c', rec(200,'c'),
528                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
529                                    'b', rec(200,'b'),
530                            );
531                    },
532                    [
533                            [ '900', ' ', ' ',
534                                    'a', 'foo',
535                                    'c', 'baz',
536                                    'a', 'bar',
537                                    'b', 42,
538                            ]
539                    ],
540            );
541    
542            cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
543            cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
544            ok(marc_leader(), 'marc_leader get');
545            diag "leader: ", dump(marc_leader()) if ($debug);
546            is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
547    
548            _debug(2);
549            test_rule(
550                    'rec1(000)',
551                    { '000' => [ 42 ]},
552                    qq{ rec('000') },
553                    [ 42 ],
554            );
555    
556            test_rec_rules(
557                    'marc_compose+split_rec_on',
558                    { '000' => [ 42 ]},
559                    qq{
560                            marc('001', rec('000') );
561                    },
562                    [
563                            [ '001', ' ', ' ', 42, ]
564                    ],
565            );
566  }  }
567    

Legend:
Removed from v.550  
changed lines
  Added in v.571

  ViewVC Help
Powered by ViewVC 1.1.26