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

Skip to content

Conversation

@myazinn
Copy link
Contributor

@myazinn myazinn commented Sep 6, 2022

Let's say we've got the following test

import zio.test._

object MySpec extends ZIOSpecDefault {
  override def spec: Spec[Any, Any] =
    suite("sweet") {
      test("test") {
        val exists = Seq(1, 2, 3).exists(i => if (i == 1) ??? else i == 2)
        assertTrue(exists)
      }
    }
}

If we run it, it fails as expected

+ sweet
  - test
    Exception in thread "zio-fiber-25" scala.NotImplementedError: an implementation is missing
        at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
        at zio.MySpec.spec(MySpec.scala:8)
0 tests passed. 1 tests failed. 0 tests ignored.

However, if we just inline exists variable, the test unexpectedly passes

import zio.test._

object MySpec extends ZIOSpecDefault {
  override def spec: Spec[Any, Any] =
    suite("sweet") {
      test("test") {
        assertTrue(Seq(1, 2, 3).exists(i => if (i == 1) ??? else i == 2))
      }
    }
}
+ sweet
  + test
1 tests passed. 0 tests failed. 0 tests ignored.

I actually faced this corner case and was surprised that my tests pass when they shouldn't.
Proposed solution to this issue is somewhat arbitrary, I don't know if it's better to fail the test with Result.die(???) using the first found exception or to use Result.succeed(false) and combine errors, but the latter looks more informative to me.
So if you apply suggested changes and run the test again, you'll see something like this

+ sweet
  - test
    ? 1 element satisfied the predicate but it's discarded since the predicate threw an exception on 1 element
    Seq(1, 2, 3).exists(i => if (i == 1) ??? else i == 2)
          ? ERROR: scala.NotImplementedError: an implementation is missing
            scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
            zio.MySpec$.$anonfun$spec$5(MySpec.scala:9)
            scala.runtime.java8.JFunction0$mcZ$sp.apply(JFunction0$mcZ$sp.scala:17)
          Seq(1, 2, 3).exists(i => if (i == 1) ??? else i == 2)
          at foo/bar/buzz/zio/MySpec.scala:9
    (1, 2, 3) = List(1, 2, 3)
    Seq = scala.collection.immutable.Seq$@1855a2b8
    at foo/bar/buzz/zio/MySpec.scala:9

0 tests passed. 1 tests failed. 0 tests ignored.

Note that this solution still a little bit inconsistent with Scala's exists, because it will fail event if the exception was thrown after the predicate had been satisfied at least once. It could be fixed, but maybe it's a feature?
Also, I believe this PR might break some test somewhere. Don't think it's a big issue, but still needs to be highlighted.

@myazinn myazinn changed the title make existsIterable assertion more strict ZIO Test: make existsIterable assertion more strict Sep 6, 2022
@narma
Copy link
Contributor

narma commented Oct 17, 2022

Any chance it can be included in upcoming release?

@myazinn myazinn force-pushed the make_exists_assertion_more_strict branch from a36ac56 to 4c599af Compare October 23, 2022 15:31
@myazinn
Copy link
Contributor Author

myazinn commented Oct 23, 2022

@kitlangton could you please take a look if you get a chance?

Copy link
Member

@jdegoes jdegoes left a comment

Choose a reason for hiding this comment

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

Looks good to me, can you add tests to verify the original issue is fixed?

@myazinn myazinn force-pushed the make_exists_assertion_more_strict branch from 4c599af to 22e4b02 Compare November 23, 2022 16:52
@myazinn
Copy link
Contributor Author

myazinn commented Nov 23, 2022

@jdegoes thanks for the review
I've added tests

@adamgfraser
Copy link
Contributor

I think it would be good to make this mirror the behavior of exists on Scala collections where subsequent elements would not be evaluated if an element satisfied the predicate.

@myazinn myazinn force-pushed the make_exists_assertion_more_strict branch 4 times, most recently from 5d58cad to 005765d Compare November 25, 2022 07:42
@myazinn myazinn force-pushed the make_exists_assertion_more_strict branch from 005765d to 9837375 Compare November 25, 2022 08:10
@jdegoes jdegoes merged commit 1d375f6 into zio:series/2.x Nov 25, 2022
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.

4 participants