Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Relax assertEquals strictness about type equality. #70

Merged
merged 6 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions docs/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,18 @@ Comparing two values of different types is a compile error.
assertEquals(1, "")
```

The two types must match exactly, it's a type error to compare two values even
if one value is a subtype of the other.
The "expected" value (second argument) must be a subtype of the "obtained" value
(first argument). It's a type error to compare two values even if one value is a
subtype of the other.
Copy link
Member

Choose a reason for hiding this comment

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

is it still a type error?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not a type error, removed


```scala mdoc:fail
assertEquals(Some(1), Option(1))
```scala mdoc
assertEquals(Option(1), Some(1))
```

Upcast the subtype using a type ascription `subtype: Supertype` when you want to
compare a subtype with a supertype.
It's a compile error if you swap the order of the arguments.

```scala mdoc
// OK
assertEquals(Some(1): Option[Int], Option(1))
```scala mdoc:fail
assertEquals(Some(1), Option(1))
```

## `assertNotEquals()`
Expand Down
2 changes: 1 addition & 1 deletion munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
obtained: A,
expected: B,
clue: => Any = "values are not the same"
)(implicit loc: Location, ev: A =:= B): Unit = {
)(implicit loc: Location, ev: B <:< A): Unit = {
StackTraces.dropInside {
if (obtained != expected) {
Diffs.assertNoDiff(
Expand Down
11 changes: 11 additions & 0 deletions tests/shared/src/test/scala/munit/AssertionsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ class AssertionsSuite extends BaseSuite {
|}
|""".stripMargin
)

test("subtype".tag(NoDotty)) {
assertEquals(Option(1), Some(1))
assertNoDiff(
compileErrors("assertEquals(Some(1), Option(1))"),
"""|error: Cannot prove that Option[Int] <:< Some[Int].
|assertEquals(Some(1), Option(1))
| ^
|""".stripMargin
)
}
}