Completed
Push — master ( 1b7e63...915a02 )
by srz
05:26
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.6666
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
34
35
def eprint(*args, **kwargs):
36
    print(*args, file=sys.stderr, **kwargs)
37
38
39
class iuwandbox_test_base(unittest.TestCase):
40
    dir = None
41
42
    def setUp(self):
43
        self.capture = StringIO()
44
        sys.stdout = self.capture
45
        self.dir = os.getcwd()
46
        os.chdir(os.path.dirname(os.path.abspath(__file__)))
47
        return super(iuwandbox_test_base, self).setUp()
48
49
    def tearDown(self):
50
        sys.stdout = sys.__stdout__
51
        os.chdir(self.dir)
52
        self.capture.close()
53
        return super(iuwandbox_test_base, self).tearDown()
54
55
    def dump(self):
56
        value = self.capture.getvalue()
57
        eprint(value)
58
59
60
class nofused_iuwandbox_test(iuwandbox_test_base):
61
    def setUp(self):
62
        if 'SCRUTINIZER' in os.environ:
63
            self.skipTest('this test is not run on SCRUTINIZER.')
64
        if os.path.exists(fused_src):
65
            try:
66
                shutil.rmtree(fused_src)
67
            except:
68
                pass
69
        if os.path.exists(os.path.join(fused_src, 'iutest.min.hpp')):
70
            self.skipTest('fused-src is exists')
71
        return super(nofused_iuwandbox_test, self).setUp()
72
73
    def test_nofused(self):
74
        sys.argv[1:] = [test_src]
75
        sys.argv.extend(test_opt)
76
        with self.assertRaises(SystemExit) as cm:
77
            iuwandbox.main()
78
        self.dump()
79
        self.assertEqual(cm.exception.code, 1, self.capture.getvalue())
80
        self.assertRegex(self.capture.getvalue(), '.*please try \"make fused\".*')
81
82
83
class iuwandbox_test(iuwandbox_test_base):
84
    def setUp(self):
85
        if not os.path.exists(fused_src):
86
            try:
87
                fused_iutest_files.FusedAll(fused_iutest_files.IUTEST_INCLUDE_DIR, fused_src)
88
#              os.system('python ' + root + '/tools/fused/fused_iutest_files.py ' + fused_src)
89
            except:
90
                pass
91
        if not os.path.exists(os.path.join(fused_src, 'iutest.min.hpp')):
92
            self.skipTest('fused-src is not exists')
93
        return super(iuwandbox_test, self).setUp()
94
95
    def test_nomain(self):
96
        sys.argv[1:] = [test_src]
97
        sys.argv.extend(test_opt_nomain)
98
        with self.assertRaises(SystemExit) as cm:
99
            iuwandbox.main()
100
        self.dump()
101
        self.assertEqual(cm.exception.code, 1, self.capture.getvalue())
102
        self.assertRegex(self.capture.getvalue(), '.*hint:.*')
103
        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")*')
104
105
    def test_use_main(self):
106
        sys.argv[1:] = [test_src]
107
        sys.argv.extend(test_opt_nomain)
108
        sys.argv.append('--iutest-use-main')
109
        with self.assertRaises(SystemExit) as cm:
110
            iuwandbox.main()
111
        self.dump()
112
        self.assertEqual(cm.exception.code, 0, self.capture.getvalue())
113
        self.assertRegex(self.capture.getvalue(), '.*OK.*')
114
115
    def test_boosttest_workarround(self):
116
        sys.argv[1:] = [test_src]
117
        sys.argv.extend(test_opt_nomain)
118
        sys.argv.extend(['--boost', '1.65.0'])
119
        with self.assertRaises(SystemExit) as cm:
120
            iuwandbox.main()
121
        self.dump()
122
        self.assertEqual(cm.exception.code, 1, self.capture.getvalue())
123
        self.assertRegex(self.capture.getvalue(), '.*hint:.*')
124
        self.assertRegex(self.capture.getvalue(), '.*If you do not use boost test, please specify the file with the main function first..*')
125
126
    def test_run(self):
127
        sys.argv[1:] = [test_src]
128
        sys.argv.extend(test_opt)
129
        print(sys.argv)
130
        with self.assertRaises(SystemExit) as cm:
131
            iuwandbox.main()
132
        self.dump()
133
        self.assertEqual(cm.exception.code, 0, self.capture.getvalue())
134
        self.assertRegex(self.capture.getvalue(), '.*OK.*')
135
136
    def test_same_filename(self):
137
        sys.argv[1:] = ['src/main.cpp', 'src/A/sample.cpp', 'src/B/sample.cpp']
138
        sys.argv.extend(test_opt_nomain)
139
        print(sys.argv)
140
        with self.assertRaises(SystemExit) as cm:
141
            iuwandbox.main()
142
        self.dump()
143
        self.assertEqual(cm.exception.code, 0, self.capture.getvalue())
144
        self.assertRegex(self.capture.getvalue(), '.*OK.*')
145
146
147
if __name__ == "__main__":
148
    unittest.main()
149