Granular test configurations

This commit is contained in:
Andrey Breslav
2012-01-27 18:33:46 +04:00
parent 0da39c8bd6
commit d74b79d9db
673 changed files with 113 additions and 4 deletions
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="idea-full" level="project" />
<orderEntry type="module" module-name="j2k" />
</component>
</module>
@@ -0,0 +1,157 @@
package org.jetbrains.jet.j2k;
import com.intellij.core.JavaCoreEnvironment;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
/**
* @author ignatov
*/
public class StandaloneJavaToKotlinConverterTest extends TestCase {
private final String myDataPath;
private final String myName;
@NotNull
private final JavaCoreEnvironment myJavaCoreEnvironment;
public StandaloneJavaToKotlinConverterTest(String dataPath, String name) {
myDataPath = dataPath;
myName = name;
myJavaCoreEnvironment = JavaToKotlinTranslator.setUpJavaCoreEnvironment();
}
@Override
protected void runTest() throws Throwable {
String javaPath = "j2k/testData" + File.separator + getTestFilePath();
String kotlinPath = javaPath.replace(".jav", ".kt");
final File kotlinFile = new File(kotlinPath);
if (!kotlinFile.exists()) {
FileUtil.writeToFile(kotlinFile, "");
}
final String expected = FileUtil.loadFile(kotlinFile);
final File javaFile = new File(javaPath);
final String javaCode = FileUtil.loadFile(javaFile);
String actual = "";
if (javaFile.getParent().endsWith("/expression")) {
actual = expressionToKotlin(javaCode);
}
else if (javaFile.getParent().endsWith("/statement")) {
actual = statementToKotlin(javaCode);
}
else if (javaFile.getParent().endsWith("/method")) {
actual = methodToKotlin(javaCode);
}
else if (javaFile.getParent().endsWith("/class")) {
actual = fileToKotlin(javaCode);
}
else if (javaFile.getParent().endsWith("/file")) {
actual = fileToKotlin(javaCode);
}
else if (javaFile.getParent().endsWith("/comp")) actual = fileToFileWithCompatibilityImport(javaCode);
assert !actual.isEmpty() : "Specify what is it: file, class, method, statement or expression";
final File tmp = new File(kotlinPath + ".tmp");
if (!expected.equals(actual)) FileUtil.writeToFile(tmp, actual);
if (expected.equals(actual) && tmp.exists()) //noinspection ResultOfMethodCallIgnored
{
tmp.delete();
}
Assert.assertEquals(expected, actual);
}
@NotNull
String getTestFilePath() {
return myDataPath + File.separator + myName + ".jav";
}
@NotNull
@Override
public String getName() {
return "test_" + myName;
}
@NotNull
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestCaseBuilder.suiteForDirectory("j2k/testData", "/ast", new TestCaseBuilder.NamedTestFactory() {
@NotNull
@Override
public Test createTest(@NotNull String dataPath, @NotNull String name) {
return new StandaloneJavaToKotlinConverterTest(dataPath, name);
}
}));
return suite;
}
@NotNull
private static String fileToFileWithCompatibilityImport(@NotNull String text) {
return JavaToKotlinTranslator.generateKotlinCodeWithCompatibilityImport(text);
}
@NotNull
private String fileToKotlin(@NotNull String text) {
return generateKotlinCode(JavaToKotlinTranslator.createFile(myJavaCoreEnvironment, text));
}
@NotNull
private static String generateKotlinCode(@Nullable PsiFile file) {
if (file != null && file instanceof PsiJavaFile) {
JavaToKotlinTranslator.setClassIdentifiers(file);
return prettify(Converter.elementToKotlin(file));
}
return "";
}
@NotNull
private String methodToKotlin(String text) throws IOException {
String result = fileToKotlin("final class C {" + text + "}")
.replaceAll("class C\\(\\) \\{", "");
result = result.substring(0, result.lastIndexOf("}"));
return prettify(result);
}
@NotNull
private String statementToKotlin(String text) throws Exception {
String result = methodToKotlin("void main() {" + text + "}");
int pos = result.lastIndexOf("}");
result = result.substring(0, pos).replaceFirst("fun main\\(\\) : Unit \\{", "");
return prettify(result);
}
@NotNull
private String expressionToKotlin(String code) throws Exception {
String result = statementToKotlin("Object o =" + code + "}");
result = result.replaceFirst("var o : Any\\? =", "");
return prettify(result);
}
@NotNull
private static String prettify(@Nullable String code) {
if (code == null) {
return "";
}
return code
.trim()
.replaceAll("\r\n", "\n")
.replaceAll(" \n", "\n")
.replaceAll("\n ", "\n")
.replaceAll("\n+", "\n")
.replaceAll(" +", " ")
.trim()
;
}
}
@@ -0,0 +1,94 @@
package org.jetbrains.jet.j2k;
import com.intellij.openapi.application.PathManager;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author ignatov
*/
abstract class TestCaseBuilder {
@NotNull
private static final FilenameFilter emptyFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return true;
}
};
@NotNull
public static String getTestDataPathBase() {
return "testData";
}
public static String getHomeDirectory() {
return new File(PathManager.getResourceRoot(TestCaseBuilder.class, "/org/jetbrains/jet/TestCaseBuilder.class")).getParentFile().getParentFile().getParent();
}
public interface NamedTestFactory {
@NotNull
Test createTest(@NotNull String dataPath, @NotNull String name);
}
@NotNull
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, @NotNull NamedTestFactory factory) {
return suiteForDirectory(baseDataDir, dataPath, true, emptyFilter, factory);
}
@NotNull
private static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, @NotNull final FilenameFilter filter, @NotNull NamedTestFactory factory) {
TestSuite suite = new TestSuite(dataPath);
final String extensionJava = ".jav";
final FilenameFilter extensionFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, @NotNull String name) {
return name.endsWith(extensionJava);
}
};
FilenameFilter resultFilter;
if (filter != emptyFilter) {
resultFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return extensionFilter.accept(file, s) && filter.accept(file, s);
}
};
}
else {
resultFilter = extensionFilter;
}
File dir = new File(baseDataDir + dataPath);
FileFilter dirFilter = new FileFilter() {
@Override
public boolean accept(@NotNull File pathname) {
return pathname.isDirectory();
}
};
if (recursive) {
File[] files = dir.listFiles(dirFilter);
assert files != null : dir;
List<File> subdirs = Arrays.asList(files);
Collections.sort(subdirs);
for (File subdir : subdirs) {
suite.addTest(suiteForDirectory(baseDataDir, dataPath + "/" + subdir.getName(), recursive, filter, factory));
}
}
List<File> files = Arrays.asList(dir.listFiles(resultFilter));
Collections.sort(files);
for (File file : files) {
String fileName = file.getName();
assert fileName != null;
suite.addTest(factory.createTest(dataPath, fileName.substring(0, fileName.length() - extensionJava.length())));
}
return suite;
}
}
@@ -0,0 +1,29 @@
package test;
import org.jetbrains.annotations.NotNull;
public class Test {
@NotNull String myStr = "String2";
public Test(@NotNull String str) {
myStr = str;
}
public void sout(@NotNull String str) {
System.out.println(str);
}
@NotNull
public String dummy(@NotNull String str) {
return str;
}
public void test() {
sout("String");
@NotNull String test = "String2";
sout(test);
sout(dummy(test));
new Test(test);
}
}
@@ -0,0 +1,20 @@
package test
public open class Test(str : String) {
var myStr : String? = "String2"
open public fun sout(str : String) : Unit {
System.out?.println(str)
}
open public fun dummy(str : String) : String {
return str
}
open public fun test() : Unit {
sout("String")
var test : String = "String2"
sout(test)
sout(dummy(test))
Test(test)
}
{
myStr = str
}
}
@@ -0,0 +1,6 @@
class Test {
String str;
{
str = "Ola";
}
}
@@ -0,0 +1,6 @@
open class Test() {
var str : String? = null
{
str = "Ola"
}
}
@@ -0,0 +1,6 @@
class Test {
String str;
static {
str = "Ola";
}
}
@@ -0,0 +1,8 @@
open class Test() {
var str : String? = null
class object {
{
str = "Ola"
}
}
}
@@ -0,0 +1 @@
myArray[myLibrary.calculateIndex(100)]
@@ -0,0 +1 @@
myArray[myLibrary.calculateIndex(100)]
@@ -0,0 +1 @@
myArray[10]
@@ -0,0 +1 @@
myArray[10]
@@ -0,0 +1 @@
myArray[i]
@@ -0,0 +1 @@
myArray[i]
@@ -0,0 +1 @@
new int[] {1, 2, 3};
@@ -0,0 +1 @@
intArray(1, 2, 3)
@@ -0,0 +1 @@
double[] a = new double[]{1.0, 2, 3}
@@ -0,0 +1 @@
var a : DoubleArray? = doubleArray(1.0, 2.0, 3.0)
@@ -0,0 +1,2 @@
double a = 0, b = 0, c = 0;
double ds[] = {a, b, c};
@@ -0,0 +1,4 @@
var a : Double = 0
var b : Double = 0
var c : Double = 0
var ds : DoubleArray? = doubleArray((a).dbl, (b).dbl, (c).dbl)
@@ -0,0 +1 @@
float[] a = new float[]{1, 2, 3.0}
@@ -0,0 +1 @@
var a : FloatArray? = floatArray(1.0, 2.0, 3.0)
@@ -0,0 +1 @@
java.lang.Double[] a = new java.lang.Double[]{1, 2, 3};
@@ -0,0 +1 @@
var a : Array<java.lang.Double?>? = array<java.lang.Double?>(1.0, 2.0, 3.0)
@@ -0,0 +1 @@
java.lang.Float[] a = new java.lang.Float[]{1, 2, 3};
@@ -0,0 +1 @@
var a : Array<java.lang.Float?>? = array<java.lang.Float?>(1.0, 2.0, 3.0)
@@ -0,0 +1 @@
byte[] a = new byte[] {1, 2, 3};
@@ -0,0 +1 @@
var a : ByteArray? = byteArray(1, 2, 3)
@@ -0,0 +1 @@
int[] a = {1, 2, 3};
@@ -0,0 +1 @@
var a : IntArray? = intArray(1, 2, 3)
@@ -0,0 +1,2 @@
int a = 0, b = 0, c = 0;
int is[] = {a, b, c};
@@ -0,0 +1,4 @@
var a : Int = 0
var b : Int = 0
var c : Int = 0
var `is` : IntArray? = intArray(a, b, c)
@@ -0,0 +1 @@
int[][] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
@@ -0,0 +1 @@
var a : Array<IntArray?>? = array<IntArray?>(intArray(1, 2, 3), intArray(4, 5, 6), intArray(7, 8, 9))
@@ -0,0 +1 @@
void fromArrayToCollection(Foo[] a) {}
@@ -0,0 +1,2 @@
fun fromArrayToCollection(a : Array<Foo?>?) : Unit {
}
@@ -0,0 +1 @@
int [][] d2 = new int[][]{};
@@ -0,0 +1 @@
var d2 : Array<IntArray?>? = array<IntArray?>()
@@ -0,0 +1 @@
int [][] d2 = new int[5][];
@@ -0,0 +1 @@
var d2 : Array<IntArray?>? = Array<IntArray?>(5)
@@ -0,0 +1 @@
int [][][] d3 = new int[5][5][];
@@ -0,0 +1 @@
var d3 : Array<Array<IntArray?>?>? = Array<Array<IntArray?>?>(5, {Array<IntArray?>(5)})
@@ -0,0 +1 @@
int [][] d2 = new int[5][5];
@@ -0,0 +1 @@
var d2 : Array<IntArray?>? = Array<IntArray?>(5, {IntArray(5)})
@@ -0,0 +1 @@
String [][] ss = new String[5][5];
@@ -0,0 +1 @@
var ss : Array<Array<String?>?>? = Array<Array<String?>?>(5, {Array<String?>(5)})
@@ -0,0 +1 @@
String [][][] sss = new String[5][5][5];
@@ -0,0 +1 @@
var sss : Array<Array<Array<String?>?>?>? = Array<Array<Array<String?>?>?>(5, {Array<Array<String?>?>(5, {Array<String?>(5)})})
@@ -0,0 +1 @@
long[] a = new long[]{1, 2, 3}
@@ -0,0 +1 @@
var a : LongArray? = longArray(1, 2, 3)
@@ -0,0 +1 @@
int[] a = new int[]{1, 2, 3}
@@ -0,0 +1 @@
var a : IntArray? = intArray(1, 2, 3)
@@ -0,0 +1 @@
String[] a = new String[]{"abc"}
@@ -0,0 +1 @@
var a : Array<String?>? = array<String?>("abc")
@@ -0,0 +1 @@
assert boolMethod();
@@ -0,0 +1 @@
assert {boolMethod()}
@@ -0,0 +1 @@
assert(boolMethod());
@@ -0,0 +1 @@
assert {(boolMethod())}
@@ -0,0 +1 @@
assert true : "string details";
@@ -0,0 +1 @@
assert("string details") {true}
@@ -0,0 +1 @@
x &= 2
@@ -0,0 +1 @@
x = x and 2
@@ -0,0 +1 @@
x = 2
@@ -0,0 +1 @@
x = 2
@@ -0,0 +1 @@
x /= 2
@@ -0,0 +1 @@
x /= 2
@@ -0,0 +1 @@
x -= 2
@@ -0,0 +1 @@
x -= 2
@@ -0,0 +1 @@
x |= 2
@@ -0,0 +1 @@
x = x or 2
@@ -0,0 +1 @@
x += 2
@@ -0,0 +1 @@
x += 2
@@ -0,0 +1 @@
x %= 2
@@ -0,0 +1 @@
x %= 2
@@ -0,0 +1 @@
x <<= 2
@@ -0,0 +1 @@
x = x shl 2
@@ -0,0 +1 @@
x >>= 2
@@ -0,0 +1 @@
x = x shr 2
@@ -0,0 +1 @@
x = x ushr 2
@@ -0,0 +1 @@
x ^= 2
@@ -0,0 +1 @@
x = x xor 2
@@ -0,0 +1 @@
x & 2
@@ -0,0 +1 @@
x and 2
@@ -0,0 +1 @@
true && false
@@ -0,0 +1 @@
true && false
@@ -0,0 +1 @@
true || false
@@ -0,0 +1 @@
true || false
@@ -0,0 +1 @@
1 / 2
@@ -0,0 +1 @@
1 / 2
@@ -0,0 +1 @@
1 > 2
@@ -0,0 +1 @@
1 > 2
@@ -0,0 +1 @@
1 >= 2
@@ -0,0 +1 @@
1 >= 2
@@ -0,0 +1 @@
1 < 2

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