Skip to content

Commit 41fab70

Browse files
author
jdf
committed
Several changes to make unit testing preprocessor easy.
First unit tests in... and they caught a regression!
1 parent ef1b50e commit 41fab70

File tree

7 files changed

+97
-32
lines changed

7 files changed

+97
-32
lines changed

android/tool/src/processing/app/tools/android/Build.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public Preproc(final String sketchName) throws IOException {
330330
}
331331

332332
@Override
333-
protected int writeImports(final PrintStream out,
333+
protected int writeImports(final PrintWriter out,
334334
final List<String> programImports,
335335
final List<String> codeFolderImports) {
336336
out.println("package " + getPackageName() + ";");

app/src/processing/app/Base.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static protected boolean isCommandLine() {
199199
}
200200

201201

202-
static protected void initPlatform() {
202+
static public void initPlatform() {
203203
try {
204204
Class<?> platformClass = Class.forName("processing.app.Platform");
205205
if (Base.isMacOS()) {

app/src/processing/app/Preferences.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public class Preferences {
134134
static File preferencesFile;
135135

136136

137-
static protected void init(String commandLinePrefs) {
137+
static public void init(String commandLinePrefs) {
138138

139139
// start by loading the defaults, in case something
140140
// important was deleted from the user prefs

app/src/processing/app/Sketch.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,11 +1202,7 @@ public void prepare() {
12021202
* @return null if compilation failed, main class name if not
12031203
*/
12041204
public String preprocess(String buildPath) throws RunnerException {
1205-
try {
1206-
return preprocess(buildPath, new PdePreprocessor(name));
1207-
} catch (IOException e) {
1208-
throw new RunnerException("Error while preprocessing", true);
1209-
}
1205+
return preprocess(buildPath, new PdePreprocessor(name));
12101206
}
12111207

12121208

@@ -1253,7 +1249,7 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
12531249
final PreprocessResult result;
12541250
try {
12551251
final File java = new File(buildPath, name + ".java");
1256-
final PrintStream stream = new PrintStream(new FileOutputStream(java));
1252+
final PrintWriter stream = new PrintWriter(new FileWriter(java));
12571253
try {
12581254
result = preprocessor.write(stream, bigCode.toString(),
12591255
codeFolderPackages);

app/src/processing/app/preproc/PdeEmitter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package processing.app.preproc;
44

55
import java.io.PrintStream;
6+
import java.io.PrintWriter;
67
import java.util.BitSet;
78
import java.util.Stack;
89
import processing.app.Preferences;
@@ -31,13 +32,13 @@
3132
@SuppressWarnings("unused")
3233
public class PdeEmitter implements PdeTokenTypes {
3334
private final PdePreprocessor pdePreprocessor;
34-
private final PrintStream out;
35+
private final PrintWriter out;
3536
private final PrintStream debug = System.err;
3637

3738
private final Stack stack = new Stack();
3839
private final static int ROOT_ID = 0;
3940

40-
public PdeEmitter(final PdePreprocessor pdePreprocessor, final PrintStream out) {
41+
public PdeEmitter(final PdePreprocessor pdePreprocessor, final PrintWriter out) {
4142
this.pdePreprocessor = pdePreprocessor;
4243
this.out = out;
4344
}

app/src/processing/app/preproc/PdePreprocessor.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727

2828
package processing.app.preproc;
2929

30-
import java.io.ByteArrayOutputStream;
31-
import java.io.FileNotFoundException;
3230
import java.io.FileOutputStream;
3331
import java.io.IOException;
3432
import java.io.OutputStreamWriter;
3533
import java.io.PrintStream;
34+
import java.io.PrintWriter;
3635
import java.io.StringReader;
36+
import java.io.StringWriter;
37+
import java.io.Writer;
3738
import java.util.ArrayList;
3839
import java.util.Arrays;
3940
import java.util.List;
@@ -43,6 +44,7 @@
4344
import processing.app.antlr.PdeTokenTypes;
4445
import processing.app.debug.RunnerException;
4546
import processing.core.PApplet;
47+
import antlr.ANTLRException;
4648
import antlr.ASTFactory;
4749
import antlr.CommonAST;
4850
import antlr.CommonASTWithHiddenTokens;
@@ -179,11 +181,13 @@ public void setProgramType(final ProgramType programType) {
179181
this.programType = programType;
180182
}
181183

182-
public PdePreprocessor(final String sketchName) throws IOException {
183-
this.name = sketchName;
184+
public PdePreprocessor(final String sketchName) {
185+
this(sketchName, Preferences.getInteger("editor.tabs.size"));
186+
}
184187

185-
final char[] indentChars = new char[Preferences
186-
.getInteger("editor.tabs.size")];
188+
public PdePreprocessor(final String sketchName, final int tabSize) {
189+
this.name = sketchName;
190+
final char[] indentChars = new char[tabSize];
187191
Arrays.fill(indentChars, ' ');
188192
indent = new String(indentChars);
189193
}
@@ -261,9 +265,14 @@ private static void checkForUnterminatedMultilineComment(final String program)
261265
}
262266
}
263267

264-
public PreprocessResult write(final PrintStream stream, String program,
268+
public PreprocessResult write(final Writer out, String program)
269+
throws RunnerException, ANTLRException {
270+
return write(out, program, null);
271+
}
272+
273+
public PreprocessResult write(final Writer out, String program,
265274
final String codeFolderPackages[])
266-
throws FileNotFoundException, RunnerException, Exception {
275+
throws RunnerException, ANTLRException {
267276

268277
// these ones have the .* at the end, since a class name might be at the end
269278
// instead of .* which would make trouble other classes using this can lop
@@ -277,6 +286,10 @@ public PreprocessResult write(final PrintStream stream, String program,
277286
// need to reset whether or not this has a main()
278287
foundMain = false;
279288

289+
// bug #5
290+
if (!program.endsWith("\n"))
291+
program += "\n";
292+
280293
checkForUnterminatedMultilineComment(program);
281294

282295
if (Preferences.getBoolean("preproc.substitute_unicode")) {
@@ -309,6 +322,7 @@ public PreprocessResult write(final PrintStream stream, String program,
309322
}
310323
}
311324

325+
final PrintWriter stream = new PrintWriter(out);
312326
final int headerOffset = writeImports(stream, programImports,
313327
codeFolderImports);
314328
return new PreprocessResult(headerOffset + 2, write(program, stream),
@@ -356,8 +370,8 @@ static String substituteUnicode(String program) {
356370
* preprocesses a pde file and writes out a java file
357371
* @return the class name of the exported Java
358372
*/
359-
private String write(final String program, final PrintStream stream)
360-
throws java.lang.Exception {
373+
private String write(final String program, final PrintWriter stream)
374+
throws RunnerException, ANTLRException {
361375
// create a lexer with the stream reader, and tell it to handle
362376
// hidden tokens (eg whitespace, comments) since we want to pass these
363377
// through so that the line numbers when the compiler reports errors
@@ -438,13 +452,13 @@ private String write(final String program, final PrintStream stream)
438452

439453
// debug
440454
if (false) {
441-
final ByteArrayOutputStream buf = new ByteArrayOutputStream();
442-
final PrintStream bufout = new PrintStream(buf);
455+
final StringWriter buf = new StringWriter();
456+
final PrintWriter bufout = new PrintWriter(buf);
443457
writeDeclaration(bufout, className);
444458
new PdeEmitter(this, bufout).print(rootNode);
445459
writeFooter(bufout, className);
446460
debugAST(rootNode, true);
447-
System.err.println(new String(buf.toByteArray()));
461+
System.err.println(buf.toString());
448462
}
449463

450464
writeDeclaration(stream, className);
@@ -520,7 +534,7 @@ protected void writeParseTree(String filename, AST ast) {
520534
* @param codeFolderImports
521535
* @return the header offset
522536
*/
523-
protected int writeImports(final PrintStream out,
537+
protected int writeImports(final PrintWriter out,
524538
final List<String> programImports,
525539
final List<String> codeFolderImports) {
526540
int count = writeImportList(out, getCoreImports());
@@ -530,11 +544,11 @@ protected int writeImports(final PrintStream out,
530544
return count;
531545
}
532546

533-
protected int writeImportList(PrintStream out, List<String> imports) {
547+
protected int writeImportList(PrintWriter out, List<String> imports) {
534548
return writeImportList(out, (String[]) imports.toArray(new String[0]));
535549
}
536550

537-
protected int writeImportList(PrintStream out, String[] imports) {
551+
protected int writeImportList(PrintWriter out, String[] imports) {
538552
int count = 0;
539553
if (imports != null && imports.length != 0) {
540554
for (String item : imports) {
@@ -554,7 +568,7 @@ protected int writeImportList(PrintStream out, String[] imports) {
554568
* @param exporting Is this being exported from PDE?
555569
* @param className Name of the class being created.
556570
*/
557-
protected void writeDeclaration(PrintStream out, String className) {
571+
protected void writeDeclaration(PrintWriter out, String className) {
558572
if (programType == ProgramType.JAVA) {
559573
// Print two blank lines so that the offset doesn't change
560574
out.println();
@@ -576,7 +590,7 @@ protected void writeDeclaration(PrintStream out, String className) {
576590
*
577591
* @param out PrintStream to write it to.
578592
*/
579-
protected void writeFooter(PrintStream out, String className) {
593+
protected void writeFooter(PrintWriter out, String className) {
580594
if (programType == ProgramType.STATIC) {
581595
// close off draw() definition
582596
out.println(indent + "noLoop();");
Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,69 @@
11
package test.processing.parsing;
22

3+
import static org.junit.Assert.fail;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.InputStreamReader;
7+
import java.io.StringWriter;
8+
import org.junit.BeforeClass;
39
import org.junit.Test;
10+
import processing.app.Base;
11+
import processing.app.Preferences;
12+
import processing.app.debug.RunnerException;
13+
import processing.app.preproc.PdePreprocessor;
14+
import antlr.ANTLRException;
415

516
public class ParserTests {
617

7-
static String preprocess(final String resource) {
8-
return null;
18+
@BeforeClass
19+
static public void initPrefs() {
20+
System.err.println("Initializing prefs");
21+
Base.initPlatform();
22+
Preferences.init(null);
23+
}
24+
25+
static String read(final String path) {
26+
try {
27+
final FileInputStream fin = new FileInputStream(path);
28+
final InputStreamReader in = new InputStreamReader(fin, "UTF-8");
29+
try {
30+
final StringBuilder sb = new StringBuilder();
31+
final char[] buf = new char[1 << 12];
32+
int len;
33+
while ((len = in.read(buf)) != -1)
34+
sb.append(buf, 0, len);
35+
return sb.toString();
36+
} finally {
37+
in.close();
38+
}
39+
} catch (Exception e) {
40+
throw new RuntimeException("Unexpected", e);
41+
}
42+
}
43+
44+
static String preprocess(final String resource) throws RunnerException,
45+
ANTLRException {
46+
final String program = read("../../../app/test/resources/" + resource);
47+
final StringWriter out = new StringWriter();
48+
new PdePreprocessor(resource, 4).write(out, program);
49+
return out.toString();
50+
}
51+
52+
static void expectGood(final String resource) {
53+
try {
54+
preprocess(resource);
55+
} catch (Exception e) {
56+
fail(e.getMessage());
57+
}
958
}
1059

1160
@Test
12-
public void bug5() {
61+
public void bug5a() {
62+
expectGood("bug5.a.pde");
63+
}
1364

65+
@Test
66+
public void bug5b() {
67+
expectGood("bug5.b.pde");
1468
}
1569
}

0 commit comments

Comments
 (0)