-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare.py
49 lines (39 loc) · 1.43 KB
/
square.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def square_number(number: float) -> float:
"""
Squares a number (second degree).
Args:
number (float): The number to square
Returns:
Float: The result of squaring
Raises:
TypeError: If the input parameter is not a number
"""
if not isinstance(number, (int, float)):
raise TypeError("The input parameter must be a number")
return number ** 2
import unittest
class TestSquareNumber(unittest.TestCase):
def test_positive_numbers(self):
self.assertEqual(square_number(2), 4)
self.assertEqual(square_number(5), 25)
self.assertEqual(square_number(10), 100)
def test_negative_numbers(self):
self.assertEqual(square_number(-2), 4)
self.assertEqual(square_number(-5), 25)
def test_zero(self):
self.assertEqual(square_number(0), 0)
def test_float_numbers(self):
self.assertAlmostEqual(square_number(1.5), 2.25)
self.assertAlmostEqual(square_number(-1.5), 2.25)
def test_invalid_types(self):
# Check that the function calls TypeError for invalid types
with self.assertRaises(TypeError):
square_number("2")
with self.assertRaises(TypeError):
square_number([1, 2, 3])
with self.assertRaises(TypeError):
square_number(None)
with self.assertRaises(TypeError):
square_number({"number": 2})
if __name__ == '__main__':
unittest.main()