Test for nullability annotations generalized and generated

This commit is contained in:
Andrey Breslav
2014-05-23 08:52:52 +02:00
parent 6617a884e4
commit afca70eb41
30 changed files with 228 additions and 116 deletions
@@ -0,0 +1,86 @@
/*
* 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 com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.impl.compiled.ClsElementImpl;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.KotlinTestWithEnvironmentManagement;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CommonConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class AbstractKotlinLightClassTest extends KotlinTestWithEnvironmentManagement {
private static final Pattern SUBJECT_FQ_NAME_PATTERN = Pattern.compile("^//\\s*(.*)$", Pattern.MULTILINE);
@NotNull
private JetCoreEnvironment createEnvironment(File kotlinFile) {
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, kotlinFile.getPath());
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.getAnnotationsJar());
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.findMockJdkRtJar());
return JetCoreEnvironment.createForTests(getTestRootDisposable(), configuration);
}
@NotNull
public static JavaElementFinder createFinder(@NotNull JetCoreEnvironment environment) throws IOException {
// We need to resolve all the files in order too fill in the trace that sits inside LightClassGenerationSupport
JetTestUtils.resolveAllKotlinFiles(environment);
return JavaElementFinder.getInstance(environment.getProject());
}
protected void doTest(@NotNull String fileName) throws IOException {
File file = new File(fileName);
String text = FileUtil.loadFile(file, 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);
JetCoreEnvironment environment = createEnvironment(file);
JavaElementFinder finder = createFinder(environment);
PsiClass psiClass = finder.findClass(fqName, GlobalSearchScope.allScope(environment.getProject()));
if (!(psiClass instanceof KotlinLightClass)) {
throw new IllegalStateException("Not a light class: " + psiClass + " (" + fqName + ")");
}
PsiClass delegate = ((KotlinLightClass) psiClass).getDelegate();
if (!(delegate instanceof ClsElementImpl)) {
throw new IllegalStateException("Not a CLS element: " + delegate);
}
StringBuilder buffer = new StringBuilder();
((ClsElementImpl) delegate).appendMirrorText(0, buffer);
String actual = buffer.toString();
JetTestUtils.assertEqualsToFile(JetTestUtils.replaceExtension(file, "java"), actual);
}
}