J2K: AbstractBytecodeTextTest.java convert
(cherry picked from commit 93fce11)
This commit is contained in:
committed by
Nikolay Krasko
parent
5a873432d9
commit
32dff799ff
@@ -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<TestFile> files, @Nullable File javaFilesDir) throws Exception {
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, files, javaFilesDir);
|
||||
loadMultiFiles(files);
|
||||
@Throws(Exception::class)
|
||||
override fun doMultiFileTest(wholeFile: File, files: List<CodegenTestCase.TestFile>, javaFilesDir: File?) {
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, files, javaFilesDir)
|
||||
loadMultiFiles(files)
|
||||
|
||||
if (isMultiFileTest(files)) {
|
||||
doTestMultiFile(files);
|
||||
doTestMultiFile(files)
|
||||
}
|
||||
else {
|
||||
List<OccurrenceInfo> 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<TestFile> 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<OccurrenceInfo> 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<CodegenTestCase.TestFile>) {
|
||||
val expectedOccurrencesByOutputFile = LinkedHashMap<String, List<OccurrenceInfo>>()
|
||||
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<TestFile> files) throws Exception {
|
||||
Map<String, List<OccurrenceInfo>> expectedOccurrencesByOutputFile = new LinkedHashMap<String, List<OccurrenceInfo>>();
|
||||
for (TestFile file : files) {
|
||||
readExpectedOccurrencesForMultiFileTest(file, expectedOccurrencesByOutputFile);
|
||||
}
|
||||
@Throws(Exception::class)
|
||||
protected fun readExpectedOccurrences(filename: String): List<OccurrenceInfo> {
|
||||
val result = ArrayList<OccurrenceInfo>()
|
||||
val lines = FileUtil.loadFile(File(filename), Charsets.UTF_8.name(), true).split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
||||
|
||||
Map<String, String> generated = generateEachFileToText();
|
||||
for (String expectedOutputFile : expectedOccurrencesByOutputFile.keySet()) {
|
||||
assertTextWasGenerated(expectedOutputFile, generated);
|
||||
String generatedText = generated.get(expectedOutputFile);
|
||||
List<OccurrenceInfo> expectedOccurrences = expectedOccurrencesByOutputFile.get(expectedOutputFile);
|
||||
checkGeneratedTextAgainstExpectedOccurrences(generatedText, expectedOccurrences);
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertTextWasGenerated(String expectedOutputFile, Map<String, String> 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<OccurrenceInfo> readExpectedOccurrences(@NotNull String filename) throws Exception {
|
||||
List<OccurrenceInfo> result = new ArrayList<OccurrenceInfo>();
|
||||
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<String, List<OccurrenceInfo>> occurrenceMap
|
||||
) {
|
||||
List<OccurrenceInfo> 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<CodegenTestCase.TestFile>): Boolean {
|
||||
var kotlinFiles = 0
|
||||
for (file in files) {
|
||||
if (file.name.endsWith(".kt")) {
|
||||
kotlinFiles++
|
||||
}
|
||||
currentOccurrenceInfos = new ArrayList<OccurrenceInfo>();
|
||||
occurrenceMap.put(outputFileName, currentOccurrenceInfos);
|
||||
}
|
||||
return kotlinFiles > 1
|
||||
}
|
||||
|
||||
protected fun checkGeneratedTextAgainstExpectedOccurrences(
|
||||
text: String,
|
||||
expectedOccurrences: List<OccurrenceInfo>) {
|
||||
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 '// @<OUTPUT_FILE_NAME>:' 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<String, String>) {
|
||||
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<String, List<OccurrenceInfo>>) {
|
||||
var currentOccurrenceInfos: MutableList<OccurrenceInfo>? = 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<OccurrenceInfo>()
|
||||
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 '// @<OUTPUT_FILE_NAME>:' 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -78,6 +78,6 @@ public abstract class AbstractTopLevelMembersInvocationTest extends AbstractByte
|
||||
|
||||
List<OccurrenceInfo> expected = readExpectedOccurrences(KotlinTestUtils.getTestDataPathBase() + "/codegen/" + sourceFiles.get(0));
|
||||
String actual = generateToText();
|
||||
checkGeneratedTextAgainstExpectedOccurrences(actual, expected);
|
||||
Companion.checkGeneratedTextAgainstExpectedOccurrences(actual, expected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user