import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
import org.simantics.scl.compiler.module.Module;
import org.simantics.scl.compiler.module.repository.ModuleRepository;
import org.simantics.scl.compiler.module.repository.UpdateListener;
+import org.simantics.scl.compiler.source.ModuleSource;
import org.simantics.scl.compiler.source.StringModuleSource;
import org.simantics.scl.compiler.source.repository.CompositeModuleSourceRepository;
import org.simantics.scl.compiler.source.repository.MapModuleSourceRepository;
SourceRepositories.BUILTIN_SOURCE_REPOSITORY,
SourceRepositories.PRELUDE_SOURCE_REPOSITORY
));
- private static final Pattern TEST_SEPARATOR = Pattern.compile("^-- *$", Pattern.MULTILINE);
+ private static final Pattern TEST_SEPARATOR = Pattern.compile("^--+ *$", Pattern.MULTILINE);
private static final Charset UTF8 = Charset.forName("UTF-8");
String path;
this.path = path;
}
-
protected void test() {
String testModuleName = Thread.currentThread().getStackTrace()[2].getMethodName();
String testPath = path + "/" + testModuleName + ".scl";
try {
String[] testParts = readTestParts(testPath);
- for(int i=0;i<testParts.length;i+=2) {
- String input = testParts[i];
- String expectedOutput = testParts[i+1];
- String actualOutput = test(testModuleName, input);
+
+ int j=0;
+ ArrayList<String> auxModuleNameList = new ArrayList<String>();
+ while(j < testParts.length) {
+ String part = testParts[j];
+ if(part.startsWith("// module "))
+ auxModuleNameList.add(part.substring(10).split("\\n", 2)[0].trim());
+ else
+ break;
+ ++j;
+ }
+ int mainId = j;
+ String[] moduleNames = new String[mainId+1];
+ String[] moduleTexts = new String[mainId+1];
+ for(int i=0;i<mainId;++i) {
+ moduleNames[i] = auxModuleNameList.get(i);
+ moduleTexts[i] = testParts[i];
+ }
+ moduleNames[mainId] = testModuleName;
+
+ for(;j<testParts.length;j+=2) {
+ moduleTexts[mainId] = testParts[j];
+ String expectedOutput = j+1<testParts.length ? testParts[j+1] : "";
+ String actualOutput = test(moduleNames, moduleTexts);
Assert.assertEquals(
canonicalizeOutput(expectedOutput),
canonicalizeOutput(actualOutput));
return text.trim().replace("\r\n", "\n");
}
- protected String test(String testModuleName, String input) throws ValueNotFound {
+ protected String test(String[] moduleNames, String[] moduleTexts) throws ValueNotFound {
+ if(moduleNames.length != moduleTexts.length)
+ throw new IllegalArgumentException();
+ /*for(int i=0;i<moduleNames.length;++i) {
+ System.out.println("-- " + moduleNames[i] + " --");
+ System.out.println(moduleTexts[i]);
+ }*/
+
+ ModuleSource[] moduleSources = new ModuleSource[moduleNames.length];
+ for(int i=0;i<moduleNames.length;++i)
+ moduleSources[i] = new StringModuleSource(
+ moduleNames[i], getClass().getClassLoader(), moduleTexts[i]) {
+ @Override
+ protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
+ return ImportDeclaration.ONLY_BUILTINS;
+ }
+ };
ModuleRepository testEnvironment = new ModuleRepository(
PRELUDE_MODULE_REPOSITORY,
- new MapModuleSourceRepository(new StringModuleSource(
- testModuleName, getClass().getClassLoader(), input) {
- @Override
- protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
- return ImportDeclaration.ONLY_BUILTINS;
- }
- }
- ));
- Failable<Module> result = testEnvironment.getModule(testModuleName);
+ new MapModuleSourceRepository(moduleSources));
+ int lastId = moduleNames.length-1;
+ Failable<Module> result = testEnvironment.getModule(moduleNames[lastId]);
if(!result.didSucceed())
- return ((Failure)result).toString(input);
+ return ((Failure)result).toString(moduleTexts[lastId]);
else {
- Object main = testEnvironment.getRuntimeModule(testModuleName).getResult().getValue("main");
+ Object main = testEnvironment.getRuntimeModule(moduleNames[lastId]).getResult().getValue("main");
return String.valueOf(main);
}
}
}
String text = new String(buffer, 0, pos, UTF8);
String[] result = TEST_SEPARATOR.split(text);
- if(result.length % 2 == 1) {
- result = Arrays.copyOf(result, result.length+2);
- result[result.length-1] = "";
+ for(int i=1;i<result.length;++i) {
+ if(result[i].startsWith("\r\n"))
+ result[i] = result[i].substring(2);
+ if(result[i].startsWith("\n") || result[i].startsWith("\r"))
+ result[i] = result[i].substring(1);
}
return result;
} finally {