Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 1 | use strict; |
| 2 | use warnings; |
| 3 | use File::Basename 'dirname'; |
| 4 | use File::Spec::Functions qw/catfile/; |
Akron | 6896608 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 5 | use File::Temp ':POSIX'; |
| 6 | use IO::Uncompress::Unzip qw(unzip $UnzipError); |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 7 | |
| 8 | use Test::More; |
| 9 | use Test::Output; |
| 10 | |
Akron | 6896608 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 11 | # TODO: |
| 12 | # This is nearly unusable slow and due to namespaces extremely verbose |
| 13 | # - probably better switch to something based on Test::Mojo |
| 14 | use Test::XML::Simple; |
| 15 | |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 16 | my $f = dirname(__FILE__); |
| 17 | my $script = catfile($f, '..', 'script', 'tei2korapxml'); |
| 18 | ok(-f $script, 'Script found'); |
| 19 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame^] | 20 | stdout_like( |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 21 | sub { system('perl', $script, '--help') }, |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame^] | 22 | qr!This\s*program\s*is\s*usually\s*called\s*from\s*inside\s*another\s*script\.!, |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 23 | 'Help' |
| 24 | ); |
| 25 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame^] | 26 | stdout_like( |
| 27 | sub { system('perl', $script, '--version') }, |
| 28 | qr!tei2korapxml - v\d+?\.\d+?!, |
| 29 | 'Version' |
| 30 | ); |
| 31 | |
| 32 | |
Akron | 6896608 | 2020-02-13 15:52:18 +0100 | [diff] [blame] | 33 | # Load example file |
| 34 | my $file = catfile($f, 'data', 'goe_sample.i5.xml'); |
| 35 | my $outzip = tmpnam(); |
| 36 | |
| 37 | # Generate zip file (unportable!) |
| 38 | stderr_like( |
| 39 | sub { `cat '$file' | perl '$script' > '$outzip'` }, |
| 40 | qr!tei2korapxml: .*? text_id=GOE_AGA\.00000!, |
| 41 | 'Processing' |
| 42 | ); |
| 43 | |
| 44 | # Uncompress GOE/header.xml from zip file |
| 45 | my $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/header.xml'); |
| 46 | |
| 47 | ok($zip, 'Zip-File is created'); |
| 48 | |
| 49 | # Read GOE/header.xml |
| 50 | my $header_xml = ''; |
| 51 | $header_xml .= $zip->getline while !$zip->eof; |
| 52 | ok($zip->close, 'Closed'); |
| 53 | |
| 54 | xml_is($header_xml, '//korpusSigle', 'GOE', 'korpusSigle'); |
| 55 | xml_is($header_xml, '//h.title[@type="main"]', 'Goethes Werke', 'h.title'); |
| 56 | xml_is($header_xml, '//h.author', 'Goethe, Johann Wolfgang von', 'h.author'); |
| 57 | xml_is($header_xml, '//pubDate[@type="year"]', '1982', 'pubDate'); |
| 58 | |
| 59 | |
| 60 | # Uncompress GOE/AGA/header.xml from zip file |
| 61 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/header.xml'); |
| 62 | |
| 63 | ok($zip, 'Zip-File is found'); |
| 64 | |
| 65 | # Read GOE/AGA/header.xml |
| 66 | $header_xml = ''; |
| 67 | $header_xml .= $zip->getline while !$zip->eof; |
| 68 | ok($zip->close, 'Closed'); |
| 69 | |
| 70 | xml_is($header_xml, '//dokumentSigle', 'GOE/AGA', 'dokumentSigle'); |
| 71 | xml_is($header_xml, '//d.title', 'Goethe: Autobiographische Schriften II, (1817-1825, 1832)', 'd.title'); |
| 72 | xml_is($header_xml, '//creatDate', '1820-1822', 'creatDate'); |
| 73 | |
| 74 | |
| 75 | # Uncompress GOE/AGA/00000/header.xml from zip file |
| 76 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/header.xml'); |
| 77 | |
| 78 | ok($zip, 'Zip-File is found'); |
| 79 | |
| 80 | # Read GOE/AGA/00000/header.xml |
| 81 | $header_xml = ''; |
| 82 | $header_xml .= $zip->getline while !$zip->eof; |
| 83 | ok($zip->close, 'Closed'); |
| 84 | |
| 85 | # This is slow - should be improved for more tests |
| 86 | xml_is($header_xml, '//textSigle', 'GOE/AGA.00000', 'textSigle'); |
| 87 | xml_is($header_xml, '//analytic/h.title[@type="main"]', 'Campagne in Frankreich', 'h.title'); |
| 88 | |
| 89 | |
| 90 | # Uncompress GOE/AGA/00000/data.xml from zip file |
| 91 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/data.xml'); |
| 92 | |
| 93 | ok($zip, 'Zip-File is found'); |
| 94 | |
| 95 | # Read GOE/AGA/00000/data.xml |
| 96 | my $data_xml = ''; |
| 97 | $data_xml .= $zip->getline while !$zip->eof; |
| 98 | ok($zip->close, 'Closed'); |
| 99 | |
| 100 | xml_node($data_xml, '/*[name()="raw_text" and @docid="GOE_AGA.00000"]', 'text id'); |
| 101 | xml_like($data_xml, '/*[local-name()="raw_text"]/*[local-name()="text"]', qr!^Campagne in Frankreich 1792.*?uns allein begl.cke\.$!, 'text content'); |
| 102 | |
| 103 | # Uncompress GOE/AGA/00000/struct/structure.xml from zip file |
| 104 | $zip = IO::Uncompress::Unzip->new($outzip, Name => 'GOE/AGA/00000/struct/structure.xml'); |
| 105 | |
| 106 | ok($zip, 'Zip-File is found'); |
| 107 | |
| 108 | # Read GOE/AGA/00000/struct/structure.xml |
| 109 | my $struct_xml = ''; |
| 110 | $struct_xml .= $zip->getline while !$zip->eof; |
| 111 | ok($zip->close, 'Closed'); |
| 112 | |
| 113 | xml_is($struct_xml, '//*[name()="span" and @id="s3"]//*[@name="type"]', 'Autobiographie', 'text content'); |
| 114 | |
Akron | 797e807 | 2020-02-13 07:59:40 +0100 | [diff] [blame] | 115 | done_testing; |