Refactor REPL tests

Drop SimpleLinesParser, simplify logic
This commit is contained in:
Alexander Udalov
2014-07-27 17:39:52 +04:00
parent 125985c91d
commit f4b8874efa
3 changed files with 39 additions and 163 deletions
@@ -28,6 +28,9 @@ import org.junit.Assert;
import java.io.File;
import static org.jetbrains.jet.repl.ReplSessionTestFile.MatchType.EQUALS;
import static org.jetbrains.jet.repl.ReplSessionTestFile.MatchType.SUBSTRING;
public abstract class AbstractReplInterpreterTest extends UsefulTestCase {
static {
System.setProperty("java.awt.headless", "true");
@@ -37,14 +40,10 @@ public abstract class AbstractReplInterpreterTest extends UsefulTestCase {
CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.FULL_JDK);
ReplInterpreter repl = new ReplInterpreter(getTestRootDisposable(), configuration);
ReplSessionTestFile file = ReplSessionTestFile.load(new File(path));
for (ReplSessionTestFile.OneLine line : file.getLines()) {
String code = line.getCode();
for (ReplSessionTestFile.OneLine line : ReplSessionTestFile.load(new File(path))) {
String expected = StringUtil.convertLineSeparators(line.expected).replaceFirst("\n$", "");
String expected = StringUtil.convertLineSeparators(line.getExpected()).replaceFirst("\n$", "");
ReplSessionTestFile.MatchType matchType = line.getMatchType();
ReplInterpreter.LineResult lineResult = repl.eval(code);
ReplInterpreter.LineResult lineResult = repl.eval(line.code);
Object actual;
if (lineResult.getType() == ReplInterpreter.LineResultType.SUCCESS) {
actual = lineResult.getValue();
@@ -57,10 +56,10 @@ public abstract class AbstractReplInterpreterTest extends UsefulTestCase {
}
String actualString = StringUtil.convertLineSeparators(actual != null ? actual.toString() : "null").replaceFirst("\n$", "");
if (matchType == ReplSessionTestFile.MatchType.EQUALS) {
Assert.assertEquals("after evaluation of: " + code, expected, actualString);
if (line.matchType == EQUALS) {
Assert.assertEquals("after evaluation of: " + line.code, expected, actualString);
}
else if (matchType == ReplSessionTestFile.MatchType.SUBSTRING) {
else if (line.matchType == SUBSTRING) {
Assert.assertTrue("must contain substring: " + expected + ", actual: " + actualString, actualString.contains(expected));
}
}
@@ -16,22 +16,22 @@
package org.jetbrains.jet.repl;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.utils.UtilsPackage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReplSessionTestFile {
private static final Pattern START_PATTERN = Pattern.compile(">>>( *)(.*)$");
private static final Pattern SUBSTRING_PATTERN = Pattern.compile("substring: (.*)");
public enum MatchType {
EQUALS,
@@ -39,88 +39,50 @@ public class ReplSessionTestFile {
}
public static class OneLine {
@NotNull
private final String code;
@NotNull
private final String expected;
@NotNull
private final MatchType matchType;
public final String code;
public final String expected;
public 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<OneLine> getLines() {
return lines;
}
public static ReplSessionTestFile load(@NotNull File file) {
public static List<OneLine> load(@NotNull File file) {
Queue<String> lines;
try {
FileInputStream inputStream = new FileInputStream(file);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
List<String> lines = CharStreams.readLines(reader);
return load(new SimpleLinesParser(lines));
} finally {
inputStream.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
lines = new ArrayDeque<String>(FileUtil.loadLines(file));
}
catch (IOException e) {
throw UtilsPackage.rethrow(e);
}
}
private static ReplSessionTestFile load(@NotNull SimpleLinesParser parser) throws IOException {
List<OneLine> list = Lists.newArrayList();
List<OneLine> result = new ArrayList<OneLine>();
Pattern startPattern = Pattern.compile(">>>( |$)(.*)");
Pattern substringPattern = Pattern.compile("substring: (.*)");
while (!parser.lookingAtEof()) {
Matcher matcher = parser.next(startPattern);
while (!lines.isEmpty()) {
String line = lines.poll();
Matcher matcher = START_PATTERN.matcher(line);
assert matcher.matches() : "Line doesn't match start pattern: " + line;
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();
Matcher substringMatcher = SUBSTRING_PATTERN.matcher(lines.peek());
if (substringMatcher.matches()) {
result.add(new OneLine(code, substringMatcher.group(1), MatchType.SUBSTRING));
lines.poll();
continue;
}
while (!parser.lookingAtEof() && parser.lookingAt(startPattern) == null) {
value.append(parser.next()).append("\n");
StringBuilder value = new StringBuilder();
while (!lines.isEmpty() && !START_PATTERN.matcher(lines.peek()).matches()) {
value.append(lines.poll()).append("\n");
}
list.add(new OneLine(code, value.toString(), MatchType.EQUALS));
result.add(new OneLine(code, value.toString(), MatchType.EQUALS));
}
return new ReplSessionTestFile(list);
}
return result;
}
}
@@ -1,85 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.repl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @see org.jetbrains.jet.lang.types.ref.SimpleParser
*/
class SimpleLinesParser {
@NotNull
private final List<String> lines;
private int position;
SimpleLinesParser(@NotNull List<String> lines) {
this.lines = lines;
}
public boolean lookingAtEof() {
return position == lines.size();
}
public void checkNotEof() {
if (lookingAtEof()) {
throw new IllegalStateException("unexpected EOF");
}
}
@NotNull
public String lookahead() {
checkNotEof();
return lines.get(position);
}
@Nullable
public Matcher lookingAt(@NotNull Pattern pattern) {
if (lookingAtEof()) {
return null;
}
Matcher matcher = pattern.matcher(lookahead());
if (matcher.matches()) {
return matcher;
}
else {
return null;
}
}
@NotNull
public Matcher next(@NotNull Pattern pattern) {
Matcher r = lookingAt(pattern);
if (r == null) {
throw new IllegalStateException("line " + position + " must match " + pattern);
}
++position;
return r;
}
@NotNull
public String next() {
String r = lookahead();
++position;
return r;
}
}