-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_calculator.py
More file actions
131 lines (98 loc) · 3.78 KB
/
Copy pathtest_calculator.py
File metadata and controls
131 lines (98 loc) · 3.78 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pytest
from decimal import Decimal
from calculator import Calculator
def test_public_api_basic_operations_and_precision():
calc = Calculator(precision=2, max_value=1000)
assert calc.get_total() == Decimal("0.00")
# Basic adds: add 300 twice -> 600
calc.add(300)
calc.add(300)
assert calc.get_total() == Decimal("600.00")
# Small legitimate add
calc.add(0.5)
assert calc.get_total() == Decimal("600.50")
def test_overflow_raises_and_restores_previous_total():
calc = Calculator(precision=2, max_value=1000)
calc.add(300)
calc.add(300)
assert calc.get_total() == Decimal("600.00")
# Adding 600 would exceed max (1000) and must raise ValueError, total stays 600
with pytest.raises(ValueError):
calc.add(600)
assert calc.get_total() == Decimal("600.00")
def test_invalid_inputs_behavior():
calc = Calculator(precision=2, max_value=1000)
# valid numeric string
calc.add("12.34")
assert calc.get_total() == Decimal("12.34")
# invalid numeric string (hex) -> ValueError
with pytest.raises(ValueError):
calc.add("0x10")
# bytes, None, arbitrary object -> TypeError
bad_inputs = [b"\x00\x10", None, object()]
for bad in bad_inputs:
with pytest.raises(TypeError):
calc.add(bad)
def test_multiplication_division_and_divide_by_zero():
calc = Calculator(precision=2, max_value=1000)
calc.add(10)
calc.multiply(2)
assert calc.get_total() == Decimal("20.00")
calc.divide(4)
assert calc.get_total() == Decimal("5.00")
with pytest.raises(ValueError):
calc.divide(0)
def test_clear_is_one_shot_undo_and_clear_all():
calc = Calculator(precision=2, max_value=1000)
# build 1,2,3 -> 6
calc.add(1)
calc.add(2)
calc.add(3)
assert calc.get_total() == Decimal("6.00")
# mistake: add 44 -> 50
calc.add(44)
assert calc.get_total() == Decimal("50.00")
# one-shot clear: restore previous total 6 and forget 44
calc.clear()
assert calc.get_total() == Decimal("6.00")
# calling clear again does nothing (undo was consumed)
calc.clear()
assert calc.get_total() == Decimal("6.00")
# continue working
calc.add(4)
assert calc.get_total() == Decimal("10.00")
# clear_all wipes everything
calc.clear_all()
assert calc.get_total() == Decimal("0.00")
def test_clear_all_and_rebuild_with_varied_operations():
calc = Calculator(precision=2, max_value=1000)
# create a non-zero total using three different methods: add, multiply, subtract
calc.add(2)
calc.multiply(3) # 6.00
calc.subtract(1) # 5.00
assert calc.get_total() == Decimal("5.00")
# clear all and ensure total reset
calc.clear_all()
assert calc.get_total() == Decimal("0.00")
# double-check via another get_total() and then clear() should be a no-op
assert calc.get_total() == Decimal("0.00")
calc.clear()
assert calc.get_total() == Decimal("0.00")
# recreate a non-zero total using another three different methods: add (string), multiply, divide
calc.add("4")
calc.multiply(2) # 8.00
calc.divide(4) # 2.00
assert calc.get_total() == Decimal("2.00")
def test_clear_undo_behavior_after_clear_all():
calc = Calculator(precision=2, max_value=1000)
# create non-zero total using a Decimal input (not previously used in these tests)
calc.add(Decimal("7.77"))
assert calc.get_total() == Decimal("7.77")
# clear() should undo the last entry and restore previous total (0.00)
calc.clear()
assert calc.get_total() == Decimal("0.00")
# after clear_all, clear() should do nothing (undo cleared); ensure no exception and total remains zero
calc.add(Decimal("1.23"))
calc.clear_all()
calc.clear()
assert calc.get_total() == Decimal("0.00")