jdf
2010-12-01 03:33:22 +00:00
parent 89ebad4372
commit 4f06e64f52
3 changed files with 41 additions and 14 deletions
@@ -217,6 +217,20 @@ public class AutoFormat {
return lastNonWhitespace;
}
private void advanceToNonSpace() {
if (EOF) {
return;
}
do {
pos++;
} while (pos < chars.length && chars[pos] == ' ');
if (pos == chars.length - 1) {
EOF = true;
} else {
pos--; // reset for next()
}
}
private char next() {
if (EOF) {
return 0;
@@ -328,6 +342,7 @@ public class AutoFormat {
trimRight(buf);
buf.append(c);
buf.append(' ');
advanceToNonSpace();
break;
case ' ':
@@ -5,6 +5,7 @@ import static org.junit.Assert.fail;
import static test.processing.parsing.ProcessingTestUtil.res;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -17,18 +18,11 @@ public class AutoFormatTests {
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();
}
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());
@@ -38,11 +32,25 @@ public class AutoFormatTests {
}
}
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 bug420() {
expectGood("bug420");
@@ -45,7 +45,11 @@ public class ProcessingTestUtil {
static String format(final File resource)
{
return normalize(new AutoFormat().format(read(resource)));
return format(read(resource));
}
static String format(final String programText) {
return normalize(new AutoFormat().format(programText));
}
static File res(final String resourceName) {