diff --git a/src/fable-library-py/fable_library/util.py b/src/fable-library-py/fable_library/util.py index 37b84e4303..badbf5fb4e 100644 --- a/src/fable-library-py/fable_library/util.py +++ b/src/fable-library-py/fable_library/util.py @@ -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 diff --git a/src/fable-library-py/tests/test_assert.py b/src/fable-library-py/tests/test_assert.py new file mode 100644 index 0000000000..260f4fc22d --- /dev/null +++ b/src/fable-library-py/tests/test_assert.py @@ -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")