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 + ) + } + }