/[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 464 by dpavlin, Fri May 12 21:46:19 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 => 25;  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 = 0;  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" if ($debug);  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";
# Line 113  sub test { Line 113  sub test {
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(! $@, $@ ? "error: $@" : "no 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            ok(! set_lookup( undef ), "set_lookup(undef)");
148    
149          set_rec( $rec1 );          set_rec( $rec1 );
150    
151          cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2' );          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' );          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' );          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/ ] );          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/ ]);          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/ ]);          is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
158          is_deeply( \[ rec('902') ], \[ '900' ] );          is_deeply( \[ rec('902') ], \[ '900' ] );
159          cmp_ok( rec('902'), 'eq', rec('902','z') );  
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          set_lookup( $lookup1 );          set_lookup( $lookup1 );
169                    
# Line 145  sub test_s { Line 175  sub test_s {
175                                  )                                  )
176                          )                          )
177                  ),                  ),
178          'eq', 'lookup 1 i lookup 2');          '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 );          set_lookup( $lookup2 );
223    
224          is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ] );          is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
225    
226          ok(! lookup('non-existent') );          ok(! lookup('non-existent'), 'lookup non-existant' );
227    
228          set_rec( $rec2 );          set_rec( $rec2 );
229    
# Line 186  sub test_s { Line 258  sub test_s {
258          ok(my $ds = get_ds(), "get_ds");          ok(my $ds = get_ds(), "get_ds");
259          diag "ds = ", Dumper($ds) if ($debug);          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                    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();          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          my $n = read_file( "$abs_path/conf/normalize/isis_ffzg.pl" );          clean_ds();
293            my $n = read_file( "$abs_path/data/normalize.pl" );
294          $n .= "\n1;\n";          $n .= "\n1;\n";
295          #diag "normalize code:\n$n\n";          #diag "normalize code:\n$n\n";
296          test_s( $n );          test_s( $n );
297    
298          ok($ds = get_ds(), "get_ds");          ok($ds = get_ds(), "get_ds");
299          diag "ds = ", Dumper($ds) if ($debug);          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.464  
changed lines
  Added in v.501

  ViewVC Help
Powered by ViewVC 1.1.26