question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kotlin: Support `assertThat(foo) { isNotNull() isNotEqualTo(bar) ... }`

See original GitHub issue

It’s already possible to write nearly that by using apply:

assertThat(foo).apply {
  isNotNull()
  isNotEqualTo(bar)
}

Might it be worth a shortcut? Anecdotes welcome.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:29 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
netdpbcommented, Feb 10, 2020

Thinking a little more about foo.assertThat. I think, if we were to do it, it should not take a subject-receiver lambda, but instead a subject-parameter lambda so that it reads better as an English phrase:

// awkward. "foo: assert that is empty"?
foo.assertThat {
  isEmpty()
  contains(1, 2)
}

// better. "foo: assert that it is empty"
foo.assertThat {
  it.isEmpty()
  it.contains(1, 2)
}

// also good (imho best): "assert that foo is empty"
assertThat(foo) {
  isEmpty()
  contains(1, 2)
}
1reaction
netdpbcommented, Jun 29, 2021

Thinking further, I now think the right idiom is:

long.expression().let {
  assertThat(it).isNotNull()
  assertThat(it).isNotEqualTo(bar)
  assertThat(it).isSomeOtherThing()
}

That is very close to what I suggested above about using “it”, and uses only standard Kotlin, requiring no Truth API changes at all.

There is a small risk of someone dangling an assertion modifier off the end of the let (e.g., foo.let { ... }.inOrder()), but that could be mitigated with a check that flags any method call on a Subject whose receiver is a scope function call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AssertJ - fluent assertions java library - GitHub Pages
entry point for all assertThat methods and utility methods (e.g. entry) import static org.assertj.core.api.Assertions.
Read more >
Subject: Remove named(), actual(), and type parameters - Truth
AssertJ also defines most assertion methods to return the Subject , enabling chains like assertThat(x).isNotNull().isNotEqualTo(other).contains(x) , which we've ...
Read more >
Why doesn't this assert work - assertThat(foo, is(not(null)))
Empirically, I've found that this works instead: assertThat(foo, is(not(nullValue())));.
Read more >
Diff - platform/frameworks/support - Google Git
setOnContentRefreshListener(listener) + .build(); + + assertThat(template) + .isNotEqualTo( + new PlaceListMapTemplate.Builder() + .setTitle("Title") + .
Read more >
Spring Boot Reference Documentation
Spring Boot Applications Deployment: Cloud Deployment | OS Service. Build tool plugins: Maven | Gradle. Appendix: Application Properties | Configuration ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found