/[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 460 by dpavlin, Fri May 12 14:07:08 2006 UTC revision 501 by dpavlin, Sun May 14 22:08:51 2006 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 16;  use Test::More tests => 67;
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          ok(my $v = eval "$t", "eval: $t");  
135          ok(! $@, "$t == ".join("|", @{$v}));          my $eval_t = $t;
136            $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            set_rec( $rec1 );
150    
151            cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
152            cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
153            cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
154            diag "is_deeply checks\n";
155            is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
156            is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ],  \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
157            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            # simple list manipulatons
163            cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
164            cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
165            cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
166    
167    
168          test( join(",", rec2('200','a') ) );          set_lookup( $lookup1 );
169          test( join(",", rec2('200','a'), rec2('200','b') ) );          
170          test( join(" * ",rec1('200'), rec1('201') ) );          cmp_ok(
         test( rec1('200') );  
         test( regex( 's/0/o/g', rec1('200') ) );  
         test( grep { /\*/ } regex( 's/0/o/g', rec1('200') ) );  
         test( rec('999') );  
         test( rec('999','z') );  
         test(  
171                  join_with(" i ",                  join_with(" i ",
172                          lookup(                          lookup(
173                                  regex( 's/^/00/',                                  regex( 's/^/00/',
174                                          rec2('999','z')                                          rec2('902','z')
175                                  )                                  )
176                          )                          )
177                  )                  ),
178          );          'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
179    
180            # check join_with operations
181    
182            sub test_join_with_2 {
183                    my ($a,$b,$e) = @_;
184    
185                    cmp_ok(
186                            join_with(" <1> ",
187                                    rec('201',$a),
188                                    rec('201',$b),
189                            ),
190                    'eq', $e, "join_with $a <1> $b = $e");
191            }
192    
193            test_join_with_2('_','_','');
194            test_join_with_2('x','_','201x');
195            test_join_with_2('_','x','201x');
196            test_join_with_2('x','y','201x <1> 201y');
197    
198            sub test_join_with_3 {
199                    my ($a,$b,$c,$e) = @_;
200    
201                    cmp_ok(
202                            join_with(" <1> ", rec('201',$a),
203                                    join_with(" <2> ", rec('201',$b),
204                                            rec('201',$c),
205                                    )
206                            ),
207                    'eq', $e, "join_with $a <1> $b <2> $c = $e");
208            };
209    
210            test_join_with_3('_','_','_','');
211            test_join_with_3('x','_','_','201x');
212            test_join_with_3('_','x','_','201x');
213            test_join_with_3('_','_','x','201x');
214            test_join_with_3('x','y','_','201x <1> 201y');
215            test_join_with_3('x','_','y','201x <1> 201y');
216            test_join_with_3('_','x','y','201x <2> 201y');
217            test_join_with_3('x','_','y','201x <1> 201y');
218            test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
219    
220            # test lookups
221    
222            set_lookup( $lookup2 );
223    
224            is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
225    
226            ok(! lookup('non-existent'), 'lookup non-existant' );
227    
228            set_rec( $rec2 );
229    
230          test_s(qq{          test_s(qq{
231                  tag('Title',                  tag('Title',
# Line 143  sub test_s { Line 256  sub test_s {
256          });          });
257    
258          ok(my $ds = get_ds(), "get_ds");          ok(my $ds = get_ds(), "get_ds");
259          diag "ds = ", Dumper($ds);          diag "ds = ", Dumper($ds) if ($debug);
260    
261    
262            sub test_check_ds {
263    
264                    my $t = shift;
265    
266                    ok($ds = get_ds(), 'get_ds');
267                    diag Dumper( $ds ) if ($debug);
268    
269          #my $n = read_file( "$abs_path/conf/normalize/isis_ffzg.pl" );                  ok( $ds && $ds->{something}, 'get_ds->something exists' );
270                    ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
271                    ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
272    
273                    return $ds;
274            }
275    
276            clean_ds();
277            test_s(qq{ search('something', '42'); });
278            test_s(qq{ search('empty', ''); });
279            test_check_ds('search');
280    
281            clean_ds();
282            test_s(qq{ display('something', '42'); });
283            test_s(qq{ display('empty', ''); });
284            test_check_ds('display');
285    
286            clean_ds();
287            test_s(qq{ tag('something', '42'); });
288            test_s(qq{ tag('empty', ''); });
289            test_check_ds('search');
290            test_check_ds('display');
291    
292            clean_ds();
293            my $n = read_file( "$abs_path/data/normalize.pl" );
294            $n .= "\n1;\n";
295          #diag "normalize code:\n$n\n";          #diag "normalize code:\n$n\n";
296          #eval "$n";          test_s( $n );
297          #warn $@ if ($@);  
298            ok($ds = get_ds(), "get_ds");
299            diag "ds = ", Dumper($ds) if ($debug);
300    
301            my $rec = {
302                    '200' => [{
303                            'a' => '200a',
304                            'b' => '200b',
305                    }],
306            };
307            my $rules = qq{ search('mixed', rec('200') ) };
308            
309            clean_ds();
310            set_rec( $rec );
311            test_s( $rules );
312            ok($ds = get_ds(), "get_ds");
313            is_deeply( $ds, {
314                    'mixed' => {
315                            'search' => [ '200a', '200b' ],
316                            'tag' => 'mixed'
317                    }
318            }, 'correct get_ds');
319    
320            ok(my $ds2 = WebPAC::Normalize::Set::data_structure(
321                    row => $rec,
322                    rules => $rules,
323            ), 'data_structure');
324            is_deeply( $ds, $ds2, 'data_structure(s) same');
325    
326            # wird and non-valid structure which is supported anyway
327            clean_ds();
328            set_rec({
329                    '200' => [{
330                            'a' => '200a',
331                    },
332                            '200-solo'
333                    ]
334            });
335            test_s(qq{ search('mixed', rec('200') ) });
336            ok($ds = get_ds(), "get_ds");
337            is_deeply( $ds, {
338                    'mixed' => {
339                            'search' => [ '200a', '200-solo' ],
340                            'tag' => 'mixed'
341                    }
342            }, 'correct get_ds');
343    
344  }  }
345    

Legend:
Removed from v.460  
changed lines
  Added in v.501

  ViewVC Help
Powered by ViewVC 1.1.26