--- trunk/t/3-normalize-xml.t 2005/07/24 15:03:11 31 +++ trunk/t/3-normalize-xml.t 2006/01/07 18:23:27 352 @@ -1,10 +1,11 @@ #!/usr/bin/perl -w -use Test::More tests => 10; +use Test::More tests => 84; use Test::Exception; use Cwd qw/abs_path/; use blib; use strict; +use Data::Dumper; BEGIN { use_ok( 'WebPAC::Normalize::XML' ); @@ -12,26 +13,182 @@ 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"; -throws_ok { $n->open( tag => 'isis', xml_file => 'foo' ) } qr/file.*doesn't exist/, "open with invalid xml_file"; +throws_ok { $n->open( tag => 'isis', xml_file => '/foo/bar/baz' ) } qr/file.*doesn't exist/, "open with invalid xml_file"; ok( $n->open( tag => 'isis', - xml_file => "$abs_path../conf/normalize/isis.xml", + xml_file => "$abs_path/data/normalize.xml", ), "open"); my $rec = { - '000' => [ '001' ], - '800' => [ 'foo' ], - '900' => [ 'bar' ], + '675' => [ + { + 'a' => '159.9' + } + ], + '210' => [ + { + 'c' => 'New York University press', + 'a' => 'New York', + 'd' => 'cop. 1988' + } + ], + '700' => [ + { + 'a' => 'Haynal', + 'b' => 'André' + } + ], + '801' => [ + 'FFZG' + ], + '991' => [ + '8302' + ], + '000' => [ + 1 + ], + '702' => [ + { + 'a' => 'Holder', + 'b' => 'Elizabeth' + } + ], + '215' => [ + { + 'c' => 'ilustr', + 'a' => 'xix, 202 str', + 'd' => '23cm' + } + ], + '990' => [ + '2140', + '88', + 'HAY' + ], + '200' => [ + { + 'e' => 'from Freud and Ferenczi to Michael balint', + 'a' => 'Controversies in psychoanalytic method', + 'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern', + 'f' => 'by André E. Haynal' + } + ], + '610' => [ + 'povijest psihoanalize' + ], + '994' => [ + { + 'c' => '', + 'a' => 'PS', + 'b' => 'MG' + } + ], + '320' => [ + 'Kazalo' + ], + '101' => [ + 'ENG' + ], + '686' => [ + '2140' + ], + '300' => [ + 'Prijevod djela: ' + ] }; -ok(my @ds = $n->data_structure( $rec ), "data_structure"); +foreach my $fld (keys %$rec) { + my $r = 0; + foreach my $item ($rec->{$fld}) { + 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($found, "found"); + } + my $found = 0; + ok(! $n->get_data(\$rec, $fld, 'x', $r, \$found), "no v${fld}^x"); + ok(! $found, "not found"); + } else { + my $found = 0; + ok($n->get_data(\$rec, $fld, '', $r, \$found), "v${fld}"); + ok($found, "found"); + } + } + my $found = 0; + ok(! $n->get_data(\$rec, '999', '', $r, \$found), "no v${fld}"); + ok(! $found, "not found"); +} + +ok(my $ds = $n->data_structure( $rec ), "data_structure"); + +#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,$rec,$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); + cmp_ok($ds->{Tag}->{display}->[0], 'eq', $r, "parse $r"); +} + +parse_test($import, $rec, '1 + 2 = 3 [yap]'); + +delete($rec->{'900'}->[0]->{'b'}); +parse_test($import, $rec, '1 = 3 [yap]'); + +$rec->{'900'}->[0]->{'b'} = 5; +$rec->{'900'}->[0]->{'c'} = 6; +parse_test($import, $rec, '1 + 5 = 6 [yap]'); + +delete($rec->{'900'}->[0]->{'c'}); +$rec->{'900'}->[0]->{'x'} = 'hmmm'; +parse_test($import, $rec, '1 + 5 [hmmm]'); + +$rec->{'900'}->[0]->{'x'} = 'nope!'; +delete($rec->{'900'}->[0]->{'a'}); +parse_test($import, $rec, '5 [nope!]');