Skip to content

Commit

Permalink
Reintroduce special-case msgs for comparing arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
valencik committed Apr 15, 2022
1 parent 822e5bf commit 1503667
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
)(implicit loc: Location, compare: Compare[A, B]): Unit = {
StackTraces.dropInside {
if (!compare.isEqual(obtained, expected)) {
(obtained, expected) match {
case (a: Array[_], b: Array[_]) if a.sameElements(b) =>
// Special-case error message when comparing arrays. See
// https://github.com/scalameta/munit/pull/393 and
// https://github.com/scalameta/munit/issues/339 for a related
// discussion on how MUnit should handle array comparisons. Other
// testing frameworks have special cases for arrays so the
// comparison succeeds as long as `sameElements()` returns true.
// MUnit chooses instead to fail the test with a custom error
// message because arrays have reference equality, for better or
// worse, and we should not hide that fact from our users.
failComparison(
"arrays have the same elements but different reference equality. " +
"Convert the arrays to a non-Array collection if you intend to assert the two arrays have the same elements. " +
"For example, `assertEquals(a.toSeq, b.toSeq)",
obtained,
expected
)
case _ =>
}
compare.failEqualsComparison(obtained, expected, clue, loc, this)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/shared/src/test/scala/munit/AssertionsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ assertEquals(new A, new B)
|""".stripMargin
)
}
test("array-sameElements".fail) {
test("array-sameElements") {
val e = intercept[ComparisonFailException] {
assertEquals(Array(1, 2), Array(1, 2))
}
Expand Down

0 comments on commit 1503667

Please sign in to comment.