From 826bbec39b0acb7988c72da84a22ec60617a0681 Mon Sep 17 00:00:00 2001 From: Dani Pinyol Date: Thu, 11 Jul 2024 12:52:41 +0200 Subject: [PATCH] Add `is_nan` --- examples/test_example_functions_fail.py | 10 +++++++++- examples/test_example_functions_pass.py | 10 +++++++++- src/pytest_check/check_functions.py | 21 ++++++++++++++++++++- tests/test_functions.py | 4 ++-- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/examples/test_example_functions_fail.py b/examples/test_example_functions_fail.py index 36755d2..ca09efb 100644 --- a/examples/test_example_functions_fail.py +++ b/examples/test_example_functions_fail.py @@ -2,7 +2,7 @@ Failing versions of all of the check helper functions. """ from pytest_check import check - +import math def test_equal(): check.equal(1, 2) @@ -24,6 +24,14 @@ def test_is_not(): check.is_not(x, y) +def test_is_nan(): + check.is_nan(42) + + +def test_is_not_nan(): + check.is_not_nan(math.nan) + + def test_is_true(): check.is_true(False) diff --git a/examples/test_example_functions_pass.py b/examples/test_example_functions_pass.py index e216ebe..5543318 100644 --- a/examples/test_example_functions_pass.py +++ b/examples/test_example_functions_pass.py @@ -2,7 +2,7 @@ Passing versions of all of the check helper functions. """ from pytest_check import check - +import math def test_equal(): check.equal(1, 1) @@ -18,6 +18,14 @@ def test_is(): check.is_(x, y) +def test_is_nan(): + check.is_nan(math.nan) + + +def test_is_not_nan(): + check.is_not_nan(0) + + def test_is_not(): x = ["foo"] y = ["foo"] diff --git a/src/pytest_check/check_functions.py b/src/pytest_check/check_functions.py index c601a31..344e121 100644 --- a/src/pytest_check/check_functions.py +++ b/src/pytest_check/check_functions.py @@ -1,7 +1,7 @@ import functools import pytest - +import math from .check_log import log_failure __all__ = [ @@ -14,6 +14,8 @@ "is_false", "is_none", "is_not_none", + "is_nan", + "is_not_nan", "is_in", "is_not_in", "is_instance", @@ -121,6 +123,23 @@ def is_not_none(x, msg=""): return False +def is_nan(a, msg=""): + __tracebackhide__ = True + if math.isnan(a): + return True + else: + log_failure(f"check {a} is NaN", msg) + return False + + +def is_not_nan(a, msg=""): + __tracebackhide__ = True + if not math.isnan(a): + return True + else: + log_failure(f"check {a} is not NaN", msg) + return False + def is_in(a, b, msg=""): __tracebackhide__ = True if a in b: diff --git a/tests/test_functions.py b/tests/test_functions.py index 8f220b1..38f3f5b 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -1,10 +1,10 @@ def test_passing_check_functions(pytester): pytester.copy_example("examples/test_example_functions_pass.py") result = pytester.runpytest() - result.assert_outcomes(failed=0, passed=23) + result.assert_outcomes(failed=0, passed=25) def test_failing_check_functions(pytester): pytester.copy_example("examples/test_example_functions_fail.py") result = pytester.runpytest() - result.assert_outcomes(failed=23, passed=0) + result.assert_outcomes(failed=25, passed=0)