Skip to content

Commit

Permalink
Suggested changes to make function better
Browse files Browse the repository at this point in the history
  • Loading branch information
olumide-AI committed Jan 12, 2025
1 parent d6b44c0 commit 23a5705
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
10 changes: 7 additions & 3 deletions solutions/multiply_two_numbers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""
A module for multiplying two integers.
Modul contents:
-multiply_two_numbers: A fnuction to multiply two integers.
Module contents:
- multiply_two_numbers: A function to multiply two integers.
created 2025-01-04
@author: Manezhah Mohmand
"""


def multiply_two_numbers(a: int, b: int) -> int:
"""Multiplies two integers and returns the product.
g
parameters:
a (int): The first integer
b (int):The second integer
Expand All @@ -24,4 +24,8 @@ def multiply_two_numbers(a: int, b: int) -> int:
>>> multiply_two_numbers(2, 3)
6
"""

#Raise an error if a and b are not integers
if not isinstance(a, int) or not isinstance(b, int):
raise TypeError("Both arguments must be integers.")
return a * b
33 changes: 27 additions & 6 deletions solutions/tests/test_multiply_two_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,39 @@
class TestMultiplyTwoNumbers(unittest.TestCase):
"""Tests for multiply_two_numbers function."""

def test_positive_numbers(self):
"""It should return the product of two positive numbers."""
# Standard cases
def test_two_positive_numbers(self):
"""It should return the product of two positive integers."""
self.assertEqual(multiply_two_numbers(2, 3), 6)

def test_negative_numbers(self):
"""It should return the product of two negative numbers."""
def test_two_negative_numbers(self):
"""It should return the product of two negative integers."""
self.assertEqual(multiply_two_numbers(-2, -3), 6)

def test_mixed_sign_numbers(self):
"""It should return the product of one positive and one negative number."""
def test_positive_and_negative_number(self):
"""It should return the product of a positive and a negative integer."""
self.assertEqual(multiply_two_numbers(-2, 3), -6)

# Boundary cases
def test_zero_with_positive_number(self):
"""It should return 0 when one number is 0 and the other is positive."""
self.assertEqual(multiply_two_numbers(0, 5), 0)

def test_zero_with_negative_number(self):
"""It should return 0 when one number is 0 and the other is negative."""
self.assertEqual(multiply_two_numbers(0, -7), 0)

# Defensive tests
def test_non_integer_input(self):
"""It should raise a TypeError when the inputs are not integers."""
with self.assertRaises(TypeError):
multiply_two_numbers(2.5, 3)

def test_string_input(self):
"""It should raise a TypeError when one or both inputs are strings."""
with self.assertRaises(TypeError):
multiply_two_numbers("2", "3")


if __name__ == "__main__":
unittest.main()

0 comments on commit 23a5705

Please sign in to comment.