diff --git a/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractBytecodeTextTest.kt b/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractBytecodeTextTest.kt index 0b76ff27b05..e7c30e28f3f 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractBytecodeTextTest.kt +++ b/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractBytecodeTextTest.kt @@ -14,165 +14,155 @@ * limitations under the License. */ -package org.jetbrains.kotlin.codegen; +package org.jetbrains.kotlin.codegen -import org.jetbrains.annotations.NotNull -import org.jetbrains.annotations.Nullable +import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.util.text.StringUtil import org.jetbrains.kotlin.test.ConfigurationKind +import org.jetbrains.kotlin.utils.rethrow import java.io.File import java.util.* -import java.util.List -import java.util.Map import java.util.regex.Matcher import java.util.regex.Pattern -public abstract class AbstractBytecodeTextTest extends CodegenTestCase { - private static final Pattern AT_OUTPUT_FILE_PATTERN = Pattern.compile("^\\s*//\\s*@(.*):$"); - private static final Pattern EXPECTED_OCCURRENCES_PATTERN = Pattern.compile("^\\s*//\\s*(\\d+)\\s*(.*)$"); +abstract class AbstractBytecodeTextTest : CodegenTestCase() { - @Override - protected void doMultiFileTest(@NotNull File wholeFile, @NotNull List files, @Nullable File javaFilesDir) throws Exception { - createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, files, javaFilesDir); - loadMultiFiles(files); + @Throws(Exception::class) + override fun doMultiFileTest(wholeFile: File, files: List, javaFilesDir: File?) { + createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, files, javaFilesDir) + loadMultiFiles(files) if (isMultiFileTest(files)) { - doTestMultiFile(files); + doTestMultiFile(files) } else { - List expected = readExpectedOccurrences(wholeFile.getPath()); - String actual = generateToText(); - checkGeneratedTextAgainstExpectedOccurrences(actual, expected); + val expected = readExpectedOccurrences(wholeFile.path) + val actual = generateToText() + checkGeneratedTextAgainstExpectedOccurrences(actual, expected) } } - private static boolean isMultiFileTest(@NotNull List files) { - int kotlinFiles = 0; - for (TestFile file : files) { - if (file.name.endsWith(".kt")) { - kotlinFiles++; - } - } - return kotlinFiles > 1; - } - - protected static void checkGeneratedTextAgainstExpectedOccurrences( - @NotNull String text, - @NotNull List expectedOccurrences - ) { - StringBuilder expected = new StringBuilder(); - StringBuilder actual = new StringBuilder(); - - for (OccurrenceInfo info : expectedOccurrences) { - expected.append(info).append("\n"); - actual.append(info.getActualOccurrence(text)).append("\n"); + @Throws(Exception::class) + private fun doTestMultiFile(files: List) { + val expectedOccurrencesByOutputFile = LinkedHashMap>() + for (file in files) { + readExpectedOccurrencesForMultiFileTest(file, expectedOccurrencesByOutputFile) } - try { - assertEquals(text, expected.toString(), actual.toString()); - } - catch (Throwable e) { - System.out.println(text); - throw ExceptionUtilsKt.rethrow(e); + val generated = generateEachFileToText() + for (expectedOutputFile in expectedOccurrencesByOutputFile.keys) { + assertTextWasGenerated(expectedOutputFile, generated) + val generatedText = generated[expectedOutputFile] + val expectedOccurrences = expectedOccurrencesByOutputFile[expectedOutputFile] + checkGeneratedTextAgainstExpectedOccurrences(generatedText, expectedOccurrences) } } - private void doTestMultiFile(@NotNull List files) throws Exception { - Map> expectedOccurrencesByOutputFile = new LinkedHashMap>(); - for (TestFile file : files) { - readExpectedOccurrencesForMultiFileTest(file, expectedOccurrencesByOutputFile); - } + @Throws(Exception::class) + protected fun readExpectedOccurrences(filename: String): List { + val result = ArrayList() + val lines = FileUtil.loadFile(File(filename), Charsets.UTF_8.name(), true).split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - Map generated = generateEachFileToText(); - for (String expectedOutputFile : expectedOccurrencesByOutputFile.keySet()) { - assertTextWasGenerated(expectedOutputFile, generated); - String generatedText = generated.get(expectedOutputFile); - List expectedOccurrences = expectedOccurrencesByOutputFile.get(expectedOutputFile); - checkGeneratedTextAgainstExpectedOccurrences(generatedText, expectedOccurrences); - } - } - - private static void assertTextWasGenerated(String expectedOutputFile, Map generated) { - if (!generated.containsKey(expectedOutputFile)) { - StringBuilder failMessage = new StringBuilder(); - failMessage.append("Missing output file ").append(expectedOutputFile).append(", got ").append(generated.size()).append(": "); - for (String generatedFile : generated.keySet()) { - failMessage.append(generatedFile).append(" "); - } - fail(failMessage.toString()); - } - } - - @NotNull - protected List readExpectedOccurrences(@NotNull String filename) throws Exception { - List result = new ArrayList(); - String[] lines = FileUtil.loadFile(new File(filename), Charsets.UTF_8.name(), true).split("\n"); - - for (String line : lines) { - Matcher matcher = EXPECTED_OCCURRENCES_PATTERN.matcher(line); + for (line in lines) { + val matcher = EXPECTED_OCCURRENCES_PATTERN.matcher(line) if (matcher.matches()) { - result.add(parseOccurrenceInfo(matcher)); + result.add(parseOccurrenceInfo(matcher)) } } - return result; + return result } - private static void readExpectedOccurrencesForMultiFileTest( - @NotNull TestFile file, - @NotNull Map> occurrenceMap - ) { - List currentOccurrenceInfos = null; - for (String line : file.content.split("\n")) { - Matcher atOutputFileMatcher = AT_OUTPUT_FILE_PATTERN.matcher(line); - if (atOutputFileMatcher.matches()) { - String outputFileName = atOutputFileMatcher.group(1); - if (occurrenceMap.containsKey(outputFileName)) { - throw new AssertionError( - file.name + ": Expected occurrences for output file " + outputFileName + " were already provided" - ); + protected class OccurrenceInfo private constructor(private val numberOfOccurrences: Int, private val needle: String) { + + fun getActualOccurrence(text: String): String? { + val actualCount = StringUtil.findMatches(text, Pattern.compile("($needle)")).size + return actualCount + " " + needle + } + + override fun toString(): String { + return numberOfOccurrences + " " + needle + } + } + + companion object { + private val AT_OUTPUT_FILE_PATTERN = Pattern.compile("^\\s*//\\s*@(.*):$") + private val EXPECTED_OCCURRENCES_PATTERN = Pattern.compile("^\\s*//\\s*(\\d+)\\s*(.*)$") + + private fun isMultiFileTest(files: List): Boolean { + var kotlinFiles = 0 + for (file in files) { + if (file.name.endsWith(".kt")) { + kotlinFiles++ } - currentOccurrenceInfos = new ArrayList(); - occurrenceMap.put(outputFileName, currentOccurrenceInfos); + } + return kotlinFiles > 1 + } + + protected fun checkGeneratedTextAgainstExpectedOccurrences( + text: String, + expectedOccurrences: List) { + val expected = StringBuilder() + val actual = StringBuilder() + + for (info in expectedOccurrences) { + expected.append(info).append("\n") + actual.append(info.getActualOccurrence(text)).append("\n") } - Matcher expectedOccurrencesMatcher = EXPECTED_OCCURRENCES_PATTERN.matcher(line); - if (expectedOccurrencesMatcher.matches()) { - if (currentOccurrenceInfos == null) { - throw new AssertionError( - file.name + ": Should specify output file with '// @:' before expectations" - ); + try { + TestCase.assertEquals(text, expected.toString(), actual.toString()) + } + catch (e: Throwable) { + println(text) + throw rethrow(e) + } + + } + + private fun assertTextWasGenerated(expectedOutputFile: String, generated: Map) { + if (!generated.containsKey(expectedOutputFile)) { + val failMessage = StringBuilder() + failMessage.append("Missing output file ").append(expectedOutputFile).append(", got ").append(generated.size).append(": ") + for (generatedFile in generated.keys) { + failMessage.append(generatedFile).append(" ") } - OccurrenceInfo occurrenceInfo = parseOccurrenceInfo(expectedOccurrencesMatcher); - currentOccurrenceInfos.add(occurrenceInfo); + TestCase.fail(failMessage.toString()) } } - } - @NotNull - private static OccurrenceInfo parseOccurrenceInfo(Matcher matcher) { - int numberOfOccurrences = Integer.parseInt(matcher.group(1)); - String needle = matcher.group(2); - return new OccurrenceInfo(numberOfOccurrences, needle); - } + private fun readExpectedOccurrencesForMultiFileTest( + file: CodegenTestCase.TestFile, + occurrenceMap: MutableMap>) { + var currentOccurrenceInfos: MutableList? = null + for (line in file.content.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) { + val atOutputFileMatcher = AT_OUTPUT_FILE_PATTERN.matcher(line) + if (atOutputFileMatcher.matches()) { + val outputFileName = atOutputFileMatcher.group(1) + if (occurrenceMap.containsKey(outputFileName)) { + throw AssertionError( + file.name + ": Expected occurrences for output file " + outputFileName + " were already provided") + } + currentOccurrenceInfos = ArrayList() + occurrenceMap.put(outputFileName, currentOccurrenceInfos) + } - protected static class OccurrenceInfo { - private final int numberOfOccurrences; - private final String needle; - - private OccurrenceInfo(int numberOfOccurrences, @NotNull String needle) { - this.numberOfOccurrences = numberOfOccurrences; - this.needle = needle; + val expectedOccurrencesMatcher = EXPECTED_OCCURRENCES_PATTERN.matcher(line) + if (expectedOccurrencesMatcher.matches()) { + if (currentOccurrenceInfos == null) { + throw AssertionError( + file.name + ": Should specify output file with '// @:' before expectations") + } + val occurrenceInfo = parseOccurrenceInfo(expectedOccurrencesMatcher) + currentOccurrenceInfos.add(occurrenceInfo) + } + } } - @Nullable - public String getActualOccurrence(String text) { - int actualCount = StringUtil.findMatches(text, Pattern.compile("(" + needle + ")")).size(); - return actualCount + " " + needle; - } - - @Override - public String toString() { - return numberOfOccurrences + " " + needle; + private fun parseOccurrenceInfo(matcher: Matcher): OccurrenceInfo { + val numberOfOccurrences = Integer.parseInt(matcher.group(1)) + val needle = matcher.group(2) + return OccurrenceInfo(numberOfOccurrences, needle) } } } diff --git a/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractTopLevelMembersInvocationTest.java b/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractTopLevelMembersInvocationTest.java index 27b47b0671a..9aa6a7dee01 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractTopLevelMembersInvocationTest.java +++ b/compiler/tests-common/org/jetbrains/kotlin/codegen/AbstractTopLevelMembersInvocationTest.java @@ -78,6 +78,6 @@ public abstract class AbstractTopLevelMembersInvocationTest extends AbstractByte List expected = readExpectedOccurrences(KotlinTestUtils.getTestDataPathBase() + "/codegen/" + sourceFiles.get(0)); String actual = generateToText(); - checkGeneratedTextAgainstExpectedOccurrences(actual, expected); + Companion.checkGeneratedTextAgainstExpectedOccurrences(actual, expected); } }