File: rules_test.py

package info (click to toggle)
cppcheck 2.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,132 kB
  • sloc: cpp: 268,935; python: 20,890; ansic: 8,090; sh: 1,045; makefile: 1,008; xml: 1,005; cs: 291
file content (186 lines) | stat: -rw-r--r-- 4,804 bytes parent folder | download | duplicates (2)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import sys

from testutils import cppcheck

__script_dir = os.path.dirname(os.path.abspath(__file__))
__root_dir = os.path.abspath(os.path.join(__script_dir, '..', '..'))
__rules_dir = os.path.join(__root_dir, 'rules')


def test_empty_catch_block(tmp_path):
    test_file = tmp_path / 'test.cpp'
    with open(test_file, 'wt') as f:
        f.write("""
void f()
{
    try
    {
    }
    catch (...)
    {
    }
}
""")

    rule_file = os.path.join(__rules_dir, 'empty-catch-block.xml')
    args = [
        '--template=simple',
        '--rule-file={}'.format(rule_file),
        str(test_file)
    ]
    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stdout.splitlines() == [
        'Checking {} ...'.format(test_file),
        'Processing rule: \\}\\s*catch\\s*\\(.*\\)\\s*\\{\\s*\\}'
    ]
    assert stderr.splitlines() == [
        '{}:6:0: style: Empty catch block found. [rule]'.format(test_file)
    ]


def test_show_all_defines(tmp_path):
    test_file = tmp_path / 'test.cpp'
    with open(test_file, 'wt') as f:
        f.write("""
#define DEF_1

void f()
{
}
""")

    rule_file = os.path.join(__rules_dir, 'show-all-defines.rule')
    args = [
        '--template=simple',
        '-DDEF_2',
        '--rule-file={}'.format(rule_file),
        str(test_file)
    ]
    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stdout.splitlines() == [
        'Checking {} ...'.format(test_file),
        'Processing rule: .*',
        'Checking {}: DEF_2=1...'.format(test_file)
    ]
    if sys.platform == 'win32':
        test_file = str(test_file).replace('\\', '/')
    assert stderr.splitlines() == [
        # TODO: this message looks strange
        ":1:0: information: found ' # line 2 \"{}\" # define DEF_1' [showalldefines]".format(test_file)
    ]


def test_stl(tmp_path):
    test_file = tmp_path / 'test.cpp'
    with open(test_file, 'wt') as f:
        f.write("""
void f()
{
    std::string s;
    if (s.find("t") == 17)
    {
    }
}
""")

    rule_file = os.path.join(__rules_dir, 'stl.xml')
    args = [
        '--template=simple',
        '--rule-file={}'.format(rule_file),
        str(test_file)
    ]
    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stdout.splitlines() == [
        'Checking {} ...'.format(test_file),
        'Processing rule:  \\. find \\( "[^"]+?" \\) == \\d+ '
    ]
    assert stderr.splitlines() == [
        '{}:5:0: performance: When looking for a string at a fixed position compare [UselessSTDStringFind]'.format(test_file)
    ]


def test_strlen_empty_str(tmp_path):
    test_file = tmp_path / 'test.cpp'
    with open(test_file, 'wt') as f:
        f.write("""
void f(const char* s)
{
    if (strlen(s) > 0)
    {
    }
}
""")

    rule_file = os.path.join(__rules_dir, 'strlen-empty-str.xml')
    args = [
        '--template=simple',
        '--rule-file={}'.format(rule_file),
        str(test_file)
    ]
    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stdout.splitlines() == [
        'Checking {} ...'.format(test_file),
        'Processing rule:  if \\( ([!] )*?(strlen) \\( \\w+? \\) ([>] [0] )*?\\) { '
    ]
    assert stderr.splitlines() == [
        '{}:4:0: performance: Using strlen() to check if a string is empty is not efficient. [StrlenEmptyString]'.format(test_file)
    ]


def test_suggest_nullptr(tmp_path):
    test_file = tmp_path / 'test.cpp'
    with open(test_file, 'wt') as f:
        f.write("""
void f()
{
    const char* p = 0;
}
""")

    rule_file = os.path.join(__rules_dir, 'suggest_nullptr.xml')
    args = [
        '--template=simple',
        '--rule-file={}'.format(rule_file),
        str(test_file)
    ]
    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stdout.splitlines() == [
        'Checking {} ...'.format(test_file),
        'Processing rule: (\\b\\w+\\b) \\* (\\b\\w+\\b) = 0 ;'
    ]
    assert stderr.splitlines() == [
        "{}:4:0: style: Prefer to use a 'nullptr' instead of initializing a pointer with 0. [modernizeUseNullPtr]".format(test_file)
    ]


def test_unused_deref(tmp_path):
    test_file = tmp_path / 'test.cpp'
    with open(test_file, 'wt') as f:
        f.write("""
void f(const char* p)
{
    *p++;
}
""")

    rule_file = os.path.join(__rules_dir, 'unused-deref.xml')
    args = [
        '--template=simple',
        '--rule-file={}'.format(rule_file),
        str(test_file)
    ]
    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stdout.splitlines() == [
        'Checking {} ...'.format(test_file),
        'Processing rule:  [;{}] [*] \\w+? (\\+\\+|\\-\\-) ; '
    ]
    assert stderr.splitlines() == [
        '{}:3:0: style: Redundant * found, "*p++" is the same as "*(p++)". [UnusedDeref]'.format(test_file)
    ]