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

Formatting/customizing context #232

Closed
evant opened this issue Jul 31, 2019 · 1 comment
Closed

Formatting/customizing context #232

evant opened this issue Jul 31, 2019 · 1 comment
Assignees

Comments

@evant
Copy link
Collaborator

evant commented Jul 31, 2019

In assertk the failure message is made up of 3 parts:

assertThat(Person("Sue", 22)).prop("name", Person::name).isEqualTo("Bob")
// -> expected [name]:<"[Bob]"> but was:<"[Sue]"> (Person(name=Sue, age=22))

name: [name] -> The 'name' of the object you are asserting on, this can be passed in with assertThat(actual, name = "name") and it's calculated automatically for some methods like prop.

message: <"[Bob]"> but was:<"[Sue]"> the actual failure, expressed with expected() or fail().

context: (Person(name=Sue, age=22) the object you are asserting on. There is currently no way to set this but it's displayed automatically when you are asserting on a piece of it with methods like prop.

The problem is there is no way to customize what's displayed for the context. If the class you are asserting on doesn't implement toString() in a useful way you are out of luck.

Some ideas to solve this:

  1. Allow passing in a custom context

We can do this like you can pass in a custom name now. Would have to think about the name, not sure that context is the best name for this, not much thought was put into the naming as it's only used internally currently.

  1. Allow some sort of customization of show()

Right now show() is hard-coded to handle specific platform types falling back to toString(). Need to think about the api for this, are we customizing all shows() in the assertion or just the context one?

@evant
Copy link
Collaborator Author

evant commented Aug 20, 2020

Proposed solution:

Add a new argument to assertThat to customize displaying the actual value

assertThat(actual: T, name: String? = null, displayActual: (T) -> String = { display(it) })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants