add generated tests

This commit is contained in:
dedoz
2014-08-26 21:10:55 +04:00
committed by Yan Zhulanow
parent a56fa7f192
commit f2ab424c41
45 changed files with 1135 additions and 234 deletions
@@ -0,0 +1,59 @@
/*
* Copyright 2010-2014 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.lang.resolve.android
import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest
import org.jetbrains.jet.ConfigurationKind
import com.intellij.compiler.CompilerConfiguration
import org.jetbrains.jet.TestJdkKind
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment
import org.jetbrains.jet.JetTestUtils
import java.util.Collections
import com.intellij.util.ArrayUtil
import com.intellij.openapi.util.io.FileUtil
import java.io.File
import java.util.ArrayList
import com.intellij.util.Processor
import org.jetbrains.jet.codegen.CodegenTestFiles
public abstract class AbstractAndroidBoxTest : AbstractBlackBoxCodegenTest() {
fun createEnvironment(path: String) {
val configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.ANDROID_API);
configuration.put(JVMConfigurationKeys.ANDROID_RES_PATH, path + "layout/");
configuration.put(JVMConfigurationKeys.ANDROID_MANIFEST, path + "AndroidManifest.xml");
myEnvironment = JetCoreEnvironment.createForTests(getTestRootDisposable()!!, configuration);
}
override fun doTest(path: String) {
createEnvironment(path)
val files = ArrayList<String>(2)
FileUtil.processFilesRecursively(File(path), object : Processor<File> {
override fun process(file: File?): Boolean {
if (file!!.getName().endsWith(".kt")) {
files.add(relativePath(file))
}
return true
}
})
Collections.sort(files);
myFiles = CodegenTestFiles.create(myEnvironment!!.getProject(), ArrayUtil.toStringArray(files))
blackBox();
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2014 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.lang.resolve.android
import com.intellij.testFramework.UsefulTestCase
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment
import org.jetbrains.jet.JetTestUtils
import org.jetbrains.jet.ConfigurationKind
import org.jetbrains.jet.TestJdkKind
import org.jetbrains.jet.plugin.android.IDEAndroidUIXmlProcessor
public abstract class AbstractCrossParserTest : UsefulTestCase() {
public fun doTest(path: String) {
val jetCoreEnvironment = getEnvironment(path)
val cliParser = CliAndroidUIXmlProcessor(jetCoreEnvironment.getProject(), path + "/layout", path + "AndroidManifest.xml")
// val ideParser = IDEAndroidUIXmlProcessor(jetCoreEnvironment.getProject(), path + "/layout", path + "AndroidManifest.xml")
}
private fun getEnvironment(testPath: String): JetCoreEnvironment {
val configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK)
// configuration.put<String>(JVMConfigurationKeys.ANDROID_RES_PATH, testPath + "/layout")
// configuration.put<String>(JVMConfigurationKeys.ANDROID_MANIFEST, testPath + "/AndroidManifest.xml")
return JetCoreEnvironment.createForTests(getTestRootDisposable()!!, configuration)
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2010-2014 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.lang.resolve.android;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.lang.resolve.android.AbstractAndroidBoxTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/codegen/android")
public class AndroidBoxTestGenerated extends AbstractAndroidBoxTest {
public void testAllFilesPresentInAndroid() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/android"), Pattern.compile("^([^\\.]+)$"), false);
}
@TestMetadata("fqNameInAttr")
public void testFqNameInAttr() throws Exception {
doTest("compiler/testData/codegen/android/fqNameInAttr/");
}
@TestMetadata("fqNameInTag")
public void testFqNameInTag() throws Exception {
doTest("compiler/testData/codegen/android/fqNameInTag/");
}
@TestMetadata("manyWidgets")
public void testManyWidgets() throws Exception {
doTest("compiler/testData/codegen/android/manyWidgets/");
}
@TestMetadata("multiFile")
public void testMultiFile() throws Exception {
doTest("compiler/testData/codegen/android/multiFile/");
}
@TestMetadata("singleFile")
public void testSingleFile() throws Exception {
doTest("compiler/testData/codegen/android/singleFile/");
}
}
@@ -1,141 +0,0 @@
/*
* Copyright 2010-2014 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.lang.resolve.android;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.testFramework.UsefulTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.*;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.codegen.GenerationUtils;
import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.test.TestCaseWithTmpdir;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.*;
public class AndroidXmlTest extends UsefulTestCase {
private static final String singleFilePrefix = getTestDataPath() + "/converter/singleFile/";
public static final String singleFileManifestPath = singleFilePrefix + "AndroidManifest.xml";
private final File singleFileDir = new File(singleFilePrefix + "res/layout/");
private final File fakeActivitySrc = new File(getTestDataPath() + "/fakeHelpers/Activity.kt");
private final File fakeViewSrc = new File(getTestDataPath() + "/fakeHelpers/View.kt");
private final File fakeWidgetsSrc = new File(getTestDataPath() + "/fakeHelpers/Widgets.kt");
private final File fakeMyActivitySrc = new File(singleFilePrefix + "MyActivity.kt");
private static final String singleFileResPath = singleFilePrefix + "res/layout/";
@Override
public void setUp() throws Exception {
super.setUp();
}
@NotNull
private static String getTestDataPath() {
return JetTestCaseBuilder.getTestDataPathBase() + "/android";
}
@SuppressWarnings({"ResultOfMethodCallIgnored", "IOResourceOpenedButNotSafelyClosed"})
protected static String loadOrCreate(File file, String data) throws IOException {
try {
return new Scanner(file, "UTF-8" ).useDelimiter("\\A").next();
} catch (IOException e) {
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(data);
fileWriter.close();
fail("Empty expected data, creating from actual");
return data;
}
}
private GenerationState compileManyFilesGetGenerationState(List<File> files, String resPath) throws IOException {
JetCoreEnvironment jetCoreEnvironment = getEnvironment(resPath);
Project project = jetCoreEnvironment.getProject();
List<JetFile> jetFiles = new ArrayList<JetFile>(files.size());
for (File file: files) {
jetFiles.add(JetTestUtils.loadJetFile(project, file));
}
return GenerationUtils.compileManyFilesGetGenerationStateForTest(project, jetFiles);
}
private JetCoreEnvironment getEnvironment(String resPath) {
CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL,
TestJdkKind.MOCK_JDK);
configuration.put(JVMConfigurationKeys.ANDROID_RES_PATH, resPath);
configuration.put(JVMConfigurationKeys.ANDROID_MANIFEST, singleFileManifestPath);
return JetCoreEnvironment.createForTests(getTestRootDisposable(),
configuration);
}
public void testCompileResult() throws Exception {
List<File> files = addDefaultFiles();
compileManyFilesGetGenerationState(files, singleFileResPath);
// TODO: why?
Disposer.dispose(getTestRootDisposable());
}
private List<File> addDefaultFiles() {
File fakeRClass = new File(getTestDataPath() + "/converter/singleFile/R.kt");
List<File> files = new ArrayList<File>();
files.add(fakeActivitySrc);
files.add(fakeViewSrc);
files.add(fakeRClass);
files.add(fakeWidgetsSrc);
return files;
}
// TODO: many tests
public void testConverterOneFile() throws Exception {
JetCoreEnvironment jetCoreEnvironment = getEnvironment(singleFileResPath);
AndroidUIXmlProcessor parser = new CliAndroidUIXmlProcessor(jetCoreEnvironment.getProject(),
singleFileDir.getAbsolutePath(),
singleFileManifestPath);
String actual = parser.parseToString();
String expected = loadOrCreate(new File(getTestDataPath() + "/converter/singleFile/layout.kt"), actual);
assertEquals(expected, actual);
}
// todo: unify with box tests
public void testGeneratedByteCode() throws Exception {
String resPath = getTestDataPath() + "/converter/singleFile/res/layout/";
List<File> files = addDefaultFiles();
files.add(fakeMyActivitySrc);
GenerationState state = compileManyFilesGetGenerationState(files, resPath);
ByteArrayClassLoader classLoader = new ByteArrayClassLoader(new URL[] {}, getClass().getClassLoader(), state.getFactory().asList());
classLoader.loadFiles();
Class<?> activity = classLoader.findClass("com.myapp.MyActivity");
String res =(String) activity.getMethod("test").invoke(activity.newInstance());
// todo: why?
Disposer.dispose(getTestRootDisposable());
assertEquals("OK", res);
}
}
@@ -1,84 +0,0 @@
/*
* Copyright 2010-2014 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.lang.resolve.android;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.OutputFile;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
// todo: get rid of this class
public class ByteArrayClassLoader extends URLClassLoader {
private final Queue<OutputFile> q;
private final Map<String, Class> extraClassDefs = new HashMap<String, Class>();
@NotNull
@Override
protected Class<?> findClass(@NotNull String name) throws ClassNotFoundException {
Class aClass = extraClassDefs.get(name);
if (aClass != null) return aClass;
else return super.findClass(name);
}
public ByteArrayClassLoader(URL[] urls, ClassLoader parent, List<OutputFile> files) {
super(urls, parent);
q = new LinkedList<OutputFile>(files);
}
private void loadBytes(OutputFile f) {
try {
byte[] b = f.asByteArray();
String name = relPathToClassName(f.getRelativePath());
Class<?> aClass = defineClass(name, b, 0, b.length);
extraClassDefs.put(name, aClass);
q.remove(f);
} catch (NoClassDefFoundError e) {
OutputFile found = findByClassName(e.getMessage());
if (found == null) throw e;
else {
loadBytes(found);
loadBytes(f);
}
}
}
public void loadFiles() {
OutputFile f = q.peek();
while (f != null) {
loadBytes(f);
f = q.peek();
}
}
private OutputFile findByClassName(String name) {
String path = classNameToRelPath(name);
for (OutputFile file: q)
if (file.getRelativePath().equals(path)) return file;
return null;
}
private static String relPathToClassName(String path) {
return path.replace(".class", "").replace("/", ".");
}
private static String classNameToRelPath(String name) {
return name.replace(".", "/").concat(".class");
}
}
@@ -164,7 +164,7 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
blackBox();
}
private void blackBox() {
protected void blackBox() {
// If there are many files, the first of them should contain the 'box(): String' function
JetFile firstFile = myFiles.getPsiFiles().get(0);
String fqName = getPackageClassFqName(firstFile.getPackageFqName()).asString();