mirror of
https://github.com/processing/processing4.git
synced 2026-02-03 05:39:18 +01:00
First ever unit test for AutoFormat!
This commit is contained in:
@@ -314,7 +314,7 @@ public class AutoFormat {
|
||||
chars = cleanText.toCharArray();
|
||||
lineNumber = 1;
|
||||
|
||||
EOF = false; // set in getchr when EOF
|
||||
EOF = false; // set in next() when EOF
|
||||
|
||||
while (!EOF) {
|
||||
c = next();
|
||||
|
||||
5
app/test/resources/bug109.expected
Normal file
5
app/test/resources/bug109.expected
Normal file
@@ -0,0 +1,5 @@
|
||||
class Bug {
|
||||
Bug() {
|
||||
w = random(size)/3)+10;
|
||||
}
|
||||
}
|
||||
4
app/test/resources/bug109.pde
Normal file
4
app/test/resources/bug109.pde
Normal file
@@ -0,0 +1,4 @@
|
||||
class Bug{
|
||||
Bug(){
|
||||
w = random(size)/3)+10;
|
||||
}}
|
||||
@@ -1,5 +1,7 @@
|
||||
# unit test prefs
|
||||
|
||||
editor.tabs.size=2
|
||||
|
||||
preproc.save_build_files=false
|
||||
|
||||
# preprocessor: pde.g
|
||||
|
||||
45
app/test/src/test/processing/parsing/AutoFormatTests.java
Normal file
45
app/test/src/test/processing/parsing/AutoFormatTests.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package test.processing.parsing;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static test.processing.parsing.ProcessingTestUtil.res;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AutoFormatTests {
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
ProcessingTestUtil.init();
|
||||
}
|
||||
|
||||
static void expectGood(final String id) {
|
||||
try {
|
||||
final String program = ProcessingTestUtil.format(res(id + ".pde"));
|
||||
final File expectedFile = res(id + ".expected");
|
||||
if (expectedFile.exists()) {
|
||||
final String expected = ProcessingTestUtil.read(expectedFile);
|
||||
assertEquals(expected, program);
|
||||
} else {
|
||||
System.err.println("WARN: " + id
|
||||
+ " does not have an expected output file. Generating.");
|
||||
final FileWriter sug = new FileWriter(res(id + ".expected"));
|
||||
sug.write(ProcessingTestUtil.normalize(program));
|
||||
sug.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!e.equals(e.getCause()) && e.getCause() != null)
|
||||
fail(e.getCause().toString());
|
||||
else
|
||||
e.printStackTrace(System.err);
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bug109() {
|
||||
expectGood("bug109");
|
||||
}
|
||||
}
|
||||
@@ -1,71 +1,25 @@
|
||||
package test.processing.parsing;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import processing.app.Base;
|
||||
import processing.app.Preferences;
|
||||
import processing.app.debug.RunnerException;
|
||||
import processing.app.preproc.PdePreprocessor;
|
||||
import processing.util.exec.ProcessResult;
|
||||
import antlr.ANTLRException;
|
||||
import antlr.RecognitionException;
|
||||
import static test.processing.parsing.ProcessingTestUtil.res;
|
||||
import static test.processing.parsing.ProcessingTestUtil.COMPILER;
|
||||
import static test.processing.parsing.ProcessingTestUtil.preprocess;
|
||||
|
||||
public class ParserTests {
|
||||
|
||||
private static final String RESOURCES = "test/resources/";
|
||||
private static final UTCompiler COMPILER;
|
||||
static {
|
||||
try {
|
||||
Base.initPlatform();
|
||||
COMPILER = new UTCompiler(new File("bin"), new File("../core/bin"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static File res(final String resourceName) {
|
||||
return new File(RESOURCES, resourceName);
|
||||
}
|
||||
|
||||
|
||||
@BeforeClass
|
||||
static public void initPrefs() throws Exception {
|
||||
Preferences.load(new FileInputStream(res("preferences.txt")));
|
||||
}
|
||||
|
||||
static String read(final File f) {
|
||||
try {
|
||||
final FileInputStream fin = new FileInputStream(f);
|
||||
final InputStreamReader in = new InputStreamReader(fin, "UTF-8");
|
||||
try {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
final char[] buf = new char[1 << 12];
|
||||
int len;
|
||||
while ((len = in.read(buf)) != -1)
|
||||
sb.append(buf, 0, len);
|
||||
return sb.toString().replace("\r", "");
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unexpected", e);
|
||||
}
|
||||
}
|
||||
|
||||
static String preprocess(final String name, final File resource)
|
||||
throws RunnerException, ANTLRException {
|
||||
final String program = read(resource);
|
||||
final StringWriter out = new StringWriter();
|
||||
new PdePreprocessor(name, 4).write(out, program);
|
||||
return out.toString().replace("\r", "");
|
||||
public static void init() {
|
||||
ProcessingTestUtil.init();
|
||||
}
|
||||
|
||||
static void expectRecognitionException(final String id,
|
||||
@@ -108,7 +62,8 @@ public class ParserTests {
|
||||
final String expectedMessage,
|
||||
final int expectedLine) {
|
||||
try {
|
||||
final String program = preprocess(id, res(id + ".pde"));
|
||||
final String program = ProcessingTestUtil
|
||||
.preprocess(id, res(id + ".pde"));
|
||||
final ProcessResult compilerResult = COMPILER.compile(id, program);
|
||||
if (compilerResult.succeeded()) {
|
||||
fail("Expected to fail with \"" + expectedMessage + "\" on line "
|
||||
@@ -129,8 +84,8 @@ public class ParserTests {
|
||||
|
||||
static void expectGood(final String id) {
|
||||
try {
|
||||
final String program = preprocess(id, res(id + ".pde"));
|
||||
|
||||
final String program = ProcessingTestUtil
|
||||
.preprocess(id, res(id + ".pde"));
|
||||
final ProcessResult compilerResult = COMPILER.compile(id, program);
|
||||
if (!compilerResult.succeeded()) {
|
||||
System.err.println(program);
|
||||
@@ -141,13 +96,13 @@ public class ParserTests {
|
||||
|
||||
final File expectedFile = res(id + ".expected");
|
||||
if (expectedFile.exists()) {
|
||||
final String expected = read(expectedFile);
|
||||
final String expected = ProcessingTestUtil.read(expectedFile);
|
||||
assertEquals(expected, program);
|
||||
} else {
|
||||
System.err.println("WARN: " + id
|
||||
+ " does not have an expected output file. Generating.");
|
||||
final FileWriter sug = new FileWriter(res(id + ".expected"));
|
||||
sug.write(program.replace("\r", ""));
|
||||
sug.write(ProcessingTestUtil.normalize(program));
|
||||
sug.close();
|
||||
}
|
||||
|
||||
|
||||
74
app/test/src/test/processing/parsing/ProcessingTestUtil.java
Normal file
74
app/test/src/test/processing/parsing/ProcessingTestUtil.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package test.processing.parsing;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import antlr.ANTLRException;
|
||||
import processing.app.Base;
|
||||
import processing.app.Preferences;
|
||||
import processing.app.debug.RunnerException;
|
||||
import processing.app.format.AutoFormat;
|
||||
import processing.app.preproc.PdePreprocessor;
|
||||
|
||||
public class ProcessingTestUtil {
|
||||
static void init() {
|
||||
// noop; just causes class to be loaded
|
||||
}
|
||||
|
||||
private static final String RESOURCES = "test/resources/";
|
||||
static final UTCompiler COMPILER;
|
||||
|
||||
static {
|
||||
try {
|
||||
Base.initPlatform();
|
||||
COMPILER = new UTCompiler(new File("bin"), new File("../core/bin"));
|
||||
Preferences.load(new FileInputStream(res("preferences.txt")));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.err.println("ProcessingTestUtil initialized.");
|
||||
}
|
||||
|
||||
static String normalize(final Object s) {
|
||||
return String.valueOf(s).replace("\r", "");
|
||||
}
|
||||
|
||||
static String preprocess(final String name, final File resource)
|
||||
throws RunnerException, ANTLRException {
|
||||
final String program = read(resource);
|
||||
final StringWriter out = new StringWriter();
|
||||
new PdePreprocessor(name, 4).write(out, program);
|
||||
return normalize(out);
|
||||
}
|
||||
|
||||
static String format(final File resource)
|
||||
{
|
||||
return normalize(new AutoFormat().format(read(resource)));
|
||||
}
|
||||
|
||||
static File res(final String resourceName) {
|
||||
return new File(RESOURCES, resourceName);
|
||||
}
|
||||
|
||||
static String read(final File f) {
|
||||
try {
|
||||
final FileInputStream fin = new FileInputStream(f);
|
||||
final InputStreamReader in = new InputStreamReader(fin, "UTF-8");
|
||||
try {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
final char[] buf = new char[1 << 12];
|
||||
int len;
|
||||
while ((len = in.read(buf)) != -1)
|
||||
sb.append(buf, 0, len);
|
||||
return normalize(sb);
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unexpected", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user