Skip to content

Commit 26d07e0

Browse files
author
jdf
committed
First ever unit test for AutoFormat!
1 parent 6ece11c commit 26d07e0

7 files changed

Lines changed: 145 additions & 60 deletions

File tree

app/src/processing/app/format/AutoFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public String format(final String source) {
314314
chars = cleanText.toCharArray();
315315
lineNumber = 1;
316316

317-
EOF = false; // set in getchr when EOF
317+
EOF = false; // set in next() when EOF
318318

319319
while (!EOF) {
320320
c = next();

app/test/resources/bug109.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Bug {
2+
Bug() {
3+
w = random(size)/3)+10;
4+
}
5+
}

app/test/resources/bug109.pde

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Bug{
2+
Bug(){
3+
w = random(size)/3)+10;
4+
}}

app/test/resources/preferences.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# unit test prefs
22

3+
editor.tabs.size=2
4+
35
preproc.save_build_files=false
46

57
# preprocessor: pde.g
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package test.processing.parsing;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
import static test.processing.parsing.ProcessingTestUtil.res;
6+
import java.io.File;
7+
import java.io.FileWriter;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
11+
public class AutoFormatTests {
12+
13+
@BeforeClass
14+
public static void init() {
15+
ProcessingTestUtil.init();
16+
}
17+
18+
static void expectGood(final String id) {
19+
try {
20+
final String program = ProcessingTestUtil.format(res(id + ".pde"));
21+
final File expectedFile = res(id + ".expected");
22+
if (expectedFile.exists()) {
23+
final String expected = ProcessingTestUtil.read(expectedFile);
24+
assertEquals(expected, program);
25+
} else {
26+
System.err.println("WARN: " + id
27+
+ " does not have an expected output file. Generating.");
28+
final FileWriter sug = new FileWriter(res(id + ".expected"));
29+
sug.write(ProcessingTestUtil.normalize(program));
30+
sug.close();
31+
}
32+
} catch (Exception e) {
33+
if (!e.equals(e.getCause()) && e.getCause() != null)
34+
fail(e.getCause().toString());
35+
else
36+
e.printStackTrace(System.err);
37+
fail(e.toString());
38+
}
39+
}
40+
41+
@Test
42+
public void bug109() {
43+
expectGood("bug109");
44+
}
45+
}

app/test/src/test/processing/parsing/ParserTests.java

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,25 @@
11
package test.processing.parsing;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
45
import java.io.File;
5-
import java.io.FileInputStream;
66
import java.io.FileWriter;
7-
import java.io.IOException;
8-
import java.io.InputStreamReader;
9-
import java.io.StringWriter;
107
import java.util.regex.Matcher;
118
import java.util.regex.Pattern;
129
import org.junit.BeforeClass;
1310
import org.junit.Test;
14-
import processing.app.Base;
15-
import processing.app.Preferences;
1611
import processing.app.debug.RunnerException;
17-
import processing.app.preproc.PdePreprocessor;
1812
import processing.util.exec.ProcessResult;
19-
import antlr.ANTLRException;
2013
import antlr.RecognitionException;
14+
import static test.processing.parsing.ProcessingTestUtil.res;
15+
import static test.processing.parsing.ProcessingTestUtil.COMPILER;
16+
import static test.processing.parsing.ProcessingTestUtil.preprocess;
2117

2218
public class ParserTests {
23-
24-
private static final String RESOURCES = "test/resources/";
25-
private static final UTCompiler COMPILER;
26-
static {
27-
try {
28-
Base.initPlatform();
29-
COMPILER = new UTCompiler(new File("bin"), new File("../core/bin"));
30-
} catch (IOException e) {
31-
throw new RuntimeException(e);
32-
}
33-
}
34-
35-
private static File res(final String resourceName) {
36-
return new File(RESOURCES, resourceName);
37-
}
38-
19+
3920
@BeforeClass
40-
static public void initPrefs() throws Exception {
41-
Preferences.load(new FileInputStream(res("preferences.txt")));
42-
}
43-
44-
static String read(final File f) {
45-
try {
46-
final FileInputStream fin = new FileInputStream(f);
47-
final InputStreamReader in = new InputStreamReader(fin, "UTF-8");
48-
try {
49-
final StringBuilder sb = new StringBuilder();
50-
final char[] buf = new char[1 << 12];
51-
int len;
52-
while ((len = in.read(buf)) != -1)
53-
sb.append(buf, 0, len);
54-
return sb.toString().replace("\r", "");
55-
} finally {
56-
in.close();
57-
}
58-
} catch (Exception e) {
59-
throw new RuntimeException("Unexpected", e);
60-
}
61-
}
62-
63-
static String preprocess(final String name, final File resource)
64-
throws RunnerException, ANTLRException {
65-
final String program = read(resource);
66-
final StringWriter out = new StringWriter();
67-
new PdePreprocessor(name, 4).write(out, program);
68-
return out.toString().replace("\r", "");
21+
public static void init() {
22+
ProcessingTestUtil.init();
6923
}
7024

7125
static void expectRecognitionException(final String id,
@@ -108,7 +62,8 @@ static void expectCompilerException(final String id,
10862
final String expectedMessage,
10963
final int expectedLine) {
11064
try {
111-
final String program = preprocess(id, res(id + ".pde"));
65+
final String program = ProcessingTestUtil
66+
.preprocess(id, res(id + ".pde"));
11267
final ProcessResult compilerResult = COMPILER.compile(id, program);
11368
if (compilerResult.succeeded()) {
11469
fail("Expected to fail with \"" + expectedMessage + "\" on line "
@@ -129,8 +84,8 @@ static void expectCompilerException(final String id,
12984

13085
static void expectGood(final String id) {
13186
try {
132-
final String program = preprocess(id, res(id + ".pde"));
133-
87+
final String program = ProcessingTestUtil
88+
.preprocess(id, res(id + ".pde"));
13489
final ProcessResult compilerResult = COMPILER.compile(id, program);
13590
if (!compilerResult.succeeded()) {
13691
System.err.println(program);
@@ -141,13 +96,13 @@ static void expectGood(final String id) {
14196

14297
final File expectedFile = res(id + ".expected");
14398
if (expectedFile.exists()) {
144-
final String expected = read(expectedFile);
99+
final String expected = ProcessingTestUtil.read(expectedFile);
145100
assertEquals(expected, program);
146101
} else {
147102
System.err.println("WARN: " + id
148103
+ " does not have an expected output file. Generating.");
149104
final FileWriter sug = new FileWriter(res(id + ".expected"));
150-
sug.write(program.replace("\r", ""));
105+
sug.write(ProcessingTestUtil.normalize(program));
151106
sug.close();
152107
}
153108

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package test.processing.parsing;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.io.StringWriter;
8+
import antlr.ANTLRException;
9+
import processing.app.Base;
10+
import processing.app.Preferences;
11+
import processing.app.debug.RunnerException;
12+
import processing.app.format.AutoFormat;
13+
import processing.app.preproc.PdePreprocessor;
14+
15+
public class ProcessingTestUtil {
16+
static void init() {
17+
// noop; just causes class to be loaded
18+
}
19+
20+
private static final String RESOURCES = "test/resources/";
21+
static final UTCompiler COMPILER;
22+
23+
static {
24+
try {
25+
Base.initPlatform();
26+
COMPILER = new UTCompiler(new File("bin"), new File("../core/bin"));
27+
Preferences.load(new FileInputStream(res("preferences.txt")));
28+
} catch (IOException e) {
29+
throw new RuntimeException(e);
30+
}
31+
System.err.println("ProcessingTestUtil initialized.");
32+
}
33+
34+
static String normalize(final Object s) {
35+
return String.valueOf(s).replace("\r", "");
36+
}
37+
38+
static String preprocess(final String name, final File resource)
39+
throws RunnerException, ANTLRException {
40+
final String program = read(resource);
41+
final StringWriter out = new StringWriter();
42+
new PdePreprocessor(name, 4).write(out, program);
43+
return normalize(out);
44+
}
45+
46+
static String format(final File resource)
47+
{
48+
return normalize(new AutoFormat().format(read(resource)));
49+
}
50+
51+
static File res(final String resourceName) {
52+
return new File(RESOURCES, resourceName);
53+
}
54+
55+
static String read(final File f) {
56+
try {
57+
final FileInputStream fin = new FileInputStream(f);
58+
final InputStreamReader in = new InputStreamReader(fin, "UTF-8");
59+
try {
60+
final StringBuilder sb = new StringBuilder();
61+
final char[] buf = new char[1 << 12];
62+
int len;
63+
while ((len = in.read(buf)) != -1)
64+
sb.append(buf, 0, len);
65+
return normalize(sb);
66+
} finally {
67+
in.close();
68+
}
69+
} catch (Exception e) {
70+
throw new RuntimeException("Unexpected", e);
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)