[JS TESTS] Rewrite web demo tests using new test infrastructure

This commit is contained in:
Ivan Kylchik
2021-10-20 22:15:16 +03:00
parent af0e40a0d2
commit 8a1362de03
45 changed files with 369 additions and 579 deletions
@@ -15,11 +15,11 @@ import org.jetbrains.kotlin.js.test.es6.semantics.AbstractIrJsCodegenBoxES6Test
import org.jetbrains.kotlin.js.test.es6.semantics.AbstractIrJsCodegenInlineES6Test
import org.jetbrains.kotlin.js.test.es6.semantics.AbstractIrJsTypeScriptExportES6Test
import org.jetbrains.kotlin.js.test.ir.semantics.*
import org.jetbrains.kotlin.js.test.semantics.*
import org.jetbrains.kotlin.js.test.wasm.semantics.AbstractIrCodegenBoxWasmTest
import org.jetbrains.kotlin.js.test.wasm.semantics.AbstractIrCodegenWasmJsInteropWasmTest
import org.jetbrains.kotlin.js.test.wasm.semantics.AbstractJsTranslatorWasmTest
import org.jetbrains.kotlin.js.testNew.*
import org.jetbrains.kotlin.js.testNew.AbstractWebDemoExamplesTest
import org.jetbrains.kotlin.test.TargetBackend
fun main(args: Array<String>) {
@@ -137,6 +137,10 @@ fun main(args: Array<String>) {
testClass<AbstractMultiModuleOrderTest> {
model("multiModuleOrder/")
}
testClass<AbstractWebDemoExamplesTest> {
model("webDemoExamples/")
}
}
testGroup("js/js.tests/tests-gen", "compiler/testData", testRunnerMethodName = "runTest0") {
@@ -1,40 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.js.test.semantics
import com.google.common.collect.Lists
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.js.facade.MainCallParameters
import org.jetbrains.kotlin.js.test.BasicBoxTest
import java.io.File
import javax.script.ScriptException
abstract class AbstractWebDemoExamplesTest(relativePath: String) : BasicBoxTest(
BasicBoxTest.TEST_DATA_DIR_PATH + "/$relativePath/",
relativePath,
generateNodeJsRunner = false
) {
override fun runGeneratedCode(
jsFiles: List<String>,
testModuleName: String?,
testPackage: String?,
testFunction: String,
expectedResult: String,
withModuleSystem: Boolean
) {
testChecker.checkStdout(jsFiles, expectedResult)
}
@Throws(ScriptException::class)
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 = StringUtil.convertLineSeparators(File(pathToTestDir + testName + testId + ".out").readText())
doTest(pathToTestDir + testName + ".kt", expectedResult, MainCallParameters.mainWithArguments(Lists.newArrayList(*args)))
}
}
@@ -1,93 +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.semantics;
public final class WebDemoExamples1Test extends AbstractWebDemoExamplesTest {
public WebDemoExamples1Test() {
super("webDemoExamples1/");
}
public void testPrintArg() throws Exception {
runMainAndCheckOutput("printArg.kt", "Hello, world!", "Hello, world!");
}
public void testWhileLoop() throws Exception {
runMainAndCheckOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4");
}
public void testIfAsExpression() throws Exception {
runMainAndCheckOutput("ifAsExpression.kt", "20\n", "10", "20");
}
public void testObjectOrientedHello() throws Exception {
runMainAndCheckOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel");
}
public void testMultiLanguageHello() throws Exception {
runMainAndCheckOutput("multiLanguageHello.kt", "Salut!\n", "FR");
}
public void testNullChecks() throws Exception {
runMainAndCheckOutput("nullChecks.kt", "No number supplied");
runMainAndCheckOutput("nullChecks.kt", "6", "2", "3");
}
public void testRanges() throws Exception {
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");
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 {
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 {
runMainAndCheckOutput("isCheck.kt", "3\nnull\n");
}
public void testPatternMatching() throws Exception {
runMainAndCheckOutput("patternMatching.kt", "Greeting\n" +
"One\n" +
"Not a string\n" +
"Unknown\n");
}
}
@@ -1,38 +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.semantics;
public final class WebDemoExamples2Test extends AbstractWebDemoExamplesTest {
public WebDemoExamples2Test() {
super("webDemoExamples2/");
}
public void testBottles() throws Exception {
runMainAndCheckOutputWithExpectedFile("bottles", "2", "2");
runMainAndCheckOutputWithExpectedFile("bottles", "");
}
public void testLife() throws Exception {
runMainAndCheckOutputWithExpectedFile("life", "", "2");
}
public void testBuilder() throws Exception {
runMainAndCheckOutputWithExpectedFile("builder", "");
runMainAndCheckOutputWithExpectedFile("builder", "1", "over9000");
}
}
@@ -136,3 +136,22 @@ open class AbstractMultiModuleOrderTest : AbstractJsTest(
}
}
}
open class AbstractWebDemoExamplesTest : AbstractJsTest(
pathToTestDir = "${JsEnvironmentConfigurator.TEST_DATA_DIR_PATH}/webDemoExamples/",
testGroupOutputDirPrefix = "webDemoExamples/"
) {
override fun configure(builder: TestConfigurationBuilder) {
super.configure(builder)
with(builder) {
defaultDirectives {
-JsEnvironmentConfigurationDirectives.GENERATE_NODE_JS_RUNNER
JsEnvironmentConfigurationDirectives.DONT_RUN_GENERATED_CODE.with("JS")
}
configureJsArtifactsHandlersStep {
useHandlers(::MainCallWithArgumentsHandler)
}
}
}
}
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.js.testNew.converters
import com.google.common.collect.Lists
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.cli.common.output.writeAllTo
@@ -82,8 +83,11 @@ class ClassicJsBackendFacade(
val units = unitsByPath.entries.sortedBy { it.key }.map { it.value }
val mainCallParameters = when (JsEnvironmentConfigurationDirectives.CALL_MAIN) {
in module.directives -> MainCallParameters.mainWithArguments(listOf())
val mainCallParameters = when {
JsEnvironmentConfigurationDirectives.CALL_MAIN in module.directives -> MainCallParameters.mainWithArguments(listOf())
JsEnvironmentConfigurationDirectives.MAIN_ARGS in module.directives -> {
MainCallParameters.mainWithArguments(module.directives[JsEnvironmentConfigurationDirectives.MAIN_ARGS].first())
}
else -> MainCallParameters.noCall()
}
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.js.testNew.handlers
import org.jetbrains.kotlin.js.testNew.utils.getOnlyJsFilesForRunner
import org.jetbrains.kotlin.js.testNew.utils.getTestChecker
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.moduleStructure
class MainCallWithArgumentsHandler(testServices: TestServices) : AbstractJsArtifactsCollector(testServices) {
override fun processAfterAllModules(someAssertionWasFailed: Boolean) {
if (someAssertionWasFailed) return
val testFile = testServices.moduleStructure.originalTestDataFiles.first()
val expectedFile = testFile.parentFile.resolve(testFile.nameWithoutExtension + ".out")
if (!expectedFile.exists()) {
throw IllegalArgumentException("File ${expectedFile.absolutePath} with expected result doesn't exists")
}
val allJsFiles = getOnlyJsFilesForRunner(testServices, modulesToArtifact)
getTestChecker(testServices).checkStdout(allJsFiles, expectedFile.readText())
}
}