Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/fable-library-py/fable_library/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ def assert_equal(actual: Any, expected: Any, msg: str | None = None) -> None:

def assert_not_equal[T](actual: T, expected: T, msg: str | None = None) -> None:
if equals(actual, expected):
raise Exception(msg or f"Expected: {expected} - Actual: {actual}")
# "Expected: x - Actual: x" would describe a *passing* assertion, so say what was
# actually expected: a value other than this one. Mirrors xUnit's "Expected: Not x".
raise Exception(msg or f"Expected not equal to: {expected} - Actual: {actual}")


MAX_LOCKS = 1024
Expand Down
33 changes: 33 additions & 0 deletions src/fable-library-py/tests/test_assert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from fable_library.util import assert_equal, assert_not_equal


def test_assert_equal_passes_on_equal_values():
assert_equal(1, 1)
assert_equal([1, 2], [1, 2])


def test_assert_equal_raises_on_mismatch():
with pytest.raises(Exception, match=r"^Expected: 1 - Actual: 2$"):
assert_equal(2, 1)


def test_assert_equal_uses_the_custom_message():
with pytest.raises(Exception, match=r"^values differ$"):
assert_equal(2, 1, "values differ")


def test_assert_not_equal_passes_on_different_values():
assert_not_equal(1, 2)


def test_assert_not_equal_raises_when_equal():
# The failure has to describe what was expected -- some value *other* than 1. Rendering it as
# "Expected: 1 - Actual: 1" reads as a passing assertion, which is what it used to say.
with pytest.raises(Exception, match=r"^Expected not equal to: 1 - Actual: 1$"):
assert_not_equal(1, 1)


def test_assert_not_equal_uses_the_custom_message():
with pytest.raises(Exception, match=r"^values are equal$"):
assert_not_equal(1, 1, "values are equal")
Loading