comparison truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTestRunner.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTestRunner.java@894f82515e38
children c07e64ecb528 fee42ec8c59b
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.sl.test;
24
25 import java.io.*;
26 import java.nio.charset.*;
27 import java.nio.file.*;
28 import java.nio.file.attribute.*;
29 import java.util.*;
30
31 import org.junit.*;
32 import org.junit.internal.*;
33 import org.junit.runner.*;
34 import org.junit.runner.manipulation.*;
35 import org.junit.runner.notification.*;
36 import org.junit.runners.*;
37 import org.junit.runners.model.*;
38
39 import com.oracle.truffle.api.dsl.*;
40 import com.oracle.truffle.api.vm.*;
41 import com.oracle.truffle.sl.*;
42 import com.oracle.truffle.sl.builtins.*;
43 import com.oracle.truffle.sl.test.SLTestRunner.TestCase;
44
45 public final class SLTestRunner extends ParentRunner<TestCase> {
46
47 private static int repeats = 1;
48
49 private static final String SOURCE_SUFFIX = ".sl";
50 private static final String INPUT_SUFFIX = ".input";
51 private static final String OUTPUT_SUFFIX = ".output";
52
53 private static final String LF = System.getProperty("line.separator");
54
55 static class TestCase {
56 protected final Description name;
57 protected final Path path;
58 protected final String sourceName;
59 protected final String testInput;
60 protected final String expectedOutput;
61 protected String actualOutput;
62
63 protected TestCase(Class<?> testClass, String baseName, String sourceName, Path path, String testInput, String expectedOutput) {
64 this.name = Description.createTestDescription(testClass, baseName);
65 this.sourceName = sourceName;
66 this.path = path;
67 this.testInput = testInput;
68 this.expectedOutput = expectedOutput;
69 }
70 }
71
72 private final List<TestCase> testCases;
73
74 public SLTestRunner(Class<?> runningClass) throws InitializationError {
75 super(runningClass);
76 try {
77 testCases = createTests(runningClass);
78 } catch (IOException e) {
79 throw new InitializationError(e);
80 }
81 }
82
83 @Override
84 protected Description describeChild(TestCase child) {
85 return child.name;
86 }
87
88 @Override
89 protected List<TestCase> getChildren() {
90 return testCases;
91 }
92
93 protected static List<TestCase> createTests(final Class<?> c) throws IOException, InitializationError {
94 SLTestSuite suite = c.getAnnotation(SLTestSuite.class);
95 if (suite == null) {
96 throw new InitializationError(String.format("@%s annotation required on class '%s' to run with '%s'.", SLTestSuite.class.getSimpleName(), c.getName(), SLTestRunner.class.getSimpleName()));
97 }
98
99 String[] pathes = suite.value();
100
101 Path root = null;
102 for (String path : pathes) {
103 root = FileSystems.getDefault().getPath(path);
104 if (Files.exists(root)) {
105 break;
106 }
107 }
108 if (root == null && pathes.length > 0) {
109 throw new FileNotFoundException(pathes[0]);
110 }
111
112 final Path rootPath = root;
113
114 final List<TestCase> foundCases = new ArrayList<>();
115 Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
116 @Override
117 public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException {
118 String sourceName = sourceFile.getFileName().toString();
119 if (sourceName.endsWith(SOURCE_SUFFIX)) {
120 String baseName = sourceName.substring(0, sourceName.length() - SOURCE_SUFFIX.length());
121
122 Path inputFile = sourceFile.resolveSibling(baseName + INPUT_SUFFIX);
123 String testInput = "";
124 if (Files.exists(inputFile)) {
125 testInput = readAllLines(inputFile);
126 }
127
128 Path outputFile = sourceFile.resolveSibling(baseName + OUTPUT_SUFFIX);
129 String expectedOutput = "";
130 if (Files.exists(outputFile)) {
131 expectedOutput = readAllLines(outputFile);
132 }
133
134 foundCases.add(new TestCase(c, baseName, sourceName, sourceFile, testInput, expectedOutput));
135 }
136 return FileVisitResult.CONTINUE;
137 }
138 });
139 return foundCases;
140 }
141
142 private static String readAllLines(Path file) throws IOException {
143 // fix line feeds for non unix os
144 StringBuilder outFile = new StringBuilder();
145 for (String line : Files.readAllLines(file, Charset.defaultCharset())) {
146 outFile.append(line).append(LF);
147 }
148 return outFile.toString();
149 }
150
151 public static void setRepeats(int repeats) {
152 SLTestRunner.repeats = repeats;
153 }
154
155 private static final List<NodeFactory<? extends SLBuiltinNode>> builtins = new ArrayList<>();
156
157 public static void installBuiltin(NodeFactory<? extends SLBuiltinNode> builtin) {
158 builtins.add(builtin);
159 }
160
161 @Override
162 protected void runChild(TestCase testCase, RunNotifier notifier) {
163 notifier.fireTestStarted(testCase.name);
164
165 ByteArrayOutputStream out = new ByteArrayOutputStream();
166 PrintWriter printer = new PrintWriter(out);
167 try {
168 TruffleVM vm = TruffleVM.newVM().stdIn(new BufferedReader(new StringReader(repeat(testCase.testInput, repeats)))).stdOut(printer).build();
169
170 String script = readAllLines(testCase.path);
171 SLLanguage.run(vm, testCase.path.toUri(), null, printer, repeats, builtins);
172
173 printer.flush();
174 String actualOutput = new String(out.toByteArray());
175 Assert.assertEquals(script, repeat(testCase.expectedOutput, repeats), actualOutput);
176 } catch (Throwable ex) {
177 notifier.fireTestFailure(new Failure(testCase.name, new IllegalStateException("Cannot run " + testCase.sourceName, ex)));
178 } finally {
179 notifier.fireTestFinished(testCase.name);
180 }
181 }
182
183 private static String repeat(String s, int count) {
184 StringBuilder result = new StringBuilder(s.length() * count);
185 for (int i = 0; i < count; i++) {
186 result.append(s);
187 }
188 return result.toString();
189 }
190
191 public static void runInMain(Class<?> testClass, String[] args) throws InitializationError, NoTestsRemainException {
192 JUnitCore core = new JUnitCore();
193 core.addListener(new TextListener(System.out));
194 SLTestRunner suite = new SLTestRunner(testClass);
195 if (args.length > 0) {
196 suite.filter(new NameFilter(args[0]));
197 }
198 Result r = core.run(suite);
199 if (!r.wasSuccessful()) {
200 System.exit(1);
201 }
202 }
203
204 private static final class NameFilter extends Filter {
205 private final String pattern;
206
207 private NameFilter(String pattern) {
208 this.pattern = pattern.toLowerCase();
209 }
210
211 @Override
212 public boolean shouldRun(Description description) {
213 return description.getMethodName().toLowerCase().contains(pattern);
214 }
215
216 @Override
217 public String describe() {
218 return "Filter contains " + pattern;
219 }
220 }
221
222 }