diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 26783685d23..1a01af39e66 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -1279,6 +1279,10 @@ fun main(args: Array) { testClass { model("sourcemap/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS) } + + testClass { + model("outputPrefixPostfix/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS) + } } testGroup("js/js.tests/test", "compiler/testData") { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt index 96b2b7e59d5..f2ada01b605 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt @@ -68,15 +68,21 @@ abstract class BasicBoxTest( ) : KotlinTestWithEnvironment() { val additionalCommonFileDirectories = mutableListOf() + protected open fun getOutputPrefixFile(testFilePath: String): File? = null + protected open fun getOutputPostfixFile(testFilePath: String): File? = null + fun doTest(filePath: String) { val file = File(filePath) val outputDir = getOutputDir(file) - val expectedText = KotlinTestUtils.doLoadFile(file) + val fileContent = KotlinTestUtils.doLoadFile(file) + + val outputPrefixFile = getOutputPrefixFile(filePath) + val outputPostfixFile = getOutputPostfixFile(filePath) TestFileFactoryImpl().use { testFactory -> testFactory.defaultModule.moduleKind - val inputFiles = KotlinTestUtils.createTestFiles(file.name, expectedText, testFactory) + val inputFiles = KotlinTestUtils.createTestFiles(file.name, fileContent, testFactory) val modules = inputFiles .map { it.module }.distinct() .map { it.name to it }.toMap() @@ -87,7 +93,7 @@ abstract class BasicBoxTest( val dependencies = module.dependencies.mapNotNull { modules[it]?.outputFileName(outputDir) + ".meta.js" } val outputFileName = module.outputFileName(outputDir) + ".js" - generateJavaScriptFile(file.parent, module, outputFileName, dependencies, modules.size > 1) + generateJavaScriptFile(file.parent, module, outputFileName, dependencies, modules.size > 1, outputPrefixFile, outputPostfixFile) if (!module.name.endsWith(OLD_MODULE_SUFFIX)) outputFileName else null } @@ -113,10 +119,10 @@ abstract class BasicBoxTest( val additionalFiles = mutableListOf() - val moduleKindMatcher = MODULE_KIND_PATTERN.matcher(expectedText) + val moduleKindMatcher = MODULE_KIND_PATTERN.matcher(fileContent) val moduleKind = if (moduleKindMatcher.find()) ModuleKind.valueOf(moduleKindMatcher.group(1)) else ModuleKind.PLAIN - val withModuleSystem = moduleKind != ModuleKind.PLAIN && !NO_MODULE_SYSTEM_PATTERN.matcher(expectedText).find() + val withModuleSystem = moduleKind != ModuleKind.PLAIN && !NO_MODULE_SYSTEM_PATTERN.matcher(fileContent).find() if (withModuleSystem) { additionalFiles += MODULE_EMULATION_FILE @@ -130,7 +136,7 @@ abstract class BasicBoxTest( val allJsFiles = additionalFiles + inputJsFiles + generatedJsFiles + globalCommonFiles + localCommonFiles + additionalCommonFiles - if (generateNodeJsRunner && !SKIP_NODE_JS.matcher(expectedText).find()) { + if (generateNodeJsRunner && !SKIP_NODE_JS.matcher(fileContent).find()) { val nodeRunnerName = mainModule.outputFileName(outputDir) + ".node.js" val ignored = InTextDirectivesUtils.isIgnoredTarget(TargetBackend.JS, file) val nodeRunnerText = generateNodeRunner(allJsFiles, outputDir, mainModuleName, ignored, testFactory.testPackage) @@ -139,9 +145,13 @@ abstract class BasicBoxTest( val checker = RhinoFunctionResultChecker(mainModuleName, testFactory.testPackage, TEST_FUNCTION, "OK", withModuleSystem) RhinoUtils.runRhinoTest(allJsFiles, checker) + + performAdditionalChecks(generatedJsFiles, outputPrefixFile, outputPostfixFile) } } + protected open fun performAdditionalChecks(generatedJsFiles: List, outputPrefixFile: File?, outputPostfixFile: File?) {} + private fun generateNodeRunner( files: Collection, dir: File, @@ -196,7 +206,9 @@ abstract class BasicBoxTest( module: TestModule, outputFileName: String, dependencies: List, - multiModule: Boolean + multiModule: Boolean, + outputPrefixFile: File?, + outputPostfixFile: File? ) { val testFiles = module.files.map { it.fileName }.filter { it.endsWith(".kt") } val globalCommonFiles = JsTestUtils.getFilesInDirectoryByExtension( @@ -211,10 +223,16 @@ abstract class BasicBoxTest( val config = createConfig(module, dependencies, multiModule) val outputFile = File(outputFileName) - translateFiles(psiFiles, outputFile, config) + translateFiles(psiFiles, outputFile, config, outputPrefixFile, outputPostfixFile) } - protected fun translateFiles(psiFiles: List, outputFile: File, config: JsConfig) { + protected fun translateFiles( + psiFiles: List, + outputFile: File, + config: JsConfig, + outputPrefixFile: File?, + outputPostfixFile: File? + ) { val translator = K2JSTranslator(config) val translationResult = translator.translate(psiFiles, MainCallParameters.noCall()) @@ -226,7 +244,7 @@ abstract class BasicBoxTest( throw AssertionError("The following errors occurred compiling test:\n" + messages) } - val outputFiles = translationResult.getOutputFiles(outputFile, null, null) + val outputFiles = translationResult.getOutputFiles(outputFile, outputPrefixFile, outputPostfixFile) val outputDir = outputFile.parentFile ?: error("Parent file for output file should not be null, outputFilePath: " + outputFile.path) outputFiles.writeAllTo(outputDir) diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/OutputPrefixPostfixTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/OutputPrefixPostfixTest.java deleted file mode 100644 index b5803efd23f..00000000000 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/OutputPrefixPostfixTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2010-2015 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.js.test; - -import com.intellij.openapi.util.io.FileUtil; -import junit.framework.Test; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.js.config.EcmaVersion; -import org.jetbrains.kotlin.js.test.semantics.TranslatorTestCaseBuilder; - -import java.io.File; - -@SuppressWarnings("JUnitTestCaseWithNoTests") -public final class OutputPrefixPostfixTest extends SingleFileTranslationTest { - private static final String PREFIX_EXT = ".prefix"; - private static final String POSTFIX_EXT = ".postfix"; - - @NotNull - private final String filename; - private final File outputPrefixFile; - private final File outputPostfixFile; - - @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") - public OutputPrefixPostfixTest(@NotNull String filename) { - super("outputPrefixPostfix/"); - this.filename = filename; - - String inputFilePath = getInputFilePath(filename); - this.outputPrefixFile = newFileIfExists(inputFilePath + PREFIX_EXT); - this.outputPostfixFile = newFileIfExists(inputFilePath + POSTFIX_EXT); - } - - @Override - protected File getOutputPostfixFile() { - return outputPostfixFile; - } - - @Override - protected File getOutputPrefixFile() { - return outputPrefixFile; - } - - @Override - public void runTest() throws Exception { - checkFooBoxIsOk(filename); - } - - @Override - protected void runFunctionOutputTest( - @NotNull Iterable ecmaVersions, - @NotNull String kotlinFilename, - @NotNull String packageName, - @NotNull String functionName, - @NotNull Object expectedResult - ) throws Exception { - super.runFunctionOutputTest(ecmaVersions, kotlinFilename, packageName, functionName, expectedResult); - - for (EcmaVersion ecmaVersion : ecmaVersions) { - String output = FileUtil.loadFile(new File(getOutputFilePath(filename, ecmaVersion)), true); - if (outputPrefixFile != null) { - assertTrue(output.startsWith(FileUtil.loadFile(outputPrefixFile, true))); - } - if (outputPostfixFile != null) { - assertTrue(output.endsWith(FileUtil.loadFile(outputPostfixFile, true))); - } - } - } - - public static Test suite() throws Exception { - return TranslatorTestCaseBuilder.suiteForDirectory(TEST_DATA_DIR_PATH + "outputPrefixPostfix/cases/", filename -> { - OutputPrefixPostfixTest examplesTest = new OutputPrefixPostfixTest(filename); - examplesTest.setName(filename); - return examplesTest; - }); - } - - @Nullable - private static File newFileIfExists(@NotNull String path) { - File file = new File(path); - if (!file.exists()) { - file = null; - } - - return file; - } -} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractOutputPrefixPostfixTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractOutputPrefixPostfixTest.kt new file mode 100644 index 00000000000..6ec65add515 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractOutputPrefixPostfixTest.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2017 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.js.test.semantics + +import org.jetbrains.kotlin.js.test.BasicBoxTest +import java.io.File + +abstract class AbstractOutputPrefixPostfixTest : BasicBoxTest( + BasicBoxTest.TEST_DATA_DIR_PATH + "outputPrefixPostfix/", + "${BasicBoxTest.TEST_DATA_DIR_PATH}/out/outputPrefixPostfix/", + generateNodeJsRunner = false +) { + override fun getOutputPrefixFile(testFilePath: String): File? { + return newFileIfExists(testFilePath + ".prefix") + } + + override fun getOutputPostfixFile(testFilePath: String): File? { + return newFileIfExists(testFilePath + ".postfix") + } + + override fun performAdditionalChecks(generatedJsFiles: List, outputPrefixFile: File?, outputPostfixFile: File?) { + super.performAdditionalChecks(generatedJsFiles, outputPrefixFile, outputPostfixFile) + + val output = File(generatedJsFiles.first()).readText() + + outputPrefixFile?.let { + assertTrue(output.startsWith(it.readText())) + } + outputPostfixFile?.let { + assertTrue(output.endsWith(it.readText())) + } + } + + private fun newFileIfExists(path: String): File? { + val file = File(path) + if (!file.exists()) return null + return file + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/OutputPrefixPostfixTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/OutputPrefixPostfixTestGenerated.java new file mode 100644 index 00000000000..76c4255046c --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/OutputPrefixPostfixTestGenerated.java @@ -0,0 +1,62 @@ +/* + * Copyright 2010-2017 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.js.test.semantics; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TargetBackend; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("js/js.translator/testData/outputPrefixPostfix") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class OutputPrefixPostfixTestGenerated extends AbstractOutputPrefixPostfixTest { + public void testAllFilesPresentInOutputPrefixPostfix() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("js/js.translator/testData/outputPrefixPostfix"), Pattern.compile("^([^_](.+))\\.kt$"), TargetBackend.JS, true); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/outputPrefixPostfix/simple.kt"); + doTest(fileName); + } + + @TestMetadata("simpleWithPostfix.kt") + public void testSimpleWithPostfix() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/outputPrefixPostfix/simpleWithPostfix.kt"); + doTest(fileName); + } + + @TestMetadata("simpleWithPrefix.kt") + public void testSimpleWithPrefix() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/outputPrefixPostfix/simpleWithPrefix.kt"); + doTest(fileName); + } + + @TestMetadata("simpleWithPrefixAndPostfix.kt") + public void testSimpleWithPrefixAndPostfix() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/outputPrefixPostfix/simpleWithPrefixAndPostfix.kt"); + doTest(fileName); + } +} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/SourceMapGenerationSmokeTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/SourceMapGenerationSmokeTestGenerated.java index d9470ebf8c7..dccfefd35fb 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/SourceMapGenerationSmokeTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/SourceMapGenerationSmokeTestGenerated.java @@ -53,4 +53,5 @@ public class SourceMapGenerationSmokeTestGenerated extends AbstractSourceMapGene String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/sourcemap/methodCallInMethod.kt"); doTest(fileName); } + } diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simple.kt b/js/js.translator/testData/outputPrefixPostfix/simple.kt similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simple.kt rename to js/js.translator/testData/outputPrefixPostfix/simple.kt diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPostfix.kt b/js/js.translator/testData/outputPrefixPostfix/simpleWithPostfix.kt similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPostfix.kt rename to js/js.translator/testData/outputPrefixPostfix/simpleWithPostfix.kt diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPostfix.kt.postfix b/js/js.translator/testData/outputPrefixPostfix/simpleWithPostfix.kt.postfix similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPostfix.kt.postfix rename to js/js.translator/testData/outputPrefixPostfix/simpleWithPostfix.kt.postfix diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefix.kt b/js/js.translator/testData/outputPrefixPostfix/simpleWithPrefix.kt similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefix.kt rename to js/js.translator/testData/outputPrefixPostfix/simpleWithPrefix.kt diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefix.kt.prefix b/js/js.translator/testData/outputPrefixPostfix/simpleWithPrefix.kt.prefix similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefix.kt.prefix rename to js/js.translator/testData/outputPrefixPostfix/simpleWithPrefix.kt.prefix diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefixAndPostfix.kt b/js/js.translator/testData/outputPrefixPostfix/simpleWithPrefixAndPostfix.kt similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefixAndPostfix.kt rename to js/js.translator/testData/outputPrefixPostfix/simpleWithPrefixAndPostfix.kt diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefixAndPostfix.kt.postfix b/js/js.translator/testData/outputPrefixPostfix/simpleWithPrefixAndPostfix.kt.postfix similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefixAndPostfix.kt.postfix rename to js/js.translator/testData/outputPrefixPostfix/simpleWithPrefixAndPostfix.kt.postfix diff --git a/js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefixAndPostfix.kt.prefix b/js/js.translator/testData/outputPrefixPostfix/simpleWithPrefixAndPostfix.kt.prefix similarity index 100% rename from js/js.translator/testData/outputPrefixPostfix/cases/simpleWithPrefixAndPostfix.kt.prefix rename to js/js.translator/testData/outputPrefixPostfix/simpleWithPrefixAndPostfix.kt.prefix