Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

In some cases it can be useful to label assertions to provide additional information for test reporting. For example, consider the following test:

testM("test") {
  for {
    a <- ZIO.effectTotal(Some(1))
    b <- ZIO.effectTotal(Some(1))
    c <- ZIO.effectTotal(Some(0))
    d <- ZIO.effectTotal(Some(1))
  } yield assert(a, isSome(equalTo(1)) &&
    assert(b, isSome(equalTo(1)) &&
    assert(c, isSome(equalTo(1)) &&
    assert(d, isSome(equalTo(1))
}

This test will fail with an error message that "0 did not satisfy equalTo(1)", but that is not particularly helpful here because we don't know which of the four composed assertions failed. This PR adds a method label to Assertion that labels an assertion with an arbitrary string. Using this, the test could be rewritten as:

testM("test") {
  for {
    a <- ZIO.effectTotal(Some(1))
    b <- ZIO.effectTotal(Some(1))
    c <- ZIO.effectTotal(Some(0))
    d <- ZIO.effectTotal(Some(1))
  } yield assert(a, isSome(equalTo(1)).label("first")) &&
    assert(b, isSome(equalTo(1)).label("second")) &&
    assert(c, isSome(equalTo(1)).label("third")) &&
    assert(d, isSome(equalTo(1)).label("fourth"))
}

The error message would now get reported like so:

image

@adamgfraser adamgfraser requested a review from jdegoes November 10, 2019 02:43
ghostdogpr
ghostdogpr previously approved these changes Nov 10, 2019
* Labels this assertion with the specified string.
*/
final def label(string: String): Assertion[A] =
new Assertion(infix(param(self), "@@", param(quoted(string))), run)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might suggest ?? instead of @@ to match ZIO Config; and also have ?? operator alias.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Will change now.

@jdegoes jdegoes merged commit ed76e1d into zio:master Nov 11, 2019
@adamgfraser adamgfraser deleted the label branch November 11, 2019 03:04
Twizty pushed a commit to Twizty/zio that referenced this pull request Nov 13, 2019
* implement Assertion#label

* use ?? instead of @@ and add operator alias
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants