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 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 27 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use LaTeXML::Util::Pathname;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶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 if (!(ref $reqmts)) {
90 @required_packages = ($reqmts); }
91 elsif (ref $reqmts eq 'ARRAY') {
92 @required_packages = @$reqmts; }
93 elsif (ref $reqmts eq 'HASH') {
94 @required_packages = (ref $$reqmts{packages} eq 'ARRAY' ? @{ $$reqmts{packages} } : $$reqmts{packages});
95 $texlive_min = $$reqmts{texlive_min} || 0; }
96 foreach my $reqmt (@required_packages) {
97 if (pathname_kpsewhich($reqmt) || pathname_find($reqmt)) { }
98 else {
99 my $message = "Missing requirement $reqmt for $test";
100 diag("Skip: $message");
101 skip($message, $ntests);
102 return 0; } }
103 # Check if specific texlive versions are required for this test
104 if ($texlive_min && (texlive_version() < $texlive_min)) {
105 my $message = "Minimal texlive $texlive_min requirement not met for $test";
106 diag("Skip: $message");
107 skip($message, $ntests);
108 return 0; } }
109 return 1; }
110  
111 sub do_fail {
112 my ($name, $diag) = @_;
113 my $ok = ok(0, $name);
114 diag($diag);
115 return $ok; }
116  
117 # Would like to evolve a sensible XML comparison.
118 # This is a start...
119  
120 # NOTE: This assumes you will have successfully loaded LaTeXML.
121 sub latexml_ok {
122 my ($texpath, $xmlpath, $name, $compare_kind, $core_options) = @_;
123 if (my $texstrings = process_texfile(texpath => $texpath, name => $name, core_options => $core_options, compare_kind => $compare_kind)) {
124 if (my $xmlstrings = process_xmlfile($xmlpath, $name, $compare_kind)) {
125 return is_strings($texstrings, $xmlstrings, $name); } } }
126  
127 sub latexmlpost_ok {
128 my ($xmlpath, $postxmlpath, $name) = @_;
129 if (my $texstrings = postprocess_xmlfile($xmlpath, $name)) {
130 if (my $xmlstrings = process_xmlfile($postxmlpath, $name)) {
131 return is_strings($texstrings, $xmlstrings, $name); } } }
132  
133 our %CORE_OPTIONS_FOR_TESTS = (
134 preload => [], searchpaths => [], includecomments => 0, includepathpis => 0, verbosity => -2);
135  
136 sub convert_texfile_as_test {
137 my (%options) = @_;
138 my $texpath = $options{texpath};
139 my $name = $options{name};
140 my $compare_kind = $options{compare_kind};
141 my %core_options = $options{core_options} ? %{ $options{core_options} } : %CORE_OPTIONS_FOR_TESTS;
142 my $latexml = eval { LaTeXML::Core->new(%core_options) };
143 if (!$latexml) {
144 do_fail($name, "Couldn't instanciate LaTeXML: " . @!); return; }
145 else {
146 my $dom = eval { $latexml->convertFile($texpath); };
147 if (!$dom) {
148 do_fail($name, "Couldn't convert $texpath: " . @!); return; }
149 else {
150 return $dom; } } }
151  
152 # These return the list-of-strings form of whatever was requested, if successful,
153 # otherwise undef; and they will have reported the failure
154 sub process_texfile {
155 my (%options) = @_;
156 if (my $dom = convert_texfile_as_test(%options)) {
157 my $name = $options{name};
158 my $compare_kind = $options{compare_kind};
159 return process_dom($dom, $name, $compare_kind); }
160 else { return; } }
161  
162 sub postprocess_xmlfile {
163 my ($xmlpath, $name) = @_;
164 my $xmath = LaTeXML::Post::XMath->new();
165 return do_fail($name, "Couldn't instanciate LaTeXML::Post::XMath") unless $xmath;
166 $xmath->setParallel(LaTeXML::Post::MathML::Presentation->new());
167 my @procs = ($xmath);
168 my $latexmlpost = LaTeXML::Post->new(verbosity => -1);
169 return do_fail($name, "Couldn't instanciate LaTeXML::Post:") unless $latexmlpost;
170  
171 my ($doc) = $latexmlpost->ProcessChain(
172 LaTeXML::Post::Document->newFromFile("$name.xml", validate => 1),
173 @procs);
174 return do_fail($name, "Couldn't process $name.xml") unless $doc;
175 return process_dom($doc, $name); }
176  
177 sub serialize_dom_as_test {
178 my ($xmldom) = @_;
179 my $domstring = eval { my $string = $xmldom->toString(1);
180 my $parser = XML::LibXML->new(load_ext_dtd => 0, validation => 0, keep_blanks => 1);
181 $parser->parse_string($string)->toStringC14N(0); };
182 return $domstring; }
183  
184 sub process_dom {
185 my ($xmldom, $name, $compare_kind) = @_;
186 # We want the DOM to be BOTH indented AND canonical!!
187 if (my $domstring = serialize_dom_as_test($xmldom)) {
188 return process_domstring($domstring, $name, $compare_kind); }
189 else {
190 do_fail($name, "Couldn't convert dom to string: " . $@); return; } }
191  
192 sub process_xmlfile {
193 my ($xmlpath, $name, $compare_kind) = @_;
194 my $domstring =
195 eval { my $parser = XML::LibXML->new(load_ext_dtd => 0, validation => 0, keep_blanks => 1);
196 $parser->parse_file($xmlpath)->toStringC14N(0); };
197 if (!$domstring) {
198 do_fail($name, "Could not convert file $xmlpath to string: " . $@); return; }
199 else {
200 return process_domstring($domstring, $name, $compare_kind); } }
201  
202 sub process_htmlfile {
203 my ($htmlpath, $name, $compare_kind) = @_;
204 my $domstring = eval {
205 my $dom = XML::LibXML->load_html(
206 location => $htmlpath,
207 # tags such as <article> or <math> are invalid?? ignore.
208 suppress_errors => 1,
209 recover => 1,
210 );
211 $dom && $dom->toStringHTML(); };
212 if (!$domstring) {
213 do_fail($name, "Could not convert file $htmlpath to string: " . $@); return; }
214 else {
215 return process_domstring($domstring, $name, $compare_kind); } }
216  
217 sub process_domstring {
218 my ($domstring, $name, $compare_kind) = @_;
219 if ($compare_kind && $compare_kind eq 'words') { # words
220 return [split(/\s+/, $domstring)]; }
221 else { # lines
222 return [split('\n', $domstring)]; } }
223  
224 # $strings1 is the currently generated material
225 # $strings2 is the stored expected result.
226 sub is_strings {
227 my ($strings1, $strings2, $name) = @_;
228 my $max = $#$strings1 > $#$strings2 ? $#$strings1 : $#$strings2;
229 my $ok = 1;
230 for (my $i = 0 ; $i <= $max ; $i++) {
231 my $string1 = $$strings1[$i];
232 my $string2 = $$strings2[$i];
233 if (defined $string1) {
234 chomp($string1); }
235 else {
236 $ok = 0; $string1 = ""; }
237 if (defined $string2) {
238 chomp($string2); }
239 else {
240 $ok = 0; $string2 = ""; }
241 if (!$ok || ($string1 ne $string2)) {
242 return do_fail($name,
243 "Difference at line " . ($i + 1) . " for $name\n"
244 . " got : '$string1'\n"
245 . " expected : '$string2'\n"); } }
246 return ok(1, $name); }
247  
248 sub daemon_ok {
249 my ($base, $dir, $generate) = @_;
250 my $current_dir = pathname_cwd();
251 my $localname = $base;
252 $localname =~ s/$dir\///;
253 my $opts = read_options("$base.spec", $base);
254 push @$opts, (['destination', "$localname.test.xml"],
255 ['log', "/dev/null"],
256 ['quiet', ''],
257 ['quiet', ''],
258 ['quiet', ''],
259 ['quiet', ''],
260 ['autoflush', 1],
261 ['timestamp', '0'],
262 ['nodefaultresources', ''],
263 ['xsltparameter', 'LATEXML_VERSION:TEST'],
264 ['nocomments', '']);
265 #### ['timeout', 10],
266  
267 my $latexmlc = catfile($FindBin::Bin, '..', 'blib', 'script', 'latexmlc');
268 $latexmlc =~ s/^\.\///;
269 my $path_to_perl = $Config{perlpath};
270  
271 my @invocation = ($path_to_perl, (map { ('-I', $_) } @INC), $latexmlc);
272 my $timed = undef;
273 foreach my $opt (@$opts) {
274 if ($$opt[0] eq 'timeout') { # Ensure .opt timeout takes precedence
275 if ($timed) { next; } else { $timed = 1; }
276 }
277 push(@invocation, '--' . $$opt[0] . (length($$opt[1]) ? ('=' . $$opt[1]) : ''));
278 }
279 if (!$generate) {
280 pathname_chdir($dir);
281 my $exit_code = system(@invocation);
282 if ($exit_code != 0) {
283 $exit_code = $exit_code >> 8;
284 }
285 my $target_code = $localname =~ /fatal/ ? 1 : 0;
286 is($exit_code, $target_code, "latexmlc invocation for test $localname yielded $! . \nInvocation: \"" . join('" "', @invocation) . '"');
287 pathname_chdir($current_dir);
288 # Compare the just generated $base.test.xml to the previous $base.xml
289 if (my $teststrings = process_xmlfile("$base.test.xml", $base)) {
290 if (my $xmlstrings = process_xmlfile("$base.xml", $base)) {
291 is_strings($teststrings, $xmlstrings, $base); } }
292 unlink "$base.test.xml" if -e "$base.test.xml";
293 }
294 else {
295 #TODO: Skip 3 tests
296 print STDERR ('"' . join('" "', @invocation) . "\"\n");
297 pathname_chdir($dir);
298 system(@invocation);
299 pathname_chdir($current_dir);
300 move("$base.test.xml", "$base.xml") if -e "$base.test.xml";
301 }
302 return; }
303  
304 sub read_options {
305 my ($optionfile, $testname) = @_;
306 my $opts = [];
307 my $OPT;
308 if (open($OPT, "<", $optionfile)) {
309 while (my $line = <$OPT>) {
310 next if $line =~ /^#/;
311 chomp($line);
312 if ($line =~ /(\S+)\s*=\s*(.*)/) {
313 my ($key, $value) = ($1, $2 || '');
314 $value =~ s/\s+$//;
315 push @$opts, [$key, $value]; } }
316 close $OPT; }
317 else {
318 do_fail($testname, "Could not open $optionfile"); }
319 return $opts; }
320  
321 sub get_filecontent {
322 my ($path, $testname) = @_;
323 my $IN;
324 my @lines;
325 if (-e $path) {
326 if (!open($IN, "<", $path)) {
327 do_fail($testname, "Could not open $path"); }
328 else {
329 { local $\ = undef;
330 @lines = <$IN>; }
331 close($IN);
332 }
333 }
334 if (scalar(@lines)) {
335 $lines[-1] =~ s/\s+$//;
336 } else {
337 push @lines, '';
338 }
339 return \@lines; }
340  
341 our $texlive_version;
342  
343 sub texlive_version {
344 if (defined $texlive_version) {
345 return $texlive_version; }
346 my $extra_flag = '';
347 if (defined $ENV{"LATEXML_TEST_TEXLIVE"}) {
348 return $ENV{"LATEXML_TEST_TEXLIVE"} + 0; }
349 my $tex;
350 # If kpsewhich specified, look for tex next to it
351 if (my $path = $ENV{LATEXML_KPSEWHICH}) {
352 if (($path =~ s/kpsewhich/tex/i) && (-X $path)) {
353 $tex = $path; } }
354 if (!$tex) { # Else look for executable
355 $tex = which("tex"); }
356 if ($tex && open(my $texfh, '-|', $tex, '--version')) { # If we found one, hope it has TeX Live version in it's --version
357 my $version_string;
358 { local $/; $version_string = <$texfh>; }
359 close($texfh);
360 if ($version_string =~ /TeX Live (\d+)/) {
361 $texlive_version = int($1); }
362 else {
363 $texlive_version = 0; } }
364 else {
365 $texlive_version = 0; }
366 return $texlive_version; }
367  
368 # TODO: Reconsider what else we need to test, ideas below:
369  
370 # Tier 1.3: Math setups with embedding variations
371  
372 # Tier 1.5:
373  
374 # Tier 2: Preloads and preambles
375  
376 # Tier 3: Autoflush and Timeouts
377  
378 # Tier 4: Ports and local conversion
379  
380 # Tier 5: Defaults and multi-job daemon processing
381  
382 # 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.
383  
384 # 2. We need to benchmark consecutive runs, to make sure the first run is slowest and the rest (3?5?) are not initializing.
385  
386 # 2.1. Set a --autoflush to 2 , send 3 conversions and make sure the process pid's differ.
387  
388 # 2.2. Make sure an infinite macro times out (set --timeout=3 for fast test)
389 # 2.3. Check if the server can be set up on all default ports.
390  
391 # 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.
392  
393 # 4. Moreover, we should test the option logic by comparing input-output option hashes (again, exhaustively!)
394  
395 # 5. We need to compare the final document, log and summary produced.
396  
397 1;
« lib/LaTeXML/Core/Definition/Expandable.pm ↑ Summary lib/LaTeXML.pm »