Skip to content

Commit

Permalink
Extend property tests for parser by new expressions
Browse files Browse the repository at this point in the history
Ref. #291
  • Loading branch information
treiher committed Aug 25, 2020
1 parent 3ea5b14 commit 5feced3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
29 changes: 27 additions & 2 deletions tests/property/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,31 @@ def variables(draw: Callable, elements: st.SearchStrategy[str]) -> expr.Variable

@st.composite
def attributes(draw: Callable, elements: st.SearchStrategy[expr.Expr]) -> expr.Expr:
attribute = draw(st.sampled_from([expr.Length, expr.First, expr.Length]))
attribute = draw(st.sampled_from([expr.Length, expr.First, expr.Last]))
return attribute(draw(elements))


@st.composite
def calls(draw: Callable, elements: st.SearchStrategy[expr.Expr]) -> expr.Call:
return draw(st.builds(expr.Call, identifiers(), st.lists(elements, min_size=1)))


@st.composite
def aggregates(draw: Callable, elements: st.SearchStrategy[str]) -> expr.Aggregate:
return expr.Aggregate(*draw(st.lists(elements, min_size=1)))


@st.composite
def strings(draw: Callable) -> expr.String:
return expr.String(draw(st.text(string.ascii_letters + string.digits, min_size=1)))


@st.composite
def quantified_expressions(draw: Callable, elements: st.SearchStrategy[expr.Expr]) -> expr.Expr:
operation = draw(st.sampled_from([expr.ForAllIn, expr.ForSomeIn]))
return draw(st.builds(operation, identifiers(), elements, elements))


@st.composite
def mathematical_expressions(draw: Callable, elements: st.SearchStrategy[expr.Expr]) -> expr.Expr:
operation = draw(st.sampled_from([expr.Add, expr.Mul, expr.Sub, expr.Div, expr.Pow]))
Expand All @@ -367,7 +383,16 @@ def mathematical_expressions(draw: Callable, elements: st.SearchStrategy[expr.Ex
def relations(draw: Callable, elements: st.SearchStrategy[expr.Expr]) -> expr.Relation:
relation = draw(
st.sampled_from(
[expr.Less, expr.LessEqual, expr.Equal, expr.GreaterEqual, expr.Greater, expr.NotEqual]
[
expr.Less,
expr.LessEqual,
expr.Equal,
expr.GreaterEqual,
expr.Greater,
expr.NotEqual,
expr.In,
expr.NotIn,
]
)
)
return relation(*draw(st.lists(elements, min_size=2, max_size=2)))
Expand Down
63 changes: 57 additions & 6 deletions tests/property/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,74 @@
)
@settings(deadline=None, suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow])
def test_parsing_mathematical_expressions(expression: expr.Expr) -> None:
parsed_expression = grammar.mathematical_expression().parseString(str(expression))[0]
parsed_expression = grammar.mathematical_expression().parseString(
str(expression), parseAll=True
)[0]
assert parsed_expression == expression


@given(
strategies.boolean_expressions(
st.one_of(
strategies.numbers()
| strategies.variables(strategies.identifiers())
| strategies.attributes(strategies.identifiers())
| strategies.aggregates(strategies.numbers())
strategies.aggregates(strategies.numbers())
| strategies.strings()
| strategies.mathematical_expressions(
st.one_of(
strategies.numbers()
| strategies.variables(strategies.identifiers())
| strategies.attributes(strategies.identifiers())
)
)
)
)
)
@settings(deadline=None, suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow])
def test_parsing_boolean_expressions(expression: expr.Expr) -> None:
parsed_expression = grammar.boolean_expression().parseString(str(expression))[0]
parsed_expression = grammar.boolean_expression().parseString(str(expression), parseAll=True)[0]
assert parsed_expression == expression


@given(
st.one_of(
strategies.mathematical_expressions(
st.one_of(
strategies.numbers()
| strategies.variables(strategies.identifiers())
| strategies.attributes(strategies.identifiers())
)
),
strategies.boolean_expressions(
st.one_of(
strategies.aggregates(strategies.numbers())
| strategies.strings()
| strategies.mathematical_expressions(
st.one_of(
strategies.numbers()
| strategies.variables(strategies.identifiers())
| strategies.attributes(strategies.identifiers())
)
)
)
),
strategies.calls(
st.one_of(
strategies.numbers()
| strategies.variables(strategies.identifiers())
| strategies.attributes(strategies.identifiers())
)
),
strategies.quantified_expressions(
st.one_of(
strategies.numbers()
| strategies.variables(strategies.identifiers())
| strategies.attributes(strategies.identifiers())
)
),
)
)
@settings(deadline=None, suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow])
def test_parsing_expressions(expression: expr.Expr) -> None:
parsed_expression = grammar.expression().parseString(str(expression), parseAll=True)[0]
assert parsed_expression == expression


Expand Down

0 comments on commit 5feced3

Please sign in to comment.