-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_calculator.py
76 lines (56 loc) · 1.78 KB
/
test_calculator.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
from contextlib import contextmanager
import os.path
import pytest
from calculator import Calculator
@pytest.fixture()
def calculator():
return Calculator()
@pytest.mark.parametrize("operations, result", [
("1 1 +", 2),
("1 1 -", 0),
("1 2 *", 2),
("2 2 /", 1)
])
def test_process(calculator, operations, result):
calculator.process_string(operations)
assert result == calculator.pop()
@pytest.fixture(params=[
("1 1 +", 2),
("1 1 -", 0),
("1 2 *", 2),
("2 2 /", 1)
])
def process_fixture(calculator, request):
operations, result = request.param
calculator.process_string(operations)
return calculator, result
def test_operations(process_fixture):
calculator, result = process_fixture
assert result == calculator.pop()
def test_div_by_zero(calculator):
calculator.push(2)
calculator.push(0)
with pytest.raises(ZeroDivisionError):
calculator.push('/')
def test_record(calculator, tmpdir):
operations = "1 1 + 1 + 1 +"
record_file = tmpdir.join('record.txt')
with calculator.recorder(record_file):
calculator.process_string(operations)
assert os.path.exists(record_file)
with open(record_file) as f:
saved_record = f.read()
assert operations == saved_record.strip()
def test_process_file(calculator, monkeypatch):
@contextmanager
def fake_open(*args, **kwargs):
yield ["1 1 +", "2 +"]
monkeypatch.setattr(calculator, "_open", fake_open)
calculator.process_file("filename")
assert calculator.pop() == 4
def test_process_string(calculator, mocker):
push_spy = mocker.spy(calculator, "push")
pop_spy = mocker.spy(calculator, "pop")
calculator.process_string("1 1 +")
assert push_spy.call_count == 3
assert pop_spy.call_count == 2