diff --git a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java index f433f5d1b0c..c44f64651ef 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java @@ -17,11 +17,21 @@ package org.jetbrains.k2js.test; import com.google.common.collect.Lists; +import com.google.dart.compiler.backend.js.ast.JsNode; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.StandardFileSystems; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileManager; +import com.intellij.openapi.vfs.VirtualFileSystem; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; import com.intellij.util.Consumer; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.OutputFileCollection; +import org.jetbrains.jet.cli.common.output.outputUtils.OutputUtilsPackage; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.config.CompilerConfiguration; import org.jetbrains.jet.lang.psi.JetFile; @@ -29,17 +39,16 @@ import org.jetbrains.jet.lang.resolve.lazy.KotlinTestWithEnvironment; import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.facade.MainCallParameters; -import org.jetbrains.k2js.test.config.TestConfig; -import org.jetbrains.k2js.test.config.TestConfigFactory; +import org.jetbrains.k2js.test.config.LibrarySourcesConfigWithCaching; import org.jetbrains.k2js.test.rhino.RhinoResultChecker; import org.jetbrains.k2js.test.utils.JsTestUtils; -import org.jetbrains.k2js.test.utils.TranslationUtils; import java.io.File; import java.util.Collections; import java.util.List; import java.util.Map; +import static org.jetbrains.k2js.facade.K2JSTranslator.translateWithMainCallParameters; import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; import static org.jetbrains.k2js.test.utils.JsTestUtils.convertFileNameToDotJsFile; @@ -48,21 +57,24 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { protected static final Iterable DEFAULT_ECMA_VERSIONS = Lists.newArrayList(EcmaVersion.v5); private static final boolean DELETE_OUT = false; - private static final String TEST_FILES = "js/js.translator/testData/"; + + public static final String TEST_DATA_DIR_PATH = "js/js.translator/testData/"; private static final String CASES = "cases/"; private static final String OUT = "out/"; private static final String EXPECTED = "expected/"; private static final String COMMON_FILES_DIR = "_commonFiles/"; + public static final String TEST_MODULE = "JS_TESTS"; public static final String TEST_PACKAGE = "foo"; public static final String TEST_FUNCTION = "box"; + public static final boolean IS_INLINE_ENABLED = true; @NotNull - private String mainDirectory = ""; + private String relativePathToTestDir = ""; @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") - public BasicTest(@NotNull String main) { - this.mainDirectory = main; + public BasicTest(@NotNull String relativePathToTestDir) { + this.relativePathToTestDir = relativePathToTestDir; } @Override @@ -70,11 +82,6 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { return JetCoreEnvironment.createForTests(getTestRootDisposable(), new CompilerConfiguration()); } - @NotNull - public String getMainDirectory() { - return mainDirectory; - } - protected boolean shouldCreateOut() { return true; } @@ -103,35 +110,13 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { assert success; } - @NotNull - protected List additionalJSFiles(@NotNull EcmaVersion ecmaVersion) { - return Lists.newArrayList(); - } - - protected void generateJavaScriptFiles(@NotNull String kotlinFilename, - @NotNull MainCallParameters mainCallParameters, - @NotNull Iterable ecmaVersions) throws Exception { - generateJavaScriptFiles(Collections.singletonList(getInputFilePath(kotlinFilename)), kotlinFilename, mainCallParameters, - ecmaVersions); - } - protected void generateJavaScriptFiles( - @NotNull List files, - @NotNull String testName, + @NotNull String kotlinFilename, @NotNull MainCallParameters mainCallParameters, - @NotNull Iterable ecmaVersions, - @NotNull TestConfigFactory configFactory + @NotNull Iterable ecmaVersions ) throws Exception { - Project project = getProject(); - List allFiles = withAdditionalFiles(files); - List jetFiles = TranslationUtils.createJetFileList(project, allFiles, null); - - for (EcmaVersion version : ecmaVersions) { - Config config = TranslationUtils.getConfig(project, version, configFactory); - File outputFile = new File(getOutputFilePath(testName, version)); - - translateFiles(jetFiles, outputFile, mainCallParameters, config); - } + generateJavaScriptFiles(Collections.singletonList(getInputFilePath(kotlinFilename)), + kotlinFilename, mainCallParameters, ecmaVersions); } protected void generateJavaScriptFiles( @@ -140,7 +125,16 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { @NotNull MainCallParameters mainCallParameters, @NotNull Iterable ecmaVersions ) throws Exception { - generateJavaScriptFiles(files, testName, mainCallParameters, ecmaVersions, getConfigFactory()); + Project project = getProject(); + List allFiles = withAdditionalKotlinFiles(files); + List jetFiles = createJetFileList(project, allFiles, null); + + for (EcmaVersion version : ecmaVersions) { + Config config = createConfig(getProject(), TEST_MODULE, version); + File outputFile = new File(getOutputFilePath(testName, version)); + + translateFiles(jetFiles, outputFile, mainCallParameters, config); + } } protected void translateFiles( @@ -150,25 +144,46 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { @NotNull Config config ) throws Exception { //noinspection unchecked - TranslationUtils.translateFiles(mainCallParameters, jetFiles, outputFile, null, null, config, Consumer.EMPTY_CONSUMER); + OutputFileCollection outputFiles = + translateWithMainCallParameters(mainCallParameters, jetFiles, outputFile, + getOutputPrefixFile(), getOutputPostfixFile(), + config, getConsumer()); + + File outputDir = outputFile.getParentFile(); + assert outputDir != null : "Parent file for output file should not be null, outputFilePath: " + outputFile.getPath(); + OutputUtilsPackage.writeAllTo(outputFiles, outputDir); } - @NotNull - protected TestConfigFactory getConfigFactory() { - return TestConfig.FACTORY_WITHOUT_SOURCEMAP; + protected File getOutputPostfixFile() { + return null; } - @NotNull - private List withAdditionalFiles(@NotNull List files) { - List result = Lists.newArrayList(files); - result.addAll(additionalKotlinFiles()); - return result; + protected File getOutputPrefixFile() { + return null; } - protected void runRhinoTests(@NotNull String filename, @NotNull Iterable ecmaVersions, - @NotNull RhinoResultChecker checker) throws Exception { + protected boolean shouldBeTranslateAsUnitTestClass() { + return false; + } + + protected boolean shouldGenerateSourcemap() { + return false; + } + + protected Consumer getConsumer() { + //noinspection unchecked + return Consumer.EMPTY_CONSUMER; + } + + protected void runRhinoTests( + @NotNull String filename, + @NotNull Iterable ecmaVersions, + @NotNull RhinoResultChecker checker + ) throws Exception { for (EcmaVersion ecmaVersion : ecmaVersions) { - runRhinoTest(withAdditionalFiles(getOutputFilePath(filename, ecmaVersion), ecmaVersion), checker, getRhinoTestVariables(), + runRhinoTest(withAdditionalJsFiles(getOutputFilePath(filename, ecmaVersion), ecmaVersion), + checker, + getRhinoTestVariables(), ecmaVersion); } } @@ -180,67 +195,88 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { @NotNull protected List additionalKotlinFiles() { List additionalFiles = Lists.newArrayList(); - additionalFiles.addAll(JsTestUtils.kotlinFilesInDirectory(pathToTestFilesRoot() + COMMON_FILES_DIR)); - additionalFiles.addAll(JsTestUtils.kotlinFilesInDirectory(pathToTestFiles() + COMMON_FILES_DIR)); + additionalFiles.addAll(JsTestUtils.kotlinFilesInDirectory(TEST_DATA_DIR_PATH + COMMON_FILES_DIR)); + additionalFiles.addAll(JsTestUtils.kotlinFilesInDirectory(pathToTestDir() + COMMON_FILES_DIR)); return additionalFiles; } - protected static String casesDirectoryName() { - return CASES; + @NotNull + protected List additionalJsFiles(@NotNull EcmaVersion ecmaVersion) { + return Lists.newArrayList(); } - private static String outDirectoryName() { - return OUT; - } + // helpers - private static String expectedDirectoryName() { - return EXPECTED; + @NotNull + protected final String pathToTestDir() { + return TEST_DATA_DIR_PATH + relativePathToTestDir; } @NotNull - protected String pathToTestFiles() { - return pathToTestFilesRoot() + getMainDirectory(); - } - - @NotNull - public static String pathToTestFilesRoot() { - return TEST_FILES; - } - - @NotNull - private String getOutputPath() { - return pathToTestFiles() + outDirectoryName(); - } - - @NotNull - public String getInputPath() { - return pathToTestFiles() + casesDirectoryName(); - } - - @NotNull - private String getExpectedPath() { - return pathToTestFiles() + expectedDirectoryName(); - } - - @NotNull - protected List withAdditionalFiles(@NotNull String inputFile, @NotNull EcmaVersion ecmaVersion) { - List allFiles = Lists.newArrayList(additionalJSFiles(ecmaVersion)); - allFiles.add(inputFile); - return allFiles; - } - - @NotNull - protected String getOutputFilePath(@NotNull String filename, @NotNull EcmaVersion ecmaVersion) { + protected final String getOutputFilePath(@NotNull String filename, @NotNull EcmaVersion ecmaVersion) { return getOutputPath() + convertFileNameToDotJsFile(filename, ecmaVersion); } @NotNull - protected String getInputFilePath(@NotNull String filename) { + protected final String getInputFilePath(@NotNull String filename) { return getInputPath() + filename; } @NotNull - protected String expected(@NotNull String testName) { + protected final String expectedFilePath(@NotNull String testName) { return getExpectedPath() + testName + ".out"; } + + @NotNull + private Config createConfig(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion) { + return new LibrarySourcesConfigWithCaching(project, moduleId, ecmaVersion, + shouldGenerateSourcemap(), IS_INLINE_ENABLED, shouldBeTranslateAsUnitTestClass()); + } + + @NotNull + private String getOutputPath() { + return pathToTestDir() + OUT; + } + + @NotNull + protected String getInputPath() { + return pathToTestDir() + CASES; + } + + @NotNull + private String getExpectedPath() { + return pathToTestDir() + EXPECTED; + } + + @NotNull + private List withAdditionalKotlinFiles(@NotNull List files) { + List result = Lists.newArrayList(files); + result.addAll(additionalKotlinFiles()); + return result; + } + + @NotNull + private List withAdditionalJsFiles(@NotNull String inputFile, @NotNull EcmaVersion ecmaVersion) { + List allFiles = Lists.newArrayList(additionalJsFiles(ecmaVersion)); + allFiles.add(inputFile); + return allFiles; + } + + private static List createJetFileList(@NotNull Project project, @NotNull List list, @Nullable String root) { + List libFiles = Lists.newArrayList(); + + PsiManager psiManager = PsiManager.getInstance(project); + VirtualFileSystem fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL); + + VirtualFile rootFile = root == null ? null : fileSystem.findFileByPath(root); + + for (String libFileName : list) { + VirtualFile virtualFile = rootFile == null ? fileSystem.findFileByPath(libFileName) : rootFile.findFileByRelativePath(libFileName); + //TODO logging? + assert virtualFile != null; + PsiFile psiFile = psiManager.findFile(virtualFile); + libFiles.add((JetFile) psiFile); + } + return libFiles; + } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java index 0556720d09e..9afe2069bd0 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java @@ -49,7 +49,7 @@ public abstract class MultipleFilesTranslationTest extends BasicTest { @NotNull Object expectedResult ) throws Exception { generateJsFromDir(dirName, ecmaVersions); - runRhinoTests(dirName + ".kt", ecmaVersions, new RhinoFunctionResultChecker(packageName, functionName, expectedResult)); + runRhinoTests(dirName + ".kt", ecmaVersions, new RhinoFunctionResultChecker(TEST_MODULE, packageName, functionName, expectedResult)); } public void checkFooBoxIsTrue(@NotNull String dirName) throws Exception { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/OutputPrefixPostfixTest.java b/js/js.tests/test/org/jetbrains/k2js/test/OutputPrefixPostfixTest.java index 89f71ecff90..a2b7da99e73 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/OutputPrefixPostfixTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/OutputPrefixPostfixTest.java @@ -17,19 +17,13 @@ package org.jetbrains.k2js.test; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.util.Consumer; import junit.framework.Test; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.EcmaVersion; -import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.test.semantics.TranslatorTestCaseBuilder; -import org.jetbrains.k2js.test.utils.TranslationUtils; import java.io.File; -import java.util.List; @SuppressWarnings("JUnitTestCaseWithNoTests") public final class OutputPrefixPostfixTest extends SingleFileTranslationTest { @@ -52,14 +46,13 @@ public final class OutputPrefixPostfixTest extends SingleFileTranslationTest { } @Override - protected void translateFiles( - @NotNull List jetFiles, - @NotNull File outputFile, - @NotNull MainCallParameters mainCallParameters, - @NotNull Config config - ) throws Exception { - //noinspection unchecked - TranslationUtils.translateFiles(mainCallParameters, jetFiles, outputFile, outputPrefixFile, outputPostfixFile, config, Consumer.EMPTY_CONSUMER); + protected File getOutputPostfixFile() { + return outputPostfixFile; + } + + @Override + protected File getOutputPrefixFile() { + return outputPrefixFile; } @Override @@ -90,7 +83,7 @@ public final class OutputPrefixPostfixTest extends SingleFileTranslationTest { public static Test suite() throws Exception { return TranslatorTestCaseBuilder - .suiteForDirectory(BasicTest.pathToTestFilesRoot() + "outputPrefixPostfix/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { + .suiteForDirectory(TEST_DATA_DIR_PATH + "outputPrefixPostfix/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { @NotNull @Override public Test createTest(@NotNull String filename) { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java index 663328b561b..1c286d17bd7 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java @@ -43,7 +43,7 @@ public abstract class SingleFileTranslationTest extends BasicTest { @NotNull String functionName, @NotNull Object expectedResult) throws Exception { generateJavaScriptFiles(kotlinFilename, MainCallParameters.noCall(), ecmaVersions); - runRhinoTests(kotlinFilename, ecmaVersions, new RhinoFunctionResultChecker(packageName, functionName, expectedResult)); + runRhinoTests(kotlinFilename, ecmaVersions, new RhinoFunctionResultChecker(TEST_MODULE, packageName, functionName, expectedResult)); } public void checkFooBoxIsTrue(@NotNull String filename, @NotNull Iterable ecmaVersions) throws Exception { @@ -100,7 +100,7 @@ public abstract class SingleFileTranslationTest extends BasicTest { @NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception { - checkOutput(testName + ".kt", readFile(expected(testName + testId)), ecmaVersions, args); + checkOutput(testName + ".kt", readFile(expectedFilePath(testName + testId)), ecmaVersions, args); } protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/SourceMapTest.java b/js/js.tests/test/org/jetbrains/k2js/test/SourceMapTest.java index df0c26dd64c..36e3992fea5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/SourceMapTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/SourceMapTest.java @@ -18,8 +18,6 @@ package org.jetbrains.k2js.test; import junit.framework.Test; import org.jetbrains.annotations.NotNull; -import org.jetbrains.k2js.test.config.TestConfig; -import org.jetbrains.k2js.test.config.TestConfigFactory; import org.jetbrains.k2js.test.semantics.TranslatorTestCaseBuilder; @SuppressWarnings("JUnitTestCaseWithNoTests") @@ -33,10 +31,9 @@ public final class SourceMapTest extends SingleFileTranslationTest { this.filename = filename; } - @NotNull @Override - protected TestConfigFactory getConfigFactory() { - return TestConfig.FACTORY_WITH_SOURCEMAP; + protected boolean shouldGenerateSourcemap() { + return true; } @Override @@ -46,7 +43,7 @@ public final class SourceMapTest extends SingleFileTranslationTest { public static Test suite() throws Exception { return TranslatorTestCaseBuilder - .suiteForDirectory(BasicTest.pathToTestFilesRoot() + "sourcemap/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { + .suiteForDirectory(TEST_DATA_DIR_PATH + "sourcemap/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { @NotNull @Override public Test createTest(@NotNull String filename) { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java index 65856297287..cfd2d17efd0 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java @@ -33,10 +33,10 @@ import java.util.Map; import java.util.Set; import static org.jetbrains.jet.utils.UtilsPackage.rethrow; -import static org.jetbrains.k2js.test.BasicTest.pathToTestFilesRoot; +import static org.jetbrains.k2js.test.BasicTest.TEST_DATA_DIR_PATH; public final class RhinoUtils { - private static final String KOTLIN_JS_LIB_ECMA_5 = pathToTestFilesRoot() + "kotlin_lib_ecma5.js"; + private static final String KOTLIN_JS_LIB_ECMA_5 = TEST_DATA_DIR_PATH + "kotlin_lib_ecma5.js"; private static final Set IGNORED_JSHINT_WARNINGS = Sets.newHashSet(); @@ -186,10 +186,10 @@ public final class RhinoUtils { ScriptableObject scope = context.initStandardObjects(); try { runFileWithRhino(getKotlinLibFile(version), context, scope); - runFileWithRhino(pathToTestFilesRoot() + "kotlin_lib.js", context, scope); - runFileWithRhino(pathToTestFilesRoot() + "kotlin_lib_compiled.js", context, scope); - runFileWithRhino(pathToTestFilesRoot() + "maps.js", context, scope); - runFileWithRhino(pathToTestFilesRoot() + "long.js", context, scope); + runFileWithRhino(TEST_DATA_DIR_PATH + "kotlin_lib.js", context, scope); + runFileWithRhino(TEST_DATA_DIR_PATH + "kotlin_lib_compiled.js", context, scope); + runFileWithRhino(TEST_DATA_DIR_PATH + "maps.js", context, scope); + runFileWithRhino(TEST_DATA_DIR_PATH + "long.js", context, scope); //runFileWithRhino(pathToTestFilesRoot() + "jshint.js", context, scope); for (String jsLibrary : jsLibraries) { runFileWithRhino(jsLibrary, context, scope); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ExamplesTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ExamplesTest.java index 36b301a4d2a..b024a006420 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ExamplesTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ExamplesTest.java @@ -18,7 +18,6 @@ package org.jetbrains.k2js.test.semantics; import junit.framework.Test; import org.jetbrains.annotations.NotNull; -import org.jetbrains.k2js.test.BasicTest; import org.jetbrains.k2js.test.SingleFileTranslationTest; import org.jetbrains.k2js.translate.context.Namer; @@ -41,7 +40,7 @@ public final class ExamplesTest extends SingleFileTranslationTest { public static Test suite() throws Exception { return TranslatorTestCaseBuilder - .suiteForDirectory(BasicTest.pathToTestFilesRoot() + "examples/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { + .suiteForDirectory(TEST_DATA_DIR_PATH + "examples/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { @NotNull @Override public Test createTest(@NotNull String filename) { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineMultiFileTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineMultiFileTest.java index e6692d73056..0007a88bc02 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineMultiFileTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineMultiFileTest.java @@ -17,22 +17,18 @@ package org.jetbrains.k2js.test.semantics; import com.google.dart.compiler.backend.js.ast.JsNode; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.k2js.config.Config; -import org.jetbrains.k2js.facade.MainCallParameters; +import com.intellij.util.Consumer; import org.jetbrains.k2js.test.MultipleFilesTranslationTest; import org.jetbrains.k2js.test.utils.InlineTestUtils; import org.jetbrains.k2js.test.utils.JsTestUtils; -import org.jetbrains.k2js.test.utils.TranslationUtils; +import org.jetbrains.k2js.test.utils.MemoizeConsumer; -import java.io.File; import java.util.List; import static org.jetbrains.k2js.test.utils.JsTestUtils.getAllFilesInDir; public final class InlineMultiFileTest extends MultipleFilesTranslationTest { - private JsNode lastJsNode; + private final MemoizeConsumer nodeConsumer = new MemoizeConsumer(); public InlineMultiFileTest() { super("inlineMultiFile/"); @@ -41,7 +37,7 @@ public final class InlineMultiFileTest extends MultipleFilesTranslationTest { @Override public void setUp() throws Exception { super.setUp(); - lastJsNode = null; + nodeConsumer.consume(null); } public void testInlineMultiFileSimple() throws Exception { @@ -158,23 +154,23 @@ public final class InlineMultiFileTest extends MultipleFilesTranslationTest { checkFooBoxIsOk(); } - @Override - protected void translateFiles( - @NotNull List jetFiles, - @NotNull File outputFile, - @NotNull MainCallParameters mainCallParameters, - @NotNull Config config - ) throws Exception { - lastJsNode = TranslationUtils.translateFilesAndGetAst(mainCallParameters, jetFiles, outputFile, null, null, config); - } - private void processInlineDirectives() throws Exception { String dir = getTestName(true); List fileNames = getAllFilesInDir(getInputFilePath(dir)); for (String fileName : fileNames) { String fileText = JsTestUtils.readFile(fileName); + + JsNode lastJsNode = nodeConsumer.getLastValue(); + assert lastJsNode != null; + InlineTestUtils.processDirectives(lastJsNode, fileText); } } + + @Override + protected Consumer getConsumer() { + return nodeConsumer; + } + } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java index 611f878a6c9..dd2918021e0 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java @@ -17,19 +17,15 @@ package org.jetbrains.k2js.test.semantics; import com.google.dart.compiler.backend.js.ast.JsNode; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.k2js.config.Config; -import org.jetbrains.k2js.facade.MainCallParameters; +import com.intellij.util.Consumer; import org.jetbrains.k2js.inline.exception.InlineRecursionException; import org.jetbrains.k2js.test.SingleFileTranslationTest; -import org.jetbrains.k2js.test.utils.*; - -import java.io.File; -import java.util.List; +import org.jetbrains.k2js.test.utils.InlineTestUtils; +import org.jetbrains.k2js.test.utils.JsTestUtils; +import org.jetbrains.k2js.test.utils.MemoizeConsumer; public final class InlineTest extends SingleFileTranslationTest { - private JsNode lastJsNode; + private final MemoizeConsumer nodeConsumer = new MemoizeConsumer(); public InlineTest() { super("inline/"); @@ -38,7 +34,7 @@ public final class InlineTest extends SingleFileTranslationTest { @Override public void setUp() throws Exception { super.setUp(); - lastJsNode = null; + nodeConsumer.consume(null); } public void testInlineSimpleAssignment() throws Exception { @@ -262,16 +258,14 @@ public final class InlineTest extends SingleFileTranslationTest { String fileName = getInputFilePath(getTestName(true) + ".kt"); String fileText = JsTestUtils.readFile(fileName); + JsNode lastJsNode = nodeConsumer.getLastValue(); + assert lastJsNode != null; + InlineTestUtils.processDirectives(lastJsNode, fileText); } @Override - protected void translateFiles( - @NotNull List jetFiles, - @NotNull File outputFile, - @NotNull MainCallParameters mainCallParameters, - @NotNull Config config - ) throws Exception { - lastJsNode = TranslationUtils.translateFilesAndGetAst(mainCallParameters, jetFiles, outputFile, null, null, config); + protected Consumer getConsumer() { + return nodeConsumer; } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java index 88f646cf266..49196620429 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java @@ -19,23 +19,19 @@ package org.jetbrains.k2js.test.semantics; import com.google.common.collect.Lists; import junit.framework.Test; import org.jetbrains.annotations.NotNull; -import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.test.MultipleFilesTranslationTest; import org.jetbrains.k2js.test.config.JsUnitTestReporter; -import org.jetbrains.k2js.test.config.TestConfigWithUnitTests; import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker; import org.jetbrains.k2js.test.rhino.RhinoUtils; import java.util.*; -import static org.jetbrains.k2js.test.utils.LibraryFilePathsUtil.getAdditionalLibraryFiles; - public abstract class JsUnitTestBase extends MultipleFilesTranslationTest { @NotNull - private static final String JS_TESTS = pathToTestFilesRoot() + "jsTester/"; + private static final String JS_TESTS = TEST_DATA_DIR_PATH + "jsTester/"; @NotNull protected static final String JS_TESTS_KT = JS_TESTS + "jsTester.kt"; @NotNull @@ -51,8 +47,8 @@ public abstract class JsUnitTestBase extends MultipleFilesTranslationTest { @NotNull @Override - protected List additionalJSFiles(@NotNull EcmaVersion ecmaVersion) { - ArrayList result = Lists.newArrayList(super.additionalJSFiles(ecmaVersion)); + protected List additionalJsFiles(@NotNull EcmaVersion ecmaVersion) { + ArrayList result = Lists.newArrayList(super.additionalJsFiles(ecmaVersion)); result.add(JS_TESTS_JS); return result; } @@ -60,21 +56,21 @@ public abstract class JsUnitTestBase extends MultipleFilesTranslationTest { @NotNull @Override protected List additionalKotlinFiles() { - List result = Lists.newArrayList(); - List additionalLibraryFiles = getAdditionalLibraryFiles(); - additionalLibraryFiles.add(JS_TESTS_KT); - boolean removed = additionalLibraryFiles.remove(Config.LIBRARIES_LOCATION + "/stdlib/testCode.kt"); - assert removed; - result.addAll(additionalLibraryFiles); - result.add(Config.LIBRARIES_LOCATION + "/stdlib/TuplesCode.kt"); + List result = Lists.newArrayList(super.additionalKotlinFiles()); + result.add(JS_TESTS_KT); return result; } + @Override + protected boolean shouldBeTranslateAsUnitTestClass() { + return true; + } + public void runTestFile(@NotNull String pathToTestFile) throws Exception { Iterable versions = DEFAULT_ECMA_VERSIONS; String testName = pathToTestFile.substring(pathToTestFile.lastIndexOf("/")); - generateJavaScriptFiles(Lists.newArrayList(pathToTestFile), testName, MainCallParameters.noCall(), versions, - TestConfigWithUnitTests.FACTORY); + + generateJavaScriptFiles(Collections.singletonList(pathToTestFile), testName, MainCallParameters.noCall(), versions); runRhinoTests(testName, versions, new RhinoSystemOutputChecker("")); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java index e6a5f93c0d7..8e82512da24 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java @@ -35,10 +35,10 @@ public final class NativeInteropTest extends SingleFileTranslationTest { @NotNull @Override - protected List additionalJSFiles(@NotNull EcmaVersion ecmaVersion) { - List result = Lists.newArrayList(super.additionalJSFiles(ecmaVersion)); + protected List additionalJsFiles(@NotNull EcmaVersion ecmaVersion) { + List result = Lists.newArrayList(super.additionalJsFiles(ecmaVersion)); - String jsFilePath = pathToTestFiles() + NATIVE + "/" + getTestName(true) + ".js"; + String jsFilePath = pathToTestDir() + NATIVE + "/" + getTestName(true) + ".js"; File jsFile = new File(jsFilePath); if (jsFile.exists() && jsFile.isFile()) { result.add(jsFilePath); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/PropertyAccessTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/PropertyAccessTest.java index c0c08ebd70b..fc605d7710e 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/PropertyAccessTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/PropertyAccessTest.java @@ -100,10 +100,10 @@ public final class PropertyAccessTest extends SingleFileTranslationTest { @Override @NotNull - protected List additionalJSFiles(@NotNull EcmaVersion ecmaVersion) { - List result = Lists.newArrayList(super.additionalJSFiles(ecmaVersion)); + protected List additionalJsFiles(@NotNull EcmaVersion ecmaVersion) { + List result = Lists.newArrayList(super.additionalJsFiles(ecmaVersion)); if (getName().equals("testEnumerable")) { - result.add(pathToTestFiles() + "enumerate.js"); + result.add(pathToTestDir() + "enumerate.js"); } return result; } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java index 2415b8d41af..dbbcb2784f3 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java @@ -18,7 +18,6 @@ package org.jetbrains.k2js.test.semantics; import junit.framework.Test; import org.jetbrains.annotations.NotNull; -import org.jetbrains.k2js.test.BasicTest; import org.jetbrains.k2js.test.SingleFileTranslationTest; @SuppressWarnings("JUnitTestCaseWithNoTests") @@ -40,7 +39,7 @@ public final class SimpleTest extends SingleFileTranslationTest { public static Test suite() throws Exception { return TranslatorTestCaseBuilder - .suiteForDirectory(BasicTest.pathToTestFilesRoot() + "simple/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { + .suiteForDirectory(TEST_DATA_DIR_PATH + "simple/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() { @NotNull @Override public Test createTest(@NotNull String filename) { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java index ccec165f547..d4a0882b218 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java @@ -49,7 +49,7 @@ public final class StdLibTest extends SingleFileTranslationTest { generateJavaScriptFiles(files, kotlinFilename, mainCallParameters, ecmaVersions); runRhinoTests(kotlinFilename, ecmaVersions, - new RhinoFunctionNativeObjectResultChecker("test.browser", TEST_FUNCTION, "Some Dynamically Created Content!!!")); + new RhinoFunctionNativeObjectResultChecker(TEST_MODULE, "test.browser", TEST_FUNCTION, "Some Dynamically Created Content!!!")); } @Override diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/LibraryFilePathsUtil.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/LibraryFilePathsUtil.java deleted file mode 100644 index 916307b0d9e..00000000000 --- a/js/js.tests/test/org/jetbrains/k2js/test/utils/LibraryFilePathsUtil.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.k2js.test.utils; - -import com.google.common.base.Function; -import com.google.common.collect.Lists; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.k2js.config.Config; - -import java.util.ArrayList; -import java.util.List; - -public final class LibraryFilePathsUtil { - private LibraryFilePathsUtil() { - } - - @NotNull - public static List getBasicLibraryFiles() { - List files = new ArrayList(); - files.addAll(Lists.transform(Config.LIB_FILE_NAMES, new Function() { - @Override - public String apply(@Nullable String s) { - return Config.LIBRARIES_LOCATION + s; - } - })); - files.addAll(getReflectionLibraryFiles()); - return files; - } - - @NotNull - public static List getAdditionalLibraryFiles() { - List additionalKotlinFiles = Lists.newArrayList(); - // lets add the standard JS library files - Iterable names = Config.LIB_FILE_NAMES_DEPENDENT_ON_STDLIB; - for (String libFileName : names) { - System.out.println("Compiling " + libFileName); - additionalKotlinFiles.add(Config.LIBRARIES_LOCATION + libFileName); - } - - // lets add the standard Kotlin library files - for (String libFileName : Config.STDLIB_FILE_NAMES) { - System.out.println("Compiling " + libFileName); - additionalKotlinFiles.add(Config.STDLIB_LOCATION + libFileName); - } - return additionalKotlinFiles; - } - - @NotNull - public static List getReflectionLibraryFiles() { - return JsTestUtils.kotlinFilesInDirectory(Config.REFLECTION_LIB_LOCATION); - } -} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/MemoizeConsumer.kt b/js/js.tests/test/org/jetbrains/k2js/test/utils/MemoizeConsumer.kt new file mode 100644 index 00000000000..0ed550ecc99 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/utils/MemoizeConsumer.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.test.utils + +import com.intellij.util.Consumer +import com.google.dart.compiler.backend.js.ast.JsNode + +class MemoizeConsumer : Consumer { + var lastValue: T? = null + + override fun consume(value: T?) { + lastValue = value + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java deleted file mode 100644 index a3e38920754..00000000000 --- a/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * 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.k2js.test.utils; - -import com.google.common.base.Predicate; -import com.google.common.collect.Lists; -import com.google.dart.compiler.backend.js.ast.JsNode; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Ref; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.psi.PsiFile; -import com.intellij.util.Consumer; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.OutputFileCollection; -import org.jetbrains.jet.analyzer.AnalyzeExhaust; -import org.jetbrains.jet.cli.common.output.outputUtils.OutputUtilsPackage; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.k2js.analyze.TopDownAnalyzerFacadeForJS; -import org.jetbrains.k2js.config.Config; -import org.jetbrains.k2js.config.EcmaVersion; -import org.jetbrains.k2js.facade.MainCallParameters; -import org.jetbrains.k2js.facade.exceptions.TranslationException; -import org.jetbrains.k2js.test.config.TestConfigFactory; - -import java.io.File; -import java.io.IOException; -import java.lang.ref.SoftReference; -import java.util.List; - -import static org.jetbrains.jet.lang.psi.PsiPackage.JetPsiFactory; -import static org.jetbrains.k2js.facade.K2JSTranslator.translateWithMainCallParameters; - -//TODO: use method object -public final class TranslationUtils { - - private TranslationUtils() { - } - - @NotNull - private static SoftReference cachedLibraryExhaust = new SoftReference(null); - - @Nullable - private static List libFiles = null; - - @NotNull - private static List getAllLibFiles(@NotNull Project project) { - if (libFiles == null) { - libFiles = initLibFiles(project); - } - return libFiles; - } - - @NotNull - public static AnalyzeExhaust getLibraryAnalyzeExhaust(@NotNull Project project) { - AnalyzeExhaust cachedModule = cachedLibraryExhaust.get(); - if (cachedModule != null) { - return cachedModule; - } - List allLibFiles = getAllLibFiles(project); - Predicate filesWithCode = new Predicate() { - @Override - public boolean apply(@Nullable PsiFile file) { - assert file != null; - return isFileWithCode((JetFile) file); - } - }; - AnalyzeExhaust exhaust = TopDownAnalyzerFacadeForJS.analyzeFiles(allLibFiles, filesWithCode, Config.getEmptyConfig(project)); - TopDownAnalyzerFacadeForJS.checkForErrors(allLibFiles, exhaust.getBindingContext()); - cachedLibraryExhaust = new SoftReference(exhaust); - return exhaust; - } - - private static boolean isFileWithCode(@NotNull JetFile file) { - for (String filename : Config.LIB_FILES_WITH_CODE) { - if (file.getName().contains(filename)) { - return true; - } - } - return false; - } - - @NotNull - public static Config getConfig(@NotNull Project project, @NotNull EcmaVersion version, @NotNull TestConfigFactory configFactory) { - AnalyzeExhaust libraryAnalyzeExhaust = getLibraryAnalyzeExhaust(project); - return configFactory.create( - project, version, getLibFilesWithCode(getAllLibFiles(project)), - libraryAnalyzeExhaust.getBindingContext(), libraryAnalyzeExhaust.getModuleDescriptor()); - } - - public static void translateFiles( - @NotNull MainCallParameters mainCall, - @NotNull List files, - @NotNull File outputFile, - @Nullable File outputPrefixFile, - @Nullable File outputPostfixFile, - @NotNull Config config, - @NotNull Consumer astConsumer - ) throws TranslationException, IOException { - OutputFileCollection outputFiles = - translateWithMainCallParameters(mainCall, files, outputFile, outputPrefixFile, outputPostfixFile, config, astConsumer); - OutputUtilsPackage.writeAllTo(outputFiles, outputFile.getParentFile()); - } - - public static JsNode translateFilesAndGetAst( - @NotNull MainCallParameters mainCall, - @NotNull List files, - @NotNull File outputFile, - @Nullable File outputPrefixFile, - @Nullable File outputPostfixFile, - @NotNull Config config - ) throws TranslationException, IOException { - final Ref ref = new Ref(); - - translateFiles(mainCall, files, outputFile, outputPrefixFile, outputPostfixFile, config, new Consumer() { - @Override - public void consume(JsNode node) { - ref.set(node); - } - }); - - return ref.get(); - } - - @NotNull - private static List initLibFiles(@NotNull Project project) { - return createJetFileList(project, LibraryFilePathsUtil.getBasicLibraryFiles(), null); - } - - @NotNull - private static List getLibFilesWithCode(@NotNull List allFiles) { - List result = Lists.newArrayList(); - for (JetFile file : allFiles) { - if (isFileWithCode(file)) { - result.add(file); - } - } - return result; - } - - public static List createJetFileList(@NotNull Project project, @NotNull List list, @Nullable String root) { - List libFiles = Lists.newArrayList(); - for (String libFileName : list) { - try { - String path = root == null ? libFileName : (root + libFileName); - String text = FileUtil.loadFile(new File(path), true); - JetFile jetFile = JetPsiFactory(project).createFile(path, text); - libFiles.add(jetFile); - } - catch (IOException e) { - //TODO: throw generic exception - throw new IllegalStateException(e); - } - } - return libFiles; - - } -}