moving things back off the branch, and into full disaster mode

This commit is contained in:
benfry
2011-01-20 19:29:39 +00:00
parent 4922d95e59
commit 5ecf3fdf36
67 changed files with 6032 additions and 3645 deletions

View File

@@ -0,0 +1,64 @@
package test.processing.mode.java;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static test.processing.mode.java.ProcessingTestUtil.res;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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 formattedProgram = ProcessingTestUtil.format(res(id + ".pde"));
final File goldenFile = res(id + ".expected");
checkGolden(formattedProgram, goldenFile);
// check that the formatted text doesn't change
checkGolden(ProcessingTestUtil.format(formattedProgram), goldenFile);
} catch (Exception e) {
if (!e.equals(e.getCause()) && e.getCause() != null)
fail(e.getCause().toString());
else
e.printStackTrace(System.err);
fail(e.toString());
}
}
private static void checkGolden(final String expectedText,
final File goldenFile) throws IOException {
if (goldenFile.exists()) {
final String expected = ProcessingTestUtil.read(goldenFile);
assertEquals(expected, expectedText);
} else {
System.err.println("WARN: golden file " + goldenFile
+ " does not exist. Generating.");
final FileWriter sug = new FileWriter(goldenFile);
sug.write(ProcessingTestUtil.normalize(expectedText));
sug.close();
}
}
@Test
public void bug109() {
expectGood("bug109");
}
@Test
public void bug405() {
expectGood("bug405");
}
@Test
public void bug420() {
expectGood("bug420");
}
}

View File

@@ -0,0 +1,294 @@
package test.processing.mode.java;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static test.processing.mode.java.ProcessingTestUtil.COMPILER;
import static test.processing.mode.java.ProcessingTestUtil.preprocess;
import static test.processing.mode.java.ProcessingTestUtil.res;
import java.io.File;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.BeforeClass;
import org.junit.Test;
import processing.app.SketchException;
import processing.app.exec.ProcessResult;
import antlr.RecognitionException;
public class ParserTests {
@BeforeClass
public static void init() {
ProcessingTestUtil.init();
}
static void expectRecognitionException(final String id,
final String expectedMessage,
final int expectedLine) {
try {
preprocess(id, res(id + ".pde"));
fail("Expected to fail with \"" + expectedMessage + "\" on line "
+ expectedLine);
} catch (RecognitionException e) {
assertEquals(expectedMessage, e.getMessage());
assertEquals(expectedLine, e.getLine());
} catch (Exception e) {
if (!e.equals(e.getCause()) && e.getCause() != null)
fail(e.getCause().toString());
else
fail(e.toString());
}
}
static void expectRunnerException(final String id,
final String expectedMessage,
final int expectedLine) {
try {
preprocess(id, res(id + ".pde"));
fail("Expected to fail with \"" + expectedMessage + "\" on line "
+ expectedLine);
} catch (SketchException e) {
assertEquals(expectedMessage, e.getMessage());
assertEquals(expectedLine, e.getCodeLine());
} catch (Exception e) {
if (!e.equals(e.getCause()) && e.getCause() != null)
fail(e.getCause().toString());
else
fail(e.toString());
}
}
static void expectCompilerException(final String id,
final String expectedMessage,
final int expectedLine) {
try {
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 "
+ expectedLine);
}
final String e = compilerResult.getStderr().split("\n")[0];
final Matcher m = Pattern.compile(":(\\d+):\\s+(.+)$").matcher(e);
m.find();
assertEquals(expectedMessage, m.group(2));
assertEquals(String.valueOf(expectedLine), m.group(1));
} catch (Exception e) {
if (!e.equals(e.getCause()) && e.getCause() != null)
fail(e.getCause().toString());
else
fail(e.toString());
}
}
static void expectGood(final String id) {
try {
final String program = ProcessingTestUtil
.preprocess(id, res(id + ".pde"));
final ProcessResult compilerResult = COMPILER.compile(id, program);
if (!compilerResult.succeeded()) {
System.err.println(program);
System.err.println("----------------------------");
System.err.println(compilerResult.getStderr());
fail("Compilation failed with status " + compilerResult.getResult());
}
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
fail(e.toString());
}
}
@Test
public void bug4() {
expectGood("bug4");
}
@Test
public void bug5a() {
expectGood("bug5a");
}
@Test
public void bug5b() {
expectGood("bug5b");
}
@Test
public void bug6() {
expectRecognitionException("bug6", "expecting EOF, found '/'", 1);
}
@Test
public void bug16() {
expectRunnerException("bug16", "Unclosed /* comment */", 2);
}
@Test
public void bug136() {
expectGood("bug136");
}
@Test
public void bug196() {
expectRecognitionException("bug196",
"Web colors must be exactly 6 hex digits. This looks like 5.", 4);
}
@Test
public void bug281() {
expectGood("bug281");
}
@Test
public void bug481() {
expectGood("bug481");
}
@Test
public void bug507() {
expectRecognitionException("bug507", "expecting EOF, found 'else'", 5);
}
@Test
public void bug598() {
expectGood("bug598");
}
@Test
public void bug631() {
expectGood("bug631");
}
@Test
public void bug763() {
expectRunnerException("bug763", "Unterminated string constant", 6);
}
@Test
public void bug820() {
expectCompilerException("bug820", "x1 is already defined in setup()", 21);
}
@Test
public void bug1064() {
expectGood("bug1064");
}
@Test
public void bug1145() {
expectCompilerException("bug1145", "'.' expected", 4);
}
@Test
public void bug1362() {
expectGood("bug1362");
}
@Test
public void bug1442() {
expectGood("bug1442");
}
@Test
public void bug1511() {
expectGood("bug1511");
}
@Test
public void bug1512() {
expectGood("bug1512");
}
@Test
public void bug1514a() {
expectGood("bug1514a");
}
@Test
public void bug1514b() {
expectGood("bug1514b");
}
@Test
public void bug1515() {
expectGood("bug1515");
}
@Test
public void bug1516() {
expectGood("bug1516");
}
@Test
public void bug1517() {
expectGood("bug1517");
}
@Test
public void bug1518a() {
expectGood("bug1518a");
}
@Test
public void bug1518b() {
expectGood("bug1518b");
}
@Test
public void bug1519() {
expectRecognitionException("bug1519", "Maybe too many > characters?", 7);
}
@Test
public void bug1525() {
expectGood("bug1525");
}
@Test
public void bug1532() {
expectRecognitionException("bug1532", "unexpected token: break", 50);
}
@Test
public void bug1534() {
expectGood("bug1534");
}
@Test
public void bug315g() {
expectGood("bug315g");
}
@Test
public void bug400g() {
expectGood("bug400g");
}
@Test
public void bug427g() {
expectGood("bug427g");
}
@Test
public void annotations() {
expectGood("annotations");
}
}

View File

@@ -0,0 +1,78 @@
package test.processing.mode.java;
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.SketchException;
import processing.mode.java.preproc.PdePreprocessor;
import processing.mode.java.AutoFormat;
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 SketchException, 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 format(read(resource));
}
static String format(final String programText) {
return normalize(new AutoFormat().format(programText));
}
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);
}
}
}

View File

@@ -0,0 +1,71 @@
package test.processing.mode.java;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import processing.app.Base;
import processing.app.Platform;
import processing.app.exec.ProcessHelper;
import processing.app.exec.ProcessResult;
/**
* Utility class for compiling single compilationUnits.
*
* @author Jonathan Feinberg &lt;jdf@pobox.com&gt;
*
*/
class UTCompiler {
private final String classpath;
UTCompiler(File... classpath) throws IOException {
final StringBuilder sb = new StringBuilder();
for (final File f : classpath) {
if (sb.length() > 0)
sb.append(File.pathSeparatorChar);
sb.append(f.getAbsolutePath());
}
this.classpath = sb.toString();
final Platform p = Base.getPlatform();
final String javaHomeProp = System.getProperty("java.home");
if (javaHomeProp == null) {
throw new RuntimeException(
"I don't know how to deal with a null java.home proprty, to be quite frank.");
}
final File javaHome = new File(javaHomeProp).getCanonicalFile();
p.setenv("JAVA_HOME", javaHome.getCanonicalPath());
final String path = new File(javaHome, "bin").getCanonicalPath()
+ File.pathSeparator + p.getenv("PATH");
p.setenv("PATH", path);
}
ProcessResult compile(final String name, final String program)
throws IOException {
final File tmpdir = File.createTempFile("utcompiler", ".tmp");
if (!tmpdir.delete())
throw new IOException("Cannot delete " + tmpdir);
if (!tmpdir.mkdir())
throw new IOException("Cannot create " + tmpdir);
final File javaFile = new File(tmpdir, name + ".java");
final FileWriter java = new FileWriter(javaFile);
try {
java.write(program);
} finally {
java.close();
}
try {
return new ProcessHelper("javac", "-sourcepath", tmpdir.getAbsolutePath(), "-cp",
classpath, "-nowarn", "-d", tmpdir.getAbsolutePath(), javaFile.getAbsolutePath()).execute();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
for (final File f: tmpdir.listFiles())
if (!f.getName().startsWith("."))if (!f.delete())
throw new IOException("Can't delete " + f);
if (!tmpdir.delete())
throw new IOException("Can't delete " + tmpdir);
}
}
}