/[webpac2]/trunk/t/3-normalize-set.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-set.t

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

revision 461 by dpavlin, Fri May 12 14:54:25 2006 UTC revision 490 by dpavlin, Sun May 14 12:35:20 2006 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 23;  use Test::More tests => 64;
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    
11  use Data::Dumper;  use Data::Dumper;
12    my $debug = shift @ARGV;
13    
14  BEGIN {  BEGIN {
15          use_ok( 'WebPAC::Normalize::Set' );          use_ok( 'WebPAC::Normalize::Set' );
16  }  }
17    
18  ok(my $abs_path = abs_path($0), "abs_path");  ok(my $abs_path = abs_path($0), "abs_path");
19  $abs_path =~ s#/[^/]*$#/../#;  $abs_path =~ s#/[^/]*$#/#;
20  #diag "abs_path: $abs_path";  diag "abs_path: $abs_path" if ($debug);
21    
22  #throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";  #throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";
23    
24  my $rec = {  my $rec1 = {
25            '200' => [{
26                    'a' => '200a',
27                    'b' => '200b',
28                    },{
29                    'c' => '200c',
30                    'd' => '200d',
31                    },{
32                    'a' => '200a*2',
33                    'd' => '200d*2',
34                    }],
35            '201' => [{
36                    'x' => '201x',
37                    'y' => '201y',
38                    }],
39            '900' => [
40                    '900-no_subfield'
41                    ],
42            '901' => [{
43                    'a' => '900a',
44                    }],
45            '902' => [{
46                    'z' => '900',
47                    }],
48    };
49    
50    my $rec2 = {
51   '675' => [ {   '675' => [ {
52                'a' => '159.9'                'a' => '159.9'
53              } ],              } ],
# Line 66  my $rec = { Line 93  my $rec = {
93   '101' => [ 'ENG' ],   '101' => [ 'ENG' ],
94   '686' => [ '2140' ],   '686' => [ '2140' ],
95   '300' => [ 'Prijevod djela: ' ],   '300' => [ 'Prijevod djela: ' ],
  '999' => [ { 'z' => '900' } ],  
96  };  };
97    
98    
99  my $lookup = {  my $lookup1 = {
100          '00900' => [          '00900' => [
101                  'lookup vrijednost 1',                  'lookup 1',
102                  'lookup vrijednost 2',                  'lookup 2',
103          ],          ],
104  };  };
105    
106  print Dumper($rec);  my $lookup2 = {
107            '00900' => 'lookup',
108    };
109    
110    
111  sub test {  sub test {
112          print Dumper( @_ ), ("-" x 78), "\n";          print Dumper( @_ ), ("-" x 78), "\n";
113          ok( defined(@_) );          ok( defined(@_) );
114  }  }
115    
116    # how much of string evaled to display?
117    my $max_eval_output = 170;
118    
119    sub dump_error {
120            my ($msg,$code) = @_;
121    
122            my @l = split(/[\n\r]/, $code);
123            my $out = "$msg\n";
124    
125            foreach my $i ( 0 .. $#l ) {
126                    $out .= sprintf("%2d: %s\n", $i, $l[$i]);
127            }
128    
129            return $out;
130    }
131    
132  sub test_s {  sub test_s {
133          my $t = shift || die;          my $t = shift || die;
134          $t =~ s/[\n\r\s]+/ /gs;  
135          ok(my $v = eval "$t", "eval: $t");          my $eval_t = $t;
136          ok(! $@, "eval error: $@");          $eval_t =~ s/[\n\r\s]+/ /gs;
137            $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
138    
139            eval "$t";
140            ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t");
141  }  }
142    
143  {  {
144          no strict 'subs';          no strict 'subs';
145          use WebPAC::Normalize::Set;          use WebPAC::Normalize::Set;
146    
147          set_rec( $rec );          ok(! set_lookup( undef ), "set_lookup(undef)");
148    
149          test( join(",", rec2('200','a') ) );          set_rec( $rec1 );
150          test( join(",", rec2('200','a'), rec2('200','b') ) );  
151          test( join(" * ",rec1('200'), rec1('201') ) );          cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
152          test( rec1('200') );          cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
153          test( regex( 's/0/o/g', rec1('200') ) );          cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
154          test( grep { /\*/ } regex( 's/0/o/g', rec1('200') ) );          diag "is_deeply checks\n";
155          test( rec('999') );          is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
156          test( rec('999','z') );          is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ],  \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
157          test(          is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
158            is_deeply( \[ rec('902') ], \[ '900' ] );
159    
160            cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
161    
162            set_lookup( $lookup1 );
163            
164            cmp_ok(
165                  join_with(" i ",                  join_with(" i ",
166                          lookup(                          lookup(
167                                  regex( 's/^/00/',                                  regex( 's/^/00/',
168                                          rec2('999','z')                                          rec2('902','z')
169                                  )                                  )
170                          )                          )
171                  )                  ),
172          );          'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
173    
174            # check join_with operations
175    
176            sub test_join_with_2 {
177                    my ($a,$b,$e) = @_;
178    
179                    cmp_ok(
180                            join_with(" <1> ",
181                                    rec('201',$a),
182                                    rec('201',$b),
183                            ),
184                    'eq', $e, "join_with $a <1> $b = $e");
185            }
186    
187            test_join_with_2('_','_','');
188            test_join_with_2('x','_','201x');
189            test_join_with_2('_','x','201x');
190            test_join_with_2('x','y','201x <1> 201y');
191    
192            sub test_join_with_3 {
193                    my ($a,$b,$c,$e) = @_;
194    
195                    cmp_ok(
196                            join_with(" <1> ", rec('201',$a),
197                                    join_with(" <2> ", rec('201',$b),
198                                            rec('201',$c),
199                                    )
200                            ),
201                    'eq', $e, "join_with $a <1> $b <2> $c = $e");
202            };
203    
204            test_join_with_3('_','_','_','');
205            test_join_with_3('x','_','_','201x');
206            test_join_with_3('_','x','_','201x');
207            test_join_with_3('_','_','x','201x');
208            test_join_with_3('x','y','_','201x <1> 201y');
209            test_join_with_3('x','_','y','201x <1> 201y');
210            test_join_with_3('_','x','y','201x <2> 201y');
211            test_join_with_3('x','_','y','201x <1> 201y');
212            test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
213    
214            # test lookups
215    
216            set_lookup( $lookup2 );
217    
218            is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
219    
220            ok(! lookup('non-existent'), 'lookup non-existant' );
221    
222            set_rec( $rec2 );
223    
224          test_s(qq{          test_s(qq{
225                  tag('Title',                  tag('Title',
# Line 144  sub test_s { Line 250  sub test_s {
250          });          });
251    
252          ok(my $ds = get_ds(), "get_ds");          ok(my $ds = get_ds(), "get_ds");
253          diag "ds = ", Dumper($ds);          diag "ds = ", Dumper($ds) if ($debug);
254    
255    
256            sub test_check_ds {
257    
258                    my $t = shift;
259    
260                    ok($ds = get_ds(), 'get_ds');
261                    diag Dumper( $ds ) if ($debug);
262    
263                    ok( $ds && $ds->{something}, 'get_ds->something exists' );
264                    ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
265                    ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
266    
267                    return $ds;
268            }
269    
270            clean_ds();
271            test_s(qq{ search('something', '42'); });
272            test_s(qq{ search('empty', ''); });
273            test_check_ds('search');
274    
275            clean_ds();
276            test_s(qq{ display('something', '42'); });
277            test_s(qq{ display('empty', ''); });
278            test_check_ds('display');
279    
280          clean_ds();          clean_ds();
281            test_s(qq{ tag('something', '42'); });
282            test_s(qq{ tag('empty', ''); });
283            test_check_ds('search');
284            test_check_ds('display');
285    
286          my $n = read_file( "$abs_path/conf/normalize/isis_ffzg.pl" );          clean_ds();
287            my $n = read_file( "$abs_path/data/normalize.pl" );
288          $n .= "\n1;\n";          $n .= "\n1;\n";
289          #diag "normalize code:\n$n\n";          #diag "normalize code:\n$n\n";
290          test_s( $n );          test_s( $n );
291    
292          ok($ds = get_ds(), "get_ds");          ok($ds = get_ds(), "get_ds");
293          diag "ds = ", Dumper($ds);          diag "ds = ", Dumper($ds) if ($debug);
294    
295            my $rec = {
296                    '200' => [{
297                            'a' => '200a',
298                            'b' => '200b',
299                    }],
300            };
301            my $rules = qq{ search('mixed', rec('200') ) };
302            
303            clean_ds();
304            set_rec( $rec );
305            test_s( $rules );
306            ok($ds = get_ds(), "get_ds");
307            is_deeply( $ds, {
308                    'mixed' => {
309                            'search' => [ '200a', '200b' ],
310                            'tag' => 'mixed'
311                    }
312            }, 'correct get_ds');
313    
314            ok(my $ds2 = WebPAC::Normalize::Set::data_structure(
315                    row => $rec,
316                    rules => $rules,
317            ), 'data_structure');
318            is_deeply( $ds, $ds2, 'data_structure(s) same');
319    
320            # wird and non-valid structure which is supported anyway
321            clean_ds();
322            set_rec({
323                    '200' => [{
324                            'a' => '200a',
325                    },
326                            '200-solo'
327                    ]
328            });
329            test_s(qq{ search('mixed', rec('200') ) });
330            ok($ds = get_ds(), "get_ds");
331            is_deeply( $ds, {
332                    'mixed' => {
333                            'search' => [ '200a', '200-solo' ],
334                            'tag' => 'mixed'
335                    }
336            }, 'correct get_ds');
337    
338  }  }
339    

Legend:
Removed from v.461  
changed lines
  Added in v.490

  ViewVC Help
Powered by ViewVC 1.1.26