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.

[suggestion] Pass subject as parameter to Assertion.Builder callbacks

See original GitHub issue

When writing custom assertions, the only way to get the subject is directly from the Assertion receiver’s subject parameter. A more idiomatic, Kotlin-y way to access the subject would be having it passed as the sole argument to the callback, which can be accessed implicitly as the it parameter, or be named as needed by the end-user.

The new signature would look something like:

// in strikt/api/Assertion.kt
fun assert(
      description: String,
      expected: Any?,
      assert: AtomicAssertion<T>.(T) -> Unit
    ): Builder<T>

Example usage:

fun Assertion.Builder<LocalDate>.isStTibsDay() =
  assert("is St. Tib's Day") {
    when (MonthDay.from(it)) {
      MonthDay.of(2, 29) -> pass()
      else               -> fail()
    }
  }

or

fun Assertion.Builder<LocalDate>.isStTibsDay() =
  assert("is St. Tib's Day") { dateUnderTest ->
    when (MonthDay.from(dateUnderTest)) {
      MonthDay.of(2, 29) -> pass()
      else               -> fail()
    }
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
robfletchercommented, Aug 14, 2018

Implemented this on a branch. See f28f3f9. I think it simplifies some things e.g.

  • Assertion and derived types no longer require a generic type.
  • was able to eliminate the AssertionComposer interface altogether.
0reactions
robfletchercommented, Aug 15, 2018

Implemented this. It affects assert, compose, map and passesIf.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proper way to verify parameters being passed to a Mock are ...
Is this the preferred way of making sure my mock is getting the expected parameters passed to it, or should I set a...
Read more >
Validate method parameters using callbacks in PHPUnit
In this example we make sure the my_method() function was only called once and received value as the parameter. As we can see:...
Read more >
5 Identity Assertion Providers - Oracle Help Center
Identity Assertion providers enable perimeter authentication and support single sign-on. ... Passing Tokens for Perimeter Authentication.
Read more >
strikt.api
Strikt is an assertion library for Kotlin intended for use with a test runner such as JUnit or Spek.
Read more >
Espresso basics - Android Developers
This document explains how to complete common automated testing tasks using the Espresso API. The Espresso API encourages test authors to ...
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