Skip to content

Commit e2569fe

Browse files
committed
test/cli: added tests for the rules files
1 parent ff0a7fb commit e2569fe

1 file changed

Lines changed: 183 additions & 0 deletions

File tree

test/cli/rules_test.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import os
2+
3+
from testutils import cppcheck
4+
5+
__script_dir = os.path.dirname(os.path.abspath(__file__))
6+
__root_dir = os.path.abspath(os.path.join(__script_dir, '..', '..'))
7+
__rules_dir = os.path.join(__root_dir, 'rules')
8+
9+
10+
def test_empty_catch_block(tmp_path):
11+
test_file = tmp_path / 'test.cpp'
12+
with open(test_file, 'wt') as f:
13+
f.write("""
14+
void f()
15+
{
16+
try
17+
{
18+
}
19+
catch (...)
20+
{
21+
}
22+
}
23+
""")
24+
25+
rule_file = os.path.join(__rules_dir, 'empty-catch-block.xml')
26+
args = [
27+
'--template=simple',
28+
'--rule-file={}'.format(rule_file),
29+
str(test_file)
30+
]
31+
ret, stdout, stderr = cppcheck(args)
32+
assert ret == 0
33+
assert stdout.splitlines() == [
34+
'Checking {} ...'.format(test_file),
35+
'Processing rule: \\}\\s*catch\\s*\\(.*\\)\\s*\\{\\s*\\}'
36+
]
37+
assert stderr.splitlines() == [
38+
'{}:6:0: style: Empty catch block found. [rule]'.format(test_file)
39+
]
40+
41+
42+
def test_show_all_defines(tmp_path):
43+
test_file = tmp_path / 'test.cpp'
44+
with open(test_file, 'wt') as f:
45+
f.write("""
46+
#define DEF_1
47+
48+
void f()
49+
{
50+
}
51+
""")
52+
53+
rule_file = os.path.join(__rules_dir, 'show-all-defines.rule')
54+
args = [
55+
'--template=simple',
56+
'-DDEF_2',
57+
'--rule-file={}'.format(rule_file),
58+
str(test_file)
59+
]
60+
ret, stdout, stderr = cppcheck(args)
61+
assert ret == 0
62+
assert stdout.splitlines() == [
63+
'Checking {} ...'.format(test_file),
64+
'Processing rule: .*',
65+
'Checking {}: DEF_2=1...'.format(test_file)
66+
]
67+
assert stderr.splitlines() == [
68+
# TODO: this message looks strange
69+
":1:0: information: found ' # line 2 \"{}\" # define DEF_1' [showalldefines]".format(test_file)
70+
]
71+
72+
73+
def test_stl(tmp_path):
74+
test_file = tmp_path / 'test.cpp'
75+
with open(test_file, 'wt') as f:
76+
f.write("""
77+
void f()
78+
{
79+
std::string s;
80+
if (s.find("t") == 17)
81+
{
82+
}
83+
}
84+
""")
85+
86+
rule_file = os.path.join(__rules_dir, 'stl.xml')
87+
args = [
88+
'--template=simple',
89+
'--rule-file={}'.format(rule_file),
90+
str(test_file)
91+
]
92+
ret, stdout, stderr = cppcheck(args)
93+
assert ret == 0
94+
assert stdout.splitlines() == [
95+
'Checking {} ...'.format(test_file),
96+
'Processing rule: \\. find \\( "[^"]+?" \\) == \\d+ '
97+
]
98+
assert stderr.splitlines() == [
99+
'{}:5:0: performance: When looking for a string at a fixed position compare [UselessSTDStringFind]'.format(test_file)
100+
]
101+
102+
103+
def test_strlen_empty_str(tmp_path):
104+
test_file = tmp_path / 'test.cpp'
105+
with open(test_file, 'wt') as f:
106+
f.write("""
107+
void f(const char* s)
108+
{
109+
if (strlen(s) > 0)
110+
{
111+
}
112+
}
113+
""")
114+
115+
rule_file = os.path.join(__rules_dir, 'strlen-empty-str.xml')
116+
args = [
117+
'--template=simple',
118+
'--rule-file={}'.format(rule_file),
119+
str(test_file)
120+
]
121+
ret, stdout, stderr = cppcheck(args)
122+
assert ret == 0
123+
assert stdout.splitlines() == [
124+
'Checking {} ...'.format(test_file),
125+
'Processing rule: if \\( ([!] )*?(strlen) \\( \\w+? \\) ([>] [0] )*?\\) { '
126+
]
127+
assert stderr.splitlines() == [
128+
'{}:4:0: performance: Using strlen() to check if a string is empty is not efficient. [StrlenEmptyString]'.format(test_file)
129+
]
130+
131+
132+
def test_suggest_nullptr(tmp_path):
133+
test_file = tmp_path / 'test.cpp'
134+
with open(test_file, 'wt') as f:
135+
f.write("""
136+
void f()
137+
{
138+
const char* p = 0;
139+
}
140+
""")
141+
142+
rule_file = os.path.join(__rules_dir, 'suggest_nullptr.xml')
143+
args = [
144+
'--template=simple',
145+
'--rule-file={}'.format(rule_file),
146+
str(test_file)
147+
]
148+
ret, stdout, stderr = cppcheck(args)
149+
assert ret == 0
150+
assert stdout.splitlines() == [
151+
'Checking {} ...'.format(test_file),
152+
'Processing rule: (\\b\\w+\\b) \\* (\\b\\w+\\b) = 0 ;'
153+
]
154+
assert stderr.splitlines() == [
155+
"{}:4:0: style: Prefer to use a 'nullptr' instead of initializing a pointer with 0. [modernizeUseNullPtr]".format(test_file)
156+
]
157+
158+
159+
def test_unused_deref(tmp_path):
160+
test_file = tmp_path / 'test.cpp'
161+
with open(test_file, 'wt') as f:
162+
f.write("""
163+
void f(const char* p)
164+
{
165+
*p++;
166+
}
167+
""")
168+
169+
rule_file = os.path.join(__rules_dir, 'unused-deref.xml')
170+
args = [
171+
'--template=simple',
172+
'--rule-file={}'.format(rule_file),
173+
str(test_file)
174+
]
175+
ret, stdout, stderr = cppcheck(args)
176+
assert ret == 0
177+
assert stdout.splitlines() == [
178+
'Checking {} ...'.format(test_file),
179+
'Processing rule: [;{}] [*] \\w+? (\\+\\+|\\-\\-) ; '
180+
]
181+
assert stderr.splitlines() == [
182+
'{}:3:0: style: Redundant * found, "*p++" is the same as "*(p++)". [UnusedDeref]'.format(test_file)
183+
]

0 commit comments

Comments
 (0)