package org.simantics.scl.compiler.tests; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.regex.Pattern; import org.junit.Assert; import org.simantics.scl.compiler.errors.Failable; import org.simantics.scl.compiler.errors.Failure; import org.simantics.scl.compiler.module.ImportDeclaration; import org.simantics.scl.compiler.module.Module; import org.simantics.scl.compiler.module.options.ModuleCompilationOptions; 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.MapModuleSourceRepository; import org.simantics.scl.compiler.top.ValueNotFound; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.reporting.SCLReportingHandler; import org.simantics.scl.runtime.reporting.WriterSCLReportingHandler; public class TestBase { public static final ModuleRepository PRELUDE_MODULE_REPOSITORY = InitialRepository.getInitialRepository(); private static final Pattern TEST_SEPARATOR = Pattern.compile("^--+ *$", Pattern.MULTILINE); private static final Charset UTF8 = Charset.forName("UTF-8"); String path; public TestBase(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); int j=0; ArrayList auxModuleNameList = new ArrayList(); 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 ModuleCompilationOptions.SILENT); int lastId = moduleNames.length-1; Failable result = testRepository.getModule(moduleNames[lastId]); if(!result.didSucceed()) return ((Failure)result).toString(moduleTexts[lastId]); else { SCLContext context = SCLContext.getCurrent(); StringWriter writer = new StringWriter(); Object oldReportingHandler = context.put(SCLReportingHandler.REPORTING_HANDLER, new WriterSCLReportingHandler(writer)); try { Object main = testRepository.getRuntimeModule(moduleNames[lastId]).getResult().getValue("main"); writer.write(String.valueOf(main)); return writer.toString(); } finally { context.put(SCLReportingHandler.REPORTING_HANDLER, oldReportingHandler); } } } private String[] readTestParts(String testPath) throws IOException { InputStream stream = getClass().getResourceAsStream(testPath); if(stream == null) throw new FileNotFoundException(testPath); try { byte[] buffer = new byte[1024]; int pos = 0; while(true) { int c = stream.read(buffer, pos, buffer.length-pos); if(c <= 0) break; pos += c; if(pos < buffer.length) break; buffer = Arrays.copyOf(buffer, pos*2); } String text = new String(buffer, 0, pos, UTF8); String[] result = TEST_SEPARATOR.split(text); for(int i=1;i