Skip to content

Instantly share code, notes, and snippets.

@anentropic
Last active November 25, 2017 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anentropic/e9b99faf0d9bd9e951cef6d8b4355e57 to your computer and use it in GitHub Desktop.
Save anentropic/e9b99faf0d9bd9e951cef6d8b4355e57 to your computer and use it in GitHub Desktop.
pyparsing grammar tests
from collections import namedtuple
import re
import pytest
from myproject import grammar
EXAMPLES = (
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='indented_nl_indented_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah""",
id='indented_no_nl_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='indented_nl_non_indented_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='indented_nl_blank_line_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='indented_nl_blank_line_indented_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='non_indented_nl_indented_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah""",
id='non_indented_no_nl_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='non_indented_nl_non_indented_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='non_indented_nl_blank_line_end'
),
pytest.param(
"""
first_identifier: one line only
identifier: some description text here which will wrap
on to the next line. the follow-on text should be
indented. the description may contain any text including
identifier: in an awkward position like this
next_identifier: more description, short this time
last_identifier: blah blah
""",
id='non_indented_nl_blank_line_indented_end'
),
)
Definition = namedtuple('Definition', 'term description')
expected = (
Definition(
'first_identifier',
'one line only'
),
Definition(
'identifier',
'some description text here which will wrap on to the next line. the follow-on text should be indented. the description may contain any text including identifier: in an awkward position like this'
),
Definition(
'next_identifier',
'more description, short this time'
),
Definition(
'last_identifier',
'blah blah'
),
)
def normalize(val):
return re.sub(r'\s+', ' ', val).strip()
@pytest.mark.parametrize('example', EXAMPLES)
def test_parse(example):
parsed = grammar.parseString(example)
for i, expected_def in enumerate(expected):
parsed_def = parsed[i]
assert parsed_def.term == expected_def.term
assert normalize(parsed_def.description) == expected_def.description
@anentropic
Copy link
Author

small py.test suite for the grammar I was asking about on Stack Overflow here:
https://stackoverflow.com/questions/47484639/pyparsing-how-to-skipto-end-of-indented-block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment