2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 25; |
use Test::More tests => 42; |
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"; |
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 |
{ |
{ |
146 |
|
|
147 |
set_rec( $rec1 ); |
set_rec( $rec1 ); |
148 |
|
|
149 |
cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2' ); |
cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' ); |
150 |
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' ); |
151 |
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' ); |
152 |
|
diag "is_deeply checks\n"; |
153 |
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/ ] ); |
154 |
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/ ]); |
155 |
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/ ]); |
156 |
is_deeply( \[ rec('902') ], \[ '900' ] ); |
is_deeply( \[ rec('902') ], \[ '900' ] ); |
157 |
cmp_ok( rec('902'), 'eq', rec('902','z') ); |
|
158 |
|
cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' ); |
159 |
|
|
160 |
set_lookup( $lookup1 ); |
set_lookup( $lookup1 ); |
161 |
|
|
167 |
) |
) |
168 |
) |
) |
169 |
), |
), |
170 |
'eq', 'lookup 1 i lookup 2'); |
'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2'); |
171 |
|
|
172 |
set_lookup( $lookup2 ); |
set_lookup( $lookup2 ); |
173 |
|
|
174 |
is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ] ); |
is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' ); |
175 |
|
|
176 |
ok(! lookup('non-existent') ); |
ok(! lookup('non-existent'), 'lookup non-existant' ); |
177 |
|
|
178 |
set_rec( $rec2 ); |
set_rec( $rec2 ); |
179 |
|
|
208 |
ok(my $ds = get_ds(), "get_ds"); |
ok(my $ds = get_ds(), "get_ds"); |
209 |
diag "ds = ", Dumper($ds) if ($debug); |
diag "ds = ", Dumper($ds) if ($debug); |
210 |
|
|
211 |
|
|
212 |
|
sub test_check_ds { |
213 |
|
|
214 |
|
my $t = shift; |
215 |
|
|
216 |
|
ok($ds = get_ds(), 'get_ds'); |
217 |
|
diag Dumper( $ds ) if ($debug); |
218 |
|
|
219 |
|
ok( $ds && $ds->{something}, 'get_ds->something exists' ); |
220 |
|
ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t); |
221 |
|
ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' ); |
222 |
|
|
223 |
|
return $ds; |
224 |
|
} |
225 |
|
|
226 |
clean_ds(); |
clean_ds(); |
227 |
|
test_s(qq{ search('something', '42'); }); |
228 |
|
test_s(qq{ search('empty', ''); }); |
229 |
|
test_check_ds('search'); |
230 |
|
|
231 |
my $n = read_file( "$abs_path/conf/normalize/isis_ffzg.pl" ); |
clean_ds(); |
232 |
|
test_s(qq{ display('something', '42'); }); |
233 |
|
test_s(qq{ display('empty', ''); }); |
234 |
|
test_check_ds('display'); |
235 |
|
|
236 |
|
clean_ds(); |
237 |
|
test_s(qq{ tag('something', '42'); }); |
238 |
|
test_s(qq{ tag('empty', ''); }); |
239 |
|
test_check_ds('search'); |
240 |
|
test_check_ds('display'); |
241 |
|
|
242 |
|
clean_ds(); |
243 |
|
my $n = read_file( "$abs_path/data/normalize.pl" ); |
244 |
$n .= "\n1;\n"; |
$n .= "\n1;\n"; |
245 |
#diag "normalize code:\n$n\n"; |
#diag "normalize code:\n$n\n"; |
246 |
test_s( $n ); |
test_s( $n ); |