Skip to content

Commit

Permalink
Basic assert function
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Dec 11, 2024
1 parent b682976 commit e6f3cd6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ tasks.withType<Test> {

powerAssert {
functions = listOf(
"com.xemantic.kotlin.test.assert",
"com.xemantic.kotlin.test.have"
)
}
Expand Down
9 changes: 9 additions & 0 deletions src/commonMain/kotlin/Assertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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> T?.should(block: T.() -> Unit) {
Expand Down
19 changes: 19 additions & 0 deletions src/commonTest/kotlin/AssertionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,23 @@ class AssertionsTest {
)
}

@Test
fun `Should fail when assertion resolves to false`() {
val exception = assertFailsWith<AssertionError> {
assert(2 + 2 == 2 + 3)
}
assertEquals(
expected = """
|
|assert(2 + 2 == 2 + 3)
| | | |
| | | 5
| | false
| 4
|
""".trimMargin(),
actual = exception.message
)
}

}

0 comments on commit e6f3cd6

Please sign in to comment.