-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmaintest.py
177 lines (159 loc) · 5.34 KB
/
maintest.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import unittest
from pypreprocessor import pypreprocessor
pypreprocessor.readEncoding = 'utf-8'
pypreprocessor.writeEncoding = 'utf-8'
from contextlib import contextmanager
try:
from StringIO import StringIO
except ImportError:
import io
from io import StringIO
def testsuite():
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(FileParseTest))
suite.addTests(unittest.makeSuite(CapturePrintTest))
return suite
@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
#exclude
# Template for other tests
class TestTemplate(object):
def test_run_resume_save(self):
pypreprocessor.run = True
pypreprocessor.resume = True
pypreprocessor.save = True
pypreprocessor.parse()
self.assertTrue(os.path.exists(pypreprocessor.output))
pass
def test_run_resume_nosave(self):
pypreprocessor.run = True
pypreprocessor.resume = True
pypreprocessor.save = False
pypreprocessor.parse()
self.assertFalse(os.path.exists(pypreprocessor.output))
pass
def test_run_noresume_save(self):
pypreprocessor.run = True
pypreprocessor.resume = False
pypreprocessor.save = True
with self.assertRaises(SystemExit) as e:
pypreprocessor.parse()
self.assertEqual(e.exception.code, 0)
self.assertTrue(os.path.exists(pypreprocessor.output))
pass
def test_run_noresume_nosave(self):
pypreprocessor.run = True
pypreprocessor.resume = False
pypreprocessor.save = False
with self.assertRaises(SystemExit) as e:
pypreprocessor.parse()
self.assertEqual(e.exception.code, 0)
self.assertFalse(os.path.exists(pypreprocessor.output))
pass
def test_norun_resume_save(self):
pypreprocessor.run = False
pypreprocessor.resume = True
pypreprocessor.save = True
pypreprocessor.parse()
self.assertTrue(os.path.exists(pypreprocessor.output))
pass
def test_norun_resume_nosave(self):
pypreprocessor.run = False
pypreprocessor.resume = True
pypreprocessor.save = False
pypreprocessor.parse()
self.assertFalse(os.path.exists(pypreprocessor.output))
pass
def test_norun_noresume_save(self):
pypreprocessor.run = False
pypreprocessor.resume = False
pypreprocessor.save = True
with self.assertRaises(SystemExit) as e:
pypreprocessor.parse()
self.assertEqual(e.exception.code, 0)
self.assertTrue(os.path.exists(pypreprocessor.output))
pass
def test_norun_noresume_nosave(self):
pypreprocessor.run = False
pypreprocessor.resume = False
pypreprocessor.save = False
with self.assertRaises(SystemExit) as e:
pypreprocessor.parse()
self.assertEqual(e.exception.code, 0)
self.assertFalse(os.path.exists(pypreprocessor.output))
pass
# Parses parsetarget.py
class FileParseTest(unittest.TestCase, TestTemplate):
@classmethod
def setUpClass(self):
pypreprocessor.defines = []
os.chdir('./tests')
pass
@classmethod
def tearDownClass(self):
os.chdir('./..')
pass
def setUp(self):
super(FileParseTest, self).setUp()
pypreprocessor.input = './parsetarget.py'
pypreprocessor.output = self._testMethodName
pass
def tearDown(self):
f = os.path.join(os.getcwd(), pypreprocessor.output)
pypreprocessor.defines = []
if(os.path.exists(f)):
os.remove(f)
super(FileParseTest, self).tearDown()
pass
pass
# Parses parsetarget.py with 'printTest' defined and captures print
class CapturePrintTest(unittest.TestCase, TestTemplate):
@classmethod
def setUpClass(self):
os.chdir('./tests')
pass
@classmethod
def tearDownClass(self):
os.chdir('./..')
pass
def setUp(self):
super(CapturePrintTest, self).setUp()
pypreprocessor.defines = []
pypreprocessor.defines.append('printTest')
pypreprocessor.input = './parsetarget.py'
pypreprocessor.output = self._testMethodName
self.new_out, self.new_err = StringIO(), StringIO()
self.old_out, self.old_err = sys.stdout, sys.stderr
sys.stdout, sys.stderr = self.new_out, self.new_err
pass
def tearDown(self):
sys.stdout, sys.stderr = self.old_out, self.old_err
if(pypreprocessor.run):
lines = self.new_out.getvalue().strip().splitlines()
self.assertIn('Hello, world!', lines)
self.assertIn('가즈아ㅏㅏ', lines)
self.assertIn('200', lines)
else:
self.assertEqual(self.new_out.getvalue().strip(), '')
f = os.path.join(os.getcwd(), pypreprocessor.output)
pypreprocessor.defines = []
if(os.path.exists(f)):
os.remove(f)
super(CapturePrintTest, self).tearDown()
pass
pass
#endexclude
if(__name__ == 'main'):
unittest.main()