Completed
Push — master ( 453491...9915ee )
by srz
03:24 queued 10s
created

iuwandbox_test.test_use_main()   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 1
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
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 shutil
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']
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
61
62
class nofused_iuwandbox_test(iuwandbox_test_base):
63
    def setUp(self):
64
        if 'SCRUTINIZER' in os.environ:
65
            self.skipTest('this test is not run on SCRUTINIZER.')
66
        if os.path.exists(fused_src):
67
            try:
68
                shutil.rmtree(fused_src)
69
            except:
70
                pass
71
        if os.path.exists(os.path.join(fused_src, 'iutest.min.hpp')):
72
            self.skipTest('fused-src is exists')
73
        return super(nofused_iuwandbox_test, self).setUp()
74
75
    def test_nofused(self):
76
        sys.argv[1:] = [test_src]
77
        sys.argv.extend(test_opt)
78
        with self.assertRaises(SystemExit) as cm:
79
            iuwandbox.main()
80
        self.dump()
81
        self.assertEqual(cm.exception.code, 1, self.capture.getvalue())
82
        self.assertRegex(self.capture.getvalue(), '.*please try \"make fused\".*')
83
84
85
class iuwandbox_test(iuwandbox_test_base):
86
    def setUp(self):
87
        if not os.path.exists(fused_src):
88
            try:
89
                fused_iutest_files.FusedAll(fused_iutest_files.IUTEST_INCLUDE_DIR, fused_src)
90
#              os.system('python ' + root + '/tools/fused/fused_iutest_files.py ' + fused_src)
91
            except:
92
                pass
93
        if not os.path.exists(os.path.join(fused_src, 'iutest.min.hpp')):
94
            self.skipTest('fused-src is not exists')
95
        return super(iuwandbox_test, self).setUp()
96
97
    def test_nomain(self):
98
        sys.argv[1:] = [test_src]
99
        sys.argv.extend(test_opt_nomain)
100
        with self.assertRaises(SystemExit) as cm:
101
            iuwandbox.main()
102
        self.dump()
103
        self.assertEqual(cm.exception.code, 1, self.capture.getvalue())
104
        self.assertRegex(self.capture.getvalue(), '.*hint:.*')
105
        self.assertRegex(self.capture.getvalue(), '.*In "iutest" you can omit the definition of the main function, please define IUTEST_USE_MAIN. (--iutest-use-main or -f"-DIUTEST_USE_MAIN")*')
106
107
    def test_use_main(self):
108
        sys.argv[1:] = [test_src]
109
        sys.argv.extend(test_opt_nomain)
110
        sys.argv.append('--iutest-use-main')
111
        with self.assertRaises(SystemExit) as cm:
112
            iuwandbox.main()
113
        self.dump()
114
        self.assertEqual(cm.exception.code, 0, self.capture.getvalue())
115
        self.assertRegex(self.capture.getvalue(), '.*OK.*')
116
117
    def test_define_wandbox(self):
118
        sys.argv[1:] = [test_src]
119
        sys.argv.extend(test_opt)
120
        sys.argv.extend(test_opt_dryrun)
121
        sys.argv.extend(test_opt_verbose)
122
        sys.argv.append('-f"-DTEST"')
123
        with self.assertRaises(SystemExit) as cm:
124
            iuwandbox.main()
125
        self.dump()
126
        self.assertEqual(cm.exception.code, 0, self.capture.getvalue())
127
        self.assertRegex(self.capture.getvalue(), '.*-D__WNADBOX__.*')
128
        self.assertRegex(self.capture.getvalue(), '.*-DTEST.*')
129
130
    def test_boosttest_workarround(self):
131
        sys.argv[1:] = [test_src]
132
        sys.argv.extend(test_opt_nomain)
133
        sys.argv.extend(['--boost', '1.65.0'])
134
        with self.assertRaises(SystemExit) as cm:
135
            iuwandbox.main()
136
        self.dump()
137
        self.assertEqual(cm.exception.code, 1, self.capture.getvalue())
138
        self.assertRegex(self.capture.getvalue(), '.*hint:.*')
139
        self.assertRegex(self.capture.getvalue(), '.*If you do not use boost test, please specify the file with the main function first..*')
140
141
    def test_run(self):
142
        sys.argv[1:] = [test_src]
143
        sys.argv.extend(test_opt)
144
        print(sys.argv)
145
        with self.assertRaises(SystemExit) as cm:
146
            iuwandbox.main()
147
        self.dump()
148
        self.assertEqual(cm.exception.code, 0, self.capture.getvalue())
149
        self.assertRegex(self.capture.getvalue(), '.*OK.*')
150
151
    def test_same_filename(self):
152
        sys.argv[1:] = ['src/main.cpp', 'src/A/sample.cpp', 'src/B/sample.cpp']
153
        sys.argv.extend(test_opt_nomain)
154
        print(sys.argv)
155
        with self.assertRaises(SystemExit) as cm:
156
            iuwandbox.main()
157
        self.dump()
158
        self.assertEqual(cm.exception.code, 0, self.capture.getvalue())
159
        self.assertRegex(self.capture.getvalue(), '.*OK.*')
160
161
162
if __name__ == "__main__":
163
    unittest.main()
164