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.ArrayList;
7 import java.util.Arrays;
8 import java.util.regex.Pattern;
10 import org.junit.Assert;
11 import org.simantics.scl.compiler.errors.Failable;
12 import org.simantics.scl.compiler.errors.Failure;
13 import org.simantics.scl.compiler.module.ImportDeclaration;
14 import org.simantics.scl.compiler.module.Module;
15 import org.simantics.scl.compiler.module.repository.ModuleRepository;
16 import org.simantics.scl.compiler.module.repository.UpdateListener;
17 import org.simantics.scl.compiler.source.ModuleSource;
18 import org.simantics.scl.compiler.source.StringModuleSource;
19 import org.simantics.scl.compiler.source.repository.MapModuleSourceRepository;
20 import org.simantics.scl.compiler.top.ValueNotFound;
22 public class TestBase {
24 public static final ModuleRepository PRELUDE_MODULE_REPOSITORY = InitialRepository.getInitialRepository();
25 private static final Pattern TEST_SEPARATOR = Pattern.compile("^--+ *$", Pattern.MULTILINE);
26 private static final Charset UTF8 = Charset.forName("UTF-8");
30 public TestBase(String path) {
34 protected void test() {
35 String testModuleName = Thread.currentThread().getStackTrace()[2].getMethodName();
36 String testPath = path + "/" + testModuleName + ".scl";
39 String[] testParts = readTestParts(testPath);
42 ArrayList<String> auxModuleNameList = new ArrayList<String>();
43 while(j < testParts.length) {
44 String part = testParts[j];
45 if(part.startsWith("// module "))
46 auxModuleNameList.add(part.substring(10).split("\\n", 2)[0].trim());
52 String[] moduleNames = new String[mainId+1];
53 String[] moduleTexts = new String[mainId+1];
54 for(int i=0;i<mainId;++i) {
55 moduleNames[i] = auxModuleNameList.get(i);
56 moduleTexts[i] = testParts[i];
58 moduleNames[mainId] = testModuleName;
60 for(;j<testParts.length;j+=2) {
61 moduleTexts[mainId] = testParts[j];
62 String expectedOutput = j+1<testParts.length ? testParts[j+1] : "";
63 String actualOutput = test(moduleNames, moduleTexts);
65 canonicalizeOutput(expectedOutput),
66 canonicalizeOutput(actualOutput));
68 } catch(IOException e) {
69 throw new RuntimeException(e);
70 } catch (ValueNotFound e) {
71 throw new RuntimeException(e);
75 private static String canonicalizeOutput(String text) {
76 return text.trim().replace("\r\n", "\n");
79 protected String test(String[] moduleNames, String[] moduleTexts) throws ValueNotFound {
80 if(moduleNames.length != moduleTexts.length)
81 throw new IllegalArgumentException();
82 /*for(int i=0;i<moduleNames.length;++i) {
83 System.out.println("-- " + moduleNames[i] + " --");
84 System.out.println(moduleTexts[i]);
87 ModuleSource[] moduleSources = new ModuleSource[moduleNames.length];
88 for(int i=0;i<moduleNames.length;++i)
89 moduleSources[i] = new StringModuleSource(
90 moduleNames[i], getClass().getClassLoader(), moduleTexts[i]) {
92 protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
93 return ImportDeclaration.ONLY_BUILTINS;
96 ModuleRepository testEnvironment = new ModuleRepository(
97 PRELUDE_MODULE_REPOSITORY,
98 new MapModuleSourceRepository(moduleSources));
99 int lastId = moduleNames.length-1;
100 Failable<Module> result = testEnvironment.getModule(moduleNames[lastId]);
101 if(!result.didSucceed())
102 return ((Failure)result).toString(moduleTexts[lastId]);
104 Object main = testEnvironment.getRuntimeModule(moduleNames[lastId]).getResult().getValue("main");
105 return String.valueOf(main);
109 private String[] readTestParts(String testPath) throws IOException {
110 InputStream stream = getClass().getResourceAsStream(testPath);
112 byte[] buffer = new byte[1024];
115 int c = stream.read(buffer, pos, buffer.length-pos);
119 if(pos < buffer.length)
121 buffer = Arrays.copyOf(buffer, pos*2);
123 String text = new String(buffer, 0, pos, UTF8);
124 String[] result = TEST_SEPARATOR.split(text);
125 for(int i=1;i<result.length;++i) {
126 if(result[i].startsWith("\r\n"))
127 result[i] = result[i].substring(2);
128 if(result[i].startsWith("\n") || result[i].startsWith("\r"))
129 result[i] = result[i].substring(1);