5 use Test::More tests => 69;
12 my $debug = shift @ARGV;
15 use_ok( 'WebPAC::Normalize' );
18 ok(my $abs_path = abs_path($0), "abs_path");
19 $abs_path =~ s#/[^/]*$#/#;
20 diag "abs_path: $abs_path" if ($debug);
22 #throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";
55 'c' => 'New York University press',
72 'a' => 'xix, 202 str',
81 'e' => 'from Freud and Ferenczi to Michael balint',
82 'a' => 'Controversies in psychoanalytic method',
83 'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
84 'f' => 'by André E. Haynal'
86 '610' => [ 'povijest psihoanalize' ],
92 '320' => [ 'Kazalo' ],
95 '300' => [ 'Prijevod djela: ' ],
112 print Dumper( @_ ), ("-" x 78), "\n";
116 # how much of string evaled to display?
117 my $max_eval_output = 170;
120 my ($msg,$code) = @_;
122 my @l = split(/[\n\r]/, $code);
125 foreach my $i ( 0 .. $#l ) {
126 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
133 my $t = shift || die;
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);
140 ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t");
145 use WebPAC::Normalize;
147 ok(! _set_lookup( undef ), "set_lookup(undef)");
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' ] );
160 cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
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');
168 _set_lookup( $lookup1 );
178 'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
180 # check join_with operations
182 sub test_join_with_2 {
190 'eq', $e, "join_with $a <1> $b = $e");
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');
198 sub test_join_with_3 {
199 my ($a,$b,$c,$e) = @_;
202 join_with(" <1> ", rec('201',$a),
203 join_with(" <2> ", rec('201',$b),
207 'eq', $e, "join_with $a <1> $b <2> $c = $e");
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');
222 _set_lookup( $lookup2 );
224 is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
226 ok(! lookup('non-existent'), 'lookup non-existant' );
252 regex( 's/[^\\d]+//',
258 ok(my $ds = _get_ds(), "get_ds");
259 diag "ds = ", Dumper($ds) if ($debug);
266 ok($ds = _get_ds(), 'get_ds');
267 diag Dumper( $ds ) if ($debug);
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' );
277 test_s(qq{ search('something', '42'); });
278 test_s(qq{ search('empty', ''); });
279 test_check_ds('search');
282 test_s(qq{ display('something', '42'); });
283 test_s(qq{ display('empty', ''); });
284 test_check_ds('display');
287 test_s(qq{ tag('something', '42'); });
288 test_s(qq{ tag('empty', ''); });
289 test_check_ds('search');
290 test_check_ds('display');
293 my $n = read_file( "$abs_path/data/normalize.pl" );
295 #diag "normalize code:\n$n\n";
298 ok($ds = _get_ds(), "get_ds");
299 diag "ds = ", Dumper($ds) if ($debug);
307 my $rules = qq{ search('mixed', rec('200') ) };
312 ok($ds = _get_ds(), "get_ds");
315 'search' => [ '200a', '200b' ],
318 }, 'correct get_ds');
320 ok(my $ds2 = WebPAC::Normalize::data_structure(
323 ), 'data_structure');
324 is_deeply( $ds, $ds2, 'data_structure(s) same');
326 # wird and non-valid structure which is supported anyway
335 test_s(qq{ search('mixed', rec('200') ) });
336 ok($ds = _get_ds(), "get_ds");
339 'search' => [ '200a', '200-solo' ],
342 }, 'correct get_ds');
345 test_s(qq{ marc21('900','a', rec('200') ) });
347 ok(@marc = WebPAC::Normalize::_get_marc21_fields(), "_get_marc21_fields");