]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/TestBase.java
Merge "Adding more detailed message to thrown exceptions in SyncElementFactory"
[simantics/platform.git] / tests / org.simantics.scl.compiler.tests / src / org / simantics / scl / compiler / tests / TestBase.java
1 package org.simantics.scl.compiler.tests;
2
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;
9
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;
21
22 public class TestBase {
23     
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");
27
28     String path;
29
30     public TestBase(String path) {
31         this.path = path;
32     }
33     
34     protected void test() {
35         String testModuleName = Thread.currentThread().getStackTrace()[2].getMethodName();
36         String testPath = path + "/" + testModuleName + ".scl";
37         
38         try {
39             String[] testParts = readTestParts(testPath);
40             
41             int j=0;
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());
47                 else
48                     break;
49                 ++j;
50             }
51             int mainId = j;
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];
57             }
58             moduleNames[mainId] = testModuleName;
59             
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);
64                 Assert.assertEquals(
65                         canonicalizeOutput(expectedOutput),
66                         canonicalizeOutput(actualOutput));
67             }
68         } catch(IOException e) {
69             throw new RuntimeException(e);
70         } catch (ValueNotFound e) {
71             throw new RuntimeException(e);
72         }
73     }
74     
75     private static String canonicalizeOutput(String text) {
76         return text.trim().replace("\r\n", "\n");
77     }
78
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]);
85         }*/
86         
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]) {
91                 @Override
92                 protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
93                     return ImportDeclaration.ONLY_BUILTINS;
94                 }
95             };
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]);
103         else {
104             Object main = testEnvironment.getRuntimeModule(moduleNames[lastId]).getResult().getValue("main");
105             return String.valueOf(main);
106         }
107     }
108
109     private String[] readTestParts(String testPath) throws IOException {
110         InputStream stream = getClass().getResourceAsStream(testPath);
111         try {
112             byte[] buffer = new byte[1024];
113             int pos = 0;
114             while(true) {
115                 int c = stream.read(buffer, pos, buffer.length-pos);
116                 if(c <= 0)
117                     break;
118                 pos += c;
119                 if(pos < buffer.length)
120                     break;
121                 buffer = Arrays.copyOf(buffer, pos*2);
122             }
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);
130             }
131             return result;
132         } finally {
133             stream.close();
134         }
135     }
136 }