--- trunk/t/3-normalize-set.t 2006/05/12 19:59:25 463 +++ trunk/t/3-normalize-set.t 2006/05/12 21:46:19 464 @@ -2,13 +2,14 @@ use strict; -use Test::More tests => 23; +use Test::More tests => 25; use Test::Exception; use Cwd qw/abs_path/; use blib; use File::Slurp; use Data::Dumper; +my $debug = 0; BEGIN { use_ok( 'WebPAC::Normalize::Set' ); @@ -16,11 +17,37 @@ ok(my $abs_path = abs_path($0), "abs_path"); $abs_path =~ s#/[^/]*$#/../#; -#diag "abs_path: $abs_path"; +diag "abs_path: $abs_path" if ($debug); #throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup"; -my $rec = { +my $rec1 = { + '200' => [{ + 'a' => '200a', + 'b' => '200b', + },{ + 'c' => '200c', + 'd' => '200d', + },{ + 'a' => '200a*2', + 'd' => '200d*2', + }], + '201' => [{ + 'x' => '201x', + 'y' => '201y', + }], + '900' => [ + '900-no_subfield' + ], + '901' => [{ + 'a' => '900a', + }], + '902' => [{ + 'z' => '900', + }], +}; + +my $rec2 = { '675' => [ { 'a' => '159.9' } ], @@ -66,18 +93,20 @@ '101' => [ 'ENG' ], '686' => [ '2140' ], '300' => [ 'Prijevod djela: ' ], - '999' => [ { 'z' => '900' } ], }; -my $lookup = { +my $lookup1 = { '00900' => [ - 'lookup vrijednost 1', - 'lookup vrijednost 2', + 'lookup 1', + 'lookup 2', ], }; -print Dumper($rec); +my $lookup2 = { + '00900' => 'lookup', +}; + sub test { print Dumper( @_ ), ("-" x 78), "\n"; @@ -88,32 +117,43 @@ my $t = shift || die; $t =~ s/[\n\r\s]+/ /gs; ok(my $v = eval "$t", "eval: $t"); - ok(! $@, "eval error: $@"); + ok(! $@, $@ ? "error: $@" : "no error"); } { no strict 'subs'; use WebPAC::Normalize::Set; - set_rec( $rec ); + set_rec( $rec1 ); + + cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2' ); + cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b' ); + cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y' ); + is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] ); + is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]); + is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]); + is_deeply( \[ rec('902') ], \[ '900' ] ); + cmp_ok( rec('902'), 'eq', rec('902','z') ); - test( join(",", rec2('200','a') ) ); - test( join(",", rec2('200','a'), rec2('200','b') ) ); - test( join(" * ",rec1('200'), rec1('201') ) ); - 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( + set_lookup( $lookup1 ); + + cmp_ok( join_with(" i ", lookup( regex( 's/^/00/', - rec2('999','z') + rec2('902','z') ) ) - ) - ); + ), + 'eq', 'lookup 1 i lookup 2'); + + set_lookup( $lookup2 ); + + is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ] ); + + ok(! lookup('non-existent') ); + + set_rec( $rec2 ); test_s(qq{ tag('Title', @@ -144,7 +184,7 @@ }); ok(my $ds = get_ds(), "get_ds"); - diag "ds = ", Dumper($ds); + diag "ds = ", Dumper($ds) if ($debug); clean_ds(); @@ -154,6 +194,6 @@ test_s( $n ); ok($ds = get_ds(), "get_ds"); - diag "ds = ", Dumper($ds); + diag "ds = ", Dumper($ds) if ($debug); }