Android tests: add expected result for box() method

This commit is contained in:
Natalia.Ukhorskaya
2012-09-13 13:20:22 +04:00
parent acfc5b3f56
commit 9e55fd219e
3 changed files with 40 additions and 17 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.jet.compiler.android;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.lang.String;
import java.lang.reflect.Method; import java.lang.reflect.Method;
/** /**
@@ -26,13 +27,13 @@ import java.lang.reflect.Method;
public class AbstractCodegenTestCaseOnAndroid extends TestCase { public class AbstractCodegenTestCaseOnAndroid extends TestCase {
protected void invokeBoxMethod(String filePath) throws Exception { protected void invokeBoxMethod(String filePath, String expectedResult) throws Exception {
try { try {
Class clazz; Class clazz;
clazz = Class.forName(filePath.replaceAll("\\\\|-|\\.|/", "_") + ".namespace"); clazz = Class.forName(filePath.replaceAll("\\\\|-|\\.|/", "_") + ".namespace");
Method method; Method method;
method = clazz.getMethod("box"); method = clazz.getMethod("box");
assertEquals("OK", method.invoke(null)); assertEquals(expectedResult, method.invoke(null));
} }
catch (Throwable e) { catch (Throwable e) {
throw new RuntimeException("File: " + filePath, e); throw new RuntimeException("File: " + filePath, e);
@@ -37,6 +37,7 @@ import org.jetbrains.jet.utils.Printer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -48,11 +49,11 @@ import java.util.regex.Pattern;
public class CodegenTestsOnAndroidGenerator extends UsefulTestCase { public class CodegenTestsOnAndroidGenerator extends UsefulTestCase {
private final PathManager pathManager; private final PathManager pathManager;
private final String testClassPackage = "org.jetbrains.jet.compiler.android"; private static final String testClassPackage = "org.jetbrains.jet.compiler.android";
private final String testClassName = "CodegenTestCaseOnAndroid"; private static final String testClassName = "CodegenTestCaseOnAndroid";
private final String baseTestClassPackage = "org.jetbrains.jet.compiler.android"; private static final String baseTestClassPackage = "org.jetbrains.jet.compiler.android";
private final String baseTestClassName = "AbstractCodegenTestCaseOnAndroid"; private static final String baseTestClassName = "AbstractCodegenTestCaseOnAndroid";
private final String generatorName = "CodegenTestsOnAndroidGenerator"; private static final String generatorName = "CodegenTestsOnAndroidGenerator";
private JetCoreEnvironment environmentWithMockJdk = JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(myTestRootDisposable, ConfigurationKind.JDK_AND_ANNOTATIONS); private JetCoreEnvironment environmentWithMockJdk = JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(myTestRootDisposable, ConfigurationKind.JDK_AND_ANNOTATIONS);
private JetCoreEnvironment environmentWithFullJdk = JetTestUtils.createEnvironmentWithFullJdk(myTestRootDisposable); private JetCoreEnvironment environmentWithFullJdk = JetTestUtils.createEnvironmentWithFullJdk(myTestRootDisposable);
@@ -135,6 +136,7 @@ public class CodegenTestsOnAndroidGenerator extends UsefulTestCase {
Set<String> excludedFiles = SpecialFiles.getExcludedFiles(); Set<String> excludedFiles = SpecialFiles.getExcludedFiles();
Set<String> filesCompiledWithoutStdLib = SpecialFiles.getFilesCompiledWithoutStdLib(); Set<String> filesCompiledWithoutStdLib = SpecialFiles.getFilesCompiledWithoutStdLib();
Set<String> filesCompiledWithJUnit = SpecialFiles.getFilesCompiledWithJUnit(); Set<String> filesCompiledWithJUnit = SpecialFiles.getFilesCompiledWithJUnit();
Map<String, String> filesWithSpecialResult = SpecialFiles.getFilesWithSpecialResult();
for (File file : files) { for (File file : files) {
if (excludedFiles.contains(file.getName())) { if (excludedFiles.contains(file.getName())) {
continue; continue;
@@ -160,7 +162,14 @@ public class CodegenTestsOnAndroidGenerator extends UsefulTestCase {
factory = getFactoryFromText(file.getAbsolutePath(), text, environmentWithFullJdk); factory = getFactoryFromText(file.getAbsolutePath(), text, environmentWithFullJdk);
} }
generateTestMethod(p, generatedTestName, StringUtil.escapeStringCharacters(file.getPath())); String specialResult = filesWithSpecialResult.get(file.getName());
if (specialResult != null) {
generateTestMethodWithExpectedResult(p, generatedTestName, StringUtil.escapeStringCharacters(file.getPath()),
specialResult);
}
else {
generateTestMethod(p, generatedTestName, StringUtil.escapeStringCharacters(file.getPath()));
}
File outputDir = new File(pathManager.getOutputForCompiledFiles()); File outputDir = new File(pathManager.getOutputForCompiledFiles());
if (!outputDir.exists()) { if (!outputDir.exists()) {
outputDir.mkdirs(); outputDir.mkdirs();
@@ -173,7 +182,7 @@ public class CodegenTestsOnAndroidGenerator extends UsefulTestCase {
} }
} }
private ClassFileFactory getFactoryFromText(String filePath, String text, JetCoreEnvironment jetEnvironment) { private static ClassFileFactory getFactoryFromText(String filePath, String text, JetCoreEnvironment jetEnvironment) {
JetFile psiFile = JetPsiFactory.createFile(jetEnvironment.getProject(), text); JetFile psiFile = JetPsiFactory.createFile(jetEnvironment.getProject(), text);
ClassFileFactory factory; ClassFileFactory factory;
try { try {
@@ -185,7 +194,7 @@ public class CodegenTestsOnAndroidGenerator extends UsefulTestCase {
return factory; return factory;
} }
private boolean hasBoxMethod(String text) { private static boolean hasBoxMethod(String text) {
return text.contains("fun box()"); return text.contains("fun box()");
} }
@@ -199,15 +208,19 @@ public class CodegenTestsOnAndroidGenerator extends UsefulTestCase {
} }
} }
private void generateTestMethod(Printer p, String testName, String namespace) { private static void generateTestMethodWithExpectedResult(Printer p, String testName, String namespace, String expectedResult) {
p.println("public void test" + testName + "() throws Exception {"); p.println("public void test" + testName + "() throws Exception {");
p.pushIndent(); p.pushIndent();
p.println("invokeBoxMethod(\"" + namespace + "\");"); p.println("invokeBoxMethod(\"" + namespace + "\", \"" + expectedResult + "\");");
p.popIndent(); p.popIndent();
p.println("}"); p.println("}");
p.println(); p.println();
} }
private static void generateTestMethod(Printer p, String testName, String namespace) {
generateTestMethodWithExpectedResult(p, testName, namespace, "OK");
}
private String generateTestName(String fileName) { private String generateTestName(String fileName) {
String result = FileUtil.getNameWithoutExtension(StringUtil.capitalize(fileName)); String result = FileUtil.getNameWithoutExtension(StringUtil.capitalize(fileName));
@@ -16,12 +16,10 @@
package org.jetbrains.jet.compiler.android; package org.jetbrains.jet.compiler.android;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.util.ArrayList; import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** /**
* @author Natalia.Ukhorskaya * @author Natalia.Ukhorskaya
@@ -31,13 +29,16 @@ public class SpecialFiles {
private static final Set<String> excludedFiles = Sets.newHashSet(); private static final Set<String> excludedFiles = Sets.newHashSet();
private static final Set<String> filesCompiledWithoutStdLib = Sets.newHashSet(); private static final Set<String> filesCompiledWithoutStdLib = Sets.newHashSet();
private static final Set<String> filesCompiledWithJUnit = Sets.newHashSet(); private static final Set<String> filesCompiledWithJUnit = Sets.newHashSet();
private static final Map<String, String> filesWithSpecialResult = Maps.newHashMap();
static { static {
fillExcludedFiles(); fillExcludedFiles();
fillFilesCompiledWithoutStdLib(); fillFilesCompiledWithoutStdLib();
fillFilesCompiledWithJUnit(); fillFilesCompiledWithJUnit();
fillFilesWithSpecialResult();
} }
public static Set<String> getFilesCompiledWithJUnit() { public static Set<String> getFilesCompiledWithJUnit() {
return filesCompiledWithJUnit; return filesCompiledWithJUnit;
} }
@@ -50,6 +51,14 @@ public class SpecialFiles {
return filesCompiledWithoutStdLib; return filesCompiledWithoutStdLib;
} }
public static Map<String, String> getFilesWithSpecialResult() {
return filesWithSpecialResult;
}
private static void fillFilesWithSpecialResult() {
filesWithSpecialResult.put("kt2398.kt", "OKKO");
}
private static void fillFilesCompiledWithJUnit() { private static void fillFilesCompiledWithJUnit() {
filesCompiledWithJUnit.add("kt2334.kt"); filesCompiledWithJUnit.add("kt2334.kt");
} }