From 2d757c41502ce5b194b784ec04226f99441f13f4 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 18 Sep 2017 16:20:58 +0300 Subject: [PATCH] unit-tests: Add assertion and annotation tests --- backend.native/tests/build.gradle | 26 ++- backend.native/tests/testing/annotations.kt | 64 +++++++ backend.native/tests/testing/assertions.kt | 180 ++++++++++++++++++++ backend.native/tests/testing/smoke.kt | 15 -- 4 files changed, 267 insertions(+), 18 deletions(-) create mode 100644 backend.native/tests/testing/annotations.kt create mode 100644 backend.native/tests/testing/assertions.kt delete mode 100644 backend.native/tests/testing/smoke.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 885329518cc..a0812063560 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -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. diff --git a/backend.native/tests/testing/annotations.kt b/backend.native/tests/testing/annotations.kt new file mode 100644 index 00000000000..e221fa35094 --- /dev/null +++ b/backend.native/tests/testing/annotations.kt @@ -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)") } diff --git a/backend.native/tests/testing/assertions.kt b/backend.native/tests/testing/assertions.kt new file mode 100644 index 00000000000..0983dfe243f --- /dev/null +++ b/backend.native/tests/testing/assertions.kt @@ -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 { throw IllegalStateException() } + assertFailsWith { 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 { throw IllegalArgumentException() } +// } +// catch (e: AssertionError) { +// return@run +// } +// throw AssertionError("Expected to fail") +// } +// +// withDefaultAsserter run@ { +// try { +// assertFailsWith { } +// } +// catch (e: AssertionError) { +// return@run +// } +// throw AssertionError("Expected to fail") +// } +// } + + @Test + fun testAssertFailsWithClass() { + assertFailsWith { + throw IllegalArgumentException("This is illegal") + } + } + + @Test + fun testAssertFailsWithClassFails() { + checkFailedAssertion { + assertFailsWith { throw IllegalStateException() } + } + + checkFailedAssertion { + assertFailsWith { } + } + } + + @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 { withDefaultAsserter(assertion) } +} + +@Suppress("INVISIBLE_MEMBER") +private fun withDefaultAsserter(block: () -> Unit) { + val current = overrideAsserter(DefaultAsserter) + try { + block() + } + finally { + overrideAsserter(current) + } +} diff --git a/backend.native/tests/testing/smoke.kt b/backend.native/tests/testing/smoke.kt deleted file mode 100644 index 37af8b9ed8a..00000000000 --- a/backend.native/tests/testing/smoke.kt +++ /dev/null @@ -1,15 +0,0 @@ -import kotlin.test.Test -import konan.test.* - -class A { - @Test - fun test() { - println("A.test") - } -} - -@Test -fun test() { - println("test") -} -