Repository: pytest-dev/pytest
File: testing/python/approx.py (Python)

Code before:
            [1.0, 2, "different"],
            [
                "",
                r"  comparison failed. Mismatched elements: 2 / 3:",
                rf"  Max absolute difference: {SOME_FLOAT}",
                rf"  Max relative difference: {SOME_FLOAT}",
                r"  Index \| Obtained\s+\| Expected\s+",
                r"\s*0\s*\|\s*1\.1\s*\|\s*1\.0\s*±\s*1\.0e\-06\s*",
                r"\s*2\s*\|\s*word\s*\|\s*different\s*",
            ],
            verbosity_level=2,
        )

    def test_operator_overloading(self):
        assert 1 == approx(1, rel=1e-6, abs=1e-12)
        assert not (1 != approx(1, rel=1e-6, abs=1e-12))
        assert 10 != approx(1, rel=1e-6, abs=1e-12)
        assert not (10 == approx(1, rel=1e-6, abs=1e-12))

    def test_exactly_equal(self):
        examples = [
            (2.0, 2.0),
            (0.1e200, 0.1e200),
            (1.123e-300, 1.123e-300),
            (12345, 12345.0),
            (0.0, -0.0),
            (345678, 345678),
            (Decimal("1.0001"), Decimal("1.0001")),
            (Fraction(1, 3), Fraction(-1, -3)),
        ]
        for a, x in examples:
            assert a == approx(x)

    def test_opposite_sign(self):
        examples = [(eq, 1e-100, -1e-100), (ne, 1e100, -1e100)]
        for op, a, x in examples:
            assert op(a, approx(x))

    def test_zero_tolerance(self):
        within_1e10 = [(1.1e-100, 1e-100), (-1.1e-100, -1e-100)]
        for a, x in within_1e10:
            assert x == approx(x, rel=0.0, abs=0.0)
            assert a != approx(x, rel=0.0, abs=0.0)
            assert a == approx(x, rel=0.0, abs=5e-101)
            assert a != approx(x, rel=0.0, abs=5e-102)
            assert a == approx(x, rel=5e-1, abs=0.0)
            assert a != approx(x, rel=5e-2, abs=0.0)

    @pytest.mark.parametrize(
        ("rel", "abs"),
        [

Review comment (suggestion):
While at it, let's add an assert that works:

```suggestion
        )
        assert [1.1, 2, "word"] == pytest.approx([1.1, 2, "word"])
```

Code after:
        np = pytest.importorskip("numpy")
        np_array = np.array(value)
        assert repr(approx(np_array)) == expected_repr_string

    def test_bool(self):
        with pytest.raises(AssertionError) as err:
            assert approx(1)

        assert err.match(r"approx\(\) is not supported in a boolean context")

    def test_mixed_sequence(self, assert_approx_raises_regex) -> None:
        """Approx should work on sequences that also contain non-numbers (#13010)."""
        assert_approx_raises_regex(
            [1.1, 2, "word"],
            [1.0, 2, "different"],
            [
                "",
                r"  comparison failed. Mismatched elements: 2 / 3:",
                rf"  Max absolute difference: {SOME_FLOAT}",
                rf"  Max relative difference: {SOME_FLOAT}",
                r"  Index \| Obtained\s+\| Expected\s+",
                r"\s*0\s*\|\s*1\.1\s*\|\s*1\.0\s*±\s*1\.0e\-06\s*",
                r"\s*2\s*\|\s*word\s*\|\s*different\s*",
            ],
            verbosity_level=2,
        )
        assert_approx_raises_regex(
            [1.1, 2, "word"],
            [1.0, 2, "word"],
            [
                "",
                r"  comparison failed. Mismatched elements: 1 / 3:",
                rf"  Max absolute difference: {SOME_FLOAT}",
                rf"  Max relative difference: {SOME_FLOAT}",
                r"  Index \| Obtained\s+\| Expected\s+",
                r"\s*0\s*\|\s*1\.1\s*\|\s*1\.0\s*±\s*1\.0e\-06\s*",
            ],
            verbosity_level=2,
        )
        assert [1.1, 2, "word"] == pytest.approx([1.1, 2, "word"])

    def test_operator_overloading(self):
        assert 1 == approx(1, rel=1e-6, abs=1e-12)
        assert not (1 != approx(1, rel=1e-6, abs=1e-12))
        assert 10 != approx(1, rel=1e-6, abs=1e-12)
        assert not (10 == approx(1, rel=1e-6, abs=1e-12))

    def test_exactly_equal(self):
        examples = [
            (2.0, 2.0),
            (0.1e200, 0.1e200),