]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/TestScriptExecutor.java
ab39d538a23a4f808440855956ea15a5b8e02d40
[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     
16     public TestScriptExecutor(CommandSession session, BufferedReader reader, SCLReportingHandler handler) {
17         this.session = session;
18         this.reader = reader;
19         this.handler = handler == null ? SCLReportingHandler.DEFAULT : handler;
20     }
21
22     public void execute() throws IOException {
23         StringBuilder command = new StringBuilder();
24         StringBuilder response = new StringBuilder();
25         while(true) {
26             // Read command
27             while(true) {
28                 String line = reader.readLine();
29                 if(line == null) {
30                     execute(command.toString(), response.toString());
31                     return;
32                 }
33                 else if(line.startsWith("> "))
34                     command.append(line.substring(2)).append('\n');
35                 else {
36                     response.append(line).append('\n');
37                     break;
38                 }
39             }
40             
41             // Read response
42             while(true) {
43                 String line = reader.readLine();
44                 if(line == null) {
45                     execute(command.toString(), response.toString());
46                     return;
47                 }
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');
53                     break;
54                 }
55                 else
56                     response.append(line).append('\n');
57             }
58         }
59     }
60
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() {
66                     
67                     @Override
68                     public void print(String text) {
69                         handler.print(text);
70                         actualResponse.append(text).append('\n');
71                     }
72                     
73                     @Override
74                     public void printError(String error) {
75                         handler.printError(error);
76                         errors.append(error).append('\n');
77                     }
78                     
79                     @Override
80                     public void printCommand(String command) {
81                         handler.printCommand(command);
82                     }
83                 });
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);
89     }
90     
91 }