annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
1 /*
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
4 *
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
8 *
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
13 * accompanied this code).
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
14 *
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
18 *
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
21 * questions.
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
22 */
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
23 package com.oracle.truffle.sl.test;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
24
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
25 import java.io.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
26 import java.nio.file.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
27 import java.nio.file.attribute.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
28 import java.util.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
29
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
30 import org.junit.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
31
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
32 import com.oracle.truffle.api.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
33 import com.oracle.truffle.api.source.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
34 import com.oracle.truffle.sl.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
35 import com.oracle.truffle.sl.runtime.*;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
36
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
37 public class SLTestRunner {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
38
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
39 private static final int REPEATS = 10;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
40 private static final String TEST_DIR = "tests";
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
41 private static final String INPUT_SUFFIX = ".sl";
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
42 private static final String OUTPUT_SUFFIX = ".output";
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
43
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
44 static class TestCase {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
45 protected final String name;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
46 protected final Source input;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
47 protected final String expectedOutput;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
48 protected String actualOutput;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
49
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
50 protected TestCase(String name, Source input, String expectedOutput) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
51 this.name = name;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
52 this.input = input;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
53 this.expectedOutput = expectedOutput;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
54 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
55 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
56
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
57 protected boolean useConsole = false;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
58
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
59 protected final SourceManager sourceManager = new SourceManager();
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
60 protected final List<TestCase> testCases = new ArrayList<>();
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
61
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
62 protected boolean runTests(String namePattern) throws IOException {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
63 Path testsRoot = FileSystems.getDefault().getPath(TEST_DIR);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
64
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
65 Files.walkFileTree(testsRoot, new SimpleFileVisitor<Path>() {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
66 @Override
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
67 public FileVisitResult visitFile(Path inputFile, BasicFileAttributes attrs) throws IOException {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
68 String name = inputFile.getFileName().toString();
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
69 if (name.endsWith(INPUT_SUFFIX)) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
70 name = name.substring(0, name.length() - INPUT_SUFFIX.length());
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
71 Path outputFile = inputFile.resolveSibling(name + OUTPUT_SUFFIX);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
72 if (!Files.exists(outputFile)) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
73 throw new Error("Output file does not exist: " + outputFile);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
74 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
75
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
76 testCases.add(new TestCase(name, sourceManager.get(inputFile.toString()), new String(Files.readAllBytes(outputFile))));
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
77 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
78 return FileVisitResult.CONTINUE;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
79 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
80 });
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
81
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
82 if (testCases.size() == 0) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
83 System.out.format("No test cases match filter %s", namePattern);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
84 return false;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
85 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
86
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
87 boolean success = true;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
88 for (TestCase testCase : testCases) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
89 if (namePattern.length() == 0 || testCase.name.toLowerCase().contains(namePattern.toLowerCase())) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
90 success = success & executeTest(testCase);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
91 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
92 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
93 return success;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
94 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
95
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
96 protected boolean executeTest(TestCase testCase) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
97 System.out.format("Running %s\n", testCase.name);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
98
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
99 ByteArrayOutputStream out = new ByteArrayOutputStream();
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
100 PrintStream printer = new PrintStream(useConsole ? new SplitOutputStream(out, System.err) : out);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
101 PrintStream origErr = System.err;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
102 try {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
103 System.setErr(printer);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
104 SLContext context = new SLContext(sourceManager, printer);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
105 SLMain.run(context, testCase.input, null, REPEATS);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
106 } catch (Throwable ex) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
107 ex.printStackTrace(printer);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
108 } finally {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
109 System.setErr(origErr);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
110 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
111 testCase.actualOutput = new String(out.toByteArray());
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
112
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
113 if (testCase.actualOutput.equals(repeat(testCase.expectedOutput, REPEATS))) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
114 System.out.format("OK %s\n", testCase.name);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
115 return true;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
116 } else {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
117 if (!useConsole) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
118 System.out.format("== Expected ==\n%s\n", testCase.expectedOutput);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
119 System.out.format("== Actual ==\n%s\n", testCase.actualOutput);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
120 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
121 System.out.format("FAILED %s\n", testCase.name);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
122 return false;
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
123 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
124 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
125
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
126 private static String repeat(String s, int count) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
127 StringBuilder result = new StringBuilder(s.length() * count);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
128 for (int i = 0; i < count; i++) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
129 result.append(s);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
130 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
131 return result.toString();
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
132 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
133
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
134 public static void main(String[] args) throws IOException {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
135 String namePattern = "";
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
136 if (args.length > 0) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
137 namePattern = args[0];
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
138 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
139 boolean success = new SLTestRunner().runTests(namePattern);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
140 if (!success) {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
141 System.exit(1);
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
142 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
143 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
144
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
145 @Test
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
146 public void test() throws IOException {
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
147 Assert.assertTrue(runTests(""));
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
148 }
e34d5cca7496 Use source and expected output files to test Simple Language, instead of individual JUnit tests with the source and expected output as strings
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
149 }