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/Constructor.pm ↑ Summary lib/LaTeXML.pm »
1 package LaTeXML::Util::Test;
2 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use LaTeXML::Util::Pathname;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶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 &is_strings),
21 @Test::More::EXPORT);
22 # Note that this is a singlet; the same Builder is shared.
23  
24 # Test the conversion of all *.tex files in the given directory (typically t/something)
25 # Skip any that have no corresponding *.xml file.
26 sub latexml_tests {
27 my ($directory, %options) = @_;
28 my $DIR;
29 if (!opendir($DIR, $directory)) {
30 # Can't read directory? Fail (assumed single) test.
31 return do_fail($directory, "Couldn't read directory $directory:$!"); }
32 else {
33 my @dir_contents = sort readdir($DIR);
34 my $t;
35 my @core_tests = map { (($t = $_) =~ s/\.tex$// ? ($t) : ()); } @dir_contents;
36 my @post_tests = map { (($t = $_) =~ s/-post\.xml$// ? ($t) : ()); } @dir_contents;
37 my @daemon_tests = map { (($t = $_) =~ s/\.spec$// ? ($t) : ()); } @dir_contents;
38 closedir($DIR);
39 # (try to) predict how many subtests there will be.
40 # (this helps the report when the latexml crashes completely)
41 plan tests => (1
42 + scalar(@core_tests)
43 + scalar(@post_tests)
44 + 3 * scalar(@daemon_tests) - ($directory =~ /runtimes/ ? 2 : 0)); # !!
45 if (eval { use_ok("LaTeXML::Core"); }) {
46 SKIP: {
47 my $requires = $options{requires} || {}; # normally a hash: test=>[files...]
48 $requires = { '*' => $requires } unless ref $requires; # scalar== filename required by ALL
49 ## Carry out any TeX conversion tests
50 foreach my $name (@core_tests) {
51 my $test = "$directory/$name";
52 SKIP: {
53 skip("No file $test.xml", 1) unless (-f "$test.xml");
54 next unless check_requirements($test, 1, $$requires{'*'}, $$requires{$name});
55 latexml_ok("$test.tex", "$test.xml", $test, $options{compare}); } }
56 # Carry out any post-processing tests
57 foreach my $name (@post_tests) {
58 my $test = "$directory/$name";
59 SKIP: {
60 skip("No file $test.xml and/or $test-post.xml", 1)
61 unless ((-f "$test.xml") && (-f "$test-post.xml"));
62 next unless check_requirements($test, 1, $$requires{'*'}, $$requires{$name});
63 latexmlpost_ok("$test.tex", "$test-post.xml", $test); } }
64 # Carry out any daemon tests.
65 foreach my $name (@daemon_tests) {
66 my $test = "$directory/$name";
67 SKIP: {
68 skip("No file $test.xml and/or $test.status", 1)
69 unless ((-f "$test.xml") && (-f "$test.status"));
70 my $ntests = ($directory =~ /runtimes/ ? 1 : 3);
71 next unless check_requirements($test, $ntests, $$requires{'*'}, $$requires{$name});
72 daemon_ok($test, $directory, $options{generate});
73 } } } }
74 else {
75 skip_all("Couldn't load LaTeXML"); } }
76 return done_testing(); }
77  
78 sub check_requirements {
79 my ($test, $ntests, @reqmts) = @_;
80 foreach my $reqmts (@reqmts) {
81 next unless defined $reqmts;
82 foreach my $reqmt (!$reqmts ? () : (ref $reqmts ? @$reqmts : $reqmts)) {
83 if (pathname_kpsewhich($reqmt) || pathname_find($reqmt)) { }
84 else {
85 my $message = "Missing requirement $reqmt for $test";
86 diag("Skip: $message");
87 skip($message, $ntests);
88 return 0; } } }
89 return 1; }
90  
91 sub do_fail {
92 my ($name, $diag) = @_;
93 my $ok = ok(0, $name);
94 diag($diag);
95 return $ok; }
96  
97 # Would like to evolve a sensible XML comparison.
98 # This is a start...
99  
100 # NOTE: This assumes you will have successfully loaded LaTeXML.
101 sub latexml_ok {
102 my ($texpath, $xmlpath, $name, $compare_kind) = @_;
103 if (my $texstrings = process_texfile($texpath, $name, $compare_kind)) {
104 if (my $xmlstrings = process_xmlfile($xmlpath, $name, $compare_kind)) {
105 return is_strings($texstrings, $xmlstrings, $name); } } }
106  
107 sub latexmlpost_ok {
108 my ($xmlpath, $postxmlpath, $name) = @_;
109 if (my $texstrings = postprocess_xmlfile($xmlpath, $name)) {
110 if (my $xmlstrings = process_xmlfile($postxmlpath, $name)) {
111 return is_strings($texstrings, $xmlstrings, $name); } } }
112  
113 # These return the list-of-strings form of whatever was requested, if successful,
114 # otherwise undef; and they will have reported the failure
115 sub process_texfile {
116 my ($texpath, $name, $compare_kind) = @_;
117 my $latexml = eval { LaTeXML::Core->new(preload => [], searchpaths => [], includecomments => 0,
118 verbosity => -2); };
119 if (!$latexml) {
120 do_fail($name, "Couldn't instanciate LaTeXML: " . @!); return; }
121 else {
122 my $dom = eval { $latexml->convertFile($texpath); };
123 if (!$dom) {
124 do_fail($name, "Couldn't convert $texpath: " . @!); return; }
125 else {
126 return process_dom($dom, $name, $compare_kind); } } }
127  
128 sub postprocess_xmlfile {
129 my ($xmlpath, $name) = @_;
130 my $xmath = LaTeXML::Post::XMath->new();
131 return do_fail($name, "Couldn't instanciate LaTeXML::Post::XMath") unless $xmath;
132 $xmath->setParallel(LaTeXML::Post::MathML::Presentation->new());
133 my @procs = ($xmath);
134 my $latexmlpost = LaTeXML::Post->new(verbosity => -1);
135 return do_fail($name, "Couldn't instanciate LaTeXML::Post:") unless $latexmlpost;
136  
137 my ($doc) = $latexmlpost->ProcessChain(
138 LaTeXML::Post::Document->newFromFile("$name.xml", validate => 1),
139 @procs);
140 return do_fail($name, "Couldn't process $name.xml") unless $doc;
141 return process_dom($doc, $name); }
142  
143 sub process_dom {
144 my ($xmldom, $name, $compare_kind) = @_;
145 # We want the DOM to be BOTH indented AND canonical!!
146 my $domstring =
147 eval { my $string = $xmldom->toString(1);
148 my $parser = XML::LibXML->new(load_ext_dtd => 0, validation => 0, keep_blanks => 1);
149 $parser->parse_string($string)->toStringC14N(0); };
150 if (!$domstring) {
151 do_fail($name, "Couldn't convert dom to string: " . $@); return; }
152 else {
153 return process_domstring($domstring, $name, $compare_kind); } }
154  
155 sub process_xmlfile {
156 my ($xmlpath, $name, $compare_kind) = @_;
157 my $domstring =
158 eval { my $parser = XML::LibXML->new(load_ext_dtd => 0, validation => 0, keep_blanks => 1);
159 $parser->parse_file($xmlpath)->toStringC14N(0); };
160 if (!$domstring) {
161 do_fail($name, "Could not convert file $xmlpath to string: " . $@); return; }
162 else {
163 return process_domstring($domstring, $name, $compare_kind); } }
164  
165 sub process_domstring {
166 my ($domstring, $name, $compare_kind) = @_;
167 if ($compare_kind && $compare_kind eq 'words') { # words
168 return [split(/\s+/, $domstring)]; }
169 else { # lines
170 return [split('\n', $domstring)]; } }
171  
172 # $strings1 is the currently generated material
173 # $strings2 is the stored expected result.
174 sub is_strings {
175 my ($strings1, $strings2, $name) = @_;
176 my $max = $#$strings1 > $#$strings2 ? $#$strings1 : $#$strings2;
177 my $ok = 1;
178 for (my $i = 0 ; $i <= $max ; $i++) {
179 my $string1 = $$strings1[$i];
180 my $string2 = $$strings2[$i];
181 if (defined $string1) {
182 chomp($string1); }
183 else {
184 $ok = 0; $string1 = ""; }
185 if (defined $string2) {
186 chomp($string2); }
187 else {
188 $ok = 0; $string2 = ""; }
189 if (!$ok || ($string1 ne $string2)) {
190 return do_fail($name,
191 "Difference at line " . ($i + 1) . " for $name\n"
192 . " got : '$string1'\n"
193 . " expected : '$string2'\n"); } }
194 return ok(1, $name); }
195  
196 sub daemon_ok {
197 my ($base, $dir, $generate) = @_;
198 my $current_dir = pathname_cwd();
199 my $localname = $base;
200 $localname =~ s/$dir\///;
201 my $opts = read_options("$base.spec", $base);
202 push @$opts, (['destination', "$localname.test.xml"],
203 ['log', "/dev/null"],
204 ['timeout', 10],
205 ['autoflush', 1],
206 ['timestamp', '0'],
207 ['nodefaultresources', ''],
208 ['xsltparameter', 'LATEXML_VERSION:TEST'],
209 ['nocomments', '']);
210  
211 my $latexmlc = catfile($FindBin::Bin, '..', 'blib', 'script', 'latexmlc');
212 $latexmlc =~ s/^\.\///;
213 my $path_to_perl = $Config{perlpath};
214  
215 my $invocation = $path_to_perl . " " . join(" ", map { ("-I", $_) } @INC) . " " . $latexmlc . ' ';
216 my $timed = undef;
217 foreach my $opt (@$opts) {
218 if ($$opt[0] eq 'timeout') { # Ensure .opt timeout takes precedence
219 if ($timed) { next; } else { $timed = 1; }
220 }
221 $invocation .= "--" . $$opt[0] . (length($$opt[1]) ? ('="' . $$opt[1] . '" ') : (' '));
222 }
223 $invocation .= " 2>$localname.test.status ";
224 if (!$generate) {
225 pathname_chdir($dir);
226 my $exit_code = system($invocation);
227 if ($exit_code != 0) {
228 $exit_code = $exit_code >> 8;
229 }
230 is($exit_code, 0, "latexmlc invocation for test $localname: $invocation yeilded $!");
231 pathname_chdir($current_dir);
232 # Compare the just generated $base.test.xml to the previous $base.xml
233 if (my $teststrings = process_xmlfile("$base.test.xml", $base)) {
234 if (my $xmlstrings = process_xmlfile("$base.xml", $base)) {
235 is_strings($teststrings, $xmlstrings, $base); } }
236  
237 # Compare the just generated $base.test.status to the previous $base.status
238 if (my $teststatus = get_filecontent("$base.test.status", $base)) {
239 if (my $status = get_filecontent("$base.status", $base)) {
240 is_strings($teststatus, $status, $base); } }
241 unlink "$base.test.xml" if -e "$base.test.xml";
242 unlink "$base.test.status" if -e "$base.test.status";
243 }
244 else {
245 #TODO: Skip 3 tests
246 print STDERR "$invocation\n";
247 pathname_chdir($dir);
248 system($invocation);
249 pathname_chdir($current_dir);
250 move("$base.test.xml", "$base.xml") if -e "$base.test.xml";
251 move("$base.test.status", "$base.status") if -e "$base.test.status";
252 }
253 return; }
254  
255 sub read_options {
256 my ($optionfile, $testname) = @_;
257 my $opts = [];
258 my $OPT;
259 if (open($OPT, "<", $optionfile)) {
260 while (my $line = <$OPT>) {
261 next if $line =~ /^#/;
262 chomp($line);
263 if ($line =~ /(\S+)\s*=\s*(.*)/) {
264 my ($key, $value) = ($1, $2 || '');
265 $value =~ s/\s+$//;
266 push @$opts, [$key, $value]; } }
267 close $OPT; }
268 else {
269 do_fail($testname, "Could not open $optionfile"); }
270 return $opts; }
271  
272 sub get_filecontent {
273 my ($path, $testname) = @_;
274 my $IN;
275 my @lines;
276 if (-e $path) {
277 if (!open($IN, "<", $path)) {
278 do_fail($testname, "Could not open $path"); }
279 else {
280 { local $\ = undef;
281 @lines = <$IN>; }
282 close($IN);
283 }
284 }
285 if (scalar(@lines)) {
286 $lines[-1] =~ s/\s+$//;
287 } else {
288 push @lines, '';
289 }
290 return \@lines; }
291  
292 # TODO: Reconsider what else we need to test, ideas below:
293  
294 # Tier 1.3: Math setups with embedding variations
295  
296 # Tier 1.5:
297  
298 # Tier 2: Preloads and preambles
299  
300 # Tier 3: Autoflush and Timeouts
301  
302 # Tier 4: Ports and local conversion
303  
304 # Tier 5: Defaults and multi-job daemon processing
305  
306 # 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.
307  
308 # 2. We need to benchmark consecutive runs, to make sure the first run is slowest and the rest (3?5?) are not initializing.
309  
310 # 2.1. Set a --autoflush to 2 , send 3 conversions and make sure the process pid's differ.
311  
312 # 2.2. Make sure an infinite macro times out (set --timeout=3 for fast test)
313 # 2.3. Check if the server can be set up on all default ports.
314  
315 # 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.
316  
317 # 4. Moreover, we should test the option logic by comparing input-output option hashes (again, exhaustively!)
318  
319 # 5. We need to compare the final document, log and summary produced.
320  
321 1;
« lib/LaTeXML/Core/Definition/Constructor.pm ↑ Summary lib/LaTeXML.pm »