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 | |
Akron | 5eb3aa0 | 2019-01-25 18:30:47 +0100 | [diff] [blame] | 50 | _contains_not($fields, 'subTitle'); |
| 51 | |
| 52 | |
| 53 | sub _contains { |
| 54 | my ($fields, $key, $value, $type) = @_; |
| 55 | |
| 56 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 57 | |
| 58 | my $tb = Test::More->builder; |
| 59 | |
| 60 | foreach (@$fields) { |
| 61 | if ($_->{key} eq $key) { |
| 62 | |
| 63 | my $cmp_value = $_->{value}; |
| 64 | if ($_->{type} eq 'type:keywords' && ref($cmp_value) eq 'ARRAY') { |
| 65 | $cmp_value = join(' ', @{$cmp_value}); |
| 66 | }; |
| 67 | |
| 68 | if ($cmp_value eq $value) { |
| 69 | if ($_->{type} eq 'type:' . $type) { |
| 70 | $tb->ok(1, 'Contains ' . $key); |
| 71 | } |
| 72 | else { |
| 73 | $tb->ok(0, 'Contains ' . $key . ' but type ' . $_->{type} . ' != ' . $type); |
| 74 | }; |
| 75 | } |
| 76 | else { |
| 77 | $tb->ok(0, 'Contains ' . $key . ' but value ' . $cmp_value . ' != ' . $value); |
| 78 | }; |
| 79 | return; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | $tb->ok(0, 'Contains ' . $key); |
| 84 | }; |
| 85 | |
| 86 | sub _contains_not { |
| 87 | my ($fields, $key) = @_; |
| 88 | |
| 89 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 90 | |
| 91 | my $tb = Test::More->builder; |
| 92 | |
| 93 | foreach (@$fields) { |
| 94 | if ($_->{key} eq $key) { |
| 95 | $tb->ok(0, 'Contains not ' . $key); |
| 96 | return; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | $tb->ok(1, 'Contains not ' . $key); |
| 101 | }; |
| 102 | |
| 103 | done_testing; |
| 104 | __END__ |