X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fcommands%2FTestScriptExecutor.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fcommands%2FTestScriptExecutor.java;h=ab39d538a23a4f808440855956ea15a5b8e02d40;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/TestScriptExecutor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/TestScriptExecutor.java new file mode 100644 index 000000000..ab39d538a --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/TestScriptExecutor.java @@ -0,0 +1,91 @@ +package org.simantics.scl.compiler.commands; + +import java.io.BufferedReader; +import java.io.IOException; + +import org.junit.Assert; +import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; + +public class TestScriptExecutor { + + private final CommandSession session; + private final BufferedReader reader; + private final SCLReportingHandler handler; + + public TestScriptExecutor(CommandSession session, BufferedReader reader, SCLReportingHandler handler) { + this.session = session; + this.reader = reader; + this.handler = handler == null ? SCLReportingHandler.DEFAULT : handler; + } + + public void execute() throws IOException { + StringBuilder command = new StringBuilder(); + StringBuilder response = new StringBuilder(); + while(true) { + // Read command + while(true) { + String line = reader.readLine(); + if(line == null) { + execute(command.toString(), response.toString()); + return; + } + else if(line.startsWith("> ")) + command.append(line.substring(2)).append('\n'); + else { + response.append(line).append('\n'); + break; + } + } + + // Read response + while(true) { + String line = reader.readLine(); + if(line == null) { + execute(command.toString(), response.toString()); + return; + } + else if(line.startsWith("> ")) { + execute(command.toString(), response.toString()); + command = new StringBuilder(); + response = new StringBuilder(); + command.append(line.substring(2)).append('\n'); + break; + } + else + response.append(line).append('\n'); + } + } + } + + private void execute(String command, String expectedResponse) { + final StringBuilder actualResponse = new StringBuilder(); + final StringBuilder errors = new StringBuilder(); + session.execute(command, + new AbstractSCLReportingHandler() { + + @Override + public void print(String text) { + handler.print(text); + actualResponse.append(text).append('\n'); + } + + @Override + public void printError(String error) { + handler.printError(error); + errors.append(error).append('\n'); + } + + @Override + public void printCommand(String command) { + handler.printCommand(command); + } + }); + if(errors.length() > 0) + Assert.fail("Command '" + command.trim() + "' produced error " + errors.toString().trim() + "."); + String expected = expectedResponse.trim().replaceAll("\\r", ""); + String actual = actualResponse.toString().trim().replaceAll("\\r", ""); + Assert.assertEquals(expected, actual); + } + +}