OLD | NEW |
| (Empty) |
1 #!/usr/bin/env dart | |
2 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
3 // for details. All rights reserved. Use of this source code is governed by a | |
4 // BSD-style license that can be found in the LICENSE file. | |
5 | |
6 // testing ../../../tools/addlatexhash.dart | |
7 | |
8 import 'dart:io'; | |
9 import 'package:path/path.dart' as path; | |
10 import '../../../tools/addlatexhash.dart'; | |
11 | |
12 final execDir = path.dirname(path.fromUri(Platform.executable)); | |
13 final dartRootDir = path.dirname(path.dirname(execDir)); | |
14 final dartRootPath = dartRootDir.toString(); | |
15 | |
16 List<String> packageOptions() { | |
17 if (Platform.packageRoot != null) { | |
18 return <String>['--package-root=${Platform.packageRoot}']; | |
19 } else if (Platform.packageConfig != null) { | |
20 return <String>['--packages=${Platform.packageConfig}']; | |
21 } else { | |
22 return <String>[]; | |
23 } | |
24 } | |
25 | |
26 // Check that the given ProcessResult indicates success; if so | |
27 // return the standard output, otherwise report the failure | |
28 checkAction(result, errorMessage) { | |
29 if (result.exitCode != 0) { | |
30 print(result.stdout); | |
31 print(result.stderr); | |
32 throw errorMessage; | |
33 } | |
34 return result.stdout; | |
35 } | |
36 | |
37 oneTestCutMatch(line, re, expected) { | |
38 var result = cutMatch(line, new RegExp(re).firstMatch(line)); | |
39 if (result != expected) { | |
40 throw "cutMatch '$re' from '$line' yields '$result' != '$expected'"; | |
41 } | |
42 } | |
43 | |
44 void testCutMatch() { | |
45 oneTestCutMatch("test", "", "test"); | |
46 oneTestCutMatch("test", "e", "tst"); | |
47 oneTestCutMatch("test", "te", "st"); | |
48 oneTestCutMatch("test", "st", "te"); | |
49 oneTestCutMatch("test", "test", ""); | |
50 } | |
51 | |
52 oneTestSisp(sispFun, nameSuffix, line, expected) { | |
53 var result = sispFun(line); | |
54 if (result != expected) { | |
55 throw "sispIsDart$nameSuffix '$line' yields $result"; | |
56 } | |
57 } | |
58 | |
59 testSisp() { | |
60 oneTestSisp(sispIsDartBegin, "Begin", "\\begin{dartCode}\n", true); | |
61 oneTestSisp(sispIsDartBegin, "Begin", " \\begin{dartCode}\n", true); | |
62 oneTestSisp(sispIsDartBegin, "Begin", "whatever else ..", false); | |
63 oneTestSisp(sispIsDartEnd, "End", "\\end{dartCode}", true); | |
64 oneTestSisp(sispIsDartEnd, "End", " \\end{dartCode}\t \n", true); | |
65 oneTestSisp(sispIsDartEnd, "End", "whatever else ..", false); | |
66 } | |
67 | |
68 // Check that the hash values of paragraphs in the specially prepared | |
69 // LaTeX source 'addlatexhash_test_src.tex' are identical in groups | |
70 // of eight (so we get 8 identical hash values, then another hash | |
71 // value 8 times, etc.) | |
72 testSameHash(String tmpDirPath) { | |
73 // file names/paths for file containing groups of 8 variants of a paragraph | |
74 const par8timesName = "addlatexhash_test_src"; | |
75 const par8timesFileName = "$par8timesName.tex"; | |
76 final par8timesDirPath = path.join(dartRootDir, "tests", "standalone", "io"); | |
77 final par8timesPath = path.join(par8timesDirPath, par8timesFileName); | |
78 final tmpPar8timesPath = path.join(tmpDirPath, par8timesFileName); | |
79 | |
80 // file names paths for output | |
81 final hashName = par8timesName + "-hash"; | |
82 final hashFileName = "$hashName.tex"; | |
83 final hashPath = path.join(tmpDirPath, hashFileName); | |
84 final listName = par8timesName + "-list"; | |
85 final listFileName = "$listName.txt"; | |
86 final listPath = path.join(tmpDirPath, listFileName); | |
87 | |
88 // dart executable | |
89 final dartExecutable = Platform.executable; | |
90 if (dartExecutable == "") throw "dart executable not available"; | |
91 | |
92 // actions to take | |
93 runAddHash() { | |
94 var args = packageOptions(); | |
95 args.addAll([ | |
96 path.join(dartRootPath, "tools", "addlatexhash.dart"), | |
97 tmpPar8timesPath, | |
98 hashPath, | |
99 listPath | |
100 ]); | |
101 return Process.runSync(dartExecutable, args); | |
102 } | |
103 | |
104 // perform test | |
105 new File(par8timesPath).copySync(tmpPar8timesPath); | |
106 checkAction(runAddHash(), "addlatexhash.dart failed"); | |
107 var listFile = new File(listPath); | |
108 var listLines = listFile.readAsLinesSync(); | |
109 var latestLine = null; | |
110 var sameCount = 0; | |
111 for (var line in listLines) { | |
112 if (!line.startsWith(" ")) continue; // section marker | |
113 if (line.startsWith(" %")) continue; // transformed text "comment" | |
114 if (line != latestLine) { | |
115 // new hash, check for number of equal hashes, then reset | |
116 if (sameCount % 8 == 0) { | |
117 // saw zero or more blocks of 8 identical hash values: OK | |
118 latestLine = line; | |
119 sameCount = 1; | |
120 } else { | |
121 throw "normalization failed to produce same result"; | |
122 } | |
123 } else { | |
124 sameCount++; | |
125 } | |
126 } | |
127 } | |
128 | |
129 // Check that the LaTeX source transformation done by addlatexhash.dart | |
130 // does not affect the generated output, as seen via dvi2tty and diff. | |
131 // NB: Not part of normal testing (only local): latex and dvi2tty are | |
132 // not installed in the standard test environment. | |
133 testSameDVI(String tmpDirPath) { | |
134 // file names/paths for original spec | |
135 const specName = "dartLangSpec"; | |
136 const specFileName = "$specName.tex"; | |
137 final specDirPath = path.join(dartRootDir, "docs", "language"); | |
138 final specPath = path.join(specDirPath, specFileName); | |
139 final tmpSpecPath = path.join(tmpDirPath, specFileName); | |
140 const specDviFileName = "$specName.dvi"; | |
141 final specDviPath = path.join(tmpDirPath, specDviFileName); | |
142 | |
143 // file names/paths for associated sty | |
144 const styFileName = "dart.sty"; | |
145 final styPath = path.join(specDirPath, styFileName); | |
146 final tmpStyPath = path.join(tmpDirPath, styFileName); | |
147 | |
148 // file names paths for output | |
149 const hashName = "dartLangSpec-hash"; | |
150 const hashFileName = "$hashName.tex"; | |
151 final hashPath = path.join(tmpDirPath, hashFileName); | |
152 final hashDviPath = path.join(tmpDirPath, "$hashName.dvi"); | |
153 | |
154 final listName = "$specName-list"; | |
155 final listFileName = "$listName.txt"; | |
156 final listPath = path.join(tmpDirPath, listFileName); | |
157 | |
158 // dart executable | |
159 final dartExecutable = Platform.executable; | |
160 if (dartExecutable == "") throw "dart executable not available"; | |
161 | |
162 // actions to take; rely on having latex and dvi2tty in PATH | |
163 runLatex(fileName, workingDirectory) => | |
164 Process.runSync("latex", [fileName], workingDirectory: workingDirectory); | |
165 | |
166 runAddHash() { | |
167 var args = packageOptions(); | |
168 args.addAll([ | |
169 path.join(dartRootPath, "tools", "addlatexhash.dart"), | |
170 tmpSpecPath, | |
171 hashPath, | |
172 listPath | |
173 ]); | |
174 return Process.runSync(dartExecutable, args); | |
175 } | |
176 | |
177 runDvi2tty(dviFile) => | |
178 Process.runSync("dvi2tty", [dviFile], workingDirectory: tmpDirPath); | |
179 | |
180 chkDvi2tty(file, subject) => | |
181 checkAction(runDvi2tty(file), "dvitty on $subject failed"); | |
182 | |
183 // perform test | |
184 var renewLMHashCmd = r"\renewcommand{\LMHash}[1]{\OriginalLMHash{xxxx}}"; | |
185 new File(styPath) | |
186 .copySync(tmpStyPath) | |
187 .writeAsStringSync(renewLMHashCmd, mode: FileMode.APPEND); | |
188 new File(specPath).copySync(tmpSpecPath); | |
189 | |
190 checkAction(runAddHash(), "addlatexhash.dart failed"); | |
191 for (var i = 0; i < 5; i++) { | |
192 checkAction(runLatex(specName, tmpDirPath), "LaTeX on spec failed"); | |
193 } | |
194 for (var i = 0; i < 5; i++) { | |
195 checkAction(runLatex(hashFileName, tmpDirPath), "LaTeX on output failed"); | |
196 } | |
197 if (chkDvi2tty(specDviPath, "spec") != chkDvi2tty(hashDviPath, "output")) { | |
198 throw "dvi2tty spec != dvitty output"; | |
199 } | |
200 } | |
201 | |
202 runWithTempDir(void test(String tempDir)) { | |
203 final tempDir = Directory.systemTemp.createTempSync("addlatexhash_test"); | |
204 try { | |
205 test(tempDir.path); | |
206 } finally { | |
207 tempDir.delete(recursive: true); | |
208 } | |
209 } | |
210 | |
211 main([args]) { | |
212 testCutMatch(); | |
213 testSisp(); | |
214 | |
215 runWithTempDir(testSameHash); | |
216 // latex and dvi2tty are not installed in the standard test environment | |
217 if (args.length > 0 && args[0] == "local") { | |
218 runWithTempDir(testSameDVI); | |
219 } | |
220 } | |
OLD | NEW |