Klitos Kyriacou
07/06/2022, 3:26 PMextracting
feature? For example, in AssertJ I can do this:
assertThat(foo)
.extracting(Foo::id, Foo::description)
.containsExactly(1234, "this is a foo")
The closest I could find was shouldBeEqualUsingFields
but that's not suitable, as it requires another Foo argument which I can't easily construct; I only have the naked property values.Jim
07/06/2022, 3:28 PMall(foo) {
it.id shouldBe 1234
it.description shouldBe "stuff"
}
LeoColman
07/06/2022, 3:30 PMassertSoftly(foo) {
it.id shouldBe 1234
it.description shouldBe "stuff"
}
Jim
07/06/2022, 3:30 PMEmil Kantis
07/06/2022, 3:31 PMit
iirc? the assertSoftly should lambda should already be scoped to the subject?Jim
07/06/2022, 3:32 PMT.() -> Unit
or (T) -> Unit
š¤ŖT.(T)
I've seen that here and thereEmil Kantis
07/06/2022, 3:33 PMtest("hello") {
val p = Point(0.0, 3.0)
assertSoftly(p) {
x shouldBeGreaterThan -1.0
it.y shouldBe 3.0
}
}
Jim
07/06/2022, 3:34 PMsam
07/06/2022, 3:37 PMEmil Kantis
07/06/2022, 3:41 PMKlitos Kyriacou
07/06/2022, 3:42 PMextracting
method is that it tells you "expecting id to be 456 but was 123." Much clearer and much more helpful.Jim
07/06/2022, 3:49 PMLeoColman
07/06/2022, 4:29 PM(T).T -> is a bizarre signature lol"I want T both as a receiver and as a parameter". The only way to do it really is crying before, hence the T_T
Emil Kantis
07/06/2022, 5:53 PMassertProperties(p) {
Point::x shouldBe 0.0
Point::y shouldBe 3.0
}
doable? @Jim? šJim
07/06/2022, 5:55 PMsam
07/06/2022, 6:26 PMx shouldBe y
can capture the variable name on the leftJim
07/06/2022, 6:29 PMfun isOdd(x: Int) = x % 2 != 0
data class IntWrapper(val value: Int) {
fun isOddWrapper() = isOdd(value)
}
class SampleTests : FunSpec({
test("function pointer") {
all(IntWrapper(1)) {
its { ::isOddWrapper } shouldBe false
its { ::isOdd } shouldBe false
}
}
})
data class ShouldBeLeft<R>(val name: String, val actual: R) {
infix fun shouldBe(expected: R) {
withClue("Expected property $name to equal $expected but was $actual") {
actual shouldBe expected
}
}
}
infix fun <T, R> T.its(kfunc: (T) -> KFunction<R>): ShouldBeLeft<R> {
val f = kfunc(this)
return if ((f as? FunctionReferenceImpl)?.boundReceiver != null) {
ShouldBeLeft(f.name, f.call())
} else {
ShouldBeLeft(f.name, f.call(this))
}
}
Expected property isOddWrapper to equal false but was true
java.lang.AssertionError: Expected property isOddWrapper to equal false but was true
expected:<false> but was:<true>