Akron | 5eb3aa0 | 2019-01-25 18:30:47 +0100 | [diff] [blame^] | 1 | use strict; |
| 2 | use warnings; |
| 3 | use utf8; |
| 4 | use Test::More; |
| 5 | use Benchmark ':hireswallclock'; |
| 6 | use Mojo::DOM; |
| 7 | use Mojo::File; |
| 8 | use Mojo::ByteStream 'b'; |
| 9 | use Data::Dumper; |
| 10 | |
| 11 | use File::Basename 'dirname'; |
| 12 | use File::Spec::Functions 'catdir'; |
| 13 | |
| 14 | use_ok('KorAP::XML::Krill'); |
| 15 | |
| 16 | # WPD/00001 |
| 17 | my $path = catdir(dirname(__FILE__), 'corpus/WPD/00001'); |
| 18 | ok(my $doc = KorAP::XML::Krill->new( path => $path . '/' ), 'Load Korap::Document'); |
| 19 | like($doc->path, qr!\Q$path\E/!, 'Path'); |
| 20 | |
| 21 | ok($doc = KorAP::XML::Krill->new( path => $path ), 'Load Korap::Document'); |
| 22 | like($doc->path, qr!\Q$path\E/$!, 'Path'); |
| 23 | |
| 24 | ok($doc->parse, 'Parse document'); |
| 25 | |
| 26 | my $meta = $doc->meta; |
| 27 | |
| 28 | my $fields = $meta->to_koral_fields; |
| 29 | |
| 30 | # TODO: Check for foundries, tokenSource, layerInfos! |
| 31 | |
| 32 | _contains($fields, 'title', 'A', 'text'); |
| 33 | _contains($fields, 'textSigle', 'WPD/AAA/00001', 'string'); |
| 34 | _contains($fields, 'docSigle', 'WPD/AAA', 'string'); |
| 35 | _contains($fields, 'corpusSigle', 'WPD', 'string'); |
| 36 | _contains($fields, 'pubDate', '2005-03-28', 'date'); |
| 37 | _contains($fields, 'pubPlace', 'URL:http://de.wikipedia.org', 'string'); |
| 38 | _contains($fields, 'textClass', 'freizeit-unterhaltung reisen wissenschaft populaerwissenschaft', 'keywords'); |
| 39 | _contains($fields, 'author', 'Ruru; Jens.Ol; Aglarech; u.a.', 'text'); |
| 40 | |
| 41 | _contains($fields, 'editor', 'data:,wikipedia.org', 'attachement'); |
| 42 | _contains($fields, 'publisher', 'data:,Wikipedia', 'attachement'); |
| 43 | _contains($fields, 'creationDate', '2005', 'date'); |
| 44 | _contains_not($fields, 'textType'); |
| 45 | _contains_not($fields, 'textTypeArt'); |
| 46 | _contains_not($fields, 'textTypeRef'); |
| 47 | _contains_not($fields, 'textDomain'); |
| 48 | _contains_not($fields, 'keywords'); |
| 49 | |
| 50 | # diag Dumper $fields; |
| 51 | |
| 52 | _contains_not($fields, 'subTitle'); |
| 53 | |
| 54 | |
| 55 | sub _contains { |
| 56 | my ($fields, $key, $value, $type) = @_; |
| 57 | |
| 58 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 59 | |
| 60 | my $tb = Test::More->builder; |
| 61 | |
| 62 | foreach (@$fields) { |
| 63 | if ($_->{key} eq $key) { |
| 64 | |
| 65 | my $cmp_value = $_->{value}; |
| 66 | if ($_->{type} eq 'type:keywords' && ref($cmp_value) eq 'ARRAY') { |
| 67 | $cmp_value = join(' ', @{$cmp_value}); |
| 68 | }; |
| 69 | |
| 70 | if ($cmp_value eq $value) { |
| 71 | if ($_->{type} eq 'type:' . $type) { |
| 72 | $tb->ok(1, 'Contains ' . $key); |
| 73 | } |
| 74 | else { |
| 75 | $tb->ok(0, 'Contains ' . $key . ' but type ' . $_->{type} . ' != ' . $type); |
| 76 | }; |
| 77 | } |
| 78 | else { |
| 79 | $tb->ok(0, 'Contains ' . $key . ' but value ' . $cmp_value . ' != ' . $value); |
| 80 | }; |
| 81 | return; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | $tb->ok(0, 'Contains ' . $key); |
| 86 | }; |
| 87 | |
| 88 | sub _contains_not { |
| 89 | my ($fields, $key) = @_; |
| 90 | |
| 91 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 92 | |
| 93 | my $tb = Test::More->builder; |
| 94 | |
| 95 | foreach (@$fields) { |
| 96 | if ($_->{key} eq $key) { |
| 97 | $tb->ok(0, 'Contains not ' . $key); |
| 98 | return; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | $tb->ok(1, 'Contains not ' . $key); |
| 103 | }; |
| 104 | |
| 105 | done_testing; |
| 106 | __END__ |