]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/tests/org/simantics/scl/compiler/tests/TestBase.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / tests / 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.Arrays;
7 import java.util.regex.Pattern;
8
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;
21
22 public class TestBase {
23     
24     public static final ModuleRepository PRELUDE_MODULE_REPOSITORY = new ModuleRepository(
25             new CompositeModuleSourceRepository(
26                     SourceRepositories.BUILTIN_SOURCE_REPOSITORY,
27                     SourceRepositories.PRELUDE_SOURCE_REPOSITORY
28                     ));
29     private static final Pattern TEST_SEPARATOR = Pattern.compile("^-- *$", Pattern.MULTILINE);
30     private static final Charset UTF8 = Charset.forName("UTF-8");
31
32     String path;
33
34     public TestBase(String path) {
35         this.path = path;
36     }
37     
38     
39     protected void test() {
40         String testModuleName = Thread.currentThread().getStackTrace()[2].getMethodName();
41         String testPath = path + "/" + testModuleName + ".scl";
42         
43         try {
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);
49                 Assert.assertEquals(
50                         canonicalizeOutput(expectedOutput),
51                         canonicalizeOutput(actualOutput));
52             }
53         } catch(IOException e) {
54             throw new RuntimeException(e);
55         } catch (ValueNotFound e) {
56             throw new RuntimeException(e);
57         }
58     }
59     
60     private static String canonicalizeOutput(String text) {
61         return text.trim().replace("\r\n", "\n");
62     }
63
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) {
69                     @Override
70                     protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
71                         return ImportDeclaration.ONLY_BUILTINS;
72                     }
73                 }
74                 ));
75         Failable<Module> result = testEnvironment.getModule(testModuleName);
76         if(!result.didSucceed())
77             return ((Failure)result).toString(input);
78         else {
79             Object main = testEnvironment.getRuntimeModule(testModuleName).getResult().getValue("main");
80             return String.valueOf(main);
81         }
82     }
83
84     private String[] readTestParts(String testPath) throws IOException {
85         InputStream stream = getClass().getResourceAsStream(testPath);
86         try {
87             byte[] buffer = new byte[1024];
88             int pos = 0;
89             while(true) {
90                 int c = stream.read(buffer, pos, buffer.length-pos);
91                 if(c <= 0)
92                     break;
93                 pos += c;
94                 if(pos < buffer.length)
95                     break;
96                 buffer = Arrays.copyOf(buffer, pos*2);
97             }
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] = "";
103             }
104             return result;
105         } finally {
106             stream.close();
107         }
108     }
109 }