https://kotlinlang.org logo
#strikt
Title
# strikt
s

sloydev

11/26/2020, 10:41 AM
Hello! I was looking for a way to "group" multiple assertions with a descriptive message, but I did't find an easy (and non-verbose) way to do this in the docs or exploring the API. I implemented something custom, but I wonder if there is a better "native" way. Does anybody know a way to do something like this with the Strikt API?
Copy code
expectThat(output) {
  should("Replace anonymous id") {
    get { anonymousId() }.isEqualTo("anonymous_user")
    get { context().traits().anonymousId() }.isEqualTo("anonymous_user")
  }
  should("Remove advertisingId and deviceId") {
    get { advertisingId() }.isNull()
    get { deviceId() }.isNull()
  }
}
So I get error messages like these:
Copy code
✗ Replace anonymous id
    ▼ "5c6b6c02-4dd2d":
      ✗ is equal to "anonymous_user"
              found "5c6b6c02-4dd2d"
    ▼ "5c6b6c02-4dd2d":
      ✗ is equal to "anonymous_user"
              found "5c6b6c02-4dd2d"
  ✗ Remove advertisingId and deviceId
    ▼ "32c85c4d-c39b897c":
      ✗ is null
    ▼ "c55911907ad":
      ✗ is null
For reference, the custom function
should
looks like this:
Copy code
private fun <T> Assertion.Builder<T>.should(
  description: String,
  assertions: Assertion.Builder<T>.() -> Unit
) {
  compose(description) {
    assertions()
  } then {
    if (allPassed) pass() else fail()
  }
}
r

robfletcher

11/26/2020, 2:54 PM
That’s a nice idea
It’s the
and
function but with a description
I like it
s

sloydev

11/26/2020, 3:03 PM
Thanks! 🙂 From your reaction I get that there is no current alternative for this 😛
If you think it's useful I wouldn't mind contributing it, but I get a bit lost in the internal design of the library. I suppose there's a better approach than my quick hack
r

robfletcher

11/26/2020, 4:57 PM
I think the implementation is correct, it could just be a member function on
Assertion.Builder
rather than being an extension.
s

sloydev

11/27/2020, 8:19 AM
And what do you think of the naming? I used
should
because it's the first thing that came to mind and fitted our test case, but super subjective. Is it ok? Or can you think of a more strikt-ish name? An existing name like
and
or
describedAs
? A new name like
should
or
group
or something else?
r

robfletcher

11/27/2020, 2:18 PM
Yeah, I need to give that some thought
11 Views