1-
21# python -m pytest dumpfile_test.py
32
43import os
4+ import pathlib
55
66from testutils import cppcheck
7+ import xml .etree .ElementTree as ET
78
89
910def test_libraries (tmpdir ): #13701
@@ -20,3 +21,75 @@ def test_libraries(tmpdir): #13701
2021 dump = f .read ()
2122 assert '<library lib="posix"/>' in dump
2223 assert dump .find ('<library lib="posix"/>' ) < dump .find ('<dump cfg=' )
24+
25+
26+ def __test_language (tmp_path , file_ext , exp_lang , force_lang = None ):
27+ test_file = tmp_path / ('test.' + file_ext )
28+ with open (test_file , 'wt' ):
29+ pass
30+
31+ args = [
32+ '--dump' ,
33+ str (test_file )
34+ ]
35+ if force_lang :
36+ args += ['--language=' + force_lang ]
37+
38+ exitcode , stdout , stderr = cppcheck (args )
39+ assert exitcode == 0 , stdout if stdout else stderr
40+
41+ dump_s = pathlib .Path (str (test_file ) + '.dump' ).read_text ()
42+
43+ dump_xml = ET .fromstring (dump_s )
44+ assert 'language' in dump_xml .attrib
45+ assert dump_xml .attrib ['language' ] == exp_lang
46+
47+
48+ def test_language_c (tmp_path ):
49+ __test_language (tmp_path , 'c' , exp_lang = 'c' )
50+
51+
52+ def test_language_c_force_c (tmp_path ):
53+ __test_language (tmp_path , 'c' , force_lang = 'c' , exp_lang = 'c' )
54+
55+
56+ def test_language_c_force_cpp (tmp_path ):
57+ __test_language (tmp_path , 'c' , force_lang = 'c++' , exp_lang = 'cpp' )
58+
59+
60+ def test_language_cpp (tmp_path ):
61+ __test_language (tmp_path , 'cpp' , exp_lang = 'cpp' )
62+
63+
64+ def test_language_cpp_force_cpp (tmp_path ):
65+ __test_language (tmp_path , 'cpp' , force_lang = 'c++' , exp_lang = 'cpp' )
66+
67+
68+ def test_language_cpp_force_c (tmp_path ):
69+ __test_language (tmp_path , 'cpp' , force_lang = 'c' , exp_lang = 'c' )
70+
71+
72+ # headers default to C
73+ def test_language_h (tmp_path ):
74+ __test_language (tmp_path , 'h' , exp_lang = 'c' )
75+
76+
77+ def test_language_h_force_c (tmp_path ):
78+ __test_language (tmp_path , 'h' , force_lang = 'c' , exp_lang = 'c' )
79+
80+
81+ def test_language_h_force_cpp (tmp_path ):
82+ __test_language (tmp_path , 'h' , force_lang = 'c++' , exp_lang = 'cpp' )
83+
84+
85+ # files with unknown extensions default to C++
86+ def test_language_unk (tmp_path ):
87+ __test_language (tmp_path , 'src' , exp_lang = 'cpp' )
88+
89+
90+ def test_language_unk_force_c (tmp_path ):
91+ __test_language (tmp_path , 'src' , force_lang = 'c' , exp_lang = 'c' )
92+
93+
94+ def test_language_unk_force_cpp (tmp_path ):
95+ __test_language (tmp_path , 'src' , force_lang = 'c++' , exp_lang = 'cpp' )
0 commit comments