KJS: make OutputPrefixPostfixTest generated and migrate to BasicBoxTest
This commit is contained in:
@@ -1279,6 +1279,10 @@ fun main(args: Array<String>) {
|
||||
testClass<AbstractSourceMapGenerationSmokeTest> {
|
||||
model("sourcemap/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS)
|
||||
}
|
||||
|
||||
testClass<AbstractOutputPrefixPostfixTest> {
|
||||
model("outputPrefixPostfix/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS)
|
||||
}
|
||||
}
|
||||
|
||||
testGroup("js/js.tests/test", "compiler/testData") {
|
||||
|
||||
@@ -68,15 +68,21 @@ abstract class BasicBoxTest(
|
||||
) : KotlinTestWithEnvironment() {
|
||||
val additionalCommonFileDirectories = mutableListOf<String>()
|
||||
|
||||
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<String>()
|
||||
|
||||
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<String>, outputPrefixFile: File?, outputPostfixFile: File?) {}
|
||||
|
||||
private fun generateNodeRunner(
|
||||
files: Collection<String>,
|
||||
dir: File,
|
||||
@@ -196,7 +206,9 @@ abstract class BasicBoxTest(
|
||||
module: TestModule,
|
||||
outputFileName: String,
|
||||
dependencies: List<String>,
|
||||
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<KtFile>, outputFile: File, config: JsConfig) {
|
||||
protected fun translateFiles(
|
||||
psiFiles: List<KtFile>,
|
||||
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)
|
||||
|
||||
|
||||
@@ -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<EcmaVersion> 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;
|
||||
}
|
||||
}
|
||||
+53
@@ -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<String>, 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
|
||||
}
|
||||
}
|
||||
+62
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
@@ -53,4 +53,5 @@ public class SourceMapGenerationSmokeTestGenerated extends AbstractSourceMapGene
|
||||
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/sourcemap/methodCallInMethod.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user