lib/LaTeXML/Util/Test.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.0% 38 / 40 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/LaTeXML/Core/Definition/Expandable.pm ↑ Summary lib/LaTeXML.pm »
1 package LaTeXML::Util::Test;
2 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 28 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use LaTeXML::Util::Pathname;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use LaTeXML::Post;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use LaTeXML::Post::MathML::Presentation;
14 use LaTeXML::Post::XMath;
15 use Config;
16  
17 use base qw(Exporter);
18 # @Test::More::EXPORT);
19 our @EXPORT = (qw(&latexml_ok &latexml_tests),
20 qw(&process_domstring &process_xmlfile &process_htmlfile
21 &is_strings &get_filecontent
22 &convert_texfile_as_test &serialize_dom_as_test),
23 @Test::More::EXPORT);
24 # Note that this is a singlet; the same Builder is shared.
25  
26 # Test the conversion of all *.tex files in the given directory (typically t/something)
27 # Skip any that have no corresponding *.xml file.
28 sub latexml_tests {
29 my ($directory, %options) = @_;
30 my $DIR;
31 if ($options{texlive_min} && (texlive_version() < $options{texlive_min})) {
32 plan skip_all => "Requirement minimal texlive $options{texlive_min} not met.";
33 return done_testing(); }
34 if (!opendir($DIR, $directory)) {
35 # Can't read directory? Fail (assumed single) test.
36 return do_fail($directory, "Couldn't read directory $directory:$!"); }
37 else {
38 my @dir_contents = sort readdir($DIR);
39 my $t;
40 my @core_tests = map { (($t = $_) =~ s/\.tex$// ? ($t) : ()); } @dir_contents;
41 my @post_tests = map { (($t = $_) =~ s/-post\.xml$// ? ($t) : ()); } @dir_contents;
42 my @daemon_tests = map { (($t = $_) =~ s/\.spec$// ? ($t) : ()); } @dir_contents;
43 closedir($DIR);
44 # (try to) predict how many subtests there will be.
45 # (this helps the report when the latexml crashes completely)
46 plan tests => (1
47 + scalar(@core_tests)
48 + scalar(@post_tests)
49 + 2 * scalar(@daemon_tests) - ($directory =~ /runtimes/ ? 1 : 0)); # !!
50 if (eval { use_ok("LaTeXML::Core"); }) {
51 SKIP: {
52 my $requires = $options{requires} || {}; # normally a hash: test=>[files...]
53 $requires = { '*' => $requires } unless ref $requires; # scalar== filename required by ALL
54 ## Carry out any TeX conversion tests
55 foreach my $name (@core_tests) {
56 my $test = "$directory/$name";
57 SKIP: {
58 skip("No file $test.xml", 1) unless (-f "$test.xml");
59 next unless check_requirements($test, 1, $$requires{'*'}, $$requires{$name});
60 latexml_ok("$test.tex", "$test.xml", $test, $options{compare}, $options{core_options}); } }
61 # Carry out any post-processing tests
62 foreach my $name (@post_tests) {
63 my $test = "$directory/$name";
64 SKIP: {
65 skip("No file $test.xml and/or $test-post.xml", 1)
66 unless ((-f "$test.xml") && (-f "$test-post.xml"));
67 next unless check_requirements($test, 1, $$requires{'*'}, $$requires{$name});
68 latexmlpost_ok("$test.tex", "$test-post.xml", $test); } }
69 # Carry out any daemon tests.
70 foreach my $name (@daemon_tests) {
71 my $test = "$directory/$name";
72 SKIP: {
73 skip("No file $test.xml", 1)
74 unless (-f "$test.xml");
75 my $ntests = ($directory =~ /runtimes/ ? 1 : 2);
76 next unless check_requirements($test, $ntests, $$requires{'*'}, $$requires{$name});
77 daemon_ok($test, $directory, $options{generate});
78 } } } }
79 else {
80 skip_all("Couldn't load LaTeXML"); } }
81 return done_testing(); }
82  
83 sub check_requirements {
84 my ($test, $ntests, @reqmts) = @_;
85 foreach my $reqmts (@reqmts) {
86 next unless $reqmts;
87 my @required_packages = ();
88 my $texlive_min = 0;
89 my $required_env;
90 if (!(ref $reqmts)) {
91 @required_packages = ($reqmts); }
92 elsif (ref $reqmts eq 'ARRAY') {
93 @required_packages = @$reqmts; }
94 elsif (ref $reqmts eq 'HASH') {
95 @required_packages = (ref $$reqmts{packages} eq 'ARRAY' ? @{ $$reqmts{packages} } : $$reqmts{packages});
96 $required_env = $$reqmts{env};
97 $texlive_min = $$reqmts{texlive_min} || 0; }
98 foreach my $reqmt (@required_packages) {
99 if (pathname_kpsewhich($reqmt) || pathname_find($reqmt)) { }
100 else {
101 my $message = "Missing requirement $reqmt for $test";
102 diag("Skip: $message");
103 skip($message, $ntests);
104 return 0; } }
105 # Check if specific texlive versions are required for this test
106 if ($texlive_min && (texlive_version() < $texlive_min)) {
107 my $message = "Minimal texlive $texlive_min requirement not met for $test";
108 diag("Skip: $message");
109 skip($message, $ntests);
110 return 0; }
111 elsif ($required_env && !$ENV{$required_env}) {
112 my $message = "$test is only checked in continuous integration. (use make test CI=true)";
113 diag("Skip: $message");
114 skip($message, $ntests);
115 return 0; } }
116 return 1; }
117  
118 sub do_fail {
119 my ($name, $diag) = @_;
120 my $ok = ok(0, $name);
121 diag($diag);
122 return $ok; }
123  
124 # Would like to evolve a sensible XML comparison.
125 # This is a start...
126  
127 # NOTE: This assumes you will have successfully loaded LaTeXML.
128 sub latexml_ok {
129 my ($texpath, $xmlpath, $name, $compare_kind, $core_options) = @_;
130 if (my $texstrings = process_texfile(texpath => $texpath, name => $name, core_options => $core_options, compare_kind => $compare_kind)) {
131 if (my $xmlstrings = process_xmlfile($xmlpath, $name, $compare_kind)) {
132 return is_strings($texstrings, $xmlstrings, $name); } } }
133  
134 sub latexmlpost_ok {
135 my ($xmlpath, $postxmlpath, $name) = @_;
136 if (my $texstrings = postprocess_xmlfile($xmlpath, $name)) {
137 if (my $xmlstrings = process_xmlfile($postxmlpath, $name)) {
138 return is_strings($texstrings, $xmlstrings, $name); } } }
139  
140 our %CORE_OPTIONS_FOR_TESTS = (
141 preload => [], searchpaths => [], includecomments => 0, includepathpis => 0, verbosity => -2);
142  
143 sub convert_texfile_as_test {
144 my (%options) = @_;
145 my $texpath = $options{texpath};
146 my $name = $options{name};
147 my $compare_kind = $options{compare_kind};
148 my %core_options = $options{core_options} ? %{ $options{core_options} } : %CORE_OPTIONS_FOR_TESTS;
149 my $latexml = eval { LaTeXML::Core->new(%core_options) };
150 if (!$latexml) {
151 do_fail($name, "Couldn't instanciate LaTeXML: " . @!); return; }
152 else {
153 my $dom = eval { $latexml->convertFile($texpath); };
154 if (!$dom) {
155 do_fail($name, "Couldn't convert $texpath: " . @!); return; }
156 else {
157 return $dom; } } }
158  
159 # These return the list-of-strings form of whatever was requested, if successful,
160 # otherwise undef; and they will have reported the failure
161 sub process_texfile {
162 my (%options) = @_;
163 if (my $dom = convert_texfile_as_test(%options)) {
164 my $name = $options{name};
165 my $compare_kind = $options{compare_kind};
166 return process_dom($dom, $name, $compare_kind); }
167 else { return; } }
168  
169 sub postprocess_xmlfile {
170 my ($xmlpath, $name) = @_;
171 my $xmath = LaTeXML::Post::XMath->new();
172 return do_fail($name, "Couldn't instanciate LaTeXML::Post::XMath") unless $xmath;
173 $xmath->setParallel(LaTeXML::Post::MathML::Presentation->new());
174 my @procs = ($xmath);
175 my $latexmlpost = LaTeXML::Post->new(verbosity => -1);
176 return do_fail($name, "Couldn't instanciate LaTeXML::Post:") unless $latexmlpost;
177  
178 my ($doc) = $latexmlpost->ProcessChain(
179 LaTeXML::Post::Document->newFromFile("$name.xml", validate => 1),
180 @procs);
181 return do_fail($name, "Couldn't process $name.xml") unless $doc;
182 return process_dom($doc, $name); }
183  
184 sub serialize_dom_as_test {
185 my ($xmldom) = @_;
186 my $domstring = eval { my $string = $xmldom->toString(1);
187 my $parser = XML::LibXML->new(load_ext_dtd => 0, validation => 0, keep_blanks => 1);
188 $parser->parse_string($string)->toStringC14N(0); };
189 return $domstring; }
190  
191 sub process_dom {
192 my ($xmldom, $name, $compare_kind) = @_;
193 # We want the DOM to be BOTH indented AND canonical!!
194 if (my $domstring = serialize_dom_as_test($xmldom)) {
195 return process_domstring($domstring, $name, $compare_kind); }
196 else {
197 do_fail($name, "Couldn't convert dom to string: " . $@); return; } }
198  
199 sub process_xmlfile {
200 my ($xmlpath, $name, $compare_kind) = @_;
201 my $domstring =
202 eval { my $parser = XML::LibXML->new(load_ext_dtd => 0, validation => 0, keep_blanks => 1);
203 $parser->parse_file($xmlpath)->toStringC14N(0); };
204 if (!$domstring) {
205 do_fail($name, "Could not convert file $xmlpath to string: " . $@); return; }
206 else {
207 return process_domstring($domstring, $name, $compare_kind); } }
208  
209 sub process_htmlfile {
210 my ($htmlpath, $name, $compare_kind) = @_;
211 my $domstring = eval {
212 my $dom = XML::LibXML->load_html(
213 location => $htmlpath,
214 # tags such as <article> or <math> are invalid?? ignore.
215 suppress_errors => 1,
216 recover => 1,
217 );
218 $dom && $dom->toStringHTML(); };
219 if (!$domstring) {
220 do_fail($name, "Could not convert file $htmlpath to string: " . $@); return; }
221 else {
222 return process_domstring($domstring, $name, $compare_kind); } }
223  
224 sub process_domstring {
225 my ($domstring, $name, $compare_kind) = @_;
226 if ($compare_kind && $compare_kind eq 'words') { # words
227 return [split(/\s+/, $domstring)]; }
228 else { # lines
229 return [split('\n', $domstring)]; } }
230  
231 # $strings1 is the currently generated material
232 # $strings2 is the stored expected result.
233 sub is_strings {
234 my ($strings1, $strings2, $name) = @_;
235 my $max = $#$strings1 > $#$strings2 ? $#$strings1 : $#$strings2;
236 my $ok = 1;
237 for (my $i = 0 ; $i <= $max ; $i++) {
238 my $string1 = $$strings1[$i];
239 my $string2 = $$strings2[$i];
240 if (defined $string1) {
241 chomp($string1); }
242 else {
243 $ok = 0; $string1 = ""; }
244 if (defined $string2) {
245 chomp($string2); }
246 else {
247 $ok = 0; $string2 = ""; }
248 if (!$ok || ($string1 ne $string2)) {
249 return do_fail($name,
250 "Difference at line " . ($i + 1) . " for $name\n"
251 . " got : '$string1'\n"
252 . " expected : '$string2'\n"); } }
253 return ok(1, $name); }
254  
255 sub daemon_ok {
256 my ($base, $dir, $generate) = @_;
257 my $current_dir = pathname_cwd();
258 my $localname = $base;
259 $localname =~ s/$dir\///;
260 my $opts = read_options("$base.spec", $base);
261 push @$opts, (['destination', "$localname.test.xml"],
262 ['log', "/dev/null"],
263 ['quiet', ''],
264 ['quiet', ''],
265 ['quiet', ''],
266 ['quiet', ''],
267 ['autoflush', 1],
268 ['timestamp', '0'],
269 ['nodefaultresources', ''],
270 ['xsltparameter', 'LATEXML_VERSION:TEST'],
271 ['nocomments', '']);
272 #### ['timeout', 10],
273  
274 my $latexmlc = catfile($FindBin::Bin, '..', 'blib', 'script', 'latexmlc');
275 $latexmlc =~ s/^\.\///;
276 my $path_to_perl = $Config{perlpath};
277  
278 my @invocation = ($path_to_perl, (map { ('-I', $_) } @INC), $latexmlc);
279 my $timed = undef;
280 foreach my $opt (@$opts) {
281 if ($$opt[0] eq 'timeout') { # Ensure .opt timeout takes precedence
282 if ($timed) { next; } else { $timed = 1; }
283 }
284 push(@invocation, '--' . $$opt[0] . (length($$opt[1]) ? ('=' . $$opt[1]) : ''));
285 }
286 if (!$generate) {
287 pathname_chdir($dir);
288 my $exit_code = system(@invocation);
289 if ($exit_code != 0) {
290 $exit_code = $exit_code >> 8;
291 }
292 my $target_code = $localname =~ /fatal/ ? 1 : 0;
293 is($exit_code, $target_code, "latexmlc invocation for test $localname yielded $! . \nInvocation: \"" . join('" "', @invocation) . '"');
294 pathname_chdir($current_dir);
295 # Compare the just generated $base.test.xml to the previous $base.xml
296 if (my $teststrings = process_xmlfile("$base.test.xml", $base)) {
297 if (my $xmlstrings = process_xmlfile("$base.xml", $base)) {
298 is_strings($teststrings, $xmlstrings, $base); } }
299 unlink "$base.test.xml" if -e "$base.test.xml";
300 }
301 else {
302 #TODO: Skip 3 tests
303 print STDERR ('"' . join('" "', @invocation) . "\"\n");
304 pathname_chdir($dir);
305 system(@invocation);
306 pathname_chdir($current_dir);
307 move("$base.test.xml", "$base.xml") if -e "$base.test.xml";
308 }
309 return; }
310  
311 sub read_options {
312 my ($optionfile, $testname) = @_;
313 my $opts = [];
314 my $OPT;
315 if (open($OPT, "<", $optionfile)) {
316 while (my $line = <$OPT>) {
317 next if $line =~ /^#/;
318 chomp($line);
319 if ($line =~ /(\S+)\s*=\s*(.*)/) {
320 my ($key, $value) = ($1, $2 || '');
321 $value =~ s/\s+$//;
322 push @$opts, [$key, $value]; } }
323 close $OPT; }
324 else {
325 do_fail($testname, "Could not open $optionfile"); }
326 return $opts; }
327  
328 sub get_filecontent {
329 my ($path, $testname) = @_;
330 my $IN;
331 my @lines;
332 if (-e $path) {
333 if (!open($IN, "<", $path)) {
334 do_fail($testname, "Could not open $path"); }
335 else {
336 { local $\ = undef;
337 @lines = <$IN>; }
338 close($IN);
339 }
340 }
341 if (scalar(@lines)) {
342 $lines[-1] =~ s/\s+$//;
343 } else {
344 push @lines, '';
345 }
346 return \@lines; }
347  
348 our $texlive_version;
349  
350 sub texlive_version {
351 if (defined $texlive_version) {
352 return $texlive_version; }
353 my $extra_flag = '';
354 if (defined $ENV{"LATEXML_TEST_TEXLIVE"}) {
355 return $ENV{"LATEXML_TEST_TEXLIVE"} + 0; }
356 my $tex;
357 # If kpsewhich specified, look for tex next to it
358 if (my $path = $ENV{LATEXML_KPSEWHICH}) {
359 if (($path =~ s/kpsewhich/tex/i) && (-X $path)) {
360 $tex = $path; } }
361 if (!$tex) { # Else look for executable
362 $tex = which("tex"); }
363 if ($tex && open(my $texfh, '-|', $tex, '--version')) { # If we found one, hope it has TeX Live version in it's --version
364 my $version_string;
365 { local $/; $version_string = <$texfh>; }
366 close($texfh);
367 if ($version_string =~ /TeX Live (\d+)/) {
368 $texlive_version = int($1); }
369 else {
370 $texlive_version = 0; } }
371 else {
372 $texlive_version = 0; }
373 return $texlive_version; }
374  
375 # TODO: Reconsider what else we need to test, ideas below:
376  
377 # Tier 1.3: Math setups with embedding variations
378  
379 # Tier 1.5:
380  
381 # Tier 2: Preloads and preambles
382  
383 # Tier 3: Autoflush and Timeouts
384  
385 # Tier 4: Ports and local conversion
386  
387 # Tier 5: Defaults and multi-job daemon processing
388  
389 # 1. We need to test daemon in fragment mode with fragment tests, math mode with math tests and standard mode with standard tests. Essentially, this is all about having the right preambles.
390  
391 # 2. We need to benchmark consecutive runs, to make sure the first run is slowest and the rest (3?5?) are not initializing.
392  
393 # 2.1. Set a --autoflush to 2 , send 3 conversions and make sure the process pid's differ.
394  
395 # 2.2. Make sure an infinite macro times out (set --timeout=3 for fast test)
396 # 2.3. Check if the server can be set up on all default ports.
397  
398 # 3. Exhaustively test all possible option combinations - we need triples of option vector with a test case and XML result, or some sane setup of this nature.
399  
400 # 4. Moreover, we should test the option logic by comparing input-output option hashes (again, exhaustively!)
401  
402 # 5. We need to compare the final document, log and summary produced.
403  
404 1;
« lib/LaTeXML/Core/Definition/Expandable.pm ↑ Summary lib/LaTeXML.pm »