-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_snapshot_3.py
46 lines (35 loc) · 1.29 KB
/
test_snapshot_3.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
from contextlib import contextmanager
from io import StringIO
import unittest
import ast
import calc_snapshot_3
import sys
@contextmanager
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
def assertThose(testcase, text_input, expect):
with captured_output() as (out, err):
x = calc_snapshot_3.parse(text_input)
return testcase.assertEqual(out.getvalue(), expect)
class test_2_3(unittest.TestCase):
def test_3_1_ints(self):
assertThose(self, "2 2", "('int', 4)\n")
def test_3_2_floats(self):
assertThose(self, "2.0 2.0", "('float', 4.0)\n")
def test_3_3_incompatible_types(self):
assertThose(self, "2 2.0", "Error, incomatbile types\n")
def test_6_1_is_declard(self):
calc_snapshot_3.parse("int i = 0")
assertThose(self, "i", "('int', 0)\n")
def test_6_2_should_fail(self):
assertThose(self, "int if = 0", "Syntax error at 'if'\n('int', 0)\n")
def test_6_3_should_fail(self):
assertThose(self, "j", "Undefined name 'j'\n")
if __name__ == '__main__':
unittest.main()