diff --git a/app/src/processing/app/format/AutoFormat.java b/app/src/processing/app/format/AutoFormat.java index 1e03ae63b..ea8d11ab2 100644 --- a/app/src/processing/app/format/AutoFormat.java +++ b/app/src/processing/app/format/AutoFormat.java @@ -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(); diff --git a/app/test/resources/bug109.expected b/app/test/resources/bug109.expected new file mode 100644 index 000000000..c2aeb68c2 --- /dev/null +++ b/app/test/resources/bug109.expected @@ -0,0 +1,5 @@ +class Bug { + Bug() { + w = random(size)/3)+10; + } +} diff --git a/app/test/resources/bug109.pde b/app/test/resources/bug109.pde new file mode 100644 index 000000000..c34e64cc6 --- /dev/null +++ b/app/test/resources/bug109.pde @@ -0,0 +1,4 @@ +class Bug{ +Bug(){ +w = random(size)/3)+10; +}} \ No newline at end of file diff --git a/app/test/resources/preferences.txt b/app/test/resources/preferences.txt index a2f84a4bc..f80996856 100644 --- a/app/test/resources/preferences.txt +++ b/app/test/resources/preferences.txt @@ -1,5 +1,7 @@ # unit test prefs +editor.tabs.size=2 + preproc.save_build_files=false # preprocessor: pde.g diff --git a/app/test/src/test/processing/parsing/AutoFormatTests.java b/app/test/src/test/processing/parsing/AutoFormatTests.java new file mode 100644 index 000000000..a210dc64d --- /dev/null +++ b/app/test/src/test/processing/parsing/AutoFormatTests.java @@ -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"); + } +} diff --git a/app/test/src/test/processing/parsing/ParserTests.java b/app/test/src/test/processing/parsing/ParserTests.java index b224ffa64..9a003d1f8 100644 --- a/app/test/src/test/processing/parsing/ParserTests.java +++ b/app/test/src/test/processing/parsing/ParserTests.java @@ -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(); } diff --git a/app/test/src/test/processing/parsing/ProcessingTestUtil.java b/app/test/src/test/processing/parsing/ProcessingTestUtil.java new file mode 100644 index 000000000..a6bae7bc3 --- /dev/null +++ b/app/test/src/test/processing/parsing/ProcessingTestUtil.java @@ -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); + } + } + +}