Skip to content

Commit 7533f3a

Browse files
CouldNotpre-commit-ci[bot]
authored andcommitted
Add error tests in doctest and fix error message (TheAlgorithms#10930)
* 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>
1 parent 52ddfd3 commit 7533f3a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

maths/prime_check.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ def is_prime(number: int) -> bool:
2929
True
3030
>>> is_prime(67483)
3131
False
32+
>>> is_prime(16.1)
33+
Traceback (most recent call last):
34+
...
35+
ValueError: is_prime() only accepts positive integers
36+
>>> is_prime(-4)
37+
Traceback (most recent call last):
38+
...
39+
ValueError: is_prime() only accepts positive integers
3240
"""
3341

3442
# precondition
35-
assert isinstance(number, int) and (
36-
number >= 0
37-
), "'number' must been an int and positive"
43+
if not isinstance(number, int) or not number >= 0:
44+
raise ValueError("is_prime() only accepts positive integers")
3845

3946
if 1 < number < 4:
4047
# 2 and 3 are primes
@@ -64,7 +71,7 @@ def test_primes(self):
6471
assert is_prime(29)
6572

6673
def test_not_primes(self):
67-
with pytest.raises(AssertionError):
74+
with pytest.raises(ValueError):
6875
is_prime(-19)
6976
assert not is_prime(
7077
0

0 commit comments

Comments
 (0)