[JS IR] Add infra to test compilation with error

- add bunch of tests
 - fix help test
This commit is contained in:
Roman Artemev
2020-09-01 12:04:15 +03:00
parent e592b3af1d
commit 0bff406a12
19 changed files with 460 additions and 13 deletions
+1
View File
@@ -3,6 +3,7 @@ where advanced options include:
-Xdisable-fake-override-validator
Disable IR fake override validator
-Xenable-js-scripting Enable experimental support of .kts files using K/JS (with -Xir only)
-Xerror-tolerance-policy Set up error tolerance policy (NONE, SEMANTIC, SYNTAX, ALL)
-Xfriend-modules=<path> Paths to friend modules
-Xfriend-modules-disabled Disable internal declaration export
-Xgenerate-dts Generate TypeScript declarations .d.ts file alongside JS file. Available in IR backend only.
@@ -0,0 +1,16 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
fun foo(o: Any): Any = o as ErrType
// FILE: b.kt
fun box(): String {
try {
foo(Any())
} catch (e: IllegalStateException) {
return "OK"
}
return "fail"
}
@@ -0,0 +1,24 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
fun bar() { throw Exception("..") }
fun foo(): String {
try {
bar()
} catch (e: ErrType) {
throw Expception(e)
}
}
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: IllegalStateException) {
return "OK"
}
return "fail"
}
@@ -0,0 +1,31 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
fun bar(aa: Any, bb: Any, cc: Any) {
}
fun foo() {
qux(a(), b(), c())
beer()
f()
}
fun a(): Any { storage += "a"; return storage }
fun b(): Any { storage += "b"; return storage }
fun c(): Any { storage += "c"; return storage }
fun f(): Any { storage += "FAIL"; return storage }
var storage = ""
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: IllegalStateException) {
return if (storage == "abc") "OK" else "FAIL ABC"
return "FAIL"
}
}
@@ -0,0 +1,19 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
fun bar<T>(a: T): T = a
var storage = ""
fun foo() {
storage += bar("O")
storage += bar<Any, String, Number>("K")
}
// FILE: b.kt
fun box(): String {
foo()
return storage
}
@@ -0,0 +1,17 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
fun bar(a: String, b: String): String
fun foo(): String {
return bar("O", "K")
}
// FILE: b.kt
fun box(): String {
val r = foo()
if (r is String) return r
return "OK"
}
@@ -0,0 +1,23 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
fun <reified T> bar(t: T) = t
fun <reified T> qux() = T::class
fun foo(): String {
return bar<String>("OK")
}
fun dec() { qux() }
// FILE: b.kt
fun box(): String {
try {
dec()
} catch (e: Throwable /*js ReferenceError*/) {
return foo()
}
}
@@ -0,0 +1,22 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
class A
class B
fun bar(): B = B()
fun foo(): A {
return bar()
}
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: ClassCastException) {
return "OK"
}
return "FAIL"
}
@@ -0,0 +1,31 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
var storage = ""
fun bar(a: String, b: String) { storage += a; storage += b; }
fun foo1() {
bar("O", "K")
bar("FAIL1")
}
fun foo2() {
bar("FAIL2", "FAIL2", "FAIL2", "FAIL2")
}
// FILE: b.kt
fun box(): String {
try {
foo1()
} catch (e: IllegalStateException) {
try {
foo2()
} catch (e: IllegalStateException) {
return storage
}
}
return "FAIL"
}
@@ -0,0 +1,17 @@
// ERROR_POLICY: SEMANTIC
// FILE: t.kt
fun foo() { bar() }
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: IllegalStateException) {
return "OK"
}
return "FAIL"
}
@@ -0,0 +1,16 @@
// ERROR_POLICY: SYNTAX
// FILE: t.kt
fun foo() { this->bar() }
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: IllegalStateException) {
return "OK"
}
return "FAIL"
}
@@ -0,0 +1,33 @@
// ERROR_POLICY: SYNTAX
// FILE: t.kt
fun bar(aa: Any, bb: Any, cc: Any) {
<<;;;;;
d8as9d89as
??????? ====----
}
fun foo() {
bar(a(), b(), c())
<<<<<,,,,>>>>>
f()
}
fun a(): Any { storage += "a"; return storage }
fun b(): Any { storage += "b"; return storage }
fun c(): Any { storage += "c"; return storage }
fun f(): Any { storage += "FAIL"; return storage }
var storage = ""
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: IllegalStateException) {
return if (storage == "abc") "OK" else "FAIL ABC"
return "FAIL"
}
@@ -0,0 +1,38 @@
// ERROR_POLICY: SYNTAX
// FILE: t.kt
384jfjfj2934829...:::%:ББББ
fun 124gga() {}
val 481gu: Boolean = true
var 981llj): Int = 42
// just check if it is compiled
class **jhghssk
fun foo() { 124gga() }
fun bar() { return 481gu }
fun qux() { 981llj) = 481 }
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: IllegalStateException) {
try {
bar()
} catch (e: IllegalStateException) {
try {
qux()
} catch (e: IllegalStateException) {
return "OK"
}
}
}
return "FAIL"
}
@@ -0,0 +1,20 @@
// ERROR_POLICY: SYNTAX
// FILE: t.kt
1sdasf
fun bar() {}
fun foo() { bar(,,,,,,,) }
// FILE: b.kt
fun box(): String {
try {
foo()
} catch (e: IllegalStateException) {
return "OK"
}
return "FAIL"
}
+4 -4
View File
@@ -2,7 +2,7 @@ FILE fqName:<root> fileName:/unresolvedReference.kt
PROPERTY name:test1 visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test1 type:IrErrorType visibility:private [final,static]
EXPRESSION_BODY
ERROR_CALL '' type=IrErrorType
ERROR_CALL 'unresolved' type=IrErrorType
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test1> visibility:public modality:FINAL <> () returnType:IrErrorType
correspondingProperty: PROPERTY name:test1 visibility:public modality:FINAL [val]
BLOCK_BODY
@@ -11,7 +11,7 @@ FILE fqName:<root> fileName:/unresolvedReference.kt
PROPERTY name:test2 visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test2 type:IrErrorType visibility:private [final,static]
EXPRESSION_BODY
ERROR_CALL '' type=IrErrorType
ERROR_CALL 'unresolved()' type=IrErrorType
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test2> visibility:public modality:FINAL <> () returnType:IrErrorType
correspondingProperty: PROPERTY name:test2 visibility:public modality:FINAL [val]
BLOCK_BODY
@@ -20,7 +20,7 @@ FILE fqName:<root> fileName:/unresolvedReference.kt
PROPERTY name:test3 visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test3 type:IrErrorType visibility:private [final,static]
EXPRESSION_BODY
ERROR_CALL '' type=IrErrorType
ERROR_CALL 'unresolved(56)' type=IrErrorType
receiver: CONST Int type=kotlin.Int value=42
CONST Int type=kotlin.Int value=56
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test3> visibility:public modality:FINAL <> () returnType:IrErrorType
@@ -31,7 +31,7 @@ FILE fqName:<root> fileName:/unresolvedReference.kt
PROPERTY name:test4 visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test4 type:IrErrorType visibility:private [final,static]
EXPRESSION_BODY
ERROR_EXPR '' type=IrErrorType
ERROR_EXPR '42 *' type=IrErrorType
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test4> visibility:public modality:FINAL <> () returnType:IrErrorType
correspondingProperty: PROPERTY name:test4 visibility:public modality:FINAL [val]
BLOCK_BODY
@@ -81,6 +81,10 @@ fun main(args: Array<String>) {
model("codegen/box", targetBackend = TargetBackend.JS_IR)
}
testClass<AbstractIrJsCodegenBoxErrorTest> {
model("codegen/boxError", targetBackend = TargetBackend.JS_IR)
}
testClass<AbstractIrJsCodegenBoxES6Test> {
model("codegen/box", targetBackend = TargetBackend.JS_IR_ES6)
}
@@ -28,10 +28,7 @@ import org.jetbrains.kotlin.incremental.js.TranslationResultValue
import org.jetbrains.kotlin.js.JavaScript
import org.jetbrains.kotlin.js.backend.JsToStringGenerationVisitor
import org.jetbrains.kotlin.js.backend.ast.*
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.config.SourceMapSourceEmbedding
import org.jetbrains.kotlin.js.config.*
import org.jetbrains.kotlin.js.dce.DeadCodeElimination
import org.jetbrains.kotlin.js.dce.InputFile
import org.jetbrains.kotlin.js.dce.InputResource
@@ -127,6 +124,9 @@ abstract class BasicBoxTest(
val runPlainBoxFunction = RUN_PLAIN_BOX_FUNCTION.matcher(fileContent).find()
val inferMainModule = INFER_MAIN_MODULE.matcher(fileContent).find()
val expectActualLinker = EXPECT_ACTUAL_LINKER.matcher(fileContent).find()
val errorPolicyMatcher = ERROR_POLICY_PATTERN.matcher(fileContent)
val errorPolicy =
if (errorPolicyMatcher.find()) ErrorTolerancePolicy.resolvePolicy(errorPolicyMatcher.group(1)) else ErrorTolerancePolicy.DEFAULT
val skipDceDriven = SKIP_DCE_DRIVEN.matcher(fileContent).find()
val splitPerModule = SPLIT_PER_MODULE.matcher(fileContent).find()
@@ -174,7 +174,7 @@ abstract class BasicBoxTest(
testFactory.tmpDir,
file.parent, module, outputFileName, dceOutputFileName, pirOutputFileName, dependencies, allDependencies, friends, modules.size > 1,
!SKIP_SOURCEMAP_REMAPPING.matcher(fileContent).find(), outputPrefixFile, outputPostfixFile,
actualMainCallParameters, testPackage, testFunction, needsFullIrRuntime, isMainModule, expectActualLinker, skipDceDriven, splitPerModule
actualMainCallParameters, testPackage, testFunction, needsFullIrRuntime, isMainModule, expectActualLinker, skipDceDriven, splitPerModule, errorPolicy
)
when {
@@ -397,7 +397,8 @@ abstract class BasicBoxTest(
isMainModule: Boolean,
expectActualLinker: Boolean,
skipDceDriven: Boolean,
splitPerModule: Boolean
splitPerModule: Boolean,
errorIgnorancePolicy: ErrorTolerancePolicy
) {
val kotlinFiles = module.files.filter { it.fileName.endsWith(".kt") }
val testFiles = kotlinFiles.map { it.fileName }
@@ -413,7 +414,7 @@ abstract class BasicBoxTest(
val psiFiles = createPsiFiles(allSourceFiles.sortedBy { it.canonicalPath }.map { it.canonicalPath })
val sourceDirs = (testFiles + additionalFiles).map { File(it).parent }.distinct()
val config = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, tmpDir, incrementalData = null, expectActualLinker = expectActualLinker)
val config = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, tmpDir, incrementalData = null, expectActualLinker = expectActualLinker, errorIgnorancePolicy)
val outputFile = File(outputFileName)
val dceOutputFile = File(dceOutputFileName)
val pirOutputFile = File(pirOutputFileName)
@@ -468,7 +469,7 @@ abstract class BasicBoxTest(
.sortedBy { it.canonicalPath }
.map { sourceToTranslationUnit[it]!! }
val recompiledConfig = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, tmpDir, incrementalData, expectActualLinker)
val recompiledConfig = createConfig(sourceDirs, module, dependencies, allDependencies, friends, multiModule, tmpDir, incrementalData, expectActualLinker, ErrorTolerancePolicy.DEFAULT)
val recompiledOutputFile = File(outputFile.parentFile, outputFile.nameWithoutExtension + "-recompiled.js")
translateFiles(
@@ -684,7 +685,7 @@ abstract class BasicBoxTest(
private fun createConfig(
sourceDirs: List<String>, module: TestModule, dependencies: List<String>, allDependencies: List<String>, friends: List<String>,
multiModule: Boolean, tmpDir: File, incrementalData: IncrementalData?, expectActualLinker: Boolean
multiModule: Boolean, tmpDir: File, incrementalData: IncrementalData?, expectActualLinker: Boolean, errorIgnorancePolicy: ErrorTolerancePolicy
): JsConfig {
val configuration = environment.configuration.copy()
@@ -707,6 +708,11 @@ abstract class BasicBoxTest(
configuration.put(CommonConfigurationKeys.MODULE_NAME, module.name.removeSuffix(OLD_MODULE_SUFFIX))
configuration.put(JSConfigurationKeys.MODULE_KIND, module.moduleKind)
configuration.put(JSConfigurationKeys.TARGET, EcmaVersion.v5)
configuration.put(JSConfigurationKeys.ERROR_TOLERANCE_POLICY, errorIgnorancePolicy)
if (errorIgnorancePolicy.allowErrors) {
configuration.put(JSConfigurationKeys.DEVELOPER_MODE, true)
}
val hasFilesToRecompile = module.hasFilesToRecompile
configuration.put(JSConfigurationKeys.META_INFO, multiModule)
@@ -934,6 +940,8 @@ abstract class BasicBoxTest(
private val SKIP_DCE_DRIVEN = Pattern.compile("^// *SKIP_DCE_DRIVEN *$", Pattern.MULTILINE)
private val SPLIT_PER_MODULE = Pattern.compile("^// *SPLIT_PER_MODULE *$", Pattern.MULTILINE)
private val ERROR_POLICY_PATTERN = Pattern.compile("^// *ERROR_POLICY: *(.+)$", Pattern.MULTILINE)
@JvmStatic
protected val runTestInNashorn = getBoolean("kotlin.js.useNashorn")
@@ -0,0 +1,122 @@
/*
* 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.ir.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("compiler/testData/codegen/boxError")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class IrJsCodegenBoxErrorTestGenerated extends AbstractIrJsCodegenBoxErrorTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInBoxError() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxError"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("compiler/testData/codegen/boxError/semantic")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Semantic extends AbstractIrJsCodegenBoxErrorTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInSemantic() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxError/semantic"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("castToErrorType.kt")
public void testCastToErrorType() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/castToErrorType.kt");
}
@TestMetadata("catchErrorType.kt")
public void testCatchErrorType() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/catchErrorType.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/evaluationOrder.kt");
}
@TestMetadata("mismatchTypeParameters.kt")
public void testMismatchTypeParameters() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/mismatchTypeParameters.kt");
}
@TestMetadata("missedBody.kt")
public void testMissedBody() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/missedBody.kt");
}
@TestMetadata("reifiedNonInline.kt")
public void testReifiedNonInline() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/reifiedNonInline.kt");
}
@TestMetadata("typeMismatch.kt")
public void testTypeMismatch() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/typeMismatch.kt");
}
@TestMetadata("unmatchedArguments.kt")
public void testUnmatchedArguments() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/unmatchedArguments.kt");
}
@TestMetadata("unresolvedFunctionReferece.kt")
public void testUnresolvedFunctionReferece() throws Exception {
runTest("compiler/testData/codegen/boxError/semantic/unresolvedFunctionReferece.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxError/syntax")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Syntax extends AbstractIrJsCodegenBoxErrorTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInSyntax() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxError/syntax"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("arrowReference.kt")
public void testArrowReference() throws Exception {
runTest("compiler/testData/codegen/boxError/syntax/arrowReference.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/boxError/syntax/evaluationOrder.kt");
}
@TestMetadata("incorectLexicalName.kt")
public void testIncorectLexicalName() throws Exception {
runTest("compiler/testData/codegen/boxError/syntax/incorectLexicalName.kt");
}
@TestMetadata("missedArgument.kt")
public void testMissedArgument() throws Exception {
runTest("compiler/testData/codegen/boxError/syntax/missedArgument.kt");
}
}
}
@@ -14,6 +14,11 @@ abstract class AbstractIrJsCodegenBoxTest : BasicIrBoxTest(
"codegen/irBox/"
)
abstract class AbstractIrJsCodegenBoxErrorTest : BasicIrBoxTest(
"compiler/testData/codegen/boxError/",
"codegen/irBoxError/"
)
abstract class AbstractIrWasmBoxJsTest : BasicIrBoxTest(
TEST_DATA_DIR_PATH + "wasmBox/",
"irWasmBox/"