Mercurial > hg > truffle
comparison graal/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTestRunner.java @ 13762:e34d5cca7496
Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Fri, 24 Jan 2014 18:18:49 -0800 |
parents | |
children | b5b64fe6963f |
comparison
equal
deleted
inserted
replaced
13761:7c418666c6c9 | 13762:e34d5cca7496 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2013, 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.file.*; | |
27 import java.nio.file.attribute.*; | |
28 import java.util.*; | |
29 | |
30 import org.junit.*; | |
31 | |
32 import com.oracle.truffle.api.*; | |
33 import com.oracle.truffle.api.source.*; | |
34 import com.oracle.truffle.sl.*; | |
35 import com.oracle.truffle.sl.runtime.*; | |
36 | |
37 public class SLTestRunner { | |
38 | |
39 private static final int REPEATS = 10; | |
40 private static final String TEST_DIR = "tests"; | |
41 private static final String INPUT_SUFFIX = ".sl"; | |
42 private static final String OUTPUT_SUFFIX = ".output"; | |
43 | |
44 static class TestCase { | |
45 protected final String name; | |
46 protected final Source input; | |
47 protected final String expectedOutput; | |
48 protected String actualOutput; | |
49 | |
50 protected TestCase(String name, Source input, String expectedOutput) { | |
51 this.name = name; | |
52 this.input = input; | |
53 this.expectedOutput = expectedOutput; | |
54 } | |
55 } | |
56 | |
57 protected boolean useConsole = false; | |
58 | |
59 protected final SourceManager sourceManager = new SourceManager(); | |
60 protected final List<TestCase> testCases = new ArrayList<>(); | |
61 | |
62 protected boolean runTests(String namePattern) throws IOException { | |
63 Path testsRoot = FileSystems.getDefault().getPath(TEST_DIR); | |
64 | |
65 Files.walkFileTree(testsRoot, new SimpleFileVisitor<Path>() { | |
66 @Override | |
67 public FileVisitResult visitFile(Path inputFile, BasicFileAttributes attrs) throws IOException { | |
68 String name = inputFile.getFileName().toString(); | |
69 if (name.endsWith(INPUT_SUFFIX)) { | |
70 name = name.substring(0, name.length() - INPUT_SUFFIX.length()); | |
71 Path outputFile = inputFile.resolveSibling(name + OUTPUT_SUFFIX); | |
72 if (!Files.exists(outputFile)) { | |
73 throw new Error("Output file does not exist: " + outputFile); | |
74 } | |
75 | |
76 testCases.add(new TestCase(name, sourceManager.get(inputFile.toString()), new String(Files.readAllBytes(outputFile)))); | |
77 } | |
78 return FileVisitResult.CONTINUE; | |
79 } | |
80 }); | |
81 | |
82 if (testCases.size() == 0) { | |
83 System.out.format("No test cases match filter %s", namePattern); | |
84 return false; | |
85 } | |
86 | |
87 boolean success = true; | |
88 for (TestCase testCase : testCases) { | |
89 if (namePattern.length() == 0 || testCase.name.toLowerCase().contains(namePattern.toLowerCase())) { | |
90 success = success & executeTest(testCase); | |
91 } | |
92 } | |
93 return success; | |
94 } | |
95 | |
96 protected boolean executeTest(TestCase testCase) { | |
97 System.out.format("Running %s\n", testCase.name); | |
98 | |
99 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
100 PrintStream printer = new PrintStream(useConsole ? new SplitOutputStream(out, System.err) : out); | |
101 PrintStream origErr = System.err; | |
102 try { | |
103 System.setErr(printer); | |
104 SLContext context = new SLContext(sourceManager, printer); | |
105 SLMain.run(context, testCase.input, null, REPEATS); | |
106 } catch (Throwable ex) { | |
107 ex.printStackTrace(printer); | |
108 } finally { | |
109 System.setErr(origErr); | |
110 } | |
111 testCase.actualOutput = new String(out.toByteArray()); | |
112 | |
113 if (testCase.actualOutput.equals(repeat(testCase.expectedOutput, REPEATS))) { | |
114 System.out.format("OK %s\n", testCase.name); | |
115 return true; | |
116 } else { | |
117 if (!useConsole) { | |
118 System.out.format("== Expected ==\n%s\n", testCase.expectedOutput); | |
119 System.out.format("== Actual ==\n%s\n", testCase.actualOutput); | |
120 } | |
121 System.out.format("FAILED %s\n", testCase.name); | |
122 return false; | |
123 } | |
124 } | |
125 | |
126 private static String repeat(String s, int count) { | |
127 StringBuilder result = new StringBuilder(s.length() * count); | |
128 for (int i = 0; i < count; i++) { | |
129 result.append(s); | |
130 } | |
131 return result.toString(); | |
132 } | |
133 | |
134 public static void main(String[] args) throws IOException { | |
135 String namePattern = ""; | |
136 if (args.length > 0) { | |
137 namePattern = args[0]; | |
138 } | |
139 boolean success = new SLTestRunner().runTests(namePattern); | |
140 if (!success) { | |
141 System.exit(1); | |
142 } | |
143 } | |
144 | |
145 @Test | |
146 public void test() throws IOException { | |
147 Assert.assertTrue(runTests("")); | |
148 } | |
149 } |