Skip to content

Commit

Permalink
Add is_nan
Browse files Browse the repository at this point in the history
  • Loading branch information
Dani Pinyol committed Jul 30, 2024
1 parent a7acdc5 commit 826bbec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
10 changes: 9 additions & 1 deletion examples/test_example_functions_fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion examples/test_example_functions_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"]
Expand Down
21 changes: 20 additions & 1 deletion src/pytest_check/check_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools

import pytest

import math
from .check_log import log_failure

__all__ = [
Expand All @@ -14,6 +14,8 @@
"is_false",
"is_none",
"is_not_none",
"is_nan",
"is_not_nan",
"is_in",
"is_not_in",
"is_instance",
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 826bbec

Please sign in to comment.