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+ '--rule={}' .format (rule_file ),
28+ str (test_file )
29+ ]
30+ ret , stdout , stderr = cppcheck (args )
31+ assert ret == 0
32+ assert stdout .splitlines () == [
33+ 'Checking {} ...' .format (test_file ),
34+ 'Processing rule: {}' .format (rule_file )
35+ ]
36+ assert stderr == ''
37+ # TODO: needs to report error
38+
39+
40+ def test_show_all_defines (tmp_path ):
41+ test_file = tmp_path / 'test.cpp'
42+ with open (test_file , 'wt' ) as f :
43+ f .write ("""
44+ #define DEF_1
45+
46+ void f()
47+ {
48+ }
49+ """ )
50+
51+ rule_file = os .path .join (__rules_dir , 'show-all-defines.rule' )
52+ args = [
53+ '-DDEF_1' ,
54+ '--rule={}' .format (rule_file ),
55+ str (test_file )
56+ ]
57+ ret , stdout , stderr = cppcheck (args )
58+ assert ret == 0
59+ assert stdout .splitlines () == [
60+ 'Checking {} ...' .format (test_file ),
61+ 'Checking {}: DEF_1=1...' .format (test_file ),
62+ 'Processing rule: {}' .format (rule_file ) # requires actual code to be called
63+ ]
64+ assert stderr == ''
65+ # TODO: does not report anything
66+
67+
68+ def test_stl (tmp_path ):
69+ test_file = tmp_path / 'test.cpp'
70+ with open (test_file , 'wt' ) as f :
71+ f .write ("""
72+ void f()
73+ {
74+ std::string s;
75+ if (s.find("t") == 17)
76+ {
77+ }
78+ }
79+ """ )
80+
81+ rule_file = os .path .join (__rules_dir , 'stl.xml' )
82+ args = [
83+ '--rule={}' .format (rule_file ),
84+ str (test_file )
85+ ]
86+ ret , stdout , stderr = cppcheck (args )
87+ assert ret == 0
88+ assert stdout .splitlines () == [
89+ 'Checking {} ...' .format (test_file ),
90+ 'Processing rule: {}' .format (rule_file )
91+ ]
92+ assert stderr == ''
93+ # TODO: needs to report error
0 commit comments