1
1
import os
2
2
import pathlib
3
+ import pytest
3
4
import re
5
+ import shutil
6
+ import subprocess
7
+ import typing
4
8
5
- from cxxheaderparser .options import ParserOptions
6
- from cxxheaderparser . preprocessor import make_pcpp_preprocessor
9
+ from cxxheaderparser .options import ParserOptions , PreprocessorFunction
10
+ from cxxheaderparser import preprocessor
7
11
from cxxheaderparser .simple import (
8
12
NamespaceScope ,
9
13
ParsedData ,
22
26
)
23
27
24
28
25
- def test_basic_preprocessor () -> None :
29
+ @pytest .fixture (params = ["pcpp" ])
30
+ def make_pp (request ) -> typing .Callable [..., PreprocessorFunction ]:
31
+ param = request .param
32
+ if param == "pcpp" :
33
+ if preprocessor .pcpp is None :
34
+ pytest .skip ("pcpp not installed" )
35
+ return preprocessor .make_pcpp_preprocessor
36
+ else :
37
+ assert False
38
+
39
+
40
+ def test_basic_preprocessor (
41
+ make_pp : typing .Callable [..., PreprocessorFunction ]
42
+ ) -> None :
26
43
content = """
27
44
#define X 1
28
45
int x = X;
29
46
"""
30
- options = ParserOptions (preprocessor = make_pcpp_preprocessor ())
47
+
48
+ options = ParserOptions (preprocessor = make_pp ())
31
49
data = parse_string (content , cleandoc = True , options = options )
32
50
33
51
assert data == ParsedData (
@@ -45,7 +63,10 @@ def test_basic_preprocessor() -> None:
45
63
)
46
64
47
65
48
- def test_preprocessor_omit_content (tmp_path : pathlib .Path ) -> None :
66
+ def test_preprocessor_omit_content (
67
+ make_pp : typing .Callable [..., PreprocessorFunction ],
68
+ tmp_path : pathlib .Path ,
69
+ ) -> None :
49
70
"""Ensure that content in other headers is omitted"""
50
71
h_content = '#include "t2.h"' "\n " "int x = X;\n "
51
72
h2_content = "#define X 2\n " "int omitted = 1;\n "
@@ -56,7 +77,7 @@ def test_preprocessor_omit_content(tmp_path: pathlib.Path) -> None:
56
77
with open (tmp_path / "t2.h" , "w" ) as fp :
57
78
fp .write (h2_content )
58
79
59
- options = ParserOptions (preprocessor = make_pcpp_preprocessor ())
80
+ options = ParserOptions (preprocessor = make_pp ())
60
81
data = parse_file (tmp_path / "t1.h" , options = options )
61
82
62
83
assert data == ParsedData (
@@ -74,7 +95,10 @@ def test_preprocessor_omit_content(tmp_path: pathlib.Path) -> None:
74
95
)
75
96
76
97
77
- def test_preprocessor_omit_content2 (tmp_path : pathlib .Path ) -> None :
98
+ def test_preprocessor_omit_content2 (
99
+ make_pp : typing .Callable [..., PreprocessorFunction ],
100
+ tmp_path : pathlib .Path ,
101
+ ) -> None :
78
102
"""
79
103
Ensure that content in other headers is omitted while handling pcpp
80
104
relative path quirk
@@ -91,9 +115,7 @@ def test_preprocessor_omit_content2(tmp_path: pathlib.Path) -> None:
91
115
with open (tmp_path2 / "t2.h" , "w" ) as fp :
92
116
fp .write (h2_content )
93
117
94
- options = ParserOptions (
95
- preprocessor = make_pcpp_preprocessor (include_paths = [str (tmp_path )])
96
- )
118
+ options = ParserOptions (preprocessor = make_pp (include_paths = [str (tmp_path )]))
97
119
98
120
# Weirdness happens here
99
121
os .chdir (tmp_path )
@@ -114,7 +136,9 @@ def test_preprocessor_omit_content2(tmp_path: pathlib.Path) -> None:
114
136
)
115
137
116
138
117
- def test_preprocessor_encoding (tmp_path : pathlib .Path ) -> None :
139
+ def test_preprocessor_encoding (
140
+ make_pp : typing .Callable [..., PreprocessorFunction ], tmp_path : pathlib .Path
141
+ ) -> None :
118
142
"""Ensure we can handle alternate encodings"""
119
143
h_content = b"// \xa9 2023 someone\n " b'#include "t2.h"' b"\n " b"int x = X;\n "
120
144
@@ -126,7 +150,7 @@ def test_preprocessor_encoding(tmp_path: pathlib.Path) -> None:
126
150
with open (tmp_path / "t2.h" , "wb" ) as fp :
127
151
fp .write (h2_content )
128
152
129
- options = ParserOptions (preprocessor = make_pcpp_preprocessor (encoding = "cp1252" ))
153
+ options = ParserOptions (preprocessor = make_pp (encoding = "cp1252" ))
130
154
data = parse_file (tmp_path / "t1.h" , options = options , encoding = "cp1252" )
131
155
132
156
assert data == ParsedData (
@@ -144,6 +168,7 @@ def test_preprocessor_encoding(tmp_path: pathlib.Path) -> None:
144
168
)
145
169
146
170
171
+ @pytest .mark .skipif (preprocessor .pcpp is None , reason = "pcpp not installed" )
147
172
def test_preprocessor_passthru_includes (tmp_path : pathlib .Path ) -> None :
148
173
"""Ensure that all #include pass through"""
149
174
h_content = '#include "t2.h"\n '
@@ -155,7 +180,9 @@ def test_preprocessor_passthru_includes(tmp_path: pathlib.Path) -> None:
155
180
fp .write ("" )
156
181
157
182
options = ParserOptions (
158
- preprocessor = make_pcpp_preprocessor (passthru_includes = re .compile (".+" ))
183
+ preprocessor = preprocessor .make_pcpp_preprocessor (
184
+ passthru_includes = re .compile (".+" )
185
+ )
159
186
)
160
187
data = parse_file (tmp_path / "t1.h" , options = options )
161
188
0 commit comments