Skip to content

Commit

Permalink
Add error tests in doctest and fix error message (TheAlgorithms#10930)
Browse files Browse the repository at this point in the history
* Add error tests in doctest and fix error message

* Change AssertationError to ValueError

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
CouldNot and pre-commit-ci[bot] authored Oct 26, 2023
1 parent 0ffe506 commit 0e7f828
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions maths/prime_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ def is_prime(number: int) -> bool:
True
>>> is_prime(67483)
False
>>> is_prime(16.1)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers
>>> is_prime(-4)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers
"""

# precondition
assert isinstance(number, int) and (
number >= 0
), "'number' must been an int and positive"
if not isinstance(number, int) or not number >= 0:
raise ValueError("is_prime() only accepts positive integers")

if 1 < number < 4:
# 2 and 3 are primes
Expand Down Expand Up @@ -64,7 +71,7 @@ def test_primes(self):
assert is_prime(29)

def test_not_primes(self):
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
is_prime(-19)
assert not is_prime(
0
Expand Down

0 comments on commit 0e7f828

Please sign in to comment.