diff --git a/tests/test_toml_document.py b/tests/test_toml_document.py index 84b114b9..4d40b0b6 100644 --- a/tests/test_toml_document.py +++ b/tests/test_toml_document.py @@ -22,6 +22,44 @@ from tomlkit.toml_document import TOMLDocument +@pytest.mark.parametrize( + "content", ["value = nan", "value = [nan]", "[table]\nvalue = nan"] +) +@pytest.mark.parametrize("plain", [False, True]) +def test_document_inequality_complements_nan_equality( + content: str, plain: bool +) -> None: + doc = parse(content) + other_doc = parse(content) + other = other_doc.unwrap() if plain else other_doc + + assert doc == other + assert other == doc + assert not (doc != other) + assert not (other != doc) + + +@pytest.mark.parametrize("content", ["value = 1", "value = inf", "value = [1, 2]"]) +def test_document_inequality_detects_different_values(content: str) -> None: + doc = parse(content) + other = parse("value = 2") + assert doc != other + assert other != doc + assert not (doc == other) + + +def test_document_inequality_preserves_reflected_comparison() -> None: + class Comparator: + def __ne__(self, other: object) -> bool: + return False + + doc = parse("value = nan") + other = Comparator() + assert doc.__ne__(other) is NotImplemented + assert not (doc != other) + assert doc != object() + + def test_document_is_a_dict(example: Callable[[str], str]) -> None: content = example("example") diff --git a/tomlkit/container.py b/tomlkit/container.py index 9265c740..02ed5b57 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -1066,6 +1066,12 @@ def __eq__(self, other: object) -> bool: return bool(_equal_with_nan(self.value, other)) + def __ne__(self, other: object) -> bool: + result = self.__eq__(other) + if result is NotImplemented: + return NotImplemented + return not result + def _getstate(self, protocol: int) -> tuple[bool]: return (self._parsed,)