1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# test_iuwandbox.py |
4
|
|
|
# |
5
|
|
|
|
6
|
|
|
from __future__ import print_function |
7
|
|
|
|
8
|
|
|
import sys |
9
|
|
|
import os |
10
|
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../fused') |
11
|
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../wandbox') |
12
|
|
|
|
13
|
|
|
try: |
14
|
|
|
import unittest2 as unittest |
15
|
|
|
except: |
16
|
|
|
import unittest |
17
|
|
|
import iuwandbox |
18
|
|
|
import fused_iutest_files |
19
|
|
|
import iuwandbox_pp |
20
|
|
|
|
21
|
|
|
try: |
22
|
|
|
from StringIO import StringIO |
23
|
|
|
except ImportError: |
24
|
|
|
from io import StringIO |
25
|
|
|
|
26
|
|
|
root = os.path.normpath(os.path.dirname(os.path.abspath(__file__)) + '/../../../') |
27
|
|
|
fused_src = root + '/fused-src' |
28
|
|
|
test_src = root + '/test/syntax_tests.cpp' |
29
|
|
|
test_opt_default = ['--encoding', 'utf-8-sig', '--iutest-use-wandbox-min'] |
30
|
|
|
test_opt_nomain = test_opt_default |
31
|
|
|
test_opt = ['-f"-DIUTEST_USE_MAIN"'] |
32
|
|
|
test_opt.extend(test_opt_default) |
33
|
|
|
test_opt_verbose = ['--verbose'] |
34
|
|
|
test_opt_dryrun = ['--dryrun'] |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def eprint(*args, **kwargs): |
38
|
|
|
print(*args, file=sys.stderr, **kwargs) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class iuwandbox_test_base(unittest.TestCase): |
42
|
|
|
dir = None |
43
|
|
|
|
44
|
|
|
def setUp(self): |
45
|
|
|
self.capture = StringIO() |
46
|
|
|
sys.stdout = self.capture |
47
|
|
|
self.dir = os.getcwd() |
48
|
|
|
os.chdir(os.path.dirname(os.path.abspath(__file__))) |
49
|
|
|
return super(iuwandbox_test_base, self).setUp() |
50
|
|
|
|
51
|
|
|
def tearDown(self): |
52
|
|
|
sys.stdout = sys.__stdout__ |
53
|
|
|
os.chdir(self.dir) |
54
|
|
|
self.capture.close() |
55
|
|
|
return super(iuwandbox_test_base, self).tearDown() |
56
|
|
|
|
57
|
|
|
def dump(self): |
58
|
|
|
value = self.capture.getvalue() |
59
|
|
|
eprint(value) |
60
|
|
|
return value |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
class nofused_iuwandbox_test(iuwandbox_test_base): |
64
|
|
|
def setUp(self): |
65
|
|
|
if 'SCRUTINIZER' in os.environ: |
66
|
|
|
self.skipTest('this test is not run on SCRUTINIZER.') |
67
|
|
|
for f in ['iutest.hpp', 'iutest.min.hpp', 'iutest.wandbox.min.hpp']: |
68
|
|
|
if os.path.exists(os.path.join(fused_src, f)): |
69
|
|
|
try: |
70
|
|
|
os.remove(os.path.join(fused_src, f)) |
71
|
|
|
except Exception as e: |
72
|
|
|
self.skipTest('fused-src/' + f + ' remove failed... : ' + str(e)) |
73
|
|
|
if os.path.exists(os.path.join(fused_src, f)): |
74
|
|
|
self.skipTest('fused-src/' + f + ' is exists') |
75
|
|
|
return super(nofused_iuwandbox_test, self).setUp() |
76
|
|
|
|
77
|
|
|
def test_nofused(self): |
78
|
|
|
sys.argv[1:] = [test_src] |
79
|
|
|
sys.argv.extend(test_opt) |
80
|
|
|
with self.assertRaises(SystemExit) as cm: |
81
|
|
|
iuwandbox.main() |
82
|
|
|
self.dump() |
83
|
|
|
output = self.capture.getvalue() |
84
|
|
|
self.assertEqual(cm.exception.code, 1, output) |
85
|
|
|
self.assertRegex(output, '.*please try \"make fused\".*') |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
class iuwandbox_test(iuwandbox_test_base): |
89
|
|
|
def setUp(self): |
90
|
|
|
need_make_fused = False |
91
|
|
|
for f in ['iutest.hpp', 'iutest.min.hpp', 'iutest.wandbox.min.hpp']: |
92
|
|
|
if not os.path.exists(os.path.join(fused_src, f)): |
93
|
|
|
need_make_fused = True |
94
|
|
|
if need_make_fused: |
95
|
|
|
try: |
96
|
|
|
fused_iutest_files.FusedAll(fused_iutest_files.IUTEST_INCLUDE_DIR, fused_src) |
97
|
|
|
iuwandbox_pp.default_pp() |
98
|
|
|
# os.system('python ' + root + '/tools/fused/fused_iutest_files.py ' + fused_src) |
99
|
|
|
except: |
100
|
|
|
pass |
101
|
|
|
if not os.path.exists(os.path.join(fused_src, 'iutest.min.hpp')): |
102
|
|
|
self.skipTest('fused-src is not exists') |
103
|
|
|
return super(iuwandbox_test, self).setUp() |
104
|
|
|
|
105
|
|
|
def test_nomain(self): |
106
|
|
|
sys.argv[1:] = [test_src] |
107
|
|
|
sys.argv.extend(test_opt_nomain) |
108
|
|
|
with self.assertRaises(SystemExit) as cm: |
109
|
|
|
iuwandbox.main() |
110
|
|
|
output = self.dump() |
111
|
|
|
self.assertEqual(cm.exception.code, 1, output) |
112
|
|
|
self.assertRegex(output, '.*hint:.*') |
113
|
|
|
self.assertRegex(output, '.*In "iutest" you can omit the definition of the main function, please define IUTEST_USE_MAIN. (--iutest-use-main or -f"-DIUTEST_USE_MAIN")*') |
114
|
|
|
|
115
|
|
|
def test_use_main(self): |
116
|
|
|
sys.argv[1:] = [test_src] |
117
|
|
|
sys.argv.extend(test_opt_nomain) |
118
|
|
|
sys.argv.append('--iutest-use-main') |
119
|
|
|
with self.assertRaises(SystemExit) as cm: |
120
|
|
|
iuwandbox.main() |
121
|
|
|
output = self.dump() |
122
|
|
|
self.assertEqual(cm.exception.code, 0, output) |
123
|
|
|
self.assertRegex(output, '.*OK.*') |
124
|
|
|
|
125
|
|
|
def test_define_wandbox(self): |
126
|
|
|
sys.argv[1:] = [test_src] |
127
|
|
|
sys.argv.extend(test_opt) |
128
|
|
|
sys.argv.extend(test_opt_dryrun) |
129
|
|
|
sys.argv.extend(test_opt_verbose) |
130
|
|
|
sys.argv.append('-f"-DTEST"') |
131
|
|
|
with self.assertRaises(SystemExit) as cm: |
132
|
|
|
iuwandbox.main() |
133
|
|
|
output = self.dump() |
134
|
|
|
self.assertEqual(cm.exception.code, 0, output) |
135
|
|
|
self.assertRegex(output, '.*-D__WANDBOX__.*') |
136
|
|
|
self.assertRegex(output, '.*-DTEST.*') |
137
|
|
|
|
138
|
|
|
def test_boosttest_workarround(self): |
139
|
|
|
sys.argv[1:] = [test_src] |
140
|
|
|
sys.argv.extend(test_opt_nomain) |
141
|
|
|
sys.argv.extend(['--boost', '1.65.0']) |
142
|
|
|
with self.assertRaises(SystemExit) as cm: |
143
|
|
|
iuwandbox.main() |
144
|
|
|
output = self.dump() |
145
|
|
|
self.assertEqual(cm.exception.code, 1, output) |
146
|
|
|
self.assertRegex(output, '.*hint:.*') |
147
|
|
|
self.assertRegex(output, '.*If you do not use boost test, please specify the file with the main function first..*') |
148
|
|
|
|
149
|
|
|
def test_run(self): |
150
|
|
|
sys.argv[1:] = [test_src] |
151
|
|
|
sys.argv.extend(test_opt) |
152
|
|
|
print(sys.argv) |
153
|
|
|
with self.assertRaises(SystemExit) as cm: |
154
|
|
|
iuwandbox.main() |
155
|
|
|
output = self.dump() |
156
|
|
|
self.assertEqual(cm.exception.code, 0, output) |
157
|
|
|
self.assertRegex(output, '\[ \s+OK \]') |
158
|
|
|
self.assertFalse('-Wmisleading-indentation' in output) |
159
|
|
|
|
160
|
|
|
def test_same_filename(self): |
161
|
|
|
sys.argv[1:] = ['src/main.cpp', 'src/A/sample.cpp', 'src/B/sample.cpp'] |
162
|
|
|
sys.argv.extend(test_opt_nomain) |
163
|
|
|
print(sys.argv) |
164
|
|
|
with self.assertRaises(SystemExit) as cm: |
165
|
|
|
iuwandbox.main() |
166
|
|
|
output = self.dump() |
167
|
|
|
self.assertEqual(cm.exception.code, 0, output) |
168
|
|
|
self.assertRegex(output, '.*OK.*') |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
if __name__ == "__main__": |
172
|
|
|
unittest.main() |
173
|
|
|
|