]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/TestScriptExecutor.java
Minor SCL enhancements and fixes for logging and test executing
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / commands / TestScriptExecutor.java
1 package org.simantics.scl.compiler.commands;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5
6 import org.junit.Assert;
7 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
8 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
9
10 public class TestScriptExecutor {
11
12     private final CommandSession session;
13     private final BufferedReader reader;
14     private final SCLReportingHandler handler;
15     private boolean ignorePrints;
16     
17     public TestScriptExecutor(CommandSession session, BufferedReader reader, SCLReportingHandler handler) {
18         this(session, reader, handler, false);
19     }
20     
21     public TestScriptExecutor(CommandSession session, BufferedReader reader, SCLReportingHandler handler, boolean ignorePrints) {
22         this.session = session;
23         this.reader = reader;
24         this.handler = handler == null ? SCLReportingHandler.DEFAULT : handler;
25         this.ignorePrints = ignorePrints;
26     }
27
28     public void execute() throws IOException {
29         StringBuilder command = new StringBuilder();
30         StringBuilder response = new StringBuilder();
31         while(true) {
32             // Read command
33             while(true) {
34                 String line = reader.readLine();
35                 if(line == null) {
36                     execute(command.toString(), response.toString());
37                     return;
38                 }
39                 else if(line.startsWith("> "))
40                     command.append(line.substring(2)).append('\n');
41                 else {
42                     response.append(line).append('\n');
43                     break;
44                 }
45             }
46             
47             // Read response
48             while(true) {
49                 String line = reader.readLine();
50                 if(line == null) {
51                     execute(command.toString(), response.toString());
52                     return;
53                 }
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');
59                     break;
60                 }
61                 else
62                     response.append(line).append('\n');
63             }
64         }
65     }
66
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() {
72                     
73                     @Override
74                     public void print(String text) {
75                         handler.print(text);
76                         if (!ignorePrints)
77                             actualResponse.append(text).append('\n');
78                     }
79                     
80                     @Override
81                     public void printError(String error) {
82                         handler.printError(error);
83                         errors.append(error).append('\n');
84                     }
85                     
86                     @Override
87                     public void printCommand(String command) {
88                         handler.printCommand(command);
89                     }
90                 });
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);
96     }
97     
98 }