unit-tests: Add assertion and annotation tests
This commit is contained in:
@@ -1950,9 +1950,29 @@ task deserialized_members(type: LinkKonanTest) {
|
||||
source = "serialization/deserialize_members.kt"
|
||||
}
|
||||
|
||||
task testing_smoke(type: RunKonanTest) {
|
||||
source = "testing/smoke.kt"
|
||||
flags = ['-tr'] // TODO: Add flag to generate test-runner
|
||||
task testing_annotations(type: RunKonanTest) {
|
||||
source = "testing/annotations.kt"
|
||||
flags = ['-tr']
|
||||
goldValue = "Starting testing\nTest suite ignored: IgnoredClass.IgnoredObject\n" +
|
||||
"Test suite ignored: IgnoredClass\nTest suite ignored: IgnoredObject\nStarting test suite: A\n" +
|
||||
"beforeClass (A.companion)\nStarting test case: test1 (A)\nbefore (A)\ntest1 (A)\nafter (A)\n" +
|
||||
"Passed: test1 (A)\nIgnore: ignoredTest (A)\nafterClass (A.companion)\nTest suite finished: A\n" +
|
||||
"Starting test suite: A.O\nbeforeClass (A.object)\nStarting test case: test1 (A.O)\n" +
|
||||
"before (A.object)\ntest1 (A.object)\nafter (A.object)\nPassed: test1 (A.O)\n" +
|
||||
"Ignore: ignoredTest (A.O)\nafterClass (A.object)\nTest suite finished: A.O\n" +
|
||||
"Starting test suite: O\nbeforeClass (object)\nStarting test case: test1 (O)\n" +
|
||||
"before (object)\ntest1 (object)\nafter (object)\nPassed: test1 (O)\n" +
|
||||
"Ignore: ignoredTest (O)\nafterClass (object)\nTest suite finished: O\n" +
|
||||
"Starting test suite: Tests in file: annotations.kt\nbeforeClass (file)\n" +
|
||||
"Starting test case: test1 (Tests in file: annotations.kt)\nbefore (file)\ntest1 (file)\nafter (file)\n" +
|
||||
"Passed: test1 (Tests in file: annotations.kt)\nIgnore: ignoredTest (Tests in file: annotations.kt)\n" +
|
||||
"afterClass (file)\nTest suite finished: Tests in file: annotations.kt\nTesting finished\n"
|
||||
}
|
||||
|
||||
task testing_assertions(type: RunKonanTest) {
|
||||
source = "testing/assertions.kt"
|
||||
flags = ['-tr']
|
||||
expectedExitStatus = 0
|
||||
}
|
||||
|
||||
// Just check that the driver is able to produce runnable binaries.
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import kotlin.test.*
|
||||
import konan.test.*
|
||||
|
||||
|
||||
@Ignore
|
||||
class IgnoredClass {
|
||||
@Ignore object IgnoredObject {
|
||||
@Test fun test() { throw AssertionError("Ignored test") }
|
||||
}
|
||||
|
||||
@Test fun test() { throw AssertionError("Ignored test") }
|
||||
}
|
||||
|
||||
@Ignore
|
||||
object IgnoredObject {
|
||||
@Test fun test() { throw AssertionError("Ignored test") }
|
||||
}
|
||||
|
||||
class A {
|
||||
companion object {
|
||||
@BeforeEach fun before() { println("before (A.companion)") }
|
||||
@AfterEach fun after() { println("after (A.companion)") }
|
||||
@Test fun test1() { println("test1 (A.companion)") }
|
||||
|
||||
@BeforeClass fun beforeClass() { println("beforeClass (A.companion)") }
|
||||
@AfterClass fun afterClass() { println("afterClass (A.companion)") }
|
||||
}
|
||||
|
||||
@BeforeEach fun before() { println("before (A)") }
|
||||
@AfterEach fun after() { println("after (A)") }
|
||||
@Test fun test1() { println("test1 (A)") }
|
||||
@Ignore @Test fun ignoredTest() { throw AssertionError("Ignored test") }
|
||||
|
||||
@BeforeClass fun beforeClass() { println("beforeClass (A)") }
|
||||
@AfterClass fun afterClass() { println("afterClass (A)") }
|
||||
|
||||
object O {
|
||||
@BeforeEach fun before() { println("before (A.object)") }
|
||||
@AfterEach fun after() { println("after (A.object)") }
|
||||
@Test fun test1() { println("test1 (A.object)") }
|
||||
@Ignore @Test fun ignoredTest() { throw AssertionError("Ignored test") }
|
||||
|
||||
@BeforeClass fun beforeClass() { println("beforeClass (A.object)") }
|
||||
@AfterClass fun afterClass() { println("afterClass (A.object)") }
|
||||
}
|
||||
}
|
||||
|
||||
object O {
|
||||
@BeforeEach fun before() { println("before (object)") }
|
||||
@AfterEach fun after() { println("after (object)") }
|
||||
@Test fun test1() { println("test1 (object)") }
|
||||
@Ignore @Test fun ignoredTest() { throw AssertionError("Ignored test") }
|
||||
|
||||
@BeforeClass fun beforeClass() { println("beforeClass (object)") }
|
||||
@AfterClass fun afterClass() { println("afterClass (object)") }
|
||||
}
|
||||
|
||||
@BeforeEach fun before() { println("before (file)") }
|
||||
@AfterEach fun after() { println("after (file)") }
|
||||
@Test fun test1() { println("test1 (file)") }
|
||||
@Ignore @Test fun ignoredTest() { throw AssertionError("Ignored test") }
|
||||
|
||||
@BeforeClass fun beforeClass() { println("beforeClass (file)") }
|
||||
@AfterClass fun afterClass() { println("afterClass (file)") }
|
||||
@@ -0,0 +1,180 @@
|
||||
package kotlin.test.tests
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
class BasicAssertionsTest {
|
||||
@Test
|
||||
fun testAssertEquals() {
|
||||
assertEquals(1, 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertEqualsString() {
|
||||
assertEquals("a", "a")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertFailsWith() {
|
||||
assertFailsWith<IllegalStateException> { throw IllegalStateException() }
|
||||
assertFailsWith<AssertionError> { throw AssertionError() }
|
||||
}
|
||||
|
||||
// TODO: Uncomment with the catch serialization bug is fixed.
|
||||
// @Test fun testAssertFailsWithFails() {
|
||||
// assertTrue(true) // at least one assertion required for qunit
|
||||
//
|
||||
// withDefaultAsserter run@ {
|
||||
// try {
|
||||
// assertFailsWith<IllegalStateException> { throw IllegalArgumentException() }
|
||||
// }
|
||||
// catch (e: AssertionError) {
|
||||
// return@run
|
||||
// }
|
||||
// throw AssertionError("Expected to fail")
|
||||
// }
|
||||
//
|
||||
// withDefaultAsserter run@ {
|
||||
// try {
|
||||
// assertFailsWith<IllegalStateException> { }
|
||||
// }
|
||||
// catch (e: AssertionError) {
|
||||
// return@run
|
||||
// }
|
||||
// throw AssertionError("Expected to fail")
|
||||
// }
|
||||
// }
|
||||
|
||||
@Test
|
||||
fun testAssertFailsWithClass() {
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
throw IllegalArgumentException("This is illegal")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertFailsWithClassFails() {
|
||||
checkFailedAssertion {
|
||||
assertFailsWith<IllegalArgumentException> { throw IllegalStateException() }
|
||||
}
|
||||
|
||||
checkFailedAssertion {
|
||||
assertFailsWith<Exception> { }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertEqualsFails() {
|
||||
checkFailedAssertion { assertEquals(1, 2) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertTrue() {
|
||||
assertTrue(true)
|
||||
assertTrue { true }
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun testAssertTrueFails() {
|
||||
checkFailedAssertion { assertTrue(false) }
|
||||
checkFailedAssertion { assertTrue { false } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertFalse() {
|
||||
assertFalse(false)
|
||||
assertFalse { false }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertFalseFails() {
|
||||
checkFailedAssertion { assertFalse(true) }
|
||||
checkFailedAssertion{ assertFalse { true } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertFails() {
|
||||
assertFails { throw IllegalStateException() }
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun testAssertFailsFails() {
|
||||
checkFailedAssertion { assertFails { } }
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testAssertNotEquals() {
|
||||
assertNotEquals(1, 2)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun testAssertNotEqualsFails() {
|
||||
checkFailedAssertion { assertNotEquals(1, 1) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertNotNull() {
|
||||
assertNotNull(true)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun testAssertNotNullFails() {
|
||||
checkFailedAssertion { assertNotNull(null) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertNotNullLambda() {
|
||||
assertNotNull("") { assertEquals("", it) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertNotNullLambdaFails() {
|
||||
checkFailedAssertion {
|
||||
val value: String? = null
|
||||
assertNotNull(value) {
|
||||
it.substring(0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertNull() {
|
||||
assertNull(null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertNullFails() {
|
||||
checkFailedAssertion { assertNull("") }
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun testFail() {
|
||||
checkFailedAssertion { fail("should fail") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testExpect() {
|
||||
expect(1) { 1 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testExpectFails() {
|
||||
checkFailedAssertion { expect(1) { 2 } }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun checkFailedAssertion(assertion: () -> Unit) {
|
||||
assertFailsWith<AssertionError> { withDefaultAsserter(assertion) }
|
||||
}
|
||||
|
||||
@Suppress("INVISIBLE_MEMBER")
|
||||
private fun withDefaultAsserter(block: () -> Unit) {
|
||||
val current = overrideAsserter(DefaultAsserter)
|
||||
try {
|
||||
block()
|
||||
}
|
||||
finally {
|
||||
overrideAsserter(current)
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import kotlin.test.Test
|
||||
import konan.test.*
|
||||
|
||||
class A {
|
||||
@Test
|
||||
fun test() {
|
||||
println("A.test")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
println("test")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user