1 package org.simantics.scl.compiler.commands;
3 import java.io.BufferedReader;
4 import java.io.IOException;
6 import org.junit.Assert;
7 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
8 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
10 public class TestScriptExecutor {
12 private final CommandSession session;
13 private final BufferedReader reader;
14 private final SCLReportingHandler handler;
16 public TestScriptExecutor(CommandSession session, BufferedReader reader, SCLReportingHandler handler) {
17 this.session = session;
19 this.handler = handler == null ? SCLReportingHandler.DEFAULT : handler;
22 public void execute() throws IOException {
23 StringBuilder command = new StringBuilder();
24 StringBuilder response = new StringBuilder();
28 String line = reader.readLine();
30 execute(command.toString(), response.toString());
33 else if(line.startsWith("> "))
34 command.append(line.substring(2)).append('\n');
36 response.append(line).append('\n');
43 String line = reader.readLine();
45 execute(command.toString(), response.toString());
48 else if(line.startsWith("> ")) {
49 execute(command.toString(), response.toString());
50 command = new StringBuilder();
51 response = new StringBuilder();
52 command.append(line.substring(2)).append('\n');
56 response.append(line).append('\n');
61 private void execute(String command, String expectedResponse) {
62 final StringBuilder actualResponse = new StringBuilder();
63 final StringBuilder errors = new StringBuilder();
64 session.execute(command,
65 new AbstractSCLReportingHandler() {
68 public void print(String text) {
70 actualResponse.append(text).append('\n');
74 public void printError(String error) {
75 handler.printError(error);
76 errors.append(error).append('\n');
80 public void printCommand(String command) {
81 handler.printCommand(command);
84 if(errors.length() > 0)
85 Assert.fail("Command '" + command.trim() + "' produced error " + errors.toString().trim() + ".");
86 String expected = expectedResponse.trim().replaceAll("\\r", "");
87 String actual = actualResponse.toString().trim().replaceAll("\\r", "");
88 Assert.assertEquals(expected, actual);