--- trunk/t/3-normalize-xml.t 2005/11/19 23:48:24 70 +++ trunk/t/3-normalize-xml.t 2006/01/08 21:16:22 370 @@ -1,6 +1,6 @@ #!/usr/bin/perl -w -use Test::More tests => 74; +use Test::More tests => 132; use Test::Exception; use Cwd qw/abs_path/; use blib; @@ -13,12 +13,21 @@ ok(my $abs_path = abs_path($0), "abs_path"); $abs_path =~ s#/[^/]*$#/#; -diag "abs_path: $abs_path"; +#diag "abs_path: $abs_path"; throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup"; throws_ok { new WebPAC::Normalize::XML( lookup => 'bar' ) } qr/pair/, "lookup without lookup_regex"; -ok(my $n = new WebPAC::Normalize::XML( debug => 0 ), "new"); +ok(my $n = new WebPAC::Normalize::XML( + debug => 0, + filter => { + regex => sub { + my ($val, $regex) = @_; + eval "\$val =~ $regex"; + return $val; + }, + }, +), "new"); throws_ok { $n->open() } qr/tag/, "open without tag"; throws_ok { $n->open( tag => 'isis' ) } qr/xml_file/, "open without xml_file"; @@ -112,24 +121,141 @@ if (ref($item) eq 'HASH') { foreach my $sf (keys %$item) { my $found = 0; - ok($n->get_data(\$rec, $fld, $sf, $r, \$found), "v${fld}^${sf}"); + ok($n->get_data(\$rec, $fld, $sf, $r, \$found), "v${fld}^${sf} / $r"); ok($found, "found"); } my $found = 0; - ok(! $n->get_data(\$rec, $fld, 'x', $r, \$found), "no v${fld}^x"); + ok(! $n->get_data(\$rec, $fld, 'x', $r, \$found), "no v${fld}^x / $r"); ok(! $found, "not found"); } else { my $found = 0; - ok($n->get_data(\$rec, $fld, '', $r, \$found), "v${fld}"); + ok($n->get_data(\$rec, $fld, undef, $r, \$found), "v${fld} / $r"); ok($found, "found"); } } my $found = 0; - ok(! $n->get_data(\$rec, '999', '', $r, \$found), "no v${fld}"); + ok(! $n->get_data(\$rec, '999', '', $r, \$found), "no v${fld} / $r"); ok(! $found, "not found"); } ok(my $ds = $n->data_structure( $rec ), "data_structure"); -diag Dumper($rec, $ds); +#diag Dumper($rec, $ds); + +# fake load of our test normalisation data +$n->{tag} = 'isis'; + +#diag Dumper($n->{import_xml}->{indexer}); + + +$rec = { + '900' => [ { + 'a' => '1', + 'b' => '2', + 'c' => '3', + 'x' => 'yap', + } ], +}; + +my $import = { + 'Tag' => { 'isis' => [ + { content => 'v900^a + v900^b = v900^c [v900^x]' }, + ] }, +}; + +sub parse_test($$$$$) { + my ($import,$tag,$rec,$i,$r) = @_; + $n->{import_xml}->{indexer} = $import; + # erase internal cache (yak!) + delete($n->{tags_by_order}); + push @{$rec->{'000'}}, 42 unless ($rec->{'000'}); + #diag "test normalisation of: ",Dumper($n->{import_xml}->{indexer}, $rec); + ok(my $ds = $n->data_structure( $rec ), "data_structure"); + diag Dumper($ds) if ($i == 0); + cmp_ok($ds->{$tag}->{display}->[$i], 'eq', $r, "parse $tag/$i - $r"); +} + +parse_test($import, 'Tag', $rec, 0, '1 + 2 = 3 [yap]'); + +delete($rec->{'900'}->[0]->{'b'}); +parse_test($import, 'Tag', $rec, 0, '1 = 3 [yap]'); + +$rec->{'900'}->[0]->{'b'} = 5; +$rec->{'900'}->[0]->{'c'} = 6; +parse_test($import, 'Tag', $rec, 0, '1 + 5 = 6 [yap]'); + +delete($rec->{'900'}->[0]->{'c'}); +$rec->{'900'}->[0]->{'x'} = 'hmmm'; +parse_test($import, 'Tag', $rec, 0, '1 + 5 [hmmm]'); + +$rec->{'900'}->[0]->{'x'} = 'nope!'; +delete($rec->{'900'}->[0]->{'a'}); +parse_test($import, 'Tag', $rec, 0, '5 [nope!]'); + +$rec = { + '900' => [ { + 'b' => 'b1', + 'x' => 'b1', + },{ + 'a' => 'a1', + 'b' => 'b2', + 'x' => 'a1_b2', + },{ + 'b' => 'b3', + 'c' => 'c1', + 'x' => 'b3_c1', + },{ + 'a' => 'a2', + 'b' => 'b4', + 'c' => 'c2', + 'x' => 'a2_b4_c2', + } ], +}; + +$import = { + 'Tag' => { 'isis' => [ + { content => '0 v900^a 1 v900^b 2 v900^c 3 v900^x 4' }, + ] }, +}; + +parse_test($import, 'Tag', $rec, 0, '0 b1 3 b1 4'); +parse_test($import, 'Tag', $rec, 1, '0 a1 1 b2 3 a1_b2 4'); +parse_test($import, 'Tag', $rec, 2, '0 b3 2 c1 3 b3_c1 4'); +parse_test($import, 'Tag', $rec, 3, '0 a2 1 b4 2 c2 3 a2_b4_c2 4'); + +sub parse_test_arr($$$$) { + my ($import, $tag, $rec, $arr) = @_; + my $i = 0; + foreach my $res (@{$arr}) { + parse_test($import, $tag, $rec, $i, $res); + $i++; + } +} + +$import = { + 'Tag_a' => { 'isis' => [ + { content => 'v900^a' }, + ] }, + 'Tag_b' => { 'isis' => [ + { content => 'v900^b' }, + ] }, + 'Tag_c' => { 'isis' => [ + { content => 'v900^c' }, + ] }, + 'Tag_x' => { 'isis' => [ + { content => 'v900^x' }, + ] }, + 'Tag_s1' => { 'isis' => [ + { content => 's900^b = s900^x' }, + ] }, + 'Tag_s2' => { 'isis' => [ + { content => 's900^a s900^b s900^c s900^x' }, + ] }, +}; + +parse_test_arr($import, 'Tag_a', $rec, [ '','a1','','a2' ] ); +parse_test_arr($import, 'Tag_b', $rec, [ 'b1','b2','b3','b4' ] ); +parse_test_arr($import, 'Tag_c', $rec, [ '','','c1','c2' ] ); +parse_test_arr($import, 'Tag_x', $rec, [ 'b1','a1_b2','b3_c1','a2_b4_c2' ] ); +parse_test_arr($import, 'Tag_s1', $rec, [ 'b1 = b1', 'b1 = b1', 'b1 = b1', 'b1 = b1' ] );