test for repl evaluation exception

This commit is contained in:
Stepan Koltsov
2012-06-14 18:30:49 +04:00
parent 970c2ae1f9
commit 1c306eefd8
3 changed files with 65 additions and 14 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
>>> throw Exception("hi there")
stack trace
substring: Exception: hi there
>>> fun foo() = 2
null
>>> foo()
@@ -56,9 +56,10 @@ public class ReplInterpreterTest {
ReplInterpreter repl = new ReplInterpreter(disposable, compilerDependencies, Collections.singletonList(new File("out/production/runtime")));
ReplSessionTestFile file = ReplSessionTestFile.load(new File("compiler/testData/repl/" + relativePath));
for (Pair<String, String> t : file.getLines()) {
String code = t.first;
String expected = t.second.replaceFirst("\r?\n$", "");
for (ReplSessionTestFile.OneLine t : file.getLines()) {
String code = t.getCode();
String expected = t.getExpected().replaceFirst("\r?\n$", "");
ReplSessionTestFile.MatchType matchType = t.getMatchType();
ReplInterpreter.LineResult lineResult = repl.eval(code);
Object actual;
@@ -70,7 +71,13 @@ public class ReplInterpreterTest {
}
String actualString = (actual != null ? actual.toString() : "null").replaceFirst("\r?\n$", "");
Assert.assertEquals("after evaluation of: " + code, expected, actualString);
if (matchType == ReplSessionTestFile.MatchType.EQUALS) {
Assert.assertEquals("after evaluation of: " + code, expected, actualString);
}
else if (matchType == ReplSessionTestFile.MatchType.SUBSTRING) {
Assert.assertTrue("must contain substring: " + expected + ", actual: " + actualString, actualString.contains(expected));
}
}
}
@@ -133,8 +140,7 @@ public class ReplInterpreterTest {
@Test
public void evaluationErrors() {
// TODO
//testFile("evaluationErrors.repl");
testFile("evaluationErrors.repl");
}
@@ -36,15 +36,50 @@ import java.util.regex.Pattern;
*/
public class ReplSessionTestFile {
@NotNull
private final List<Pair<String, String>> lines;
public enum MatchType {
EQUALS,
SUBSTRING,
}
public ReplSessionTestFile(@NotNull List<Pair<String, String>> lines) {
public static class OneLine {
@NotNull
private final String code;
@NotNull
private final String expected;
@NotNull
private final MatchType matchType;
public OneLine(@NotNull String code, @NotNull String expected, @NotNull MatchType matchType) {
this.code = code;
this.expected = expected;
this.matchType = matchType;
}
@NotNull
public String getCode() {
return code;
}
@NotNull
public String getExpected() {
return expected;
}
@NotNull
public MatchType getMatchType() {
return matchType;
}
}
@NotNull
private final List<OneLine> lines;
public ReplSessionTestFile(@NotNull List<OneLine> lines) {
this.lines = lines;
}
@NotNull
public List<Pair<String, String>> getLines() {
public List<OneLine> getLines() {
return lines;
}
@@ -64,19 +99,29 @@ public class ReplSessionTestFile {
}
private static ReplSessionTestFile load(@NotNull SimpleLinesParser parser) throws IOException {
List<Pair<String, String>> list = Lists.newArrayList();
List<OneLine> list = Lists.newArrayList();
Pattern startPattern = Pattern.compile(">>>( |$)(.*)");
Pattern substringPattern = Pattern.compile("substring: (.*)");
while (!parser.lookingAtEof()) {
Pattern startPattern = Pattern.compile(">>>( |$)(.*)");
Matcher matcher = parser.next(startPattern);
String code = matcher.group(2);
StringBuilder value = new StringBuilder();
Matcher substringMatcher = parser.lookingAt(substringPattern);
if (substringMatcher != null) {
list.add(new OneLine(code, substringMatcher.group(1), MatchType.SUBSTRING));
parser.next();
continue;
}
while (!parser.lookingAtEof() && parser.lookingAt(startPattern) == null) {
value.append(parser.next()).append("\n");
}
list.add(Pair.create(code, value.toString()));
list.add(new OneLine(code, value.toString(), MatchType.EQUALS));
}
return new ReplSessionTestFile(list);
}