-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong order of arguments for assertEquals #189
Comments
Thank you for reporting! This has been brought up before and I agree it's unfortunate. The current choice of the parameter ordering is not random, it's motivated by a particular pattern I use a lot in my own codebases, which is to write assertions where the assertNoDiff(
someMethod(),
"""|Hello
|World!
|""".stripMargin
) The I am afraid it's unlikely this will change. The only workaround would be to roll your own
However, we may need to do some extra work to avoid the |
I can see there are tests that benefit slightly from the actual-first ordering, but this pattern benefits from the expected-first ordering:
Personally I think this is considerably more common than the "long expected" string case, though it would be hard to prove that. In any case, either pattern is easy enough to accommodate with an extra local
Given how easy that is to do either way, I really think it's very bad for MUnit not to follow the JUnit precedent, if you want MUnit to be widely used. The gain of using a nonstandard ordering is so small, and the cost is so large: endless mental friction. |
Stability is one of my key motivations for creating in MUnit. I'm hoping to release v1 before the end of the year and never introduce breaking changes after that. While I sympathize with your perspective, I struggle to justify introducing such a breaking change to 100% of existing MUnit users in favor or making the migration easier to hypothetical new users coming from JUnit assertions. ScalaTest and utest |
I'll add to the discussion, saying that this is a problem of migrating from JUnit to MUnit, which (while annoying in some instances where the order matters) is still a fairly rare use case. I don't think many projects in Scala other than the compiler itself use JUnit directly, so I don't think it would make sense to introduce a huge breaking change to support that specific use case. For MUnit to get larger adoption, I think it would rather make sense to focus on making Scalatest / Speca2 migration path easier, but that's an entirely different topic |
That's completely fair. However, I think a possible middle ground would be to remove the To up this in perspective, it took me ~4 hours to migrate EPFL's moocs repo (~40k loc, half tests) from JUnit to MUnit, and about half of that time was spent messing around with I'm all in favor of compile time safety, but in the precise case of |
I can imagine that must have been an annoying migration. I took a stab in #191 at implementing a basic Scalafix migration rule that swaps the arguments for JUnit
Did you try to use |
well, with Product with Serializable, Jokes apart, this is educational material so I really tried to avoid |
Right, it's a hard balance to strike, but I think it's really nice (even more so for educational purposes) that you get a compiler error for stuff like val x = Some(2)
val y = 4
assertEquals(x, y) // woops I make many of those little mistakes while writing tests and so far it's been really nice for those to be caught by the compiler |
If this is for education material then I agree it would be better to remove the type parameters. The best argument IMO to remove the type parameters is that it makes the library less accessible for beginners, who will struggle to understand the |
At the same time, it may be quite surprising for a beginner that a strongly typed language like Scala can't detect a simple mistake like the one I've shown above. |
@gabro i think it’s more likely that students become frustrated by the cryptic error message that don’t explain how to fix the problem. The current API is pretty much designed for power users. |
Another way to deal with these in Dotty is to use the |
I agree. I’ve been considering whether to use the Eql typeclass in Dotty only. |
I opened #223 to start using |
As another data point, I prefer It's an interesting discussion, whether it's more LOC to construct the expected or execute the SUT. I may contribute it as an alternative flavor and see if it catches on; or just continue to use the idiom privately. |
TIL olafurpg's paradigmatic example is
|
Gonna close this issue as "won't fix". The PR #225 is still open because I wanted to avoid breaking changes during the Scala 3 milestone phase. I think it's a good idea to relax the type constraints on |
Even though this is a closed issue now, I would like to encourage you to never make a breaking change in assertEquals and instead create a new method (e.g. named "assertEq") with swapped actual and expected parameters and then disable assertEquals one day. |
|
In junit,
assertEquals
is defined asassertEquals(expected, actual)
, but in munit, it's the other way around.It's really unfortunate because the type constraint on assertEquals will sometimes require the arguments to be swapped, for instance when asserrting that something is
Nil
orNone
, meaning this not just a documentation issue, but a migration burden...I know this is probably too late to fix because it would mess up everyone's munit tests, so just close it and give me the most annoying issue award 😛
The text was updated successfully, but these errors were encountered: