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 f2ada01b605..d8ddcd67988 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 @@ -60,7 +60,7 @@ import java.nio.charset.Charset import java.util.regex.Pattern abstract class BasicBoxTest( - private val pathToTestDir: String, + protected val pathToTestDir: String, private val pathToOutputDir: String, private val typedArraysEnabled: Boolean = false, private val generateSourceMap: Boolean = false, @@ -72,6 +72,10 @@ abstract class BasicBoxTest( protected open fun getOutputPostfixFile(testFilePath: String): File? = null fun doTest(filePath: String) { + doTest(filePath, "OK", MainCallParameters.noCall()) + } + + fun doTest(filePath: String, expectedResult: String, mainCallParameters: MainCallParameters) { val file = File(filePath) val outputDir = getOutputDir(file) val fileContent = KotlinTestUtils.doLoadFile(file) @@ -93,7 +97,8 @@ 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, outputPrefixFile, outputPostfixFile) + generateJavaScriptFile(file.parent, module, outputFileName, dependencies, modules.size > 1, + outputPrefixFile, outputPostfixFile, mainCallParameters) if (!module.name.endsWith(OLD_MODULE_SUFFIX)) outputFileName else null } @@ -143,13 +148,24 @@ abstract class BasicBoxTest( FileUtil.writeToFile(File(nodeRunnerName), nodeRunnerText) } - val checker = RhinoFunctionResultChecker(mainModuleName, testFactory.testPackage, TEST_FUNCTION, "OK", withModuleSystem) - RhinoUtils.runRhinoTest(allJsFiles, checker) + runGeneratedCode(allJsFiles, mainModuleName, testFactory.testPackage, TEST_FUNCTION, expectedResult, withModuleSystem) performAdditionalChecks(generatedJsFiles, outputPrefixFile, outputPostfixFile) } } + protected open fun runGeneratedCode( + jsFiles: List, + testModuleName: String, + testPackage: String?, + testFunction: String, + expectedResult: String, + withModuleSystem: Boolean + ) { + val checker = RhinoFunctionResultChecker(testModuleName, testPackage, testFunction, expectedResult, withModuleSystem) + RhinoUtils.runRhinoTest(jsFiles, checker) + } + protected open fun performAdditionalChecks(generatedJsFiles: List, outputPrefixFile: File?, outputPostfixFile: File?) {} private fun generateNodeRunner( @@ -208,7 +224,8 @@ abstract class BasicBoxTest( dependencies: List, multiModule: Boolean, outputPrefixFile: File?, - outputPostfixFile: File? + outputPostfixFile: File?, + mainCallParameters: MainCallParameters ) { val testFiles = module.files.map { it.fileName }.filter { it.endsWith(".kt") } val globalCommonFiles = JsTestUtils.getFilesInDirectoryByExtension( @@ -223,7 +240,7 @@ abstract class BasicBoxTest( val config = createConfig(module, dependencies, multiModule) val outputFile = File(outputFileName) - translateFiles(psiFiles, outputFile, config, outputPrefixFile, outputPostfixFile) + translateFiles(psiFiles, outputFile, config, outputPrefixFile, outputPostfixFile, mainCallParameters) } protected fun translateFiles( @@ -231,10 +248,11 @@ abstract class BasicBoxTest( outputFile: File, config: JsConfig, outputPrefixFile: File?, - outputPostfixFile: File? + outputPostfixFile: File?, + mainCallParameters: MainCallParameters ) { val translator = K2JSTranslator(config) - val translationResult = translator.translate(psiFiles, MainCallParameters.noCall()) + val translationResult = translator.translate(psiFiles, mainCallParameters) if (translationResult !is TranslationResult.Success) { val outputStream = ByteArrayOutputStream() @@ -375,7 +393,8 @@ abstract class BasicBoxTest( } companion object { - val TEST_DATA_DIR_PATH = "js/js.translator/testData/" + const val TEST_DATA_DIR_PATH = "js/js.translator/testData/" + const val DIST_DIR_JS_PATH = "dist/js/" private val COMMON_FILES_NAME = "_common" private val COMMON_FILES_DIR = "_commonFiles/" diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicTest.java deleted file mode 100644 index 27ddf5b2040..00000000000 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicTest.java +++ /dev/null @@ -1,402 +0,0 @@ -/* - * Copyright 2010-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.js.test; - -import com.google.common.collect.Lists; -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 org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.backend.common.output.OutputFileCollection; -import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport; -import org.jetbrains.kotlin.cli.common.messages.MessageRenderer; -import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector; -import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt; -import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles; -import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment; -import org.jetbrains.kotlin.config.CommonConfigurationKeys; -import org.jetbrains.kotlin.config.CompilerConfiguration; -import org.jetbrains.kotlin.idea.KotlinFileType; -import org.jetbrains.kotlin.js.JavaScript; -import org.jetbrains.kotlin.js.backend.ast.JsProgram; -import org.jetbrains.kotlin.js.config.EcmaVersion; -import org.jetbrains.kotlin.js.config.JSConfigurationKeys; -import org.jetbrains.kotlin.js.config.JsConfig; -import org.jetbrains.kotlin.js.facade.K2JSTranslator; -import org.jetbrains.kotlin.js.facade.MainCallParameters; -import org.jetbrains.kotlin.js.facade.TranslationResult; -import org.jetbrains.kotlin.js.test.rhino.RhinoResultChecker; -import org.jetbrains.kotlin.js.test.utils.DirectiveTestUtils; -import org.jetbrains.kotlin.js.test.utils.JsTestUtils; -import org.jetbrains.kotlin.js.test.utils.JsVerificationKt; -import org.jetbrains.kotlin.psi.KtFile; -import org.jetbrains.kotlin.test.KotlinTestUtils; -import org.jetbrains.kotlin.test.KotlinTestWithEnvironment; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import static org.jetbrains.kotlin.js.test.rhino.RhinoUtils.runRhinoTest; -import static org.jetbrains.kotlin.js.test.utils.JsTestUtils.convertFileNameToDotJsFile; -import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isDirectiveDefined; -import static org.jetbrains.kotlin.utils.PathUtil.getKotlinPathsForDistDirectory; - -public abstract class BasicTest extends KotlinTestWithEnvironment { - // predictable order of ecma version in tests - protected static final Iterable DEFAULT_ECMA_VERSIONS = Lists.newArrayList(EcmaVersion.v5); - - private static final boolean DELETE_OUT = false; - - public static final String TEST_DATA_DIR_PATH = "js/js.translator/testData/"; - public static final String DIST_DIR_JS_PATH = "dist/js/"; - - 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"; - private static final String NO_INLINE_DIRECTIVE = "// NO_INLINE"; - - @NotNull - private String relativePathToTestDir = ""; - - @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") - public BasicTest(@NotNull String relativePathToTestDir) { - this.relativePathToTestDir = relativePathToTestDir; - } - - protected abstract void checkFooBoxIsOkByPath(String filePath) throws Exception; - - @Override - protected KotlinCoreEnvironment createEnvironment() { - return KotlinCoreEnvironment.createForTests(getTestRootDisposable(), new CompilerConfiguration(), EnvironmentConfigFiles.JS_CONFIG_FILES); - } - - @Override - public void setUp() throws Exception { - super.setUp(); - File outDir = new File(getOutputPath()); - - KotlinTestUtils.mkdirs(outDir); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - if (!DELETE_OUT) { - return; - } - File outDir = new File(getOutputPath()); - assertTrue(outDir.exists()); - assertTrue(FileUtil.delete(outDir)); - } - - public void doTest(@NotNull String filePath) { - try { - checkFooBoxIsOkByPath(filePath); - } - catch (Exception e) { - throw new RuntimeException(e); - } - } - - protected void generateJavaScriptFiles( - @NotNull String kotlinFilePath, - @NotNull MainCallParameters mainCallParameters, - @NotNull Iterable ecmaVersions - ) throws Exception { - generateJavaScriptFiles(Collections.singletonList(kotlinFilePath), getBaseName(kotlinFilePath), mainCallParameters, ecmaVersions); - } - - protected void generateJavaScriptFiles( - @NotNull List files, - @NotNull String testName, - @NotNull MainCallParameters mainCallParameters, - @NotNull Iterable ecmaVersions - ) throws Exception { - generateJavaScriptFiles(files, testName, mainCallParameters, ecmaVersions, TEST_MODULE, null); - } - - private void generateJavaScriptFiles( - @NotNull List files, - @NotNull String testName, - @NotNull MainCallParameters mainCallParameters, - @NotNull Iterable ecmaVersions, - @NotNull String moduleName, - @Nullable List libraries - ) throws Exception { - for (EcmaVersion version : ecmaVersions) { - generateJavaScriptFiles(files, testName, mainCallParameters, version, moduleName, libraries); - } - } - - private void generateJavaScriptFiles( - @NotNull List files, - @NotNull String testName, - @NotNull MainCallParameters mainCallParameters, - @NotNull EcmaVersion version, - @NotNull String moduleName, - @Nullable List libraries - - ) throws Exception { - Project project = getProject(); - List allFiles = withAdditionalKotlinFiles(files); - List jetFiles = createJetFileList(project, allFiles, null); - - JsConfig config = createConfig(getProject(), moduleName, version, libraries, jetFiles); - File outputFile = new File(getOutputFilePath(testName, version)); - - translateFiles(jetFiles, outputFile, mainCallParameters, config); - } - - private void translateFiles( - @NotNull List jetFiles, - @NotNull File outputFile, - @NotNull MainCallParameters mainCallParameters, - @NotNull JsConfig config - ) throws Exception { - K2JSTranslator translator = new K2JSTranslator(config); - TranslationResult translationResult = translator.translate(jetFiles, mainCallParameters); - - if (!(translationResult instanceof TranslationResult.Success)) { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - PrintingMessageCollector collector = new PrintingMessageCollector( - new PrintStream(outputStream), - MessageRenderer.PLAIN_FULL_PATHS, - true - ); - AnalyzerWithCompilerReport.Companion.reportDiagnostics(translationResult.getDiagnostics(), collector); - String messages = new String(outputStream.toByteArray(), "UTF-8"); - throw new AssertionError("The following errors occurred compiling test:\n" + messages); - } - - TranslationResult.Success successResult = (TranslationResult.Success) translationResult; - - OutputFileCollection outputFiles = successResult.getOutputFiles(outputFile, getOutputPrefixFile(), getOutputPostfixFile()); - File outputDir = outputFile.getParentFile(); - assert outputDir != null : "Parent file for output file should not be null, outputFilePath: " + outputFile.getPath(); - OutputUtilsKt.writeAllTo(outputFiles, outputDir); - - processJsProgram(successResult.getProgram(), jetFiles); - } - - protected File getOutputPostfixFile() { - return null; - } - - protected File getOutputPrefixFile() { - return null; - } - - protected boolean shouldGenerateSourceMap() { - return false; - } - - private static void processJsProgram(@NotNull JsProgram program, @NotNull List jetFiles) throws Exception { - for (KtFile file : jetFiles) { - String text = file.getText(); - DirectiveTestUtils.processDirectives(program, text); - } - JsVerificationKt.verifyAst(program); - } - - protected void runRhinoTests( - @NotNull String testName, - @NotNull Iterable ecmaVersions, - @NotNull RhinoResultChecker checker - ) throws Exception { - for (EcmaVersion ecmaVersion : ecmaVersions) { - runRhinoTest(withAdditionalJsFiles(getOutputFilePath(testName, ecmaVersion), ecmaVersion), - checker, - getRhinoTestVariables(), - ecmaVersion); - } - } - - protected Map getRhinoTestVariables() throws Exception { - return null; - } - - @NotNull - private List additionalKotlinFiles() { - List additionalFiles = Lists.newArrayList(); - - // add all kotlin files from testData/_commonFiles - additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(TEST_DATA_DIR_PATH + COMMON_FILES_DIR, KotlinFileType.EXTENSION)); - // add all kotlin files from /_commonFiles - additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(pathToTestDir() + COMMON_FILES_DIR, KotlinFileType.EXTENSION)); - - return additionalFiles; - } - - @NotNull - private List additionalJsFiles(@NotNull EcmaVersion ecmaVersion) { - List additionalFiles = Lists.newArrayList(); - - // add all js files from testData/_commonFiles - additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(TEST_DATA_DIR_PATH + COMMON_FILES_DIR, JavaScript.EXTENSION)); - // add all js files from /_commonFiles - additionalFiles.addAll(JsTestUtils.getFilesInDirectoryByExtension(pathToTestDir() + COMMON_FILES_DIR, JavaScript.EXTENSION)); - - // add /cases/.js if it exists - String jsFilePath = getInputFilePath(getTestName(true) + JavaScript.DOT_EXTENSION); - File jsFile = new File(jsFilePath); - if (jsFile.exists() && jsFile.isFile()) { - additionalFiles.add(jsFilePath); - } - - return additionalFiles; - } - - // helpers - - @NotNull - private String pathToTestDir() { - return TEST_DATA_DIR_PATH + relativePathToTestDir; - } - - @NotNull - protected final String getOutputFilePath(@NotNull String testName, @NotNull EcmaVersion ecmaVersion) { - return getOutputPath() + convertFileNameToDotJsFile(testName, ecmaVersion); - } - - @NotNull - protected final String getInputFilePath(@NotNull String filename) { - return getInputPath() + filename; - } - - @NotNull - protected final String expectedFilePath(@NotNull String testName) { - return getExpectedPath() + testName + ".out"; - } - - @NotNull - private JsConfig createConfig( - @NotNull Project project, - @NotNull String moduleName, - @NotNull EcmaVersion ecmaVersion, - @Nullable List libraries, - @NotNull List files - ) { - CompilerConfiguration configuration = getEnvironment().getConfiguration().copy(); - - configuration.put(CommonConfigurationKeys.DISABLE_INLINE, hasNoInline(files)); - - List librariesWithStdlib = new ArrayList<>(JsConfig.JS_STDLIB); - if (libraries != null) { - librariesWithStdlib.addAll(libraries); - } - librariesWithStdlib.add(getKotlinPathsForDistDirectory().getJsKotlinTestJarPath().getAbsolutePath()); - - configuration.put(JSConfigurationKeys.LIBRARIES, librariesWithStdlib); - - configuration.put(CommonConfigurationKeys.MODULE_NAME, moduleName); - configuration.put(JSConfigurationKeys.TARGET, ecmaVersion); - - configuration.put(JSConfigurationKeys.SOURCE_MAP, shouldGenerateSourceMap()); - configuration.put(JSConfigurationKeys.META_INFO, false); - - return new JsConfig(project, configuration); - } - - private static boolean hasNoInline(@NotNull List files) { - for (KtFile file : files) { - if (isDirectiveDefined(file.getText(), NO_INLINE_DIRECTIVE)) { - return true; - } - } - - return false; - } - - @NotNull - private String getOutputPath() { - return pathToTestDir() + OUT; - } - - @NotNull - private 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 : "virtual file is missing, most likely the file doesn't exist: " + libFileName; - PsiFile psiFile = psiManager.findFile(virtualFile); - libFiles.add((KtFile) psiFile); - } - return libFiles; - } - - protected static String getBaseName(String path) { - String systemIndependentPath = FileUtil.toSystemIndependentName(path); - - int start = systemIndependentPath.lastIndexOf("/"); - if (start == -1) { - start = 0; - } - - int end = systemIndependentPath.lastIndexOf("."); - if (end == -1) { - end = path.length(); - } - - return path.substring(start, end); - } -} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/SingleFileTranslationTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/SingleFileTranslationTest.java deleted file mode 100644 index 72009406090..00000000000 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/SingleFileTranslationTest.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2010-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.js.test; - -import com.google.common.collect.Lists; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.js.config.EcmaVersion; -import org.jetbrains.kotlin.js.facade.MainCallParameters; -import org.jetbrains.kotlin.js.test.rhino.RhinoFunctionResultChecker; -import org.jetbrains.kotlin.js.test.rhino.RhinoSystemOutputChecker; - -import java.util.Collections; -import java.util.List; - -import static org.jetbrains.kotlin.js.test.utils.JsTestUtils.readFile; - -@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") -public abstract class SingleFileTranslationTest extends BasicTest { - public SingleFileTranslationTest(@NotNull String main) { - super(main); - } - - protected void runFunctionOutputTest( - @NotNull String kotlinFilename, - @NotNull String packageName, - @NotNull String functionName, - @NotNull Object expectedResult - ) throws Exception { - runFunctionOutputTest(DEFAULT_ECMA_VERSIONS, kotlinFilename, packageName, functionName, expectedResult); - } - - protected void runFunctionOutputTest( - @NotNull Iterable ecmaVersions, - @NotNull String kotlinFilename, - @NotNull String packageName, - @NotNull String functionName, - @NotNull Object expectedResult - ) throws Exception { - runFunctionOutputTestByPath(ecmaVersions, getInputFilePath(kotlinFilename), packageName, functionName, expectedResult); - } - - private void runFunctionOutputTestByPath( - @NotNull Iterable ecmaVersions, - @NotNull String kotlinFilePath, - @NotNull String packageName, - @NotNull String functionName, - @NotNull Object expectedResult - ) throws Exception { - runFunctionOutputTestByPaths(ecmaVersions, Collections.singletonList(kotlinFilePath), packageName, functionName, expectedResult); - } - - private void runFunctionOutputTestByPaths( - @NotNull Iterable ecmaVersions, - @NotNull List kotlinFilePaths, - @NotNull String packageName, - @NotNull String functionName, - @NotNull Object expectedResult - ) throws Exception { - String testName = getTestName(true); - generateJavaScriptFiles(kotlinFilePaths, testName, MainCallParameters.noCall(), ecmaVersions); - RhinoFunctionResultChecker checker = new RhinoFunctionResultChecker(TEST_MODULE, packageName, functionName, expectedResult); - runRhinoTests(testName, ecmaVersions, checker); - } - - protected void checkFooBoxIsOk(@NotNull String filename) throws Exception { - checkFooBoxIsOkByPath(getInputFilePath(filename)); - } - - @Override - protected void checkFooBoxIsOkByPath(@NotNull String filePath) throws Exception { - runFunctionOutputTestByPath(DEFAULT_ECMA_VERSIONS, filePath, TEST_PACKAGE, TEST_FUNCTION, "OK"); - } - - protected void checkOutput(@NotNull String kotlinFilename, - @NotNull String expectedResult, - @NotNull String... args) throws Exception { - checkOutput(kotlinFilename, expectedResult, DEFAULT_ECMA_VERSIONS, args); - } - - private void checkOutput( - @NotNull String kotlinFilename, - @NotNull String expectedResult, - @NotNull Iterable ecmaVersions, - String... args - ) throws Exception { - generateJavaScriptFiles(getInputFilePath(kotlinFilename), MainCallParameters.mainWithArguments(Lists.newArrayList(args)), ecmaVersions); - runRhinoTests(getBaseName(kotlinFilename), ecmaVersions, new RhinoSystemOutputChecker(expectedResult)); - } - - protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception { - checkOutput(testName + ".kt", readFile(expectedFilePath(testName + testId)), DEFAULT_ECMA_VERSIONS, args); - } -} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt index aa2e6806991..8acc6de4b80 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.js.backend.ast.* import org.jetbrains.kotlin.js.inline.clean.FunctionPostProcessor import org.jetbrains.kotlin.js.parser.parse import org.jetbrains.kotlin.js.sourceMap.JsSourceGenerationVisitor -import org.jetbrains.kotlin.js.test.BasicTest +import org.jetbrains.kotlin.js.test.BasicBoxTest import org.jetbrains.kotlin.js.translate.utils.JsAstUtils import org.junit.Assert import org.junit.Rule @@ -41,7 +41,7 @@ abstract class BasicOptimizerTest(private var basePath: String) { protected fun box() { val methodName = testName.methodName - val baseName = "${BasicTest.TEST_DATA_DIR_PATH}/js-optimizer/$basePath" + val baseName = "${BasicBoxTest.TEST_DATA_DIR_PATH}/js-optimizer/$basePath" val unoptimizedName = "$baseName/$methodName.original.js" val optimizedName = "$baseName/$methodName.optimized.js" diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractWebDemoExamplesTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractWebDemoExamplesTest.kt new file mode 100644 index 00000000000..f6a5db1c5f8 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractWebDemoExamplesTest.kt @@ -0,0 +1,50 @@ +/* + * 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.google.common.collect.Lists +import org.jetbrains.kotlin.js.facade.MainCallParameters +import org.jetbrains.kotlin.js.test.BasicBoxTest +import org.jetbrains.kotlin.js.test.rhino.RhinoSystemOutputChecker +import org.jetbrains.kotlin.js.test.rhino.RhinoUtils +import java.io.File + +abstract class AbstractWebDemoExamplesTest(relativePath: String) : BasicBoxTest( + BasicBoxTest.TEST_DATA_DIR_PATH + "/$relativePath/", + BasicBoxTest.TEST_DATA_DIR_PATH + "out/$relativePath/", + generateNodeJsRunner = false +) { + override fun runGeneratedCode( + jsFiles: List, + testModuleName: String, + testPackage: String?, + testFunction: String, + expectedResult: String, + withModuleSystem: Boolean + ) { + RhinoUtils.runRhinoTest(jsFiles, RhinoSystemOutputChecker(expectedResult)) + } + + protected fun runMainAndCheckOutput(fileName: String, expectedResult: String, vararg args: String) { + doTest(pathToTestDir + fileName, expectedResult, MainCallParameters.mainWithArguments(Lists.newArrayList(*args))) + } + + protected fun runMainAndCheckOutputWithExpectedFile(testName: String, testId: String, vararg args: String) { + val expectedResult = File(pathToTestDir + testName + testId + ".out").readText() + doTest(pathToTestDir + testName + ".kt", expectedResult, MainCallParameters.mainWithArguments(Lists.newArrayList(*args))) + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoCanvasExamplesTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoCanvasExamplesTest.java index e932c3a3cc8..dc68d754118 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoCanvasExamplesTest.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoCanvasExamplesTest.java @@ -17,14 +17,13 @@ package org.jetbrains.kotlin.js.test.semantics; import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.js.test.SingleFileTranslationTest; import org.mozilla.javascript.EcmaError; /* * We can't really check that this examples work in non-browser environment, so we just check that examples compile and * running them produce expected error. * */ -public final class WebDemoCanvasExamplesTest extends SingleFileTranslationTest { +public final class WebDemoCanvasExamplesTest extends AbstractWebDemoExamplesTest { public WebDemoCanvasExamplesTest() { super("webDemoCanvasExamples/"); @@ -48,7 +47,7 @@ public final class WebDemoCanvasExamplesTest extends SingleFileTranslationTest { private void doTest(@NotNull String filename, @NotNull String firstUnknownSymbolEncountered) throws Exception { try { - checkOutput(filename, ""); + runMainAndCheckOutput(filename, ""); fail(); } catch (EcmaError e) { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples1Test.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples1Test.java index b2b063507af..b42dea9fc7b 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples1Test.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples1Test.java @@ -16,80 +16,78 @@ package org.jetbrains.kotlin.js.test.semantics; -import org.jetbrains.kotlin.js.test.SingleFileTranslationTest; - -public final class WebDemoExamples1Test extends SingleFileTranslationTest { +public final class WebDemoExamples1Test extends AbstractWebDemoExamplesTest { public WebDemoExamples1Test() { super("webDemoExamples1/"); } public void testPrintArg() throws Exception { - checkOutput("printArg.kt", "Hello, world!", "Hello, world!"); + runMainAndCheckOutput("printArg.kt", "Hello, world!", "Hello, world!"); } public void testWhileLoop() throws Exception { - checkOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4"); + runMainAndCheckOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4"); } public void testIfAsExpression() throws Exception { - checkOutput("ifAsExpression.kt", "20\n", "10", "20"); + runMainAndCheckOutput("ifAsExpression.kt", "20\n", "10", "20"); } public void testObjectOrientedHello() throws Exception { - checkOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel"); + runMainAndCheckOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel"); } public void testMultiLanguageHello() throws Exception { - checkOutput("multiLanguageHello.kt", "Salut!\n", "FR"); + runMainAndCheckOutput("multiLanguageHello.kt", "Salut!\n", "FR"); } public void testNullChecks() throws Exception { - checkOutput("nullChecks.kt", "No number supplied"); - checkOutput("nullChecks.kt", "6", "2", "3"); + runMainAndCheckOutput("nullChecks.kt", "No number supplied"); + runMainAndCheckOutput("nullChecks.kt", "6", "2", "3"); } public void testRanges() throws Exception { - checkOutput("ranges.kt", "OK\n" + - " 1 2 3 4 5\n" + - "Out: array has only 3 elements. x = 4\n" + - "Yes: array contains aaa\n" + - "No: array doesn't contains ddd\n", "4"); + runMainAndCheckOutput("ranges.kt", "OK\n" + + " 1 2 3 4 5\n" + + "Out: array has only 3 elements. x = 4\n" + + "Yes: array contains aaa\n" + + "No: array doesn't contains ddd\n", "4"); - checkOutput("ranges.kt", " 1 2 3 4 5\n" + - "Out: array has only 3 elements. x = 10\n" + - "Yes: array contains aaa\n" + - "No: array doesn't contains ddd\n", "10"); + runMainAndCheckOutput("ranges.kt", " 1 2 3 4 5\n" + + "Out: array has only 3 elements. x = 10\n" + + "Yes: array contains aaa\n" + + "No: array doesn't contains ddd\n", "10"); } public void testForLoop() throws Exception { - checkOutput("forLoop.kt", "a\n" + - "b\n" + - "c\n" + - "\n" + - "a\n" + - "b\n" + - "c\n", "a", "b", "c"); - checkOutput("forLoop.kt", "123\n\n123\n", "123"); + runMainAndCheckOutput("forLoop.kt", "a\n" + + "b\n" + + "c\n" + + "\n" + + "a\n" + + "b\n" + + "c\n", "a", "b", "c"); + runMainAndCheckOutput("forLoop.kt", "123\n\n123\n", "123"); } public void testIsCheck() throws Exception { - checkOutput("isCheck.kt", "3\nnull\n"); + runMainAndCheckOutput("isCheck.kt", "3\nnull\n"); } public void testPatternMatching() throws Exception { - checkOutput("patternMatching.kt", "Greeting\n" + - "One\n" + - "Not a string\n" + - "Unknown\n"); + runMainAndCheckOutput("patternMatching.kt", "Greeting\n" + + "One\n" + + "Not a string\n" + + "Unknown\n"); } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples2Test.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples2Test.java index dd48f0cc87c..b9bd9a2e073 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples2Test.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/WebDemoExamples2Test.java @@ -16,25 +16,23 @@ package org.jetbrains.kotlin.js.test.semantics; -import org.jetbrains.kotlin.js.test.SingleFileTranslationTest; - -public final class WebDemoExamples2Test extends SingleFileTranslationTest { +public final class WebDemoExamples2Test extends AbstractWebDemoExamplesTest { public WebDemoExamples2Test() { super("webDemoExamples2/"); } public void testBottles() throws Exception { - performTestWithMain("bottles", "2", "2"); - performTestWithMain("bottles", ""); + runMainAndCheckOutputWithExpectedFile("bottles", "2", "2"); + runMainAndCheckOutputWithExpectedFile("bottles", ""); } public void testLife() throws Exception { - performTestWithMain("life", "", "2"); + runMainAndCheckOutputWithExpectedFile("life", "", "2"); } public void testBuilder() throws Exception { - performTestWithMain("builder", ""); - performTestWithMain("builder", "1", "over9000"); + runMainAndCheckOutputWithExpectedFile("builder", ""); + runMainAndCheckOutputWithExpectedFile("builder", "1", "over9000"); } } diff --git a/js/js.translator/testData/webDemoCanvasExamples/cases/Creatures.kt b/js/js.translator/testData/webDemoCanvasExamples/Creatures.kt similarity index 100% rename from js/js.translator/testData/webDemoCanvasExamples/cases/Creatures.kt rename to js/js.translator/testData/webDemoCanvasExamples/Creatures.kt diff --git a/js/js.translator/testData/webDemoCanvasExamples/cases/Fancy lines.kt b/js/js.translator/testData/webDemoCanvasExamples/Fancy lines.kt similarity index 100% rename from js/js.translator/testData/webDemoCanvasExamples/cases/Fancy lines.kt rename to js/js.translator/testData/webDemoCanvasExamples/Fancy lines.kt diff --git a/js/js.translator/testData/webDemoCanvasExamples/cases/Hello, Kotlin.kt b/js/js.translator/testData/webDemoCanvasExamples/Hello, Kotlin.kt similarity index 100% rename from js/js.translator/testData/webDemoCanvasExamples/cases/Hello, Kotlin.kt rename to js/js.translator/testData/webDemoCanvasExamples/Hello, Kotlin.kt diff --git a/js/js.translator/testData/webDemoCanvasExamples/cases/Traffic light.kt b/js/js.translator/testData/webDemoCanvasExamples/Traffic light.kt similarity index 100% rename from js/js.translator/testData/webDemoCanvasExamples/cases/Traffic light.kt rename to js/js.translator/testData/webDemoCanvasExamples/Traffic light.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/forLoop.kt b/js/js.translator/testData/webDemoExamples1/forLoop.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/forLoop.kt rename to js/js.translator/testData/webDemoExamples1/forLoop.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/ifAsExpression.kt b/js/js.translator/testData/webDemoExamples1/ifAsExpression.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/ifAsExpression.kt rename to js/js.translator/testData/webDemoExamples1/ifAsExpression.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/isCheck.kt b/js/js.translator/testData/webDemoExamples1/isCheck.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/isCheck.kt rename to js/js.translator/testData/webDemoExamples1/isCheck.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/multiLanguageHello.kt b/js/js.translator/testData/webDemoExamples1/multiLanguageHello.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/multiLanguageHello.kt rename to js/js.translator/testData/webDemoExamples1/multiLanguageHello.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/nullChecks.kt b/js/js.translator/testData/webDemoExamples1/nullChecks.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/nullChecks.kt rename to js/js.translator/testData/webDemoExamples1/nullChecks.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/objectOrientedHello.kt b/js/js.translator/testData/webDemoExamples1/objectOrientedHello.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/objectOrientedHello.kt rename to js/js.translator/testData/webDemoExamples1/objectOrientedHello.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/patternMatching.kt b/js/js.translator/testData/webDemoExamples1/patternMatching.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/patternMatching.kt rename to js/js.translator/testData/webDemoExamples1/patternMatching.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/printArg.kt b/js/js.translator/testData/webDemoExamples1/printArg.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/printArg.kt rename to js/js.translator/testData/webDemoExamples1/printArg.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/ranges.kt b/js/js.translator/testData/webDemoExamples1/ranges.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/ranges.kt rename to js/js.translator/testData/webDemoExamples1/ranges.kt diff --git a/js/js.translator/testData/webDemoExamples1/cases/whileLoop.kt b/js/js.translator/testData/webDemoExamples1/whileLoop.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples1/cases/whileLoop.kt rename to js/js.translator/testData/webDemoExamples1/whileLoop.kt diff --git a/js/js.translator/testData/webDemoExamples2/cases/bottles.kt b/js/js.translator/testData/webDemoExamples2/bottles.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples2/cases/bottles.kt rename to js/js.translator/testData/webDemoExamples2/bottles.kt diff --git a/js/js.translator/testData/webDemoExamples2/expected/bottles.out b/js/js.translator/testData/webDemoExamples2/bottles.out similarity index 100% rename from js/js.translator/testData/webDemoExamples2/expected/bottles.out rename to js/js.translator/testData/webDemoExamples2/bottles.out diff --git a/js/js.translator/testData/webDemoExamples2/expected/bottles2.out b/js/js.translator/testData/webDemoExamples2/bottles2.out similarity index 100% rename from js/js.translator/testData/webDemoExamples2/expected/bottles2.out rename to js/js.translator/testData/webDemoExamples2/bottles2.out diff --git a/js/js.translator/testData/webDemoExamples2/cases/builder.kt b/js/js.translator/testData/webDemoExamples2/builder.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples2/cases/builder.kt rename to js/js.translator/testData/webDemoExamples2/builder.kt diff --git a/js/js.translator/testData/webDemoExamples2/expected/builder.out b/js/js.translator/testData/webDemoExamples2/builder.out similarity index 100% rename from js/js.translator/testData/webDemoExamples2/expected/builder.out rename to js/js.translator/testData/webDemoExamples2/builder.out diff --git a/js/js.translator/testData/webDemoExamples2/expected/builder1.out b/js/js.translator/testData/webDemoExamples2/builder1.out similarity index 100% rename from js/js.translator/testData/webDemoExamples2/expected/builder1.out rename to js/js.translator/testData/webDemoExamples2/builder1.out diff --git a/js/js.translator/testData/webDemoExamples2/cases/life.kt b/js/js.translator/testData/webDemoExamples2/life.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples2/cases/life.kt rename to js/js.translator/testData/webDemoExamples2/life.kt diff --git a/js/js.translator/testData/webDemoExamples2/expected/life.out b/js/js.translator/testData/webDemoExamples2/life.out similarity index 100% rename from js/js.translator/testData/webDemoExamples2/expected/life.out rename to js/js.translator/testData/webDemoExamples2/life.out diff --git a/js/js.translator/testData/webDemoExamples2/cases/maze.kt b/js/js.translator/testData/webDemoExamples2/maze.kt similarity index 100% rename from js/js.translator/testData/webDemoExamples2/cases/maze.kt rename to js/js.translator/testData/webDemoExamples2/maze.kt diff --git a/js/js.translator/testData/webDemoExamples2/expected/maze.out b/js/js.translator/testData/webDemoExamples2/maze.out similarity index 100% rename from js/js.translator/testData/webDemoExamples2/expected/maze.out rename to js/js.translator/testData/webDemoExamples2/maze.out