From 7668141f8535d306809dc5143d8506a514cb9428 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Mon, 14 Jan 2013 18:08:58 +0400 Subject: [PATCH] Base class for tests introduced --- .../tests/org/jetbrains/jet/JetTestUtils.java | 12 ++++ .../jet/asJava/JavaElementFinderTest.java | 41 ++--------- .../jet/asJava/KotlinAsJavaTestBase.java | 60 ++++++++++++++++ .../jet/asJava/KotlinLightClassTest.java | 72 +++++-------------- 4 files changed, 96 insertions(+), 89 deletions(-) create mode 100644 compiler/tests/org/jetbrains/jet/asJava/KotlinAsJavaTestBase.java diff --git a/compiler/tests/org/jetbrains/jet/JetTestUtils.java b/compiler/tests/org/jetbrains/jet/JetTestUtils.java index 45927d53d4d..59f71052a48 100644 --- a/compiler/tests/org/jetbrains/jet/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/jet/JetTestUtils.java @@ -37,6 +37,7 @@ import org.jetbrains.jet.cli.jvm.compiler.CliLightClassGenerationSupport; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileRuntime; import org.jetbrains.jet.codegen.forTestCompile.ForTestPackJdkAnnotations; +import org.jetbrains.jet.config.CommonConfigurationKeys; import org.jetbrains.jet.config.CompilerConfiguration; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Severity; @@ -48,6 +49,7 @@ import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM; +import org.jetbrains.jet.lang.resolve.lazy.LazyResolveTestUtil; import org.jetbrains.jet.plugin.JetLanguage; import org.jetbrains.jet.test.InnerTestClasses; import org.jetbrains.jet.test.TestMetadata; @@ -339,6 +341,16 @@ public class JetTestUtils { CliLightClassGenerationSupport.getInstanceForCli(environment.getProject()).newBindingTrace(); } + public static void resolveAllKotlinFiles(JetCoreEnvironment environment) throws IOException { + List paths = environment.getConfiguration().get(CommonConfigurationKeys.SOURCE_ROOTS_KEY); + assert paths != null; + List jetFiles = Lists.newArrayList(); + for (String path : paths) { + jetFiles.add(loadJetFile(environment.getProject(), new File(path))); + } + LazyResolveTestUtil.resolveEagerly(jetFiles, environment); + } + public interface TestFileFactory { F create(String fileName, String text); } diff --git a/compiler/tests/org/jetbrains/jet/asJava/JavaElementFinderTest.java b/compiler/tests/org/jetbrains/jet/asJava/JavaElementFinderTest.java index 6cefff16277..13b30fc5ec0 100644 --- a/compiler/tests/org/jetbrains/jet/asJava/JavaElementFinderTest.java +++ b/compiler/tests/org/jetbrains/jet/asJava/JavaElementFinderTest.java @@ -16,49 +16,20 @@ package org.jetbrains.jet.asJava; -import com.google.common.collect.Lists; import com.intellij.psi.PsiClass; import com.intellij.psi.search.GlobalSearchScope; -import org.jetbrains.jet.JetLiteFixture; -import org.jetbrains.jet.JetTestUtils; -import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; -import org.jetbrains.jet.config.CommonConfigurationKeys; -import org.jetbrains.jet.config.CompilerConfiguration; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.lang.resolve.lazy.LazyResolveTestUtil; import java.io.File; +import java.util.Collections; import java.util.List; -public class JavaElementFinderTest extends JetLiteFixture { - private JavaElementFinder finder; - - public JavaElementFinderTest() { - super("asJava/findClasses"); - } +public class JavaElementFinderTest extends KotlinAsJavaTestBase { @Override - protected JetCoreEnvironment createEnvironment() { - CompilerConfiguration configuration = new CompilerConfiguration(); - - configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, getTestDataPath() + "/asJava/findClasses/" + getTestName(false) + ".kt"); - - return new JetCoreEnvironment(getTestRootDisposable(), configuration); - } - - @Override - public void setUp() throws Exception { - super.setUp(); - - // We need to resolve all the files in order too fill in the trace that sits inside LightClassGenerationSupport - List paths = getEnvironment().getConfiguration().get(CommonConfigurationKeys.SOURCE_ROOTS_KEY); - assert paths != null; - List jetFiles = Lists.newArrayList(); - for (String path : paths) { - jetFiles.add(JetTestUtils.loadJetFile(getProject(), new File(path))); - } - LazyResolveTestUtil.resolveEagerly(jetFiles, getEnvironment()); - finder = new JavaElementFinder(getProject(), LightClassGenerationSupport.getInstance(getProject())); + protected List getKotlinSourceRoots() { + return Collections.singletonList( + new File("compiler/testData/asJava/findClasses/" + getTestName(false) + ".kt") + ); } @Override diff --git a/compiler/tests/org/jetbrains/jet/asJava/KotlinAsJavaTestBase.java b/compiler/tests/org/jetbrains/jet/asJava/KotlinAsJavaTestBase.java new file mode 100644 index 00000000000..aa195ca6907 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/asJava/KotlinAsJavaTestBase.java @@ -0,0 +1,60 @@ +/* + * 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.asJava; + +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; +import org.jetbrains.jet.config.CommonConfigurationKeys; +import org.jetbrains.jet.config.CompilerConfiguration; +import org.jetbrains.jet.lang.resolve.lazy.KotlinTestWithEnvironment; + +import java.io.File; +import java.util.List; + +public abstract class KotlinAsJavaTestBase extends KotlinTestWithEnvironment { + protected JavaElementFinder finder; + + @Override + protected JetCoreEnvironment createEnvironment() { + CompilerConfiguration configuration = new CompilerConfiguration(); + + for (File root : getKotlinSourceRoots()) { + configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, root.getPath()); + } + + return new JetCoreEnvironment(getTestRootDisposable(), configuration); + } + + protected abstract List getKotlinSourceRoots(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + + // We need to resolve all the files in order too fill in the trace that sits inside LightClassGenerationSupport + JetTestUtils.resolveAllKotlinFiles(getEnvironment()); + + finder = JavaElementFinder.getInstance(getProject()); + } + + @Override + protected void tearDown() throws Exception { + finder = null; + super.tearDown(); + } + +} diff --git a/compiler/tests/org/jetbrains/jet/asJava/KotlinLightClassTest.java b/compiler/tests/org/jetbrains/jet/asJava/KotlinLightClassTest.java index 5ca8c5f41d2..701758d3c53 100644 --- a/compiler/tests/org/jetbrains/jet/asJava/KotlinLightClassTest.java +++ b/compiler/tests/org/jetbrains/jet/asJava/KotlinLightClassTest.java @@ -18,7 +18,6 @@ package org.jetbrains.jet.asJava; import com.google.common.base.Function; import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiClassType; @@ -27,28 +26,26 @@ import com.intellij.psi.PsiTypeParameter; import com.intellij.psi.search.GlobalSearchScope; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.JetTestUtils; -import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; -import org.jetbrains.jet.config.CommonConfigurationKeys; -import org.jetbrains.jet.config.CompilerConfiguration; -import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.resolve.java.PackageClassUtils; -import org.jetbrains.jet.lang.resolve.lazy.KotlinTestWithEnvironment; -import org.jetbrains.jet.lang.resolve.lazy.LazyResolveTestUtil; import org.jetbrains.jet.lang.resolve.name.FqName; import java.io.File; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Set; import static org.jetbrains.jet.asJava.KotlinLightClassTest.ClassProperty.*; -public abstract class KotlinLightClassTest extends KotlinTestWithEnvironment { +public abstract class KotlinLightClassTest extends KotlinAsJavaTestBase { public static class Declared extends KotlinLightClassTest { - public Declared() { - super(new File("compiler/testData/asJava/lightClasses/Declared.kt")); + + @Override + protected List getKotlinSourceRoots() { + return Collections.singletonList( + new File("compiler/testData/asJava/lightClasses/Declared.kt") + ); } public void testNoModifiers() { @@ -97,8 +94,11 @@ public abstract class KotlinLightClassTest extends KotlinTestWithEnvironment { public static class DeclaredWithGenerics extends KotlinLightClassTest { - public DeclaredWithGenerics() { - super(new File("compiler/testData/asJava/lightClasses/DeclaredWithGenerics.kt")); + @Override + protected List getKotlinSourceRoots() { + return Collections.singletonList( + new File("compiler/testData/asJava/lightClasses/DeclaredWithGenerics.kt") + ); } public void testGeneric1() throws Exception { @@ -122,8 +122,11 @@ public abstract class KotlinLightClassTest extends KotlinTestWithEnvironment { public static class Package extends KotlinLightClassTest { - public Package() { - super(new File("compiler/testData/asJava/lightClasses/Package.kt")); + @Override + protected List getKotlinSourceRoots() { + return Collections.singletonList( + new File("compiler/testData/asJava/lightClasses/Package.kt") + ); } public void testPackage() throws Exception { @@ -131,45 +134,6 @@ public abstract class KotlinLightClassTest extends KotlinTestWithEnvironment { } } - private File path; - private JavaElementFinder finder; - - protected KotlinLightClassTest(File path) { - this.path = path; - } - - @Override - protected JetCoreEnvironment createEnvironment() { - CompilerConfiguration configuration = new CompilerConfiguration(); - - configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, path.getPath()); - - return new JetCoreEnvironment(getTestRootDisposable(), configuration); - } - - @Override - public void setUp() throws Exception { - super.setUp(); - - finder = JavaElementFinder.getInstance(getProject()); - - // We need to resolve all the files in order to fill in the trace that sits inside LightClassGenerationSupport - List paths = getEnvironment().getConfiguration().get(CommonConfigurationKeys.SOURCE_ROOTS_KEY); - assert paths != null; - List jetFiles = Lists.newArrayList(); - for (String path : paths) { - jetFiles.add(JetTestUtils.loadJetFile(getProject(), new File(path))); - } - LazyResolveTestUtil.resolveEagerly(jetFiles, getEnvironment()); - } - - @Override - protected void tearDown() throws Exception { - finder = null; - path = null; - super.tearDown(); - } - @NotNull protected PsiClass findClass(String qualifiedName) { PsiClass psiClass = finder.findClass(qualifiedName, GlobalSearchScope.allScope(getProject()));