From e6f3cd640f5e1ed9d384ba2344ea00dc3eaa7a41 Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Wed, 11 Dec 2024 16:09:17 +0100 Subject: [PATCH 1/2] Basic assert function --- README.md | 24 +++++++++++++++--------- build.gradle.kts | 1 + src/commonMain/kotlin/Assertions.kt | 9 +++++++++ src/commonTest/kotlin/AssertionsTest.kt | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b074449..6a08559 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Kotlin multiplatform testing library providing power-assert compatible DSL and a I am mostly using [kotest](https://kotest.io/) library for writing test assertions in my projects. When [power-assert](https://kotlinlang.org/docs/power-assert.html) became the official Kotlin compiler plugin, I also realized that most of the kotest -assertions can be replaced with something which suits my purposes better. +assertions can be replaced with something which suits my needs much better. Instead of writing: ```kotlin @@ -20,14 +20,8 @@ I could write: assert(x >= 42) ``` -Unfortunately the [assert](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/assert.html) -function is supported at the moment only for `JVM` and `Native` out of all the Kotlin -multiplatform targets. So for my multiplatform libraries it would rather be -[assertTrue](https://kotlinlang.org/api/core/kotlin-test/kotlin.test/assert-true.html), but -... it is becoming too verbose. - -Quite often I am asserting the state of hierarchical data -structures, therefore I came up with this syntax: +Next to this, I am quite often asserting the state of hierarchical data +structures, therefore I came up with such a syntax: ```kotlin message should { @@ -122,6 +116,18 @@ powerAssert { ### Basic Assertions +```kotlin +assert(2 + 2 == 4) +``` + +> [!NOTE] +> The [assert](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/assert.html) +> function in Kotlin stdlib is providing `assert` only for `JVM` and `Native` out of all the Kotlin +> multiplatform targets. The multiplatform `assert` function can be +> imported from `com.xemantic.kotlin.test.assert` + +### Asserting object properties + The library introduces the [should](src/commonMain/kotlin/Assertions.kt) infix function, which allows you to chain assertions on an object: ```kotlin diff --git a/build.gradle.kts b/build.gradle.kts index b16b011..65c7b01 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -147,6 +147,7 @@ tasks.withType { powerAssert { functions = listOf( + "com.xemantic.kotlin.test.assert", "com.xemantic.kotlin.test.have" ) } diff --git a/src/commonMain/kotlin/Assertions.kt b/src/commonMain/kotlin/Assertions.kt index 01a4ca1..a45f2bc 100644 --- a/src/commonMain/kotlin/Assertions.kt +++ b/src/commonMain/kotlin/Assertions.kt @@ -22,6 +22,15 @@ import kotlin.contracts.contract import kotlin.reflect.typeOf import kotlin.test.assertNotNull import kotlin.test.assertTrue +import kotlin.test.asserter + +@OptIn(ExperimentalContracts::class) +public fun assert(actual: Boolean, message: String? = null) { + contract { + returns() implies actual + } + return asserter.assertTrue(message ?: "Expected value to be true.", actual) +} @OptIn(ExperimentalContracts::class) public infix fun T?.should(block: T.() -> Unit) { diff --git a/src/commonTest/kotlin/AssertionsTest.kt b/src/commonTest/kotlin/AssertionsTest.kt index 53ff95b..90bdefe 100644 --- a/src/commonTest/kotlin/AssertionsTest.kt +++ b/src/commonTest/kotlin/AssertionsTest.kt @@ -208,4 +208,23 @@ class AssertionsTest { ) } + @Test + fun `Should fail when assertion resolves to false`() { + val exception = assertFailsWith { + assert(2 + 2 == 2 + 3) + } + assertEquals( + expected = """ + | + |assert(2 + 2 == 2 + 3) + | | | | + | | | 5 + | | false + | 4 + | + """.trimMargin(), + actual = exception.message + ) + } + } From 3a3d1c1d4be181f131e1778670f277666a5a5cc4 Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Wed, 11 Dec 2024 16:24:16 +0100 Subject: [PATCH 2/2] api update --- api/xemantic-kotlin-test.api | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/xemantic-kotlin-test.api b/api/xemantic-kotlin-test.api index 9c8d6e1..abb0f44 100644 --- a/api/xemantic-kotlin-test.api +++ b/api/xemantic-kotlin-test.api @@ -1,4 +1,6 @@ public final class com/xemantic/kotlin/test/AssertionsKt { + public static final fun assert (ZLjava/lang/String;)V + public static synthetic fun assert$default (ZLjava/lang/String;ILjava/lang/Object;)V public static final fun have (ZLjava/lang/String;)V public static synthetic fun have$default (ZLjava/lang/String;ILjava/lang/Object;)V public static final fun should (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V