forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from MIT-Emerging-Talent/positive-number-function
Add Positive number function to the main
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on XX XX XX | ||
A module for checking if an integer is positive. | ||
@author: Luyando .E. Chitindi | ||
""" | ||
|
||
|
||
def is_positive(number: int) -> bool: | ||
""" | ||
This checks if an integer is positive. | ||
Parameters: | ||
number: int, the number to check | ||
Returns -> bool: | ||
True if the number is positive, false otherwise. | ||
Raises: | ||
AssertionError: if the input is not an integer | ||
Example: | ||
>>> is_positive(4) | ||
True | ||
>>> is_positive(-3) | ||
False | ||
>>> is_positive(0) | ||
False | ||
>>> is_positive("hello") | ||
Traceback (most recent call last): | ||
... | ||
AssertionError: Input must be an integer. | ||
""" | ||
assert isinstance(number, int), "Input must be an integer" | ||
return number > 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Unit tests for the is_positive function. | ||
@author: Luyando .E. Chitindi | ||
""" | ||
|
||
import unittest | ||
from solutions.is_positive import is_positive | ||
|
||
|
||
class TestIsPositive(unittest.TestCase): | ||
"""Test the is_positive function""" | ||
|
||
def test_positive_number(self): | ||
"""It should evaluate 4 to True""" | ||
actual = is_positive(4) | ||
expected = True | ||
self.assertEqual(actual, expected) | ||
|
||
def test_negative_number(self): | ||
"""It should evaluate -3 to False""" | ||
actual = is_positive(-3) | ||
expected = False | ||
self.assertEqual(actual, expected) | ||
|
||
def test_zero(self): | ||
"""It should evaluate 0 to False""" | ||
actual = is_positive(0) | ||
expected = False | ||
self.assertEqual(actual, expected) | ||
|
||
def test_non_integer_input(self): | ||
"""It should raise an assertion error if the argument is not an integer""" | ||
with self.assertRaises(AssertionError): | ||
is_positive(3.5) | ||
|
||
with self.assertRaises(AssertionError): | ||
is_positive("string") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |