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;
15 private boolean ignorePrints;
17 public TestScriptExecutor(CommandSession session, BufferedReader reader, SCLReportingHandler handler) {
18 this(session, reader, handler, false);
21 public TestScriptExecutor(CommandSession session, BufferedReader reader, SCLReportingHandler handler, boolean ignorePrints) {
22 this.session = session;
24 this.handler = handler == null ? SCLReportingHandler.DEFAULT : handler;
25 this.ignorePrints = ignorePrints;
28 public void execute() throws IOException {
29 StringBuilder command = new StringBuilder();
30 StringBuilder response = new StringBuilder();
34 String line = reader.readLine();
36 execute(command.toString(), response.toString());
39 else if(line.startsWith("> "))
40 command.append(line.substring(2)).append('\n');
42 response.append(line).append('\n');
49 String line = reader.readLine();
51 execute(command.toString(), response.toString());
54 else if(line.startsWith("> ")) {
55 execute(command.toString(), response.toString());
56 command = new StringBuilder();
57 response = new StringBuilder();
58 command.append(line.substring(2)).append('\n');
62 response.append(line).append('\n');
67 private void execute(String command, String expectedResponse) {
68 final StringBuilder actualResponse = new StringBuilder();
69 final StringBuilder errors = new StringBuilder();
70 session.execute(command,
71 new AbstractSCLReportingHandler() {
74 public void print(String text) {
77 actualResponse.append(text).append('\n');
81 public void printError(String error) {
82 handler.printError(error);
83 errors.append(error).append('\n');
87 public void printCommand(String command) {
88 handler.printCommand(command);
91 if(errors.length() > 0)
92 Assert.fail("Command '" + command.trim() + "' produced error " + errors.toString().trim() + ".");
93 String expected = expectedResponse.trim().replaceAll("\\r", "");
94 String actual = actualResponse.toString().trim().replaceAll("\\r", "");
95 Assert.assertEquals(expected, actual);