@@ -1182,7 +1182,7 @@ def test_unknown_extension(tmpdir):
11821182 assert stderr == ''
11831183
11841184
1185- def test_multiple_define_rules (tmpdir ):
1185+ def test_rule_file_define_multiple (tmpdir ):
11861186 rule_file = os .path .join (tmpdir , 'rule_file.xml' )
11871187 with open (rule_file , 'wt' ) as f :
11881188 f .write ("""
@@ -1201,6 +1201,7 @@ def test_multiple_define_rules(tmpdir):
12011201 <message>
12021202 <severity>error</severity>
12031203 <id>ruleId2</id>
1204+ <summary>define2</summary>
12041205 </message>
12051206 </rule>
12061207</rules>""" )
@@ -1213,18 +1214,134 @@ def test_multiple_define_rules(tmpdir):
12131214void f() { }
12141215''' )
12151216
1216- exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), test_file ])
1217+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), '-DDEF_3' , test_file ])
12171218 assert exitcode == 0 , stderr
12181219 lines = stdout .splitlines ()
12191220 assert lines == [
12201221 'Checking {} ...' .format (test_file ),
12211222 'Processing rule: DEF_1' ,
1222- 'Processing rule: DEF_2'
1223+ 'Processing rule: DEF_2' ,
1224+ 'Checking {}: DEF_3=1...' .format (test_file )
12231225 ]
12241226 lines = stderr .splitlines ()
12251227 assert lines == [
12261228 "{}:2:0: error: found 'DEF_1' [ruleId1]" .format (test_file ),
1227- "{}:3:0: error: found 'DEF_2' [ruleId2]" .format (test_file )
1229+ "{}:3:0: error: define2 [ruleId2]" .format (test_file )
1230+ ]
1231+
1232+
1233+ def test_rule_file_define (tmpdir ):
1234+ rule_file = os .path .join (tmpdir , 'rule_file.xml' )
1235+ with open (rule_file , 'wt' ) as f :
1236+ f .write ("""
1237+ <rule>
1238+ <tokenlist>define</tokenlist>
1239+ <pattern>DEF_.</pattern>
1240+ </rule>
1241+ """ )
1242+
1243+ test_file = os .path .join (tmpdir , 'test.c' )
1244+ with open (test_file , 'wt' ) as f :
1245+ f .write ('''
1246+ #define DEF_1
1247+ #define DEF_2
1248+ void f() { }
1249+ ''' )
1250+
1251+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), '-DDEF_3' , test_file ])
1252+ assert exitcode == 0 , stdout
1253+ lines = stdout .splitlines ()
1254+ assert lines == [
1255+ 'Checking {} ...' .format (test_file ),
1256+ 'Processing rule: DEF_.' ,
1257+ 'Checking {}: DEF_3=1...' .format (test_file )
1258+ ]
1259+ lines = stderr .splitlines ()
1260+ assert lines == [
1261+ "{}:2:0: style: found 'DEF_1' [rule]" .format (test_file ),
1262+ "{}:3:0: style: found 'DEF_2' [rule]" .format (test_file )
1263+ ]
1264+
1265+
1266+ def test_rule_file_normal (tmpdir ):
1267+ rule_file = os .path .join (tmpdir , 'rule_file.xml' )
1268+ with open (rule_file , 'wt' ) as f :
1269+ f .write ("""
1270+ <rule>
1271+ <pattern>f</pattern>
1272+ </rule>
1273+ """ )
1274+
1275+ test_file = os .path .join (tmpdir , 'test.c' )
1276+ with open (test_file , 'wt' ) as f :
1277+ f .write ('''
1278+ #define DEF_1
1279+ #define DEF_2
1280+ void f() { }
1281+ ''' )
1282+
1283+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), test_file ])
1284+ assert exitcode == 0 , stdout
1285+ lines = stdout .splitlines ()
1286+ assert lines == [
1287+ 'Checking {} ...' .format (test_file ),
1288+ 'Processing rule: f' ,
1289+ ]
1290+ lines = stderr .splitlines ()
1291+ assert lines == [
1292+ "{}:4:0: style: found 'f' [rule]" .format (test_file )
1293+ ]
1294+
1295+
1296+ # TODO: what is the difference to "normal"
1297+ def test_rule_file_raw (tmpdir ):
1298+ rule_file = os .path .join (tmpdir , 'rule_file.xml' )
1299+ with open (rule_file , 'wt' ) as f :
1300+ f .write ("""
1301+ <rule>
1302+ <tokenlist>raw</tokenlist>
1303+ <pattern>f</pattern>
1304+ </rule>
1305+ """ )
1306+
1307+ test_file = os .path .join (tmpdir , 'test.c' )
1308+ with open (test_file , 'wt' ) as f :
1309+ f .write ('''
1310+ #define DEF_1
1311+ #define DEF_2
1312+ void f() { }
1313+ ''' )
1314+
1315+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), test_file ])
1316+ assert exitcode == 0 , stdout
1317+ lines = stdout .splitlines ()
1318+ assert lines == [
1319+ 'Checking {} ...' .format (test_file ),
1320+ 'Processing rule: f' ,
1321+ ]
1322+ lines = stderr .splitlines ()
1323+ assert lines == [
1324+ "{}:4:0: style: found 'f' [rule]" .format (test_file )
12281325 ]
12291326
1230- # TODO: test "raw" and "normal" rules
1327+
1328+ def test_rule (tmpdir ):
1329+ test_file = os .path .join (tmpdir , 'test.c' )
1330+ with open (test_file , 'wt' ) as f :
1331+ f .write ('''
1332+ #define DEF_1
1333+ #define DEF_2
1334+ void f() { }
1335+ ''' )
1336+
1337+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule=f' , test_file ])
1338+ assert exitcode == 0 , stdout
1339+ lines = stdout .splitlines ()
1340+ assert lines == [
1341+ 'Checking {} ...' .format (test_file ),
1342+ 'Processing rule: f' ,
1343+ ]
1344+ lines = stderr .splitlines ()
1345+ assert lines == [
1346+ "{}:4:0: style: found 'f' [rule]" .format (test_file )
1347+ ]
0 commit comments