kotlin-test-js: pass test function result to test framework (e.g. Promise)

This commit is contained in:
Anton Bannykh
2017-12-27 16:23:10 +03:00
parent 3e0d83b7d1
commit 2df85ae4a9
4 changed files with 93 additions and 7 deletions
@@ -5924,6 +5924,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("returnTestResult.kt")
public void testReturnTestResult() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/kotlin.test/returnTestResult.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/kotlin.test/simple.kt");
@@ -104,15 +104,18 @@ class JSTestGenerator(val context: TranslationContext) {
val classVal = innerContext.defineTemporary(classDescriptor.instance(innerContext))
fun FunctionDescriptor.buildCall() = CallTranslator.buildCall(context, this, emptyList(), classVal).makeStmt()
fun FunctionDescriptor.buildCall() = CallTranslator.buildCall(context, this, emptyList(), classVal)
functionToTest.body.statements += beforeDescriptors.map { it.buildCall() }
functionToTest.body.statements += beforeDescriptors.map { it.buildCall().makeStmt() }
functionToTest.body.statements += if (afterDescriptors.isEmpty()) {
functionDescriptor.buildCall()
if (afterDescriptors.isEmpty()) {
functionToTest.body.statements += JsReturn(functionDescriptor.buildCall())
}
else {
JsTry(JsBlock(functionDescriptor.buildCall()), listOf(), JsBlock(afterDescriptors.map { it.buildCall() }))
functionToTest.body.statements += JsTry(
JsBlock(JsReturn(functionDescriptor.buildCall())),
listOf(),
JsBlock(afterDescriptors.map { it.buildCall().makeStmt() }))
}
return functionToTest
+6 -2
View File
@@ -15,8 +15,8 @@ val underscore = kotlin.test.setAdapter(object : FrameworkAdapter {
context.suite(name, ignored) { suiteFn() }
}
override fun test(name: String, ignored: Boolean, testFn: () -> Unit) {
context.test(name, ignored) { testFn() }
override fun test(name: String, ignored: Boolean, testFn: () -> dynamic) {
context.test(name, ignored) { returned(testFn()) }
}
})
@@ -58,6 +58,10 @@ class TestContext {
record("caught(\"$msg\")")
}
fun returned(msg: dynamic) = indent {
if (msg is String) record("returned(\"$msg\")")
}
private fun (TestContext.() -> Unit).runSafely() {
try {
this()
@@ -0,0 +1,73 @@
// EXPECTED_REACHABLE_NODES: 1178
import kotlin.test.Test
import kotlin.test.BeforeTest
import kotlin.test.AfterTest
open class A {
@Test fun foo(): String {
return "promise"
}
@Test fun bar() = "future"
}
interface WithBefore {
@BeforeTest fun before() {
call("before")
}
}
interface WithAfter {
@AfterTest fun after() {
call("after")
}
}
class B: A(), WithBefore
class C: A(), WithAfter
class D: A(), WithBefore, WithAfter
fun box() = checkLog {
suite("A") {
test("foo") {
returned("promise")
}
test("bar") {
returned("future")
}
}
suite("B") {
test("foo") {
call("before")
returned("promise")
}
test("bar") {
call("before")
returned("future")
}
}
suite("C") {
test("foo") {
call("after")
returned("promise")
}
test("bar") {
call("after")
returned("future")
}
}
suite("D") {
test("foo") {
call("before")
call("after")
returned("promise")
}
test("bar") {
call("before")
call("after")
returned("future")
}
}
}