comparison graal/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTestRunner.java @ 13821:b16ec83edc73

Documentation and more refactoring of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 29 Jan 2014 20:45:43 -0800
parents 030e75d4d7dc
children 4eda2fa64da6
comparison
equal deleted inserted replaced
13820:20e7727588e8 13821:b16ec83edc73
44 44
45 public final class SLTestRunner extends ParentRunner<TestCase> { 45 public final class SLTestRunner extends ParentRunner<TestCase> {
46 46
47 private static final int REPEATS = 10; 47 private static final int REPEATS = 10;
48 48
49 private static final String INPUT_SUFFIX = ".sl"; 49 private static final String SOURCE_SUFFIX = ".sl";
50 private static final String INPUT_SUFFIX = ".input";
50 private static final String OUTPUT_SUFFIX = ".output"; 51 private static final String OUTPUT_SUFFIX = ".output";
51 52
52 private static final String LF = System.getProperty("line.separator"); 53 private static final String LF = System.getProperty("line.separator");
53 54
54 public static final class TestCase { 55 static class TestCase {
55 private final Source input; 56 protected final Description name;
56 private final String expectedOutput; 57 protected final Source source;
57 private final Description name; 58 protected final String testInput;
58 59 protected final String expectedOutput;
59 public TestCase(Class<?> testClass, String name, Source input, String expectedOutput) { 60 protected String actualOutput;
60 this.input = input; 61
62 protected TestCase(Class<?> testClass, String name, Source source, String testInput, String expectedOutput) {
63 this.name = Description.createTestDescription(testClass, name);
64 this.source = source;
65 this.testInput = testInput;
61 this.expectedOutput = expectedOutput; 66 this.expectedOutput = expectedOutput;
62 this.name = Description.createTestDescription(testClass, name);
63 } 67 }
64 } 68 }
65 69
66 private final SourceManager sourceManager = new SourceManager(); 70 private final SourceManager sourceManager = new SourceManager();
67 private final List<TestCase> testCases; 71 private final List<TestCase> testCases;
112 final Path rootPath = root; 116 final Path rootPath = root;
113 117
114 final List<TestCase> foundCases = new ArrayList<>(); 118 final List<TestCase> foundCases = new ArrayList<>();
115 Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() { 119 Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
116 @Override 120 @Override
117 public FileVisitResult visitFile(Path inputFile, BasicFileAttributes attrs) throws IOException { 121 public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException {
118 String name = inputFile.getFileName().toString(); 122 String sourceName = sourceFile.getFileName().toString();
119 if (name.endsWith(INPUT_SUFFIX)) { 123 if (sourceName.endsWith(SOURCE_SUFFIX)) {
120 String baseName = name.substring(0, name.length() - INPUT_SUFFIX.length()); 124 String baseName = sourceName.substring(0, sourceName.length() - SOURCE_SUFFIX.length());
121 125
122 Path outputFile = inputFile.resolveSibling(baseName + OUTPUT_SUFFIX); 126 Path inputFile = sourceFile.resolveSibling(baseName + INPUT_SUFFIX);
123 if (!Files.exists(outputFile)) { 127 String testInput = "";
124 throw new Error("Output file does not exist: " + outputFile); 128 if (Files.exists(inputFile)) {
129 testInput = readAllLines(inputFile);
125 } 130 }
126 131
127 // fix line feeds for non unix os 132 Path outputFile = sourceFile.resolveSibling(baseName + OUTPUT_SUFFIX);
128 StringBuilder outFile = new StringBuilder(); 133 String expectedOutput = "";
129 for (String line : Files.readAllLines(outputFile, Charset.defaultCharset())) { 134 if (Files.exists(outputFile)) {
130 outFile.append(line); 135 expectedOutput = readAllLines(outputFile);
131 outFile.append(LF);
132 } 136 }
133 foundCases.add(new TestCase(c, baseName, sourceManager.get(inputFile.toString()), outFile.toString())); 137
138 foundCases.add(new TestCase(c, baseName, sourceManager.get(sourceName, readAllLines(sourceFile)), testInput, expectedOutput));
134 } 139 }
135 return FileVisitResult.CONTINUE; 140 return FileVisitResult.CONTINUE;
136 } 141 }
137 }); 142 });
138 return foundCases; 143 return foundCases;
139 } 144 }
140 145
146 private static String readAllLines(Path file) throws IOException {
147 // fix line feeds for non unix os
148 StringBuilder outFile = new StringBuilder();
149 for (String line : Files.readAllLines(file, Charset.defaultCharset())) {
150 outFile.append(line).append(LF);
151 }
152 return outFile.toString();
153 }
154
141 @Override 155 @Override
142 protected void runChild(TestCase testCase, RunNotifier notifier) { 156 protected void runChild(TestCase testCase, RunNotifier notifier) {
143 notifier.fireTestStarted(testCase.name); 157 notifier.fireTestStarted(testCase.name);
144 158
145 ByteArrayOutputStream out = new ByteArrayOutputStream(); 159 ByteArrayOutputStream out = new ByteArrayOutputStream();
146 PrintStream printer = new PrintStream(out); 160 PrintStream printer = new PrintStream(out);
147 PrintStream origErr = System.err;
148 try { 161 try {
149 System.setErr(printer); 162 SLContext context = new SLContext(sourceManager, new BufferedReader(new StringReader(repeat(testCase.testInput, REPEATS))), printer);
150 SLContext context = new SLContext(sourceManager, printer); 163 SLMain.run(context, testCase.source, null, REPEATS);
151 SLMain.run(context, testCase.input, null, REPEATS);
152 164
153 String actualOutput = new String(out.toByteArray()); 165 String actualOutput = new String(out.toByteArray());
154
155 Assert.assertEquals(repeat(testCase.expectedOutput, REPEATS), actualOutput); 166 Assert.assertEquals(repeat(testCase.expectedOutput, REPEATS), actualOutput);
156 } catch (AssertionError e) {
157 notifier.fireTestFailure(new Failure(testCase.name, e));
158 } catch (Throwable ex) { 167 } catch (Throwable ex) {
159 notifier.fireTestFailure(new Failure(testCase.name, ex)); 168 notifier.fireTestFailure(new Failure(testCase.name, ex));
160 } finally { 169 } finally {
161 System.setErr(origErr);
162 notifier.fireTestFinished(testCase.name); 170 notifier.fireTestFinished(testCase.name);
163 } 171 }
164 } 172 }
165 173
166 private static String repeat(String s, int count) { 174 private static String repeat(String s, int count) {