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

Skip to content

Conversation

@guizmaii
Copy link
Member

@guizmaii guizmaii commented May 28, 2025

It's extremely frustrating to have a test framework which doesn't show/report the data when there's a diff between the result of the test execution and the expected result

I always have to add some debug println and relaunch my tests, to be able to see this diff.
IMO, that shouldn't be the case.
zio-test should show me the result data and the expected data in its output so I can fix my test

Effectively reverting #8692

I tried to optmise a bit the code so that performances aren't impacted too much by this change.

I tested the perfs with this test (coming from the initally reported issue #8644):

test("speed test - comes from https://github.com/zio/zio/issues/8644") {
      final case class ComplexObject(
        id: String,
        name: String,
        name2: String,
        name3: String,
        name4: String,
        name5: String,
        name6: String,
        name7: String,
        name8: String,
        name9: String,
        name10: String,
        name11: String
      )

      object ComplexObject {
        def of(i: Int): ComplexObject = {
          val s = i.toString
          ComplexObject(
            id = s,
            name = s,
            name2 = s,
            name3 = s,
            name4 = s,
            name5 = s,
            name6 = s,
            name7 = s,
            name8 = s,
            name9 = s,
            name10 = s,
            name11 = s
          )
        }
      }

      for {
        _ <- ZIO.unit
        // sample of complex objects
        hugeList = (1 to 1000).map(i => ComplexObject.of(i)).toList

        shouldNotHave20  = assertTrue(!hugeList.exists(_.name3 == "20"))
        shouldNotHave510 = assertTrue(!hugeList.exists(_.name3 == "510"))
        shouldNotHave780 = assertTrue(!hugeList.exists(_.name3 == "780"))
        shouldNotHave999 = assertTrue(!hugeList.exists(_.name3 == "999"))
      } yield TestResult.allSuccesses(
        shouldNotHave20,
        shouldNotHave510,
        shouldNotHave780,
        shouldNotHave999
      )
    }

With the code optimisations, the test indeed takes a bit longer to execute, but only around ~550-600ms, which isn't "forever" in my opinion (the author of the original ticket was reporting, I quote, "The exists assertions take forever")
I have a similar computer to the reporter of the issue: Macbook Pro M3 36Gb of RAM.

@guizmaii guizmaii marked this pull request as draft May 28, 2025 08:52
@guizmaii guizmaii self-assigned this May 28, 2025
@guizmaii guizmaii force-pushed the test_remove_+_more branch 4 times, most recently from 485e014 to e3430f6 Compare May 28, 2025 09:20
@guizmaii guizmaii force-pushed the test_remove_+_more branch 4 times, most recently from 6768862 to 85be7d7 Compare May 28, 2025 09:38
@guizmaii guizmaii marked this pull request as ready for review May 28, 2025 09:54
s"""$name($spacer$indentedBody$spacer)"""

case string: String =>
val surround = if (string.split("\n").length > 1) "\"\"\"" else "\""
Copy link
Member Author

@guizmaii guizmaii May 28, 2025

Choose a reason for hiding this comment

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

string.split("\n").length > 1 not a great way to check if a String contains a Character 😕

@guizmaii guizmaii marked this pull request as draft May 28, 2025 10:41
@guizmaii guizmaii force-pushed the test_remove_+_more branch from 53bb9df to 8daf5f9 Compare May 28, 2025 11:25
Comment on lines -42 to -50
val body = labels0
.zip(product.productIterator)
.map { case (key, value) =>
s"${(key + " =").faint} ${PrettyPrint(value)}"
}
.toList
.mkString(",\n")
val isMultiline = body.split("\n").length > 1
val indentedBody = indent(body, if (isMultiline) 2 else 0)
Copy link
Member Author

Choose a reason for hiding this comment

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

This wasn't great. We were building a String with \n as separator to then split it twice on this same character. I implemented an optimised version of this algorithm

@guizmaii guizmaii force-pushed the test_remove_+_more branch 2 times, most recently from be23ade to 58b60ad Compare May 28, 2025 11:33
@guizmaii guizmaii requested a review from plokhotnyuk May 28, 2025 11:35
@guizmaii guizmaii marked this pull request as ready for review May 28, 2025 11:40
@guizmaii guizmaii marked this pull request as draft May 28, 2025 11:49
@guizmaii guizmaii force-pushed the test_remove_+_more branch from 262c98d to fd887c8 Compare May 28, 2025 11:57
Iterator.continually("")
}

def prettyPrintProduct(product: Product): String = {
Copy link
Member Author

@guizmaii guizmaii May 28, 2025

Choose a reason for hiding this comment

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

previous, non-optimized, compatible with Scala 2.12, version of the algorithm

@guizmaii guizmaii force-pushed the test_remove_+_more branch from 27cd172 to 4f5ade0 Compare July 1, 2025 08:02
@guizmaii
Copy link
Member Author

guizmaii commented Jul 1, 2025

@kyri-petrou @hearnadam I took all your comments into account

@guizmaii guizmaii requested review from hearnadam and kyri-petrou July 1, 2025 16:31
@guizmaii guizmaii marked this pull request as ready for review July 1, 2025 16:31
Copy link
Collaborator

@hearnadam hearnadam left a comment

Choose a reason for hiding this comment

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

LGTM aside from the test changes.


case set: Set[_] =>
prettyPrintIterator(set.iterator, set.size, className(set))
// For why `Nil.type` is used. See https://github.com/zio/zio/pull/9900#discussion_r2121380398
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of a comment, can you add a test for Vector.empty? (or any other non-List empty Seq)?

Copy link
Member Author

@guizmaii guizmaii Jul 2, 2025

Choose a reason for hiding this comment

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

Done!

Interestingly, the previous code was printing Nil for Vector.empty while now it correctly prints Vector() 🙂

I keep the comment as using _: Nil.type is not usual code

}

for {
_ <- ZIO.unit
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you avoid the ZIO/for completely?

val x =
val y = ...
assertTrue(

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@guizmaii guizmaii force-pushed the test_remove_+_more branch from f773da7 to f0a851f Compare July 2, 2025 08:14
@hearnadam hearnadam merged commit b268b69 into series/2.x Jul 2, 2025
19 checks passed
@hearnadam hearnadam deleted the test_remove_+_more branch July 2, 2025 09:47
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.

5 participants