Converter tests:

Use generated tests approach in converter tests

Introduce two seperate generated test cases of each of configurations, all tests are generated in both test cases
Test data for plugin configuration has extension "ide.kt", for basic configuration "*.kt"
Test data file is used to determine type of the test (file/class/expression/...) instead of directory, all test data is moved accordingly
Add abstract base classes for generated tests
Rename test folder "file" to "misc" (for the lack of imagination)
This commit is contained in:
Pavel V. Talanov
2013-11-16 16:30:24 +04:00
parent cba8d3b6db
commit 82b19499e3
1091 changed files with 7197 additions and 230 deletions
+1
View File
@@ -22,6 +22,7 @@
<orderEntry type="module" module-name="jps-plugin" scope="TEST" />
<orderEntry type="module" module-name="util" scope="TEST" />
<orderEntry type="library" scope="TEST" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="j2k-tests" scope="TEST" />
</component>
</module>
@@ -78,6 +78,8 @@ import org.jetbrains.jet.resolve.calls.AbstractResolvedCallsTest
import org.jetbrains.jet.plugin.refactoring.rename.AbstractRenameTest
import org.jetbrains.jet.generators.tests.generator.SingleClassTestModel
import org.jetbrains.jet.generators.tests.generator.TestClassModel
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterPluginTest
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterBasicTest
fun main(args: Array<String>) {
System.setProperty("java.awt.headless", "true")
@@ -413,6 +415,16 @@ fun main(args: Array<String>) {
model("dataFlowValueRendering")
}
}
testGroup("j2k/tests/test", "j2k/tests/testData") {
testClass(javaClass<AbstractJavaToKotlinConverterPluginTest>()) {
model("ast", extension = "jav")
}
testClass(javaClass<AbstractJavaToKotlinConverterBasicTest>()) {
model("ast", extension = "jav")
}
}
}
private class TestGroup(val testsRoot: String, val testDataRoot: String) {
@@ -21,27 +21,28 @@ import com.intellij.openapi.util.io.FileUtil
import junit.framework.Assert
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.PsiFile
import junit.framework.Test
import junit.framework.TestSuite
import java.io.FilenameFilter
import java.io.FileFilter
import java.util.Collections
import org.jetbrains.jet.j2k.Converter
import org.jetbrains.jet.j2k.JavaToKotlinTranslator
import org.jetbrains.jet.ConfigurationKind
import org.jetbrains.jet.JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations
import com.intellij.testFramework.UsefulTestCase
import org.jetbrains.jet.j2k.ConverterSettings
import org.jetbrains.jet.j2k.PluginSettings
import org.jetbrains.jet.j2k.TestSettings
import java.util.regex.Pattern
public abstract class StandaloneJavaToKotlinConverterTest(val dataPath: String,
val name: String,
val settings: ConverterSettings) : UsefulTestCase() {
public abstract class AbstractJavaToKotlinConverterPluginTest() : AbstractJavaToKotlinConverterTest("ide.kt", PluginSettings)
public abstract class AbstractJavaToKotlinConverterBasicTest() : AbstractJavaToKotlinConverterTest("kt", TestSettings)
protected override fun runTest(): Unit {
public abstract class AbstractJavaToKotlinConverterTest(val kotlinFileExtension: String,
val settings: ConverterSettings) : UsefulTestCase() {
val testHeaderPattern = Pattern.compile("//(expression|statement|method|class|file|comp)\n")
protected fun doTest(javaPath: String) {
val jetCoreEnvironment = createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), ConfigurationKind.JDK_ONLY)
val converter = Converter(jetCoreEnvironment.getProject(), settings)
val javaPath = "j2k/tests/testData/" + getTestFilePath()
val kotlinPath = javaPath.replace(".jav", ".kt")
val kotlinPath = javaPath.replace(".jav", ".$kotlinFileExtension")
val kotlinFile = File(kotlinPath)
if (!kotlinFile.exists()) {
FileUtil.writeToFile(kotlinFile, "")
@@ -49,18 +50,20 @@ public abstract class StandaloneJavaToKotlinConverterTest(val dataPath: String,
val expected = FileUtil.loadFile(kotlinFile, true)
val javaFile = File(javaPath)
val javaCode = FileUtil.loadFile(javaFile, true)
val parentFileName = javaFile.getParentFile()?.getName()
val actual = when (parentFileName) {
val fileContents = FileUtil.loadFile(javaFile, true)
val matcher = testHeaderPattern.matcher(fileContents)
matcher.find()
val prefix = matcher.group().trim().substring(2)
val javaCode = matcher.replaceFirst("")
val actual = when (prefix) {
"expression" -> expressionToKotlin(converter, javaCode)
"statement" -> statementToKotlin(converter, javaCode)
"method" -> methodToKotlin(converter, javaCode)
"class" -> fileToKotlin(converter, javaCode)
"file" -> fileToKotlin(converter, javaCode)
"comp" -> fileToFileWithCompatibilityImport(javaCode)
else -> throw IllegalStateException("Specify what is it: file, class, method, statement or expression:" +
"$javaPath parent: $parentFileName")
else -> throw IllegalStateException("Specify what is it: file, class, method, statement or expression "+
"using the first line of test data file")
}
val tmp = File(kotlinPath + ".tmp")
@@ -75,10 +78,6 @@ public abstract class StandaloneJavaToKotlinConverterTest(val dataPath: String,
Assert.assertEquals(expected, actual)
}
fun getTestFilePath(): String {
return "$dataPath/$name.jav"
}
private fun fileToKotlin(converter: Converter, text: String): String {
return generateKotlinCode(converter, JavaToKotlinTranslator.createFile(converter.project, text))
}
@@ -98,7 +97,7 @@ public abstract class StandaloneJavaToKotlinConverterTest(val dataPath: String,
private fun expressionToKotlin(converter: Converter, code: String?): String {
var result = statementToKotlin(converter, "final Object o =" + code + "}")
result = result.replaceFirst("val o : Any\\? =", "").replaceFirst("val o : Any = ", "")
result = result.replaceFirst("val o : Any\\? =", "").replaceFirst("val o : Any = ", "").replaceFirst("val o = ", "")
return prettify(result)
}
@@ -122,65 +121,4 @@ public abstract class StandaloneJavaToKotlinConverterTest(val dataPath: String,
return code.trim().replaceAll("\r\n", "\n").replaceAll(" \n", "\n").replaceAll("\n ", "\n").replaceAll("\n+", "\n").replaceAll(" +", " ").trim()
}
}
private val emptyFilter = object : FilenameFilter {
public override fun accept(dir: File, name: String): Boolean {
return true
}
}
public trait NamedTestFactory {
fun createTest(dataPath: String, name: String): Test
}
public fun suiteForDirectory(baseDataDir: String?, dataPath: String, factory: NamedTestFactory): TestSuite {
return suiteForDirectory(baseDataDir, dataPath, true, emptyFilter, factory)
}
public fun suiteForDirectory(baseDataDir: String?, dataPath: String, recursive: Boolean, filter: FilenameFilter, factory: NamedTestFactory): TestSuite {
val suite = TestSuite(dataPath)
val extensionJava = ".jav"
val extensionFilter = object : FilenameFilter {
public override fun accept(dir: File, name: String): Boolean {
return name.endsWith(extensionJava)
}
}
val resultFilter =
if (filter != emptyFilter) {
object : FilenameFilter {
public override fun accept(dir: File, name: String): Boolean {
return (extensionFilter.accept(dir, name)) && filter.accept(dir, name)
}
}
}
else {
extensionFilter
}
val dir = File(baseDataDir + dataPath)
val dirFilter = object : FileFilter {
public override fun accept(pathname: File): Boolean {
return pathname.isDirectory()
}
}
if (recursive) {
val files = dir.listFiles(dirFilter)
val subdirs = files!!.toLinkedList()
Collections.sort(subdirs)
for (subdir in subdirs) {
suite.addTest(suiteForDirectory(baseDataDir, dataPath + "/" + subdir.getName(), recursive, filter, factory))
}
}
val files = (dir.listFiles(resultFilter))!!.toLinkedList()
Collections.sort(files)
for (file in files) {
val testName = file.getName().substring(0, (file.getName().length()) - (extensionJava.length()))
suite.addTest(factory.createTest(dataPath, testName))
}
return suite
}
}
@@ -1,46 +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.j2k.test;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jetbrains.jet.j2k.J2kPackage;
import static org.jetbrains.jet.j2k.test.TestPackage.suiteForDirectory;
public class BasicConverterTestSuite {
private BasicConverterTestSuite() {
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(suiteForDirectory("j2k/tests/testData", "/ast", new NamedTestFactory() {
@Override
public Test createTest(String dataPath, String name) {
//noinspection JUnitTestCaseWithNoTests
return new StandaloneJavaToKotlinConverterTest(dataPath, name, J2kPackage.getTestSettings()) {
@Override
protected void runTest() {
super.runTest();
}
};
}
}));
return suite;
}
}
@@ -1,46 +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.j2k.test;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jetbrains.jet.j2k.J2kPackage;
import static org.jetbrains.jet.j2k.test.TestPackage.suiteForDirectory;
public class ConverterTestSuiteForPlugin {
private ConverterTestSuiteForPlugin() {
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(suiteForDirectory("j2k/tests/testData", "/plugin", new NamedTestFactory() {
@Override
public Test createTest(String dataPath, String name) {
//noinspection JUnitTestCaseWithNoTests
return new StandaloneJavaToKotlinConverterTest(dataPath, name, J2kPackage.getPluginSettings()) {
@Override
protected void runTest() {
super.runTest();
}
};
}
}));
return suite;
}
}
@@ -0,0 +1,20 @@
package test
public open class Test(str : String) {
var myStr : String = "String2"
public open fun sout(str : String) : Unit {
System.out.println(str)
}
public open fun dummy(str : String) : String {
return str
}
public open fun test() : Unit {
sout("String")
val test = "String2"
sout(test)
sout(dummy(test))
Test(test)
}
{
myStr = str
}
}
@@ -1,3 +1,4 @@
//file
package test;
import org.jetbrains.annotations.NotNull;
@@ -0,0 +1,17 @@
package test
open class Foo() {
open fun execute() : Unit {
}
}
open class Bar() {
var fooNotNull : Foo = Foo()
var fooNullable : Foo = null
}
open class Test() {
public open fun test(barNotNull : Bar, barNullable : Bar) : Unit {
barNotNull.fooNotNull.execute()
barNotNull.fooNullable.execute()
barNullable.fooNotNull.execute()
barNullable.fooNullable.execute()
}
}
@@ -1,3 +1,4 @@
//file
package test;
import org.jetbrains.annotations.NotNull;
@@ -0,0 +1,6 @@
open class Test() {
var str : String = 0
{
str = "Ola"
}
}
@@ -0,0 +1,8 @@
open class Test() {
var str : String = 0
class object {
{
str = "Ola"
}
}
}
@@ -0,0 +1,2 @@
//expression
myArray[myLibrary.calculateIndex(100)]
@@ -0,0 +1,2 @@
//expression
myArray[10]
@@ -0,0 +1,2 @@
//expression
myArray[i]
@@ -0,0 +1 @@
val a = doubleArray(1.0, 2.0, 3.0)
@@ -0,0 +1,4 @@
val a = 0
val b = 0
val c = 0
val ds = doubleArray((a).toDouble(), (b).toDouble(), (c).toDouble())
@@ -0,0 +1 @@
val a = floatArray(1.0, 2.0, 3.0)
@@ -0,0 +1 @@
val a = IntArray(10)
@@ -0,0 +1 @@
val a = array<java.lang.Double>(1.0, 2.0, 3.0)
@@ -1 +1,2 @@
//statement
java.lang.Double[] a = new java.lang.Double[]{1, 2, 3};
@@ -0,0 +1 @@
val a = array<java.lang.Float>(1.0, 2.0, 3.0)
@@ -1 +1,2 @@
//statement
java.lang.Float[] a = new java.lang.Float[]{1, 2, 3};
@@ -0,0 +1 @@
val a = byteArray(1, 2, 3)
@@ -0,0 +1 @@
intArray(1, 2, 3)
@@ -0,0 +1 @@
val a = arrayOfNulls<Any>(10)
@@ -0,0 +1 @@
val a = intArray(1, 2, 3)
@@ -0,0 +1,4 @@
val a = 0
val b = 0
val c = 0
val `is` = intArray(a, b, c)
@@ -0,0 +1 @@
val a = array<IntArray>(intArray(1, 2, 3), intArray(4, 5, 6), intArray(7, 8, 9))
@@ -1 +1,2 @@
//statement
int[][] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
@@ -0,0 +1 @@
val d2 = array<IntArray>()
@@ -0,0 +1 @@
val d2 = arrayOfNulls<IntArray>(5)
@@ -0,0 +1 @@
val d3 = Array<Array<IntArray>>(5, {arrayOfNulls<IntArray>(5)})
@@ -0,0 +1 @@
val d2 = Array<IntArray>(5, {IntArray(5)})
@@ -0,0 +1 @@
val ss = Array<Array<String>>(5, {arrayOfNulls<String>(5)})
@@ -1 +1,2 @@
//statement
String [][] ss = new String[5][5];
@@ -0,0 +1 @@
val sss = Array<Array<Array<String>>>(5, {Array<Array<String>>(5, {arrayOfNulls<String>(5)})})
@@ -1 +1,2 @@
//statement
String [][][] sss = new String[5][5][5];
@@ -0,0 +1 @@
val a = longArray(1, 2, 3)
@@ -1 +1,2 @@
//statement
long[] a = new long[]{1, 2, 3}
@@ -0,0 +1,2 @@
fun fromArrayToCollection(a : Array<Foo>) : Unit {
}
@@ -1 +1,2 @@
//method
void fromArrayToCollection(Foo[] a) {}
@@ -0,0 +1 @@
val a = intArray(1, 2, 3)
@@ -1 +1,2 @@
//statement
int[] a = new int[]{1, 2, 3}
@@ -0,0 +1 @@
val a = array<String>("abc")
@@ -1 +1,2 @@
//statement
String[] a = new String[]{"abc"}
@@ -0,0 +1 @@
assert {boolMethod()}
@@ -0,0 +1 @@
assert {(boolMethod())}

Some files were not shown because too many files have changed in this diff Show More