1 package org.simantics.scl.compiler.tests;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.nio.charset.Charset;
6 import java.util.Arrays;
7 import java.util.regex.Pattern;
9 import org.junit.Assert;
10 import org.simantics.scl.compiler.errors.Failable;
11 import org.simantics.scl.compiler.errors.Failure;
12 import org.simantics.scl.compiler.module.ImportDeclaration;
13 import org.simantics.scl.compiler.module.Module;
14 import org.simantics.scl.compiler.module.repository.ModuleRepository;
15 import org.simantics.scl.compiler.module.repository.UpdateListener;
16 import org.simantics.scl.compiler.source.StringModuleSource;
17 import org.simantics.scl.compiler.source.repository.CompositeModuleSourceRepository;
18 import org.simantics.scl.compiler.source.repository.MapModuleSourceRepository;
19 import org.simantics.scl.compiler.source.repository.SourceRepositories;
20 import org.simantics.scl.compiler.top.ValueNotFound;
22 public class TestBase {
24 public static final ModuleRepository PRELUDE_MODULE_REPOSITORY = new ModuleRepository(
25 new CompositeModuleSourceRepository(
26 SourceRepositories.BUILTIN_SOURCE_REPOSITORY,
27 SourceRepositories.PRELUDE_SOURCE_REPOSITORY
29 private static final Pattern TEST_SEPARATOR = Pattern.compile("^-- *$", Pattern.MULTILINE);
30 private static final Charset UTF8 = Charset.forName("UTF-8");
34 public TestBase(String path) {
39 protected void test() {
40 String testModuleName = Thread.currentThread().getStackTrace()[2].getMethodName();
41 String testPath = path + "/" + testModuleName + ".scl";
44 String[] testParts = readTestParts(testPath);
45 for(int i=0;i<testParts.length;i+=2) {
46 String input = testParts[i];
47 String expectedOutput = testParts[i+1];
48 String actualOutput = test(testModuleName, input);
50 canonicalizeOutput(expectedOutput),
51 canonicalizeOutput(actualOutput));
53 } catch(IOException e) {
54 throw new RuntimeException(e);
55 } catch (ValueNotFound e) {
56 throw new RuntimeException(e);
60 private static String canonicalizeOutput(String text) {
61 return text.trim().replace("\r\n", "\n");
64 protected String test(String testModuleName, String input) throws ValueNotFound {
65 ModuleRepository testEnvironment = new ModuleRepository(
66 PRELUDE_MODULE_REPOSITORY,
67 new MapModuleSourceRepository(new StringModuleSource(
68 testModuleName, getClass().getClassLoader(), input) {
70 protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
71 return ImportDeclaration.ONLY_BUILTINS;
75 Failable<Module> result = testEnvironment.getModule(testModuleName);
76 if(!result.didSucceed())
77 return ((Failure)result).toString(input);
79 Object main = testEnvironment.getRuntimeModule(testModuleName).getResult().getValue("main");
80 return String.valueOf(main);
84 private String[] readTestParts(String testPath) throws IOException {
85 InputStream stream = getClass().getResourceAsStream(testPath);
87 byte[] buffer = new byte[1024];
90 int c = stream.read(buffer, pos, buffer.length-pos);
94 if(pos < buffer.length)
96 buffer = Arrays.copyOf(buffer, pos*2);
98 String text = new String(buffer, 0, pos, UTF8);
99 String[] result = TEST_SEPARATOR.split(text);
100 if(result.length % 2 == 1) {
101 result = Arrays.copyOf(result, result.length+2);
102 result[result.length-1] = "";