forked from platformps/exercise.python_fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator_test.py
81 lines (68 loc) · 2.35 KB
/
calculator_test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Created by Leon Hunter at 11:23 AM 10/24/2020
import unittest
from numbers import Number
from src.main.calculator import Calculator
class CalculatorTest(unittest.TestCase):
def _test(self, method_to_test, value_sets):
for value_set in value_sets:
# given
first_value = value_set[0]
second_value = value_set[1]
expected_calculation = value_set[2]
# when
actual_calculation = method_to_test(first_value, second_value)
calculation_error_message = '''
first_value = {}
second_value = {}
expected_calculation = {}
actual_calculation = {}
'''.format(first_value, second_value, expected_calculation, actual_calculation)
return_type_error_message = '''
expected return value of `{}` to be of type `Number`
instead was of type `{}`
'''.format(method_to_test.__name__, type(actual_calculation))
# then
self.assertTrue(isinstance(actual_calculation, Number), return_type_error_message)
self.assertAlmostEqual(expected_calculation, actual_calculation, msg=calculation_error_message)
def test_add(self):
self._test(Calculator().add, [
(1, 3, 4),
(5, 8, 13),
(13, 21, 34),
(0, 0, 0),
(3, 1, 4),
(8, 5, 13),
(21, 13, 34),
])
def test_subtract(self):
self._test(Calculator().subtract, [
(1, 3, -2),
(5, 8, -3),
(13, 21, -8),
(0, 0, 0),
(3, 1, 2),
(8, 5, 3),
(21, 13, 8),
])
def test_multiply(self):
self._test(Calculator().multiply, [
(1, 3, 3),
(5, 8, 40),
(13, 21, 273),
(0, 0, 0),
(3, 1, 3),
(8, 5, 40),
(21, 13, 273),
])
def test_divide(self):
self._test(Calculator().divide, [
(1, 3, .333),
(5, 8, .625),
(13, 21, .619),
#(0, 0, ZeroDivisionError), # this is another change
(3, 1, 3),
(8, 5, 1.6),
(21, 13, 1.615),
])
if __name__ == '__main__':
unittest.main()