2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 25; |
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 = 0; |
my $debug = shift @ARGV; |
13 |
|
|
14 |
BEGIN { |
BEGIN { |
15 |
use_ok( 'WebPAC::Normalize::Set' ); |
use_ok( 'WebPAC::Normalize::Set' ); |
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', 'join rec2' ); |
cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' ); |
171 |
), |
), |
172 |
'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2'); |
'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 ); |
set_lookup( $lookup2 ); |
217 |
|
|
218 |
is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' ); |
is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' ); |
252 |
ok(my $ds = get_ds(), "get_ds"); |
ok(my $ds = get_ds(), "get_ds"); |
253 |
diag "ds = ", Dumper($ds) if ($debug); |
diag "ds = ", Dumper($ds) if ($debug); |
254 |
|
|
|
clean_ds(); |
|
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'); }); |
test_s(qq{ search('something', '42'); }); |
272 |
test_s(qq{ search('empty', ''); }); |
test_s(qq{ search('empty', ''); }); |
273 |
|
test_check_ds('search'); |
274 |
|
|
275 |
ok($ds = get_ds(), 'get_ds'); |
clean_ds(); |
276 |
diag Dumper( $ds ) if ($debug); |
test_s(qq{ display('something', '42'); }); |
277 |
|
test_s(qq{ display('empty', ''); }); |
278 |
ok( $ds && $ds->{something}, 'get_ds->something' ); |
test_check_ds('display'); |
|
ok( $ds && !$ds->{empty}, 'get_ds->empty' ); |
|
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 |
|
clean_ds(); |
287 |
my $n = read_file( "$abs_path/data/normalize.pl" ); |
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"; |
291 |
|
|
292 |
ok($ds = get_ds(), "get_ds"); |
ok($ds = get_ds(), "get_ds"); |
293 |
diag "ds = ", Dumper($ds) if ($debug); |
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 |
|
|