Refactor REPL tests
Drop SimpleLinesParser, simplify logic
This commit is contained in:
@@ -28,6 +28,9 @@ import org.junit.Assert;
|
|||||||
|
|
||||||
import java.io.File;
|
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 {
|
public abstract class AbstractReplInterpreterTest extends UsefulTestCase {
|
||||||
static {
|
static {
|
||||||
System.setProperty("java.awt.headless", "true");
|
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);
|
CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.FULL_JDK);
|
||||||
ReplInterpreter repl = new ReplInterpreter(getTestRootDisposable(), configuration);
|
ReplInterpreter repl = new ReplInterpreter(getTestRootDisposable(), configuration);
|
||||||
|
|
||||||
ReplSessionTestFile file = ReplSessionTestFile.load(new File(path));
|
for (ReplSessionTestFile.OneLine line : ReplSessionTestFile.load(new File(path))) {
|
||||||
for (ReplSessionTestFile.OneLine line : file.getLines()) {
|
String expected = StringUtil.convertLineSeparators(line.expected).replaceFirst("\n$", "");
|
||||||
String code = line.getCode();
|
|
||||||
|
|
||||||
String expected = StringUtil.convertLineSeparators(line.getExpected()).replaceFirst("\n$", "");
|
ReplInterpreter.LineResult lineResult = repl.eval(line.code);
|
||||||
ReplSessionTestFile.MatchType matchType = line.getMatchType();
|
|
||||||
|
|
||||||
ReplInterpreter.LineResult lineResult = repl.eval(code);
|
|
||||||
Object actual;
|
Object actual;
|
||||||
if (lineResult.getType() == ReplInterpreter.LineResultType.SUCCESS) {
|
if (lineResult.getType() == ReplInterpreter.LineResultType.SUCCESS) {
|
||||||
actual = lineResult.getValue();
|
actual = lineResult.getValue();
|
||||||
@@ -57,10 +56,10 @@ public abstract class AbstractReplInterpreterTest extends UsefulTestCase {
|
|||||||
}
|
}
|
||||||
String actualString = StringUtil.convertLineSeparators(actual != null ? actual.toString() : "null").replaceFirst("\n$", "");
|
String actualString = StringUtil.convertLineSeparators(actual != null ? actual.toString() : "null").replaceFirst("\n$", "");
|
||||||
|
|
||||||
if (matchType == ReplSessionTestFile.MatchType.EQUALS) {
|
if (line.matchType == EQUALS) {
|
||||||
Assert.assertEquals("after evaluation of: " + code, expected, actualString);
|
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));
|
Assert.assertTrue("must contain substring: " + expected + ", actual: " + actualString, actualString.contains(expected));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,22 +16,22 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.repl;
|
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 com.intellij.openapi.util.io.FileUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.utils.UtilsPackage;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class ReplSessionTestFile {
|
public class ReplSessionTestFile {
|
||||||
|
private static final Pattern START_PATTERN = Pattern.compile(">>>( *)(.*)$");
|
||||||
|
private static final Pattern SUBSTRING_PATTERN = Pattern.compile("substring: (.*)");
|
||||||
|
|
||||||
public enum MatchType {
|
public enum MatchType {
|
||||||
EQUALS,
|
EQUALS,
|
||||||
@@ -39,88 +39,50 @@ public class ReplSessionTestFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class OneLine {
|
public static class OneLine {
|
||||||
@NotNull
|
public final String code;
|
||||||
private final String code;
|
public final String expected;
|
||||||
@NotNull
|
public final MatchType matchType;
|
||||||
private final String expected;
|
|
||||||
@NotNull
|
|
||||||
private final MatchType matchType;
|
|
||||||
|
|
||||||
public OneLine(@NotNull String code, @NotNull String expected, @NotNull MatchType matchType) {
|
public OneLine(@NotNull String code, @NotNull String expected, @NotNull MatchType matchType) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.expected = expected;
|
this.expected = expected;
|
||||||
this.matchType = matchType;
|
this.matchType = matchType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public String getCode() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public String getExpected() {
|
|
||||||
return expected;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public MatchType getMatchType() {
|
|
||||||
return matchType;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final List<OneLine> lines;
|
public static List<OneLine> load(@NotNull File file) {
|
||||||
|
Queue<String> lines;
|
||||||
public ReplSessionTestFile(@NotNull List<OneLine> lines) {
|
|
||||||
this.lines = lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public List<OneLine> getLines() {
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ReplSessionTestFile load(@NotNull File file) {
|
|
||||||
try {
|
try {
|
||||||
FileInputStream inputStream = new FileInputStream(file);
|
lines = new ArrayDeque<String>(FileUtil.loadLines(file));
|
||||||
try {
|
}
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
|
catch (IOException e) {
|
||||||
List<String> lines = CharStreams.readLines(reader);
|
throw UtilsPackage.rethrow(e);
|
||||||
return load(new SimpleLinesParser(lines));
|
|
||||||
} finally {
|
|
||||||
inputStream.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static ReplSessionTestFile load(@NotNull SimpleLinesParser parser) throws IOException {
|
List<OneLine> result = new ArrayList<OneLine>();
|
||||||
List<OneLine> list = Lists.newArrayList();
|
|
||||||
|
|
||||||
Pattern startPattern = Pattern.compile(">>>( |$)(.*)");
|
while (!lines.isEmpty()) {
|
||||||
Pattern substringPattern = Pattern.compile("substring: (.*)");
|
String line = lines.poll();
|
||||||
|
Matcher matcher = START_PATTERN.matcher(line);
|
||||||
while (!parser.lookingAtEof()) {
|
assert matcher.matches() : "Line doesn't match start pattern: " + line;
|
||||||
Matcher matcher = parser.next(startPattern);
|
|
||||||
String code = matcher.group(2);
|
String code = matcher.group(2);
|
||||||
|
|
||||||
StringBuilder value = new StringBuilder();
|
Matcher substringMatcher = SUBSTRING_PATTERN.matcher(lines.peek());
|
||||||
|
if (substringMatcher.matches()) {
|
||||||
Matcher substringMatcher = parser.lookingAt(substringPattern);
|
result.add(new OneLine(code, substringMatcher.group(1), MatchType.SUBSTRING));
|
||||||
if (substringMatcher != null) {
|
lines.poll();
|
||||||
list.add(new OneLine(code, substringMatcher.group(1), MatchType.SUBSTRING));
|
|
||||||
parser.next();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!parser.lookingAtEof() && parser.lookingAt(startPattern) == null) {
|
StringBuilder value = new StringBuilder();
|
||||||
value.append(parser.next()).append("\n");
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user