Kapt: Simplify wrapper tests

(cherry picked from commit 1677984)
This commit is contained in:
Yan Zhulanow
2016-09-16 02:00:31 +03:00
committed by Yan Zhulanow
parent e04f834a0e
commit 238340a143
31 changed files with 225 additions and 394 deletions
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2016 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.kotlin.annotation.processing.test.wrappers
import com.intellij.openapi.util.io.FileUtil
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.codegen.AbstractBytecodeTextTest
import org.jetbrains.kotlin.codegen.CodegenTestCase
import org.jetbrains.kotlin.codegen.CodegenTestUtil
import org.jetbrains.kotlin.java.model.elements.DefaultJeElementRenderer
import org.jetbrains.kotlin.java.model.toJeElement
import org.jetbrains.kotlin.test.ConfigurationKind
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
import java.util.regex.Pattern
abstract class AbstractAnnotationProcessingTest : AbstractBytecodeTextTest() {
companion object {
private val SUBJECT_FQ_NAME_PATTERN = Pattern.compile("^// FQNAME: \\s*(.*)$", Pattern.MULTILINE)
private val RENDERER = DefaultJeElementRenderer()
}
override fun doMultiFileTest(wholeFile: File, files: List<CodegenTestCase.TestFile>, javaFilesDir: File?) {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, files, javaFilesDir)
loadMultiFiles(files)
val text = FileUtil.loadFile(wholeFile, true)
val matcher = SUBJECT_FQ_NAME_PATTERN.matcher(text)
assertTrue("No FqName specified. First line of the form '// f.q.Name' expected", matcher.find())
val fqName = matcher.group(1)
CodegenTestUtil.generateFiles(myEnvironment, myFiles)
val project = myEnvironment.project
val psiClass = JavaPsiFacade.getInstance(project).findClass(fqName, GlobalSearchScope.projectScope(project))!!
val modelFile = File(wholeFile.parent, wholeFile.nameWithoutExtension + ".txt")
val jeElement = psiClass.toJeElement() ?: error("JeElement is null")
KotlinTestUtils.assertEqualsToFile(modelFile, RENDERER.render(jeElement))
}
}
@@ -1,89 +0,0 @@
/*
* Copyright 2010-2016 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.kotlin.annotation.processing.test.wrappers;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithJava;
import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class AbstractJavaAnnotationProcessingTest extends KotlinMultiFileTestWithJava<Void, Void> {
private final static Pattern SUBJECT_FQ_NAME_PATTERN = Pattern.compile("^//\\s*(.*)$", Pattern.MULTILINE);
@NotNull
@Override
protected ConfigurationKind getConfigurationKind() {
return ConfigurationKind.ALL;
}
@Override
protected boolean isKotlinSourceRootNeeded() {
return true;
}
@Override
protected void doTest(String testDirPath) throws Exception {
File testDir = new File(testDirPath);
File javaFile = new File(testDirPath, testDir.getName() + ".java");
String text = FileUtil.loadFile(javaFile, true);
Matcher matcher = SUBJECT_FQ_NAME_PATTERN.matcher(text);
TestCase.assertTrue("No FqName specified. First line of the form '// f.q.Name' expected", matcher.find());
String fqName = matcher.group(1);
getEnvironment().tryUpdateClasspath(Collections.singletonList(testDir));
super.doTest(new File(testDir.getParentFile(), "common.kt").getCanonicalPath());
KotlinTestUtils.resolveAllKotlinFiles(getEnvironment());
Project project = getEnvironment().getProject();
PsiClass javaClass = JavaPsiFacade.getInstance(project).findClass(fqName, GlobalSearchScope.allScope(project));
TestCase.assertNotNull("Class $fqName was not found.", javaClass != null);
doTest(javaFile, javaClass);
}
@Override
protected void doMultiFileTest(File testDataFile, Map<String, ModuleAndDependencies> modules, List<Void> files) throws IOException {}
protected abstract void doTest(File testDataFile, PsiClass lightClass);
@Override
protected Void createTestModule(@NotNull String name) {
return null;
}
@Override
protected Void createTestFile(Void module, String fileName, String text, Map<String, String> directives) {
return null;
}
}
@@ -1,90 +0,0 @@
/*
* Copyright 2010-2016 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.kotlin.annotation.processing.test.wrappers;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.asJava.finder.JavaElementFinder;
import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithJava;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class AbstractKotlinAnnotationProcessingTest extends KotlinMultiFileTestWithJava<Void, Void> {
private final static Pattern SUBJECT_FQ_NAME_PATTERN = Pattern.compile("^//\\s*(.*)$", Pattern.MULTILINE);
@NotNull
@Override
protected ConfigurationKind getConfigurationKind() {
return ConfigurationKind.ALL;
}
@Override
protected boolean isKotlinSourceRootNeeded() {
return true;
}
@Override
protected void doMultiFileTest(File testDataFile, Map<String, ModuleAndDependencies> modules, List<Void> files) throws IOException {
String text = FileUtil.loadFile(testDataFile, true);
Matcher matcher = SUBJECT_FQ_NAME_PATTERN.matcher(text);
assertTrue("No FqName specified. First line of the form '// f.q.Name' expected", matcher.find());
String fqName = matcher.group(1);
PsiClass lightClass = findLightClass(fqName);
doTest(testDataFile, lightClass);
}
protected abstract void doTest(File testDataFile, PsiClass lightClass);
private PsiClass findLightClass(String fqName) {
try {
return createFinder(getEnvironment()).findClass(fqName, GlobalSearchScope.allScope(getEnvironment().getProject()));
}
catch (IOException e) {
throw ExceptionUtilsKt.rethrow(e);
}
}
@Override
protected Void createTestModule(@NotNull String name) {
return null;
}
@Override
protected Void createTestFile(Void module, String fileName, String text, Map<String, String> directives) {
return null;
}
@NotNull
private static JavaElementFinder createFinder(@NotNull KotlinCoreEnvironment environment) throws IOException {
// We need to resolve all the files in order too fill in the trace that sits inside LightClassGenerationSupport
KotlinTestUtils.resolveAllKotlinFiles(environment);
return JavaElementFinder.getInstance(environment.getProject());
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2016 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.kotlin.annotation.processing.test.wrappers
import com.intellij.psi.PsiClass
import org.jetbrains.kotlin.java.model.elements.DefaultJeElementRenderer
import org.jetbrains.kotlin.java.model.toJeElement
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
private val RENDERER = DefaultJeElementRenderer()
private fun runTest(testDataFile: File, lightClass: PsiClass) {
val modelFile = File(testDataFile.parent, testDataFile.nameWithoutExtension + ".txt")
val jeElement = lightClass.toJeElement() ?: error("JeElement is null")
KotlinTestUtils.assertEqualsToFile(modelFile, RENDERER.render(jeElement))
}
abstract class AbstractKotlinModelWrappersTest : AbstractKotlinAnnotationProcessingTest() {
override fun doTest(testDataFile: File, lightClass: PsiClass) = runTest(testDataFile, lightClass)
}
abstract class AbstractJavaModelWrappersTest : AbstractJavaAnnotationProcessingTest() {
override fun doTest(testDataFile: File, lightClass: PsiClass) = runTest(testDataFile, lightClass)
}
@@ -0,0 +1,121 @@
/*
* Copyright 2010-2016 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.kotlin.annotation.processing.test.wrappers;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("plugins/annotation-processing/testData/wrappers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class AnnotationProcessingTestGenerated extends AbstractAnnotationProcessingTest {
public void testAllFilesPresentInWrappers() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/annotation-processing/testData/wrappers"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("plugins/annotation-processing/testData/wrappers/javaWrappers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class JavaWrappers extends AbstractAnnotationProcessingTest {
public void testAllFilesPresentInJavaWrappers() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/annotation-processing/testData/wrappers/javaWrappers"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("EnumClass.kt")
public void testEnumClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/javaWrappers/EnumClass.kt");
doTest(fileName);
}
@TestMetadata("MetaAnnotation.kt")
public void testMetaAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/javaWrappers/MetaAnnotation.kt");
doTest(fileName);
}
@TestMetadata("Simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/javaWrappers/Simple.kt");
doTest(fileName);
}
@TestMetadata("WithNested.kt")
public void testWithNested() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/javaWrappers/WithNested.kt");
doTest(fileName);
}
}
@TestMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class KotlinWrappers extends AbstractAnnotationProcessingTest {
public void testAllFilesPresentInKotlinWrappers() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/annotation-processing/testData/wrappers/kotlinWrappers"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("annotations.kt")
public void testAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers/annotations.kt");
doTest(fileName);
}
@TestMetadata("enum.kt")
public void testEnum() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers/enum.kt");
doTest(fileName);
}
@TestMetadata("kotlinAnnotations.kt")
public void testKotlinAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers/kotlinAnnotations.kt");
doTest(fileName);
}
@TestMetadata("metaAnnotations.kt")
public void testMetaAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers/metaAnnotations.kt");
doTest(fileName);
}
@TestMetadata("nestedClasses.kt")
public void testNestedClasses() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers/nestedClasses.kt");
doTest(fileName);
}
@TestMetadata("repeatableAnnotations.kt")
public void testRepeatableAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers/repeatableAnnotations.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/wrappers/kotlinWrappers/simple.kt");
doTest(fileName);
}
}
}
@@ -1,62 +0,0 @@
/*
* Copyright 2010-2016 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.kotlin.annotation.processing.test.wrappers;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("plugins/annotation-processing/testData/javaWrappers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class JavaModelWrappersTestGenerated extends AbstractJavaModelWrappersTest {
public void testAllFilesPresentInJavaWrappers() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/annotation-processing/testData/javaWrappers"), Pattern.compile("^([^\\.]+)$"), true);
}
@TestMetadata("EnumClass")
public void testEnumClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/javaWrappers/EnumClass/");
doTest(fileName);
}
@TestMetadata("MetaAnnotation")
public void testMetaAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/javaWrappers/MetaAnnotation/");
doTest(fileName);
}
@TestMetadata("Simple")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/javaWrappers/Simple/");
doTest(fileName);
}
@TestMetadata("WithNested")
public void testWithNested() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/javaWrappers/WithNested/");
doTest(fileName);
}
}
@@ -1,79 +0,0 @@
/*
* Copyright 2010-2016 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.kotlin.annotation.processing.test.wrappers;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("plugins/annotation-processing/testData/kotlinWrappers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class KotlinModelWrappersTestGenerated extends AbstractKotlinModelWrappersTest {
public void testAllFilesPresentInKotlinWrappers() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/annotation-processing/testData/kotlinWrappers"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("annotations.kt")
public void testAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/annotations.kt");
doTest(fileName);
}
@TestMetadata("enum.kt")
public void testEnum() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/enum.kt");
doTest(fileName);
}
@TestMetadata("kotlinAnnotations.kt")
public void testKotlinAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.kt");
doTest(fileName);
}
@TestMetadata("metaAnnotations.kt")
public void testMetaAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/metaAnnotations.kt");
doTest(fileName);
}
@TestMetadata("nestedClasses.kt")
public void testNestedClasses() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/nestedClasses.kt");
doTest(fileName);
}
@TestMetadata("repeatableAnnotations.kt")
public void testRepeatableAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/repeatableAnnotations.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/simple.kt");
doTest(fileName);
}
}