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

Use correct subject description when using with passing a lambda #298

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
13 changes: 7 additions & 6 deletions strikt-core/src/main/kotlin/strikt/api/Assertion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ interface Assertion {
* @return an assertion builder whose subject is the value returned by
* [function].
*/
fun <R> get(function: T.() -> R): DescribeableBuilder<R> = get(function.describe(), function)
fun <R> get(function: T.() -> R): DescribeableBuilder<R> =
get(function.describe("get"), function)

/**
* Runs a group of assertions on the subject returned by [function].
Expand Down Expand Up @@ -247,7 +248,7 @@ interface Assertion {
fun <R> with(
function: T.() -> R,
block: Builder<R>.() -> Unit
) = with(function.describe(), function, block)
) = with(function.describe("with"), function, block)

/**
* Maps the assertion subject to the result of [function].
Expand Down Expand Up @@ -319,17 +320,17 @@ interface Assertion {
}
}

private fun <Receiver, Result> (Receiver.() -> Result).describe(): String =
private fun <Receiver, Result> (Receiver.() -> Result).describe(name: String): String =
when (this) {
is KProperty<*> ->
"value of property $name"
"value of property ${this.name}"
is KFunction<*> ->
"return value of $name"
"return value of ${this.name}"
is CallableReference -> "value of $propertyName"
else -> {
try {
val line = FilePeek.filePeek.getCallerFileInfo().line
LambdaBody("get", line).body.trim()
LambdaBody(name, line).body.trim()
} catch (e: Exception) {
"%s"
}
Expand Down
94 changes: 94 additions & 0 deletions strikt-core/src/test/kotlin/strikt/Formatting.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package strikt

import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import strikt.api.expectThat
Expand All @@ -12,6 +13,7 @@ import strikt.assertions.isNotEqualTo
import strikt.assertions.isNotNull
import strikt.assertions.isUpperCase
import strikt.assertions.startsWith
import java.time.LocalDate

@DisplayName("error message formatting")
internal class Formatting {
Expand Down Expand Up @@ -175,4 +177,96 @@ internal class Formatting {
""".trimMargin()
)
}

@Nested
@DisplayName("subject description")
inner class SubjectDescription {
@Nested
@DisplayName("when using get")
inner class Get {
@Test
fun `is the property name when passing a lambda`() {
val subject = Person("David", LocalDate.of(1947, 1, 8))

val error = assertThrows<AssertionError> {
expectThat(subject).get { birthDate }
.get(LocalDate::getYear)
.isEqualTo(1971)
}

expectThat(error.message).isEqualTo(
"""▼ Expect that Person(name=David, birthDate=1947-01-08):
| ▼ birthDate:
| ▼ return value of getYear:
| ✗ is equal to 1971
| found 1947""".trimMargin()
)
}

@Test
fun `is the property name when passing a property`() {
val subject = Person("David", LocalDate.of(1947, 1, 8))

val error = assertThrows<AssertionError> {
expectThat(subject).get(Person::birthDate)
.get(LocalDate::getYear)
.isEqualTo(1971)
}

expectThat(error.message).isEqualTo(
"""▼ Expect that Person(name=David, birthDate=1947-01-08):
| ▼ value of property birthDate:
| ▼ return value of getYear:
| ✗ is equal to 1971
| found 1947""".trimMargin()
)
}
}

@Nested
@DisplayName("when using with")
inner class With {
@Test
fun `is the property name when passing a lambda`() {
val subject = Person("David", LocalDate.of(1947, 1, 8))

val error = assertThrows<AssertionError> {
expectThat(subject) {
with({ birthDate }) {
get { year }.isEqualTo(1971)
}
}
}

expectThat(error.message).isEqualTo(
"""▼ Expect that Person(name=David, birthDate=1947-01-08):
| ▼ birthDate:
| ▼ year:
| ✗ is equal to 1971
| found 1947""".trimMargin()
)
}

@Test
fun `is property name when passing a property`() {
val subject = Person("David", LocalDate.of(1947, 1, 8))

val error = assertThrows<AssertionError> {
expectThat(subject) {
with(Person::birthDate) {
get { year }.isEqualTo(1971)
}
}
}

expectThat(error.message).isEqualTo(
"""▼ Expect that Person(name=David, birthDate=1947-01-08):
| ▼ value of property birthDate:
| ▼ year:
| ✗ is equal to 1971
| found 1947""".trimMargin()
)
}
}
}
}