Skip to content
Draft
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
55 changes: 55 additions & 0 deletions strikt-core/src/test/kotlin/strikt/exp/ClearlyTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package strikt.exp

import failgood.Test
import failgood.describe
import strikt.api.DescribeableBuilder
import strikt.api.expectThat
import strikt.assertions.endsWith
import strikt.assertions.isEqualTo
import strikt.internal.AssertionBuilder
import strikt.internal.AssertionStrategy
import strikt.internal.AssertionSubject


@Test
class ClearlyTest {
val context = describe("experimental Any receiver syntax") {
describe("has") {

it("works with simple asserts") {
val kiddo = "jakob"
// instead of
expectThat(kiddo).isEqualTo("jakob")
// we could also write
kiddo.clearly.isEqualTo("jakob")
// or likely, doubtless, withoutADoubt, promisesIt, saysIt, you get the idea

// or using the nested syntax:
kiddo.has { this.that.isEqualTo("jakob") }
}
it("works with nested asserts") {
data class User(val name: String, val email: String)

val user = User("chris", "[email protected]")
user.has {
name.that.isEqualTo("chris")
email.that.endsWith("example.com")
}
}
}
}
private val <T> T.clearly: DescribeableBuilder<T>
get() = AssertionBuilder(
AssertionSubject(this), AssertionStrategy.Throwing
)

private val <T> T.that: DescribeableBuilder<T>
get() = AssertionBuilder(
AssertionSubject(this), AssertionStrategy.Throwing
)

fun <T> T.has(function: T.() -> Unit) {
this.function()
}
}

68 changes: 68 additions & 0 deletions strikt-core/src/test/kotlin/strikt/exp/HasWithReceiverTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package strikt.exp

import failgood.Test
import failgood.describe
import strikt.api.Assertion
import strikt.api.DescribeableBuilder
import strikt.assertions.endsWith
import strikt.assertions.isEqualTo
import strikt.assertions.isNotNull
import strikt.internal.AssertionBuilder
import strikt.internal.AssertionStrategy
import strikt.internal.AssertionSubject

@Suppress("NAME_SHADOWING")
@Test
class HasWithReceiverTest {
inline fun <T> T.has(function: DSL.(T) -> Unit) {
DSL().function(this)
}

class DSL {
val errors = mutableListOf<String>()

@Suppress("UNUSED_VARIABLE")
val <T> T.that: DescribeableBuilder<T>
get() {
val errors = [email protected] // we can access the errors collection in the dsl collector
return AssertionBuilder(
AssertionSubject(this), AssertionStrategy.Throwing
)
}
fun <T> Assertion.Builder<T>.andHas(block: DSL.(T) -> Unit) {
[email protected](this.subject)
}
}


val context = describe("has with receiver") {
it("works with nested asserts") {
data class Nested(val field: String)
data class Role(val nested: Nested)
data class User(val name: String, val email: String, val role: Role?)

val user = User("chris", "[email protected]", Role(Nested("field")))


user.has { a ->
a.name.that.isEqualTo("chris")
a.email.that.endsWith("example.com")
a.role.that.isNotNull().andHas { a ->
a.nested.field.that.isEqualTo("field")
}
}
}
it("can call suspend methods") {
class Coroutine {
@Suppress("RedundantSuspendModifier")
suspend fun otto() = 8
}
Coroutine().has { a ->
a.otto().that.isEqualTo(8)
}
}
}

}