Add basic preprocessor test
This commit is contained in:
parent
4ab7b3fd16
commit
a60bb7fd18
2
.github/workflows/dist.yml
vendored
2
.github/workflows/dist.yml
vendored
@ -81,7 +81,7 @@ jobs:
|
|||||||
run: python setup.py bdist_wheel
|
run: python setup.py bdist_wheel
|
||||||
|
|
||||||
- name: Install test dependencies
|
- name: Install test dependencies
|
||||||
run: python -m pip --disable-pip-version-check install pytest
|
run: python -m pip --disable-pip-version-check install pytest pcpp
|
||||||
|
|
||||||
- name: Test wheel
|
- name: Test wheel
|
||||||
shell: bash
|
shell: bash
|
||||||
|
@ -6,6 +6,7 @@ import subprocess
|
|||||||
import typing
|
import typing
|
||||||
|
|
||||||
from .errors import CxxParseError
|
from .errors import CxxParseError
|
||||||
|
from .preprocessor import make_pcpp_preprocessor
|
||||||
from .options import ParserOptions
|
from .options import ParserOptions
|
||||||
from .simple import parse_string, ParsedData
|
from .simple import parse_string, ParsedData
|
||||||
|
|
||||||
@ -49,14 +50,23 @@ def nondefault_repr(data: ParsedData) -> str:
|
|||||||
return _inner_repr(data)
|
return _inner_repr(data)
|
||||||
|
|
||||||
|
|
||||||
def gentest(infile: str, name: str, outfile: str, verbose: bool, fail: bool) -> None:
|
def gentest(
|
||||||
|
infile: str, name: str, outfile: str, verbose: bool, fail: bool, pcpp: bool
|
||||||
|
) -> None:
|
||||||
# Goal is to allow making a unit test as easy as running this dumper
|
# Goal is to allow making a unit test as easy as running this dumper
|
||||||
# on a file and copy/pasting this into a test
|
# on a file and copy/pasting this into a test
|
||||||
|
|
||||||
with open(infile, "r") as fp:
|
with open(infile, "r") as fp:
|
||||||
content = fp.read()
|
content = fp.read()
|
||||||
|
|
||||||
|
maybe_options = ""
|
||||||
|
popt = ""
|
||||||
|
|
||||||
options = ParserOptions(verbose=verbose)
|
options = ParserOptions(verbose=verbose)
|
||||||
|
if options:
|
||||||
|
options.preprocessor = make_pcpp_preprocessor()
|
||||||
|
maybe_options = "options = ParserOptions(preprocessor=make_pcpp_preprocessor())"
|
||||||
|
popt = ", options=options"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = parse_string(content, options=options)
|
data = parse_string(content, options=options)
|
||||||
@ -74,15 +84,17 @@ def gentest(infile: str, name: str, outfile: str, verbose: bool, fail: bool) ->
|
|||||||
if not fail:
|
if not fail:
|
||||||
stmt = nondefault_repr(data)
|
stmt = nondefault_repr(data)
|
||||||
stmt = f"""
|
stmt = f"""
|
||||||
data = parse_string(content, cleandoc=True)
|
{maybe_options}
|
||||||
|
data = parse_string(content, cleandoc=True{popt})
|
||||||
|
|
||||||
assert data == {stmt}
|
assert data == {stmt}
|
||||||
"""
|
"""
|
||||||
else:
|
else:
|
||||||
stmt = f"""
|
stmt = f"""
|
||||||
|
{maybe_options}
|
||||||
err = {repr(err)}
|
err = {repr(err)}
|
||||||
with pytest.raises(CxxParseError, match=re.escape(err)):
|
with pytest.raises(CxxParseError, match=re.escape(err)):
|
||||||
parse_string(content, cleandoc=True)
|
parse_string(content, cleandoc=True{popt})
|
||||||
"""
|
"""
|
||||||
|
|
||||||
content = ("\n" + content.strip()).replace("\n", "\n ")
|
content = ("\n" + content.strip()).replace("\n", "\n ")
|
||||||
@ -113,6 +125,7 @@ if __name__ == "__main__":
|
|||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("header")
|
parser.add_argument("header")
|
||||||
parser.add_argument("name", nargs="?", default="TODO")
|
parser.add_argument("name", nargs="?", default="TODO")
|
||||||
|
parser.add_argument("--pcpp", default=False, action="store_true")
|
||||||
parser.add_argument("-v", "--verbose", default=False, action="store_true")
|
parser.add_argument("-v", "--verbose", default=False, action="store_true")
|
||||||
parser.add_argument("-o", "--output", default="-")
|
parser.add_argument("-o", "--output", default="-")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@ -120,4 +133,4 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
gentest(args.header, args.name, args.output, args.verbose, args.fail)
|
gentest(args.header, args.name, args.output, args.verbose, args.fail, args.pcpp)
|
||||||
|
27
tests/test_preprocessor.py
Normal file
27
tests/test_preprocessor.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from cxxheaderparser.options import ParserOptions
|
||||||
|
from cxxheaderparser.preprocessor import make_pcpp_preprocessor
|
||||||
|
from cxxheaderparser.simple import NamespaceScope, ParsedData, parse_string
|
||||||
|
from cxxheaderparser.types import FundamentalSpecifier, NameSpecifier, PQName, Token, Type, Value, Variable
|
||||||
|
|
||||||
|
|
||||||
|
def test_basic_preprocessor() -> None:
|
||||||
|
content = """
|
||||||
|
#define X 1
|
||||||
|
int x = X;
|
||||||
|
"""
|
||||||
|
options = ParserOptions(preprocessor=make_pcpp_preprocessor())
|
||||||
|
data = parse_string(content, cleandoc=True, options=options)
|
||||||
|
|
||||||
|
assert data == ParsedData(
|
||||||
|
namespace=NamespaceScope(
|
||||||
|
variables=[
|
||||||
|
Variable(
|
||||||
|
name=PQName(segments=[NameSpecifier(name="x")]),
|
||||||
|
type=Type(
|
||||||
|
typename=PQName(segments=[FundamentalSpecifier(name="int")])
|
||||||
|
),
|
||||||
|
value=Value(tokens=[Token(value="1")]),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user