Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic assert function #1

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions api/xemantic-kotlin-test.api
Original file line number Diff line number Diff line change
@@ -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
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
)
}

}
Loading