forked from johnthagen/cppcheck-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
135 lines (105 loc) · 5.2 KB
/
test.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
#! /usr/bin/env python3
"""cppcheck-junit tests."""
import sys
import unittest
from xml.etree import ElementTree
from cppcheck_junit import (CppcheckError, generate_single_success_test_suite,
generate_test_suite, parse_arguments, parse_cppcheck)
class ParseCppcheckTestCase(unittest.TestCase):
def test_good(self): # type: () -> None
errors = parse_cppcheck('tests/cppcheck-out-good.xml')
self.assertEqual(errors, {})
def test_bad(self): # type: () -> None
file1 = 'bad.cpp'
errors = parse_cppcheck('tests/cppcheck-out-bad.xml')
self.assertEqual(errors[file1][0].file, file1)
self.assertEqual(errors[file1][0].line, 4)
self.assertEqual(errors[file1][0].message,
"Variable 'a' is assigned a value that is never used.")
self.assertEqual(errors[file1][1].file, file1)
self.assertEqual(errors[file1][1].line, 4)
self.assertEqual(errors[file1][1].message,
"Array 'a[10]' accessed at index 10, which is out of bounds.")
def test_no_location_element(self): # type: () -> None
file = ''
errors = parse_cppcheck('tests/cppcheck-out-no-location-element.xml')
self.assertEqual(len(errors), 1)
error = errors[file][0]
self.assertEqual(error.file, file)
self.assertEqual(error.line, 0)
self.assertEqual(
error.message,
'Too many #ifdef configurations - cppcheck only checks 12 configurations. '
'Use --force to check all configurations. For more details, use '
'--enable=information.')
self.assertEqual(error.severity, 'information')
def test_bad_large(self): # type: () -> None
errors = parse_cppcheck('tests/cppcheck-out-bad-large.xml')
self.assertEqual(len(errors), 43)
def test_all(self): # type: () -> None
file1 = 'bad.cpp'
file2 = 'bad2.cpp'
errors = parse_cppcheck('tests/cppcheck-out-all.xml')
self.assertEqual(errors[file1][0].file, file1)
self.assertEqual(errors[file1][0].line, 4)
self.assertEqual(errors[file1][0].message,
"Variable 'a' is assigned a value that is never used.")
self.assertEqual(errors[file1][1].file, file1)
self.assertEqual(errors[file1][1].line, 4)
self.assertEqual(errors[file1][1].message,
"Array 'a[10]' accessed at index 10, which is out of bounds.")
self.assertEqual(errors[file2][0].file, file2)
self.assertEqual(errors[file2][0].line, 4)
self.assertEqual(errors[file2][0].message,
"Variable 'a' is assigned a value that is never used.")
self.assertEqual(errors[file2][1].file, file2)
self.assertEqual(errors[file2][1].line, 4)
self.assertEqual(errors[file2][1].message,
"Array 'a[10]' accessed at index 10, which is out of bounds.")
def test_xml_version_1(self): # type: () -> None
with self.assertRaises(ValueError):
parse_cppcheck('tests/cppcheck-out-bad-xml-version-1.xml')
def test_file_not_found(self): # type: () -> None
with self.assertRaises(IOError):
parse_cppcheck('tests/file_does_not_exist.xml')
def test_malformed(self): # type: () -> None
with self.assertRaises(ElementTree.ParseError):
parse_cppcheck('tests/cppcheck-out-malformed.xml')
class GenerateTestSuiteTestCase(unittest.TestCase):
def test_single(self): # type: () -> None
errors = {'file_name':
[CppcheckError('file_name',
4,
'error message',
'severity',
'error_id',
'verbose error message')]}
tree = generate_test_suite(errors)
root = tree.getroot()
self.assertEqual(root.get('errors'), str(1))
self.assertEqual(root.get('failures'), str(0))
self.assertEqual(root.get('tests'), str(1))
test_case_element = root.find('testcase')
self.assertEqual(test_case_element.get('name'), 'file_name')
error_element = test_case_element.find('error')
self.assertEqual(error_element.get('file'), 'file_name')
self.assertEqual(error_element.get('line'), str(4))
self.assertEqual(error_element.get('message'), '4: (severity) error message')
class GenerateSingleSuccessTestSuite(unittest.TestCase):
def test(self): # type: () -> None
tree = generate_single_success_test_suite()
root = tree.getroot()
self.assertEqual(root.get('tests'), str(1))
test_case_element = root.find('testcase')
self.assertEqual(test_case_element.get('name'), 'Cppcheck success')
class ParseArgumentsTestCase(unittest.TestCase):
def test_no_arguments(self): # type: () -> None
with self.assertRaises(SystemExit):
# Suppress argparse stderr.
class NullWriter:
def write(self, s): # type: (str) -> None
pass
sys.stderr = NullWriter()
parse_arguments()
if __name__ == '__main__':
unittest.main()