diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java.as32 b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java.as32 deleted file mode 100644 index 9ecdfc1f196..00000000000 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java.as32 +++ /dev/null @@ -1,1295 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.test; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.intellij.openapi.Disposable; -import com.intellij.openapi.editor.Caret; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Comparing; -import com.intellij.openapi.util.ShutDownTracker; -import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.util.text.StringUtilRt; -import com.intellij.openapi.vfs.CharsetToolkit; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFileFactory; -import com.intellij.psi.impl.PsiFileFactoryImpl; -import com.intellij.rt.execution.junit.FileComparisonFailure; -import com.intellij.testFramework.LightVirtualFile; -import com.intellij.testFramework.TestDataFile; -import com.intellij.util.containers.ContainerUtil; -import junit.framework.TestCase; -import kotlin.collections.CollectionsKt; -import kotlin.collections.SetsKt; -import kotlin.jvm.functions.Function1; -import kotlin.text.StringsKt; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.TestOnly; -import org.jetbrains.kotlin.CoroutineTestUtilKt; -import org.jetbrains.kotlin.analyzer.AnalysisResult; -import org.jetbrains.kotlin.builtins.DefaultBuiltIns; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.checkers.CompilerTestLanguageVersionSettings; -import org.jetbrains.kotlin.checkers.CompilerTestLanguageVersionSettingsKt; -import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys; -import org.jetbrains.kotlin.cli.common.config.ContentRootsKt; -import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot; -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation; -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity; -import org.jetbrains.kotlin.cli.common.messages.MessageCollector; -import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles; -import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment; -import org.jetbrains.kotlin.cli.jvm.config.JvmContentRootsKt; -import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime; -import org.jetbrains.kotlin.config.*; -import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl; -import org.jetbrains.kotlin.diagnostics.Diagnostic; -import org.jetbrains.kotlin.diagnostics.Errors; -import org.jetbrains.kotlin.diagnostics.Severity; -import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages; -import org.jetbrains.kotlin.idea.KotlinLanguage; -import org.jetbrains.kotlin.jvm.compiler.LoadDescriptorUtil; -import org.jetbrains.kotlin.lexer.KtTokens; -import org.jetbrains.kotlin.name.Name; -import org.jetbrains.kotlin.psi.KtExpression; -import org.jetbrains.kotlin.psi.KtFile; -import org.jetbrains.kotlin.psi.KtPsiFactoryKt; -import org.jetbrains.kotlin.resolve.BindingContext; -import org.jetbrains.kotlin.resolve.BindingTrace; -import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics; -import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil; -import org.jetbrains.kotlin.storage.LockBasedStorageManager; -import org.jetbrains.kotlin.test.util.JetTestUtilsKt; -import org.jetbrains.kotlin.types.KotlinType; -import org.jetbrains.kotlin.types.expressions.KotlinTypeInfo; -import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice; -import org.jetbrains.kotlin.util.slicedMap.SlicedMap; -import org.jetbrains.kotlin.util.slicedMap.WritableSlice; -import org.jetbrains.kotlin.utils.ExceptionUtilsKt; -import org.jetbrains.kotlin.utils.PathUtil; -import org.junit.Assert; - -import javax.tools.*; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.StringWriter; -import java.lang.reflect.Method; -import java.nio.charset.Charset; -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import static com.intellij.openapi.application.PathManager.PROPERTY_CONFIG_PATH; -import static com.intellij.openapi.application.PathManager.PROPERTY_SYSTEM_PATH; -import static org.jetbrains.kotlin.test.InTextDirectivesUtils.*; - -public class KotlinTestUtils { - public static String TEST_MODULE_NAME = "test-module"; - - public static final String TEST_GENERATOR_NAME = "org.jetbrains.kotlin.generators.tests.TestsPackage"; - private static final String PLEASE_REGENERATE_TESTS = "Please regenerate tests (GenerateTests.kt)"; - - private static final boolean RUN_IGNORED_TESTS_AS_REGULAR = - Boolean.getBoolean("org.jetbrains.kotlin.run.ignored.tests.as.regular"); - - private static final boolean PRINT_STACKTRACE_FOR_IGNORED_TESTS = - Boolean.getBoolean("org.jetbrains.kotlin.print.stacktrace.for.ignored.tests"); - - private static final boolean AUTOMATICALLY_UNMUTE_PASSED_TESTS = false; - private static final boolean AUTOMATICALLY_MUTE_FAILED_TESTS = false; - - private static final List filesToDelete = new ArrayList<>(); - - // It's important that this is not created per test, but rather per process. - public static final String IDEA_SYSTEM_PATH; - - static { - try { - IDEA_SYSTEM_PATH = tmpDirForReusableFolder("idea-system").getPath(); - } - catch (IOException e) { - throw new RuntimeException(e); - } - } - - - /** - * Syntax: - * - * // MODULE: name(dependency1, dependency2, ...) - * - * // FILE: name - * - * Several files may follow one module - */ - private static final String MODULE_DELIMITER = ",\\s*"; - - private static final Pattern FILE_OR_MODULE_PATTERN = Pattern.compile( - "(?://\\s*MODULE:\\s*([^()\\n]+)(?:\\(([^()]+(?:" + MODULE_DELIMITER + "[^()]+)*)\\))?\\s*(?:\\(([^()]+(?:" + MODULE_DELIMITER + "[^()]+)*)\\))?\\s*)?" + - "//\\s*FILE:\\s*(.*)$", Pattern.MULTILINE); - private static final Pattern DIRECTIVE_PATTERN = Pattern.compile("^//\\s*!([\\w_]+)(:\\s*(.*)$)?", Pattern.MULTILINE); - private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile("\\r\\n|\\r|\\n"); - - public static final BindingTrace DUMMY_TRACE = new BindingTrace() { - @NotNull - @Override - public BindingContext getBindingContext() { - return new BindingContext() { - - @NotNull - @Override - public Diagnostics getDiagnostics() { - return Diagnostics.Companion.getEMPTY(); - } - - @Override - public V get(ReadOnlySlice slice, K key) { - return DUMMY_TRACE.get(slice, key); - } - - @NotNull - @Override - public Collection getKeys(WritableSlice slice) { - return DUMMY_TRACE.getKeys(slice); - } - - @NotNull - @TestOnly - @Override - public ImmutableMap getSliceContents(@NotNull ReadOnlySlice slice) { - return ImmutableMap.of(); - } - - @Nullable - @Override - public KotlinType getType(@NotNull KtExpression expression) { - return DUMMY_TRACE.getType(expression); - } - - @Override - public void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics) { - // do nothing - } - }; - } - - @Override - public void record(WritableSlice slice, K key, V value) { - } - - @Override - public void record(WritableSlice slice, K key) { - } - - @Override - @SuppressWarnings("unchecked") - public V get(ReadOnlySlice slice, K key) { - if (slice == BindingContext.PROCESSED) return (V) Boolean.FALSE; - return SlicedMap.DO_NOTHING.get(slice, key); - } - - @NotNull - @Override - public Collection getKeys(WritableSlice slice) { - assert slice.isCollective(); - return Collections.emptySet(); - } - - @Nullable - @Override - public KotlinType getType(@NotNull KtExpression expression) { - KotlinTypeInfo typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression); - return typeInfo != null ? typeInfo.getType() : null; - } - - @Override - public void recordType(@NotNull KtExpression expression, @Nullable KotlinType type) { - } - - @Override - public void report(@NotNull Diagnostic diagnostic) { - if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) { - throw new IllegalStateException("Unresolved: " + diagnostic.getPsiElement().getText()); - } - } - - @Override - public boolean wantsDiagnostics() { - return false; - } - }; - - public static BindingTrace DUMMY_EXCEPTION_ON_ERROR_TRACE = new BindingTrace() { - @NotNull - @Override - public BindingContext getBindingContext() { - return new BindingContext() { - @NotNull - @Override - public Diagnostics getDiagnostics() { - throw new UnsupportedOperationException(); - } - - @Override - public V get(ReadOnlySlice slice, K key) { - return DUMMY_EXCEPTION_ON_ERROR_TRACE.get(slice, key); - } - - @NotNull - @Override - public Collection getKeys(WritableSlice slice) { - return DUMMY_EXCEPTION_ON_ERROR_TRACE.getKeys(slice); - } - - @NotNull - @TestOnly - @Override - public ImmutableMap getSliceContents(@NotNull ReadOnlySlice slice) { - return ImmutableMap.of(); - } - - @Nullable - @Override - public KotlinType getType(@NotNull KtExpression expression) { - return DUMMY_EXCEPTION_ON_ERROR_TRACE.getType(expression); - } - - @Override - public void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics) { - // do nothing - } - }; - } - - @Override - public void record(WritableSlice slice, K key, V value) { - } - - @Override - public void record(WritableSlice slice, K key) { - } - - @Override - public V get(ReadOnlySlice slice, K key) { - return null; - } - - @NotNull - @Override - public Collection getKeys(WritableSlice slice) { - assert slice.isCollective(); - return Collections.emptySet(); - } - - @Nullable - @Override - public KotlinType getType(@NotNull KtExpression expression) { - return null; - } - - @Override - public void recordType(@NotNull KtExpression expression, @Nullable KotlinType type) { - } - - @Override - public void report(@NotNull Diagnostic diagnostic) { - if (diagnostic.getSeverity() == Severity.ERROR) { - throw new IllegalStateException(DefaultErrorMessages.render(diagnostic)); - } - } - - @Override - public boolean wantsDiagnostics() { - return true; - } - }; - - private KotlinTestUtils() { - } - - @NotNull - public static AnalysisResult analyzeFile(@NotNull KtFile file, @NotNull KotlinCoreEnvironment environment) { - return JvmResolveUtil.analyze(file, environment); - } - - @NotNull - public static KotlinCoreEnvironment createEnvironmentWithMockJdkAndIdeaAnnotations(Disposable disposable) { - return createEnvironmentWithMockJdkAndIdeaAnnotations(disposable, ConfigurationKind.ALL); - } - - @NotNull - public static KotlinCoreEnvironment createEnvironmentWithMockJdkAndIdeaAnnotations(Disposable disposable, @NotNull ConfigurationKind configurationKind) { - return createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(disposable, configurationKind, TestJdkKind.MOCK_JDK); - } - - @NotNull - public static KotlinCoreEnvironment createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea( - @NotNull Disposable disposable, - @NotNull ConfigurationKind configurationKind, - @NotNull TestJdkKind jdkKind - ) { - return KotlinCoreEnvironment.createForTests( - disposable, newConfiguration(configurationKind, jdkKind, getAnnotationsJar()), EnvironmentConfigFiles.JVM_CONFIG_FILES - ); - } - - @NotNull - public static KotlinCoreEnvironment createEnvironmentWithFullJdkAndIdeaAnnotations(Disposable disposable) { - return createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(disposable, ConfigurationKind.ALL, TestJdkKind.FULL_JDK); - } - - @NotNull - public static String getTestDataPathBase() { - return getHomeDirectory() + "/compiler/testData"; - } - - private static String homeDir = computeHomeDirectory(); - - @NotNull - public static String getHomeDirectory() { - return homeDir; - } - - @NotNull - private static String computeHomeDirectory() { - String userDir = System.getProperty("user.dir"); - File dir = new File(userDir == null ? "." : userDir); - return FileUtil.toCanonicalPath(dir.getAbsolutePath()); - } - - public static File findMockJdkRtJar() { - return new File(getHomeDirectory(), "compiler/testData/mockJDK/jre/lib/rt.jar"); - } - - // Differs from common mock JDK only by one additional 'nonExistingMethod' in Collection and constructor from Double in Throwable - // It's needed to test the way we load additional built-ins members that neither in black nor white lists - public static File findMockJdkRtModified() { - return new File(getHomeDirectory(), "compiler/testData/mockJDKModified/rt.jar"); - } - - public static File findAndroidApiJar() { - String androidJarProp = System.getProperty("android.jar"); - File androidJarFile = androidJarProp == null ? null : new File(androidJarProp); - if (androidJarFile == null || !androidJarFile.isFile()) { - throw new RuntimeException( - "Unable to get a valid path from 'android.jar' property (" + - androidJarProp + - "), please point it to the 'android.jar' file location"); - } - return androidJarFile; - } - - @NotNull - public static File findAndroidSdk() { - String androidSdkProp = System.getProperty("android.sdk"); - File androidSdkDir = androidSdkProp == null ? null : new File(androidSdkProp); - if (androidSdkDir == null || !androidSdkDir.isDirectory()) { - throw new RuntimeException( - "Unable to get a valid path from 'android.sdk' property (" + - androidSdkProp + - "), please point it to the android SDK location"); - } - return androidSdkDir; - } - - public static String getAndroidSdkSystemIndependentPath() { - return com.intellij.util.PathUtil.toSystemIndependentName(findAndroidSdk().getAbsolutePath()); - } - - public static File getAnnotationsJar() { - return new File(getHomeDirectory(), "compiler/testData/mockJDK/jre/lib/annotations.jar"); - } - - public static void mkdirs(@NotNull File file) { - if (file.isDirectory()) { - return; - } - if (!file.mkdirs()) { - if (file.exists()) { - throw new IllegalStateException("Failed to create " + file + ": file exists and not a directory"); - } - throw new IllegalStateException("Failed to create " + file); - } - } - - @NotNull - public static File tmpDirForTest(@NotNull String testClassName, @NotNull String testName) throws IOException { - return normalizeFile(FileUtil.createTempDirectory(testClassName, testName, false)); - } - - @NotNull - public static File tmpDirForTest(TestCase test) throws IOException { - return tmpDirForTest(test.getClass().getSimpleName(), test.getName()); - } - - @NotNull - public static File tmpDir(String name) throws IOException { - return normalizeFile(FileUtil.createTempDirectory(name, "", false)); - } - - @NotNull - public static File tmpDirForReusableFolder(String name) throws IOException { - return normalizeFile(FileUtil.createTempDirectory(new File(System.getProperty("java.io.tmpdir")), name, "", true)); - } - - private static File normalizeFile(File file) throws IOException { - // Get canonical file to be sure that it's the same as inside the compiler, - // for example, on Windows, if a canonical path contains any space from FileUtil.createTempDirectory we will get - // a File with short names (8.3) in its path and it will break some normalization passes in tests. - return file.getCanonicalFile(); - } - - @NotNull - public static KtFile createFile(@NotNull @NonNls String name, @NotNull String text, @NotNull Project project) { - String shortName = name.substring(name.lastIndexOf('/') + 1); - shortName = shortName.substring(shortName.lastIndexOf('\\') + 1); - LightVirtualFile virtualFile = new LightVirtualFile(shortName, KotlinLanguage.INSTANCE, StringUtilRt.convertLineSeparators(text)) { - @NotNull - @Override - public String getPath() { - //TODO: patch LightVirtualFile - return "/" + name; - } - }; - - virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET); - PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project); - //noinspection ConstantConditions - return (KtFile) factory.trySetupPsiForFile(virtualFile, KotlinLanguage.INSTANCE, true, false); - } - - public static String doLoadFile(String myFullDataPath, String name) throws IOException { - String fullName = myFullDataPath + File.separatorChar + name; - return doLoadFile(new File(fullName)); - } - - public static String doLoadFile(@NotNull File file) throws IOException { - try { - return FileUtil.loadFile(file, CharsetToolkit.UTF8, true); - } - catch (FileNotFoundException fileNotFoundException) { - /* - * Unfortunately, the FileNotFoundException will only show the relative path in it's exception message. - * This clarifies the exception by showing the full path. - */ - String messageWithFullPath = file.getAbsolutePath() + " (No such file or directory)"; - throw new IOException( - "Ensure you have your 'Working Directory' configured correctly as the root " + - "Kotlin project directory in your test configuration\n\t" + - messageWithFullPath, - fileNotFoundException); - } - } - - public static String getFilePath(File file) { - return FileUtil.toSystemIndependentName(file.getPath()); - } - - @NotNull - public static CompilerConfiguration newConfiguration() { - CompilerConfiguration configuration = new CompilerConfiguration(); - configuration.put(CommonConfigurationKeys.MODULE_NAME, TEST_MODULE_NAME); - - if ("true".equals(System.getProperty("kotlin.ni"))) { - // Enable new inference for tests which do not declare their own language version settings - CommonConfigurationKeysKt.setLanguageVersionSettings(configuration, new CompilerTestLanguageVersionSettings( - Collections.emptyMap(), - LanguageVersionSettingsImpl.DEFAULT.getApiVersion(), - LanguageVersionSettingsImpl.DEFAULT.getLanguageVersion(), - Collections.emptyMap() - )); - } - - configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, new MessageCollector() { - @Override - public void clear() { - } - - @Override - public void report( - @NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location - ) { - if (severity == CompilerMessageSeverity.ERROR) { - String prefix = location == null - ? "" - : "(" + location.getPath() + ":" + location.getLine() + ":" + location.getColumn() + ") "; - throw new AssertionError(prefix + message); - } - } - - @Override - public boolean hasErrors() { - return false; - } - }); - - return configuration; - } - - @NotNull - public static CompilerConfiguration newConfiguration( - @NotNull ConfigurationKind configurationKind, - @NotNull TestJdkKind jdkKind, - @NotNull File... extraClasspath - ) { - return newConfiguration(configurationKind, jdkKind, Arrays.asList(extraClasspath), Collections.emptyList()); - } - - @NotNull - public static CompilerConfiguration newConfiguration( - @NotNull ConfigurationKind configurationKind, - @NotNull TestJdkKind jdkKind, - @NotNull List classpath, - @NotNull List javaSource - ) { - CompilerConfiguration configuration = newConfiguration(); - JvmContentRootsKt.addJavaSourceRoots(configuration, javaSource); - if (jdkKind == TestJdkKind.MOCK_JDK) { - JvmContentRootsKt.addJvmClasspathRoot(configuration, findMockJdkRtJar()); - configuration.put(JVMConfigurationKeys.NO_JDK, true); - } - else if (jdkKind == TestJdkKind.MODIFIED_MOCK_JDK) { - JvmContentRootsKt.addJvmClasspathRoot(configuration, findMockJdkRtModified()); - configuration.put(JVMConfigurationKeys.NO_JDK, true); - } - else if (jdkKind == TestJdkKind.ANDROID_API) { - JvmContentRootsKt.addJvmClasspathRoot(configuration, findAndroidApiJar()); - configuration.put(JVMConfigurationKeys.NO_JDK, true); - } - else if (jdkKind == TestJdkKind.FULL_JDK_6) { - String jdk6 = System.getenv("JDK_16"); - assert jdk6 != null : "Environment variable JDK_16 is not set"; - configuration.put(JVMConfigurationKeys.JDK_HOME, new File(jdk6)); - } - else if (jdkKind == TestJdkKind.FULL_JDK_9) { - configuration.put(JVMConfigurationKeys.JDK_HOME, getJdk9Home()); - } - else { - JvmContentRootsKt.addJvmClasspathRoots(configuration, PathUtil.getJdkClassesRootsFromCurrentJre()); - } - - if (configurationKind.getWithRuntime()) { - JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.runtimeJarForTests()); - JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.scriptRuntimeJarForTests()); - JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.kotlinTestJarForTests()); - } - else if (configurationKind.getWithMockRuntime()) { - JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.minimalRuntimeJarForTests()); - JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.scriptRuntimeJarForTests()); - } - if (configurationKind.getWithReflection()) { - JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.reflectJarForTests()); - } - - JvmContentRootsKt.addJvmClasspathRoots(configuration, classpath); - - return configuration; - } - - @NotNull - public static File getJdk9Home() { - String jdk9 = System.getenv("JDK_9"); - if (jdk9 == null) { - jdk9 = System.getenv("JDK_19"); - if (jdk9 == null) { - throw new AssertionError("Environment variable JDK_9 is not set!"); - } - } - return new File(jdk9); - } - - public static void resolveAllKotlinFiles(KotlinCoreEnvironment environment) throws IOException { - List roots = ContentRootsKt.getKotlinSourceRoots(environment.getConfiguration()); - if (roots.isEmpty()) return; - List ktFiles = new ArrayList<>(); - for (KotlinSourceRoot root : roots) { - File file = new File(root.getPath()); - if (file.isFile()) { - ktFiles.add(loadJetFile(environment.getProject(), file)); - } - else { - //noinspection ConstantConditions - for (File childFile : file.listFiles()) { - if (childFile.getName().endsWith(".kt") || childFile.getName().endsWith(".kts")) { - ktFiles.add(loadJetFile(environment.getProject(), childFile)); - } - } - } - } - JvmResolveUtil.analyze(ktFiles, environment); - } - - public static void assertEqualsToFile(@NotNull File expectedFile, @NotNull Editor editor) { - assertEqualsToFile(expectedFile, editor, true); - } - - public static void assertEqualsToFile(@NotNull File expectedFile, @NotNull Editor editor, Boolean enableSelectionTags) { - Caret caret = editor.getCaretModel().getCurrentCaret(); - List tags = Lists.newArrayList( - new TagsTestDataUtil.TagInfo<>(caret.getOffset(), true, "caret") - ); - - if (enableSelectionTags) { - int selectionStart = caret.getSelectionStart(); - int selectionEnd = caret.getSelectionEnd(); - - tags.add(new TagsTestDataUtil.TagInfo<>(selectionStart, true, "selection")); - tags.add(new TagsTestDataUtil.TagInfo<>(selectionEnd, false, "selection")); - } - - String afterText = TagsTestDataUtil.insertTagsInText(tags, editor.getDocument().getText()); - - assertEqualsToFile(expectedFile, afterText); - } - - public static void assertEqualsToFile(@NotNull File expectedFile, @NotNull String actual) { - assertEqualsToFile(expectedFile, actual, s -> s); - } - - public static void assertEqualsToFile(@NotNull String message, @NotNull File expectedFile, @NotNull String actual) { - assertEqualsToFile(message, expectedFile, actual, s -> s); - } - - public static void assertEqualsToFile(@NotNull File expectedFile, @NotNull String actual, @NotNull Function1 sanitizer) { - assertEqualsToFile("Actual data differs from file content", expectedFile, actual, sanitizer); - } - - public static void assertEqualsToFile(@NotNull String message, @NotNull File expectedFile, @NotNull String actual, @NotNull Function1 sanitizer) { - try { - String actualText = JetTestUtilsKt.trimTrailingWhitespacesAndAddNewlineAtEOF(StringUtil.convertLineSeparators(actual.trim())); - - if (!expectedFile.exists()) { - FileUtil.writeToFile(expectedFile, actualText); - Assert.fail("Expected data file did not exist. Generating: " + expectedFile); - } - String expected = FileUtil.loadFile(expectedFile, CharsetToolkit.UTF8, true); - - String expectedText = JetTestUtilsKt.trimTrailingWhitespacesAndAddNewlineAtEOF(StringUtil.convertLineSeparators(expected.trim())); - - if (!Comparing.equal(sanitizer.invoke(expectedText), sanitizer.invoke(actualText))) { - throw new FileComparisonFailure(message + ": " + expectedFile.getName(), - expected, actual, expectedFile.getAbsolutePath()); - } - } - catch (IOException e) { - throw ExceptionUtilsKt.rethrow(e); - } - } - - public static boolean compileKotlinWithJava( - @NotNull List javaFiles, - @NotNull List ktFiles, - @NotNull File outDir, - @NotNull Disposable disposable, - @Nullable File javaErrorFile - ) throws IOException { - if (!ktFiles.isEmpty()) { - KotlinCoreEnvironment environment = createEnvironmentWithFullJdkAndIdeaAnnotations(disposable); - CompilerTestLanguageVersionSettingsKt.setupLanguageVersionSettingsForMultifileCompilerTests(ktFiles, environment); - LoadDescriptorUtil.compileKotlinToDirAndGetModule(ktFiles, outDir, environment); - } - else { - boolean mkdirs = outDir.mkdirs(); - assert mkdirs : "Not created: " + outDir; - } - if (javaFiles.isEmpty()) return true; - - return compileJavaFiles(javaFiles, Arrays.asList( - "-classpath", outDir.getPath() + File.pathSeparator + ForTestCompileRuntime.runtimeJarForTests(), - "-d", outDir.getPath() - ), javaErrorFile); - } - - public interface TestFileFactory { - F createFile(@Nullable M module, @NotNull String fileName, @NotNull String text, @NotNull Map directives); - M createModule(@NotNull String name, @NotNull List dependencies, @NotNull List friends); - } - - public static abstract class TestFileFactoryNoModules implements TestFileFactory { - @Override - public final F createFile( - @Nullable Void module, - @NotNull String fileName, - @NotNull String text, - @NotNull Map directives - ) { - return create(fileName, text, directives); - } - - @NotNull - public abstract F create(@NotNull String fileName, @NotNull String text, @NotNull Map directives); - - @Override - public Void createModule(@NotNull String name, @NotNull List dependencies, @NotNull List friends) { - return null; - } - } - - @NotNull - public static List createTestFiles(@Nullable String testFileName, String expectedText, TestFileFactory factory) { - return createTestFiles(testFileName, expectedText, factory, false, ""); - } - - @NotNull - public static List createTestFiles(@Nullable String testFileName, String expectedText, TestFileFactory factory, String coroutinesPackage) { - return createTestFiles(testFileName, expectedText, factory, false, coroutinesPackage); - } - - @NotNull - public static List createTestFiles(String testFileName, String expectedText, TestFileFactory factory, - boolean preserveLocations, String coroutinesPackage) { - Map directives = parseDirectives(expectedText); - - List testFiles = Lists.newArrayList(); - Matcher matcher = FILE_OR_MODULE_PATTERN.matcher(expectedText); - boolean hasModules = false; - if (!matcher.find()) { - assert testFileName != null : "testFileName should not be null if no FILE directive defined"; - // One file - testFiles.add(factory.createFile(null, testFileName, expectedText, directives)); - } - else { - int processedChars = 0; - M module = null; - // Many files - while (true) { - String moduleName = matcher.group(1); - String moduleDependencies = matcher.group(2); - String moduleFriends = matcher.group(3); - if (moduleName != null) { - moduleName = moduleName.trim(); - hasModules = true; - module = factory.createModule(moduleName, parseModuleList(moduleDependencies), parseModuleList(moduleFriends)); - } - - String fileName = matcher.group(4); - int start = processedChars; - - boolean nextFileExists = matcher.find(); - int end; - if (nextFileExists) { - end = matcher.start(); - } - else { - end = expectedText.length(); - } - String fileText = preserveLocations ? - substringKeepingLocations(expectedText, start, end) : - expectedText.substring(start,end); - processedChars = end; - - testFiles.add(factory.createFile(module, fileName, fileText, directives)); - - if (!nextFileExists) break; - } - assert processedChars == expectedText.length() : "Characters skipped from " + - processedChars + - " to " + - (expectedText.length() - 1); - } - - if (isDirectiveDefined(expectedText, "WITH_COROUTINES")) { - M supportModule = hasModules ? factory.createModule("support", Collections.emptyList(), Collections.emptyList()) : null; - - boolean isReleaseCoroutines = - !coroutinesPackage.contains("experimental") && - !isDirectiveDefined(expectedText, "!LANGUAGE: -ReleaseCoroutines"); - - boolean checkStateMachine = isDirectiveDefined(expectedText, "CHECK_STATE_MACHINE"); - boolean checkTailCallOptimization = isDirectiveDefined(expectedText, "CHECK_TAIL_CALL_OPTIMIZATION"); - - testFiles.add( - factory.createFile( - supportModule, - "CoroutineUtil.kt", - CoroutineTestUtilKt.createTextForHelpers(isReleaseCoroutines, checkStateMachine, checkTailCallOptimization), - directives - )); - } - - return testFiles; - } - - private static String substringKeepingLocations(String string, int start, int end) { - Matcher matcher = LINE_SEPARATOR_PATTERN.matcher(string); - StringBuilder prefix = new StringBuilder(); - int lastLineOffset = 0; - while (matcher.find()) { - if (matcher.end() > start) { - break; - } - - lastLineOffset = matcher.end(); - prefix.append('\n'); - } - - while (lastLineOffset++ < start) { - prefix.append(' '); - } - - return prefix + string.substring(start, end); - } - - private static List parseModuleList(@Nullable String dependencies) { - if (dependencies == null) return Collections.emptyList(); - return StringsKt.split(dependencies, Pattern.compile(MODULE_DELIMITER), 0); - } - - @NotNull - public static Map parseDirectives(String expectedText) { - Map directives = new HashMap<>(); - Matcher directiveMatcher = DIRECTIVE_PATTERN.matcher(expectedText); - while (directiveMatcher.find()) { - String name = directiveMatcher.group(1); - String value = directiveMatcher.group(3); - String oldValue = directives.put(name, value); - Assert.assertNull("Directive overwritten: " + name + " old value: " + oldValue + " new value: " + value, oldValue); - } - return directives; - } - - public static List loadBeforeAfterText(String filePath) { - String content; - - try { - content = FileUtil.loadFile(new File(filePath), true); - } - catch (IOException e) { - throw new RuntimeException(e); - } - - List files = createTestFiles("", content, new TestFileFactoryNoModules() { - @NotNull - @Override - public String create(@NotNull String fileName, @NotNull String text, @NotNull Map directives) { - int firstLineEnd = text.indexOf('\n'); - return StringUtil.trimTrailing(text.substring(firstLineEnd + 1)); - } - }, ""); - - Assert.assertTrue("Exactly two files expected: ", files.size() == 2); - - return files; - } - - public static String getLastCommentedLines(@NotNull Document document) { - List resultLines = new ArrayList<>(); - for (int i = document.getLineCount() - 1; i >= 0; i--) { - int lineStart = document.getLineStartOffset(i); - int lineEnd = document.getLineEndOffset(i); - if (document.getCharsSequence().subSequence(lineStart, lineEnd).toString().trim().isEmpty()) { - continue; - } - - if ("//".equals(document.getCharsSequence().subSequence(lineStart, lineStart + 2).toString())) { - resultLines.add(document.getCharsSequence().subSequence(lineStart + 2, lineEnd)); - } - else { - break; - } - } - Collections.reverse(resultLines); - StringBuilder result = new StringBuilder(); - for (CharSequence line : resultLines) { - result.append(line).append("\n"); - } - result.delete(result.length() - 1, result.length()); - return result.toString(); - } - - public enum CommentType { - ALL, - LINE_COMMENT, - BLOCK_COMMENT - } - - @NotNull - public static String getLastCommentInFile(@NotNull KtFile file) { - return CollectionsKt.first(getLastCommentsInFile(file, CommentType.ALL, true)); - } - - @NotNull - public static List getLastCommentsInFile(@NotNull KtFile file, CommentType commentType, boolean assertMustExist) { - PsiElement lastChild = file.getLastChild(); - if (lastChild != null && lastChild.getNode().getElementType().equals(KtTokens.WHITE_SPACE)) { - lastChild = lastChild.getPrevSibling(); - } - assert lastChild != null; - - List comments = ContainerUtil.newArrayList(); - - while (true) { - if (lastChild.getNode().getElementType().equals(KtTokens.BLOCK_COMMENT)) { - if (commentType == CommentType.ALL || commentType == CommentType.BLOCK_COMMENT) { - String lastChildText = lastChild.getText(); - comments.add(lastChildText.substring(2, lastChildText.length() - 2).trim()); - } - } - else if (lastChild.getNode().getElementType().equals(KtTokens.EOL_COMMENT)) { - if (commentType == CommentType.ALL || commentType == CommentType.LINE_COMMENT) { - comments.add(lastChild.getText().substring(2).trim()); - } - } - else { - break; - } - - lastChild = lastChild.getPrevSibling(); - } - - if (comments.isEmpty() && assertMustExist) { - throw new AssertionError(String.format( - "Test file '%s' should end in a comment of type %s; last node was: %s", file.getName(), commentType, lastChild)); - } - - return comments; - } - - public static boolean compileJavaFiles(@NotNull Collection files, List options) throws IOException { - return compileJavaFiles(files, options, null); - } - - private static boolean compileJavaFiles(@NotNull Collection files, List options, @Nullable File javaErrorFile) throws IOException { - JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); - DiagnosticCollector diagnosticCollector = new DiagnosticCollector<>(); - try (StandardJavaFileManager fileManager = - javaCompiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8"))) { - Iterable javaFileObjectsFromFiles = fileManager.getJavaFileObjectsFromFiles(files); - - JavaCompiler.CompilationTask task = javaCompiler.getTask( - new StringWriter(), // do not write to System.err - fileManager, - diagnosticCollector, - options, - null, - javaFileObjectsFromFiles); - - Boolean success = task.call(); // do NOT inline this variable, call() should complete before errorsToString() - if (javaErrorFile == null || !javaErrorFile.exists()) { - Assert.assertTrue(errorsToString(diagnosticCollector, true), success); - } - else { - assertEqualsToFile(javaErrorFile, errorsToString(diagnosticCollector, false)); - } - return success; - } - } - - public static boolean compileJavaFilesExternallyWithJava9(@NotNull Collection files, @NotNull List options) { - List command = new ArrayList<>(); - command.add(new File(getJdk9Home(), "bin/javac").getPath()); - command.addAll(options); - for (File file : files) { - command.add(file.getPath()); - } - - try { - Process process = new ProcessBuilder().command(command).inheritIO().start(); - process.waitFor(); - return process.exitValue() == 0; - } - catch (Exception e) { - throw ExceptionUtilsKt.rethrow(e); - } - } - - @NotNull - private static String errorsToString(@NotNull DiagnosticCollector diagnosticCollector, boolean humanReadable) { - StringBuilder builder = new StringBuilder(); - for (javax.tools.Diagnostic diagnostic : diagnosticCollector.getDiagnostics()) { - if (diagnostic.getKind() != javax.tools.Diagnostic.Kind.ERROR) continue; - - if (humanReadable) { - builder.append(diagnostic).append("\n"); - } - else { - builder.append(new File(diagnostic.getSource().toUri()).getName()).append(":") - .append(diagnostic.getLineNumber()).append(":") - .append(diagnostic.getColumnNumber()).append(":") - .append(diagnostic.getCode()).append("\n"); - } - } - return builder.toString(); - } - - public static String navigationMetadata(@TestDataFile String testFile) { - return testFile; - } - - public interface DoTest { - void invoke(String filePath) throws Exception; - } - - // In this test runner version the `testDataFile` parameter is annotated by `TestDataFile`. - // So only file paths passed to this parameter will be used in navigation actions, like "Navigate to testdata" and "Related Symbol..." - public static void runTest(DoTest test, TargetBackend targetBackend, @TestDataFile String testDataFile) throws Exception { - runTest0(test, targetBackend, testDataFile); - } - - // In this test runner version, NONE of the parameters are annotated by `TestDataFile`. - // So DevKit will use test name to determine related files in navigation actions, like "Navigate to testdata" and "Related Symbol..." - // - // Pro: - // * in most cases, it shows all related files including generated js files, for example. - // Cons: - // * sometimes, for too common/general names, it shows many variants to navigate - // * it adds an additional step for navigation -- you must choose an exact file to navigate - public static void runTest0(DoTest test, TargetBackend targetBackend, String testDataFilePath) throws Exception { - File testDataFile = new File(testDataFilePath); - - boolean isIgnored = isIgnoredTarget(targetBackend, testDataFile); - - try { - test.invoke(testDataFilePath); - } - catch (Throwable e) { - - if (!isIgnored && AUTOMATICALLY_MUTE_FAILED_TESTS) { - String text = doLoadFile(testDataFile); - String directive = InTextDirectivesUtils.IGNORE_BACKEND_DIRECTIVE_PREFIX + targetBackend.name() + "\n"; - - String newText; - if (text.startsWith("// !")) { - StringBuilder prefixBuilder = new StringBuilder(); - int l = 0; - while (text.startsWith("// !", l)) { - int r = text.indexOf("\n", l) + 1; - if (r <= 0) r = text.length(); - prefixBuilder.append(text.substring(l, r)); - l = r; - } - prefixBuilder.append(directive); - prefixBuilder.append(text.substring(l)); - - newText = prefixBuilder.toString(); - } else { - newText = directive + text; - } - - if (!newText.equals(text)) { - System.err.println("\"" + directive + "\" was added to \"" + testDataFile + "\""); - FileUtil.writeToFile(testDataFile, newText); - } - } - - if (RUN_IGNORED_TESTS_AS_REGULAR || !isIgnored) { - throw e; - } - - if (PRINT_STACKTRACE_FOR_IGNORED_TESTS) { - e.printStackTrace(); - } - return; - } - - if (isIgnored) { - if (AUTOMATICALLY_UNMUTE_PASSED_TESTS) { - String text = doLoadFile(testDataFile); - String directive = InTextDirectivesUtils.IGNORE_BACKEND_DIRECTIVE_PREFIX + targetBackend.name(); - String newText = Pattern.compile("^" + directive + "\n", Pattern.MULTILINE).matcher(text).replaceAll(""); - - if (!newText.equals(text)) { - System.err.println("\"" + directive + "\" was removed from \"" + testDataFile + "\""); - FileUtil.writeToFile(testDataFile, newText); - } - } - - throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive."); - } - } - - public static String getTestsRoot(@NotNull Class testCaseClass) { - TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class); - Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata); - return testClassMetadata.value(); - } - - /** - * @return test data file name specified in the metadata of test method - */ - @Nullable - public static String getTestDataFileName(@NotNull Class testCaseClass, @NotNull String testName) { - try { - Method method = testCaseClass.getDeclaredMethod(testName); - return getMethodMetadata(method); - } - catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - public static void assertAllTestsPresentByMetadata( - @NotNull Class testCaseClass, - @NotNull File testDataDir, - @NotNull Pattern filenamePattern, - @NotNull TargetBackend targetBackend, - boolean recursive, - @NotNull String... excludeDirs - ) { - File rootFile = new File(getTestsRoot(testCaseClass)); - - Set filePaths = collectPathsMetadata(testCaseClass); - Set exclude = SetsKt.setOf(excludeDirs); - - File[] files = testDataDir.listFiles(); - if (files != null) { - for (File file : files) { - if (file.isDirectory()) { - if (recursive && containsTestData(file, filenamePattern) && !exclude.contains(file.getName())) { - assertTestClassPresentByMetadata(testCaseClass, file); - } - } - else if (filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) { - assertFilePathPresent(file, rootFile, filePaths); - } - } - } - } - - public static void assertAllTestsPresentInSingleGeneratedClass( - @NotNull Class testCaseClass, - @NotNull File testDataDir, - @NotNull Pattern filenamePattern, - @NotNull TargetBackend targetBackend - ) { - File rootFile = new File(getTestsRoot(testCaseClass)); - - Set filePaths = collectPathsMetadata(testCaseClass); - - FileUtil.processFilesRecursively(testDataDir, file -> { - if (file.isFile() && filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) { - assertFilePathPresent(file, rootFile, filePaths); - } - - return true; - }); - } - - private static void assertFilePathPresent(File file, File rootFile, Set filePaths) { - String path = FileUtil.getRelativePath(rootFile, file); - if (path != null) { - String relativePath = nameToCompare(path); - if (!filePaths.contains(relativePath)) { - Assert.fail("Test data file missing from the generated test class: " + file + "\n" + PLEASE_REGENERATE_TESTS); - } - } - } - - private static Set collectPathsMetadata(Class testCaseClass) { - return ContainerUtil.newHashSet(ContainerUtil.map(collectMethodsMetadata(testCaseClass), KotlinTestUtils::nameToCompare)); - } - - @Nullable - private static String getMethodMetadata(Method method) { - TestMetadata testMetadata = method.getAnnotation(TestMetadata.class); - return (testMetadata != null) ? testMetadata.value() : null; - } - - private static Set collectMethodsMetadata(Class testCaseClass) { - Set filePaths = new HashSet<>(); - for (Method method : testCaseClass.getDeclaredMethods()) { - String path = getMethodMetadata(method); - if (path != null) { - filePaths.add(path); - } - } - return filePaths; - } - - private static boolean containsTestData(File dir, Pattern filenamePattern) { - File[] files = dir.listFiles(); - assert files != null; - for (File file : files) { - if (file.isDirectory()) { - if (containsTestData(file, filenamePattern)) { - return true; - } - } - else { - if (filenamePattern.matcher(file.getName()).matches()) { - return true; - } - } - } - return false; - } - - private static void assertTestClassPresentByMetadata(@NotNull Class outerClass, @NotNull File testDataDir) { - for (Class nestedClass : outerClass.getDeclaredClasses()) { - TestMetadata testMetadata = nestedClass.getAnnotation(TestMetadata.class); - if (testMetadata != null && testMetadata.value().equals(getFilePath(testDataDir))) { - return; - } - } - Assert.fail("Test data directory missing from the generated test class: " + testDataDir + "\n" + PLEASE_REGENERATE_TESTS); - } - - @NotNull - public static KtFile loadJetFile(@NotNull Project project, @NotNull File ioFile) throws IOException { - String text = FileUtil.loadFile(ioFile, true); - return KtPsiFactoryKt.KtPsiFactory(project).createPhysicalFile(ioFile.getName(), text); - } - - @NotNull - public static List loadToJetFiles(@NotNull KotlinCoreEnvironment environment, @NotNull List files) throws IOException { - List jetFiles = Lists.newArrayList(); - for (File file : files) { - jetFiles.add(loadJetFile(environment.getProject(), file)); - } - return jetFiles; - } - - @NotNull - public static ModuleDescriptorImpl createEmptyModule() { - return createEmptyModule(""); - } - - @NotNull - public static ModuleDescriptorImpl createEmptyModule(@NotNull String name) { - return createEmptyModule(name, DefaultBuiltIns.getInstance()); - } - - @NotNull - public static ModuleDescriptorImpl createEmptyModule(@NotNull String name, @NotNull KotlinBuiltIns builtIns) { - return new ModuleDescriptorImpl(Name.special(name), LockBasedStorageManager.NO_LOCKS, builtIns); - } - - @NotNull - public static File replaceExtension(@NotNull File file, @Nullable String newExtension) { - return new File(file.getParentFile(), FileUtil.getNameWithoutExtension(file) + (newExtension == null ? "" : "." + newExtension)); - } - - public static boolean isAllFilesPresentTest(String testName) { - //noinspection SpellCheckingInspection - return testName.toLowerCase().startsWith("allfilespresentin"); - } - - public static String nameToCompare(@NotNull String name) { - return (SystemInfo.isFileSystemCaseSensitive ? name : name.toLowerCase()).replace('\\', '/'); - } - - public static boolean isMultiExtensionName(@NotNull String name) { - int firstDotIndex = name.indexOf('.'); - if (firstDotIndex == -1) { - return false; - } - // Several extension if name contains another dot - return name.indexOf('.', firstDotIndex + 1) != -1; - } - - public static void setIdeaSystemPathProperties() { - System.setProperty(PROPERTY_SYSTEM_PATH, IDEA_SYSTEM_PATH); - System.setProperty(PROPERTY_CONFIG_PATH, IDEA_SYSTEM_PATH + "/config"); - } -} diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/KtParsingTestCase.java.as32 b/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/KtParsingTestCase.java.as32 deleted file mode 100644 index ffc6ea62171..00000000000 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/KtParsingTestCase.java.as32 +++ /dev/null @@ -1,335 +0,0 @@ -/* - * Copyright 2000-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.test.testFramework; - -import com.intellij.core.CoreASTFactory; -import com.intellij.lang.*; -import com.intellij.lang.impl.PsiBuilderFactoryImpl; -import com.intellij.mock.*; -import com.intellij.openapi.Disposable; -import com.intellij.openapi.application.PathManager; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.extensions.ExtensionPointName; -import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.fileEditor.FileDocumentManager; -import com.intellij.openapi.fileEditor.impl.LoadTextUtil; -import com.intellij.openapi.fileTypes.FileTypeFactory; -import com.intellij.openapi.fileTypes.FileTypeManager; -import com.intellij.openapi.progress.EmptyProgressIndicator; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.progress.impl.CoreProgressManager; -import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.util.Key; -import com.intellij.openapi.util.TextRange; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.vfs.CharsetToolkit; -import com.intellij.pom.PomModel; -import com.intellij.pom.core.impl.PomModelImpl; -import com.intellij.pom.tree.TreeAspect; -import com.intellij.psi.*; -import com.intellij.psi.impl.*; -import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry; -import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl; -import com.intellij.psi.util.CachedValuesManager; -import com.intellij.testFramework.LightVirtualFile; -import com.intellij.testFramework.TestDataFile; -import com.intellij.util.CachedValuesManagerImpl; -import com.intellij.util.Function; -import com.intellij.util.messages.MessageBus; -import com.intellij.util.messages.MessageBusFactory; -import junit.framework.TestCase; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.picocontainer.ComponentAdapter; -import org.picocontainer.MutablePicoContainer; - -import java.io.File; -import java.io.IOException; -import java.util.Set; - -@SuppressWarnings("ALL") -public abstract class KtParsingTestCase extends KtPlatformLiteFixture { - public static final Key HARD_REF_TO_DOCUMENT_KEY = Key.create("HARD_REF_TO_DOCUMENT_KEY"); - protected String myFilePrefix = ""; - protected String myFileExt; - protected final String myFullDataPath; - protected PsiFile myFile; - private MockPsiManager myPsiManager; - private PsiFileFactoryImpl myFileFactory; - protected Language myLanguage; - private final ParserDefinition[] myDefinitions; - private final boolean myLowercaseFirstLetter; - - protected KtParsingTestCase(@NonNls @NotNull String dataPath, @NotNull String fileExt, @NotNull ParserDefinition... definitions) { - this(dataPath, fileExt, false, definitions); - } - - protected KtParsingTestCase(@NonNls @NotNull String dataPath, @NotNull String fileExt, boolean lowercaseFirstLetter, @NotNull ParserDefinition... definitions) { - myDefinitions = definitions; - myFullDataPath = getTestDataPath() + "/" + dataPath; - myFileExt = fileExt; - myLowercaseFirstLetter = lowercaseFirstLetter; - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - initApplication(); - ComponentAdapter component = getApplication().getPicoContainer().getComponentAdapter(ProgressManager.class.getName()); - - Extensions.registerAreaClass("IDEA_PROJECT", null); - myProject = new MockProjectEx(getTestRootDisposable()); - myPsiManager = new MockPsiManager(myProject); - myFileFactory = new PsiFileFactoryImpl(myPsiManager); - MutablePicoContainer appContainer = getApplication().getPicoContainer(); - registerComponentInstance(appContainer, MessageBus.class, MessageBusFactory.newMessageBus(getApplication())); - final MockEditorFactory editorFactory = new MockEditorFactory(); - registerComponentInstance(appContainer, EditorFactory.class, editorFactory); - registerComponentInstance(appContainer, FileDocumentManager.class, new MockFileDocumentManagerImpl(new Function() { - @Override - public Document fun(CharSequence charSequence) { - return editorFactory.createDocument(charSequence); - } - }, HARD_REF_TO_DOCUMENT_KEY)); - registerComponentInstance(appContainer, PsiDocumentManager.class, new MockPsiDocumentManager()); - registerApplicationService(PsiBuilderFactory.class, new PsiBuilderFactoryImpl()); - registerApplicationService(DefaultASTFactory.class, new CoreASTFactory()); - registerApplicationService(ReferenceProvidersRegistry.class, new ReferenceProvidersRegistryImpl()); - - registerApplicationService(ProgressManager.class, new CoreProgressManager()); - - myProject.registerService(CachedValuesManager.class, new CachedValuesManagerImpl(myProject, new PsiCachedValuesFactory(myPsiManager))); - myProject.registerService(PsiManager.class, myPsiManager); - - this.registerExtensionPoint(FileTypeFactory.FILE_TYPE_FACTORY_EP, FileTypeFactory.class); - registerExtensionPoint(MetaLanguage.EP_NAME, MetaLanguage.class); - - for (ParserDefinition definition : myDefinitions) { - addExplicitExtension(LanguageParserDefinitions.INSTANCE, definition.getFileNodeType().getLanguage(), definition); - } - if (myDefinitions.length > 0) { - configureFromParserDefinition(myDefinitions[0], myFileExt); - } - - // That's for reparse routines - final PomModelImpl pomModel = new PomModelImpl(myProject); - myProject.registerService(PomModel.class, pomModel); - new TreeAspect(pomModel); - } - - public void configureFromParserDefinition(ParserDefinition definition, String extension) { - myLanguage = definition.getFileNodeType().getLanguage(); - myFileExt = extension; - addExplicitExtension(LanguageParserDefinitions.INSTANCE, this.myLanguage, definition); - registerComponentInstance( - getApplication().getPicoContainer(), FileTypeManager.class, - new MockFileTypeManager(new MockLanguageFileType(myLanguage, myFileExt))); - } - - protected void addExplicitExtension(final LanguageExtension instance, final Language language, final T object) { - instance.addExplicitExtension(language, object); - Disposer.register(myProject, new Disposable() { - @Override - public void dispose() { - instance.removeExplicitExtension(language, object); - } - }); - } - - @Override - protected void registerExtensionPoint(final ExtensionPointName extensionPointName, Class aClass) { - super.registerExtensionPoint(extensionPointName, aClass); - Disposer.register(myProject, new Disposable() { - @Override - public void dispose() { - Extensions.getRootArea().unregisterExtensionPoint(extensionPointName.getName()); - } - }); - } - - protected void registerApplicationService(final Class aClass, T object) { - getApplication().registerService(aClass, object); - Disposer.register(myProject, new Disposable() { - @Override - public void dispose() { - getApplication().getPicoContainer().unregisterComponent(aClass.getName()); - } - }); - } - - public MockProjectEx getProject() { - return myProject; - } - - public MockPsiManager getPsiManager() { - return myPsiManager; - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - myFile = null; - myProject = null; - myPsiManager = null; - } - - protected String getTestDataPath() { - return PathManager.getHomePath(); - } - - @NotNull - public final String getTestName() { - return getTestName(myLowercaseFirstLetter); - } - - protected boolean includeRanges() { - return false; - } - - protected boolean skipSpaces() { - return false; - } - - protected boolean checkAllPsiRoots() { - return true; - } - - protected void doTest(boolean checkResult) { - String name = getTestName(); - try { - String text = loadFile(name + "." + myFileExt); - myFile = createPsiFile(name, text); - ensureParsed(myFile); - assertEquals("light virtual file text mismatch", text, ((LightVirtualFile)myFile.getVirtualFile()).getContent().toString()); - assertEquals("virtual file text mismatch", text, LoadTextUtil.loadText(myFile.getVirtualFile())); - assertEquals("doc text mismatch", text, myFile.getViewProvider().getDocument().getText()); - assertEquals("psi text mismatch", text, myFile.getText()); - ensureCorrectReparse(myFile); - if (checkResult){ - checkResult(name, myFile); - } - else{ - toParseTreeText(myFile, skipSpaces(), includeRanges()); - } - } - catch (IOException e) { - throw new RuntimeException(e); - } - } - - protected void doTest(String suffix) throws IOException { - String name = getTestName(); - String text = loadFile(name + "." + myFileExt); - myFile = createPsiFile(name, text); - ensureParsed(myFile); - assertEquals(text, myFile.getText()); - checkResult(name + suffix, myFile); - } - - protected void doCodeTest(String code) throws IOException { - String name = getTestName(); - myFile = createPsiFile("a", code); - ensureParsed(myFile); - assertEquals(code, myFile.getText()); - checkResult(myFilePrefix + name, myFile); - } - - protected PsiFile createPsiFile(String name, String text) { - return createFile(name + "." + myFileExt, text); - } - - protected PsiFile createFile(@NonNls String name, String text) { - LightVirtualFile virtualFile = new LightVirtualFile(name, myLanguage, text); - virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET); - return createFile(virtualFile); - } - - protected PsiFile createFile(LightVirtualFile virtualFile) { - return myFileFactory.trySetupPsiForFile(virtualFile, myLanguage, true, false); - } - - protected void checkResult(@NonNls @TestDataFile String targetDataName, final PsiFile file) throws IOException { - doCheckResult(myFullDataPath, file, checkAllPsiRoots(), targetDataName, skipSpaces(), includeRanges()); - } - - public static void doCheckResult(String testDataDir, - PsiFile file, - boolean checkAllPsiRoots, - String targetDataName, - boolean skipSpaces, - boolean printRanges) throws IOException { - FileViewProvider provider = file.getViewProvider(); - Set languages = provider.getLanguages(); - - if (!checkAllPsiRoots || languages.size() == 1) { - doCheckResult(testDataDir, targetDataName + ".txt", toParseTreeText(file, skipSpaces, printRanges).trim()); - return; - } - - for (Language language : languages) { - PsiFile root = provider.getPsi(language); - String expectedName = targetDataName + "." + language.getID() + ".txt"; - doCheckResult(testDataDir, expectedName, toParseTreeText(root, skipSpaces, printRanges).trim()); - } - } - - protected void checkResult(String actual) throws IOException { - String name = getTestName(); - doCheckResult(myFullDataPath, myFilePrefix + name + ".txt", actual); - } - - protected void checkResult(@TestDataFile @NonNls String targetDataName, String actual) throws IOException { - doCheckResult(myFullDataPath, targetDataName, actual); - } - - public static void doCheckResult(String fullPath, String targetDataName, String actual) throws IOException { - String expectedFileName = fullPath + File.separatorChar + targetDataName; - KtUsefulTestCase.assertSameLinesWithFile(expectedFileName, actual); - } - - protected static String toParseTreeText(PsiElement file, boolean skipSpaces, boolean printRanges) { - return DebugUtil.psiToString(file, skipSpaces, printRanges); - } - - protected String loadFile(@NonNls @TestDataFile String name) throws IOException { - return loadFileDefault(myFullDataPath, name); - } - - public static String loadFileDefault(String dir, String name) throws IOException { - return FileUtil.loadFile(new File(dir, name), CharsetToolkit.UTF8, true).trim(); - } - - public static void ensureParsed(PsiFile file) { - file.accept(new PsiElementVisitor() { - @Override - public void visitElement(PsiElement element) { - element.acceptChildren(this); - } - }); - } - - public static void ensureCorrectReparse(@NotNull PsiFile file) { - String psiToStringDefault = DebugUtil.psiToString(file, false, false); - String fileText = file.getText(); - DiffLog diffLog = (new BlockSupportImpl(file.getProject())).reparseRange( - file, file.getNode(), TextRange.allOf(fileText), fileText, new EmptyProgressIndicator(), fileText); - diffLog.performActualPsiChange(file); - - TestCase.assertEquals(psiToStringDefault, DebugUtil.psiToString(file, false, false)); - } -} \ No newline at end of file