Convert AbstractReplInterpreterTest to Kotlin

This commit is contained in:
Alexander Udalov
2014-07-27 21:48:19 +02:00
parent 77417639ef
commit 9f718005b9
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 JetBrains s.r.o. * Copyright 2010-2014 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -14,115 +14,89 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.jet.repl; package org.jetbrains.jet.repl
import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.util.text.StringUtil; import com.intellij.testFramework.UsefulTestCase
import com.intellij.testFramework.UsefulTestCase; import org.jetbrains.jet.ConfigurationKind
import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetTestUtils
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.TestJdkKind
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.cli.jvm.repl.ReplInterpreter
import org.jetbrains.jet.TestJdkKind; import java.io.File
import org.jetbrains.jet.cli.jvm.repl.ReplInterpreter; import java.util.ArrayDeque
import org.jetbrains.jet.config.CompilerConfiguration; import java.util.ArrayList
import org.jetbrains.jet.utils.UtilsPackage; import java.util.regex.Pattern
import org.junit.Assert; import org.junit.Assert
import java.io.File; private val START_PATTERN = Pattern.compile(">>>( *)(.*)$")
import java.io.IOException; private val SUBSTRING_PATTERN = Pattern.compile("substring: (.*)")
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 abstract class AbstractReplInterpreterTest extends UsefulTestCase { public abstract class AbstractReplInterpreterTest : UsefulTestCase() {
private static final Pattern START_PATTERN = Pattern.compile(">>>( *)(.*)$"); {
private static final Pattern SUBSTRING_PATTERN = Pattern.compile("substring: (.*)"); System.setProperty("java.awt.headless", "true")
static {
System.setProperty("java.awt.headless", "true");
} }
private enum MatchType { private enum class MatchType {
EQUALS, EQUALS
SUBSTRING, SUBSTRING
} }
private static class OneLine { private data class OneLine(val code: String, val expected: String, val 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) { private fun loadLines(file: File): List<OneLine> {
this.code = code; val lines = ArrayDeque(file.readLines())
this.expected = expected;
this.matchType = matchType; val result = ArrayList<OneLine>()
while (lines.isNotEmpty()) {
val line = lines.poll()!!
val matcher = START_PATTERN.matcher(line)
assert(matcher.matches()) { "Line doesn't match start pattern: $line" }
val code = matcher.group(2)!!
if (lines.isNotEmpty()) {
val substringMatcher = SUBSTRING_PATTERN.matcher(lines.peek()!!)
if (substringMatcher.matches()) {
result.add(OneLine(code, substringMatcher.group(1)!!, MatchType.SUBSTRING))
lines.poll()
continue
}
}
val value = StringBuilder()
while (lines.isNotEmpty() && !START_PATTERN.matcher(lines.peek()!!).matches()) {
value.appendln(lines.poll()!!)
}
result.add(OneLine(code, value.toString(), MatchType.EQUALS))
} }
return result
} }
@NotNull protected fun doTest(path: String) {
private static List<OneLine> loadLines(@NotNull File file) { val configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.FULL_JDK)
Queue<String> lines; val repl = ReplInterpreter(getTestRootDisposable()!!, configuration)
try {
lines = new ArrayDeque<String>(FileUtil.loadLines(file));
}
catch (IOException e) {
throw UtilsPackage.rethrow(e);
}
List<OneLine> result = new ArrayList<OneLine>(); for ((code, expected, matchType) in loadLines(File(path))) {
val expectedString = StringUtil.convertLineSeparators(expected).replaceFirst("\n$", "")
while (!lines.isEmpty()) { val lineResult = repl.eval(code)
String line = lines.poll(); val actual = when (lineResult.getType()) {
Matcher matcher = START_PATTERN.matcher(line); ReplInterpreter.LineResultType.SUCCESS -> lineResult.getValue()?.toString() ?: ""
assert matcher.matches() : "Line doesn't match start pattern: " + line; ReplInterpreter.LineResultType.ERROR -> lineResult.getErrorText()
String code = matcher.group(2); ReplInterpreter.LineResultType.INCOMPLETE -> "incomplete"
Matcher substringMatcher = SUBSTRING_PATTERN.matcher(lines.peek());
if (substringMatcher.matches()) {
result.add(new OneLine(code, substringMatcher.group(1), MatchType.SUBSTRING));
lines.poll();
continue;
} }
val actualString = StringUtil.convertLineSeparators(actual).replaceFirst("\n$", "")
StringBuilder value = new StringBuilder(); when (matchType) {
while (!lines.isEmpty() && !START_PATTERN.matcher(lines.peek()).matches()) { MatchType.EQUALS -> {
value.append(lines.poll()).append("\n"); Assert.assertEquals("After evaluation of: $code", expectedString, actualString)
} }
MatchType.SUBSTRING -> {
result.add(new OneLine(code, value.toString(), MatchType.EQUALS)); Assert.assertTrue("Evaluated result must contain substring: $expectedString, actual: $actualString, line: $code",
} expectedString in actualString)
}
return result;
}
protected void doTest(@NotNull String path) {
CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.FULL_JDK);
ReplInterpreter repl = new ReplInterpreter(getTestRootDisposable(), configuration);
for (OneLine line : loadLines(new File(path))) {
String expected = StringUtil.convertLineSeparators(line.expected).replaceFirst("\n$", "");
ReplInterpreter.LineResult lineResult = repl.eval(line.code);
Object actual;
if (lineResult.getType() == ReplInterpreter.LineResultType.SUCCESS) {
actual = lineResult.getValue();
}
else if (lineResult.getType() == ReplInterpreter.LineResultType.INCOMPLETE) {
actual = "incomplete";
}
else {
actual = lineResult.getErrorText();
}
String actualString = StringUtil.convertLineSeparators(actual != null ? actual.toString() : "").replaceFirst("\n$", "");
if (line.matchType == MatchType.EQUALS) {
Assert.assertEquals("after evaluation of: " + line.code, expected, actualString);
}
else if (line.matchType == MatchType.SUBSTRING) {
Assert.assertTrue("must contain substring: " + expected + ", actual: " + actualString, actualString.contains(expected));
} }
} }
} }