| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import json |
| 12 |
|
| 13 |
import pytest |
| 14 |
from lark.lark import Lark |
| 15 |
|
| 16 |
from hypothesis import given |
| 17 |
from hypothesis.errors import InvalidArgument |
| 18 |
from hypothesis.extra.lark import from_lark |
| 19 |
from hypothesis.strategies import characters, data, just |
| 20 |
|
| 21 |
from tests.common.debug import check_can_generate_examples, find_any |
| 22 |
|
| 23 |
# Adapted from the official Lark tutorial, with modifications to ensure |
| 24 |
# that the generated JSON is valid. i.e. no numbers starting with ".", |
| 25 |
# \f is not ignorable whitespace, and restricted strings only. Source: |
| 26 |
# https://github.com/lark-parser/lark/blob/master/docs/json_tutorial.md |
| 27 |
EBNF_GRAMMAR = r""" |
| 28 |
value: dict |
| 29 |
| list |
| 30 |
| STRING |
| 31 |
| NUMBER |
| 32 |
| "true" -> true |
| 33 |
| "false" -> false |
| 34 |
| "null" -> null |
| 35 |
list : "[" [value ("," value)*] "]" |
| 36 |
dict : "{" [STRING ":" value ("," STRING ":" value)*] "}" |
| 37 |
|
| 38 |
STRING : /"[a-z]*"/ |
| 39 |
NUMBER : /-?[1-9][0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?/ |
| 40 |
|
| 41 |
WS : /[ \t\r\n]+/ |
| 42 |
%ignore WS |
| 43 |
""" |
| 44 |
|
| 45 |
LIST_GRAMMAR = r""" |
| 46 |
list : "[" [NUMBER ("," NUMBER)*] "]" |
| 47 |
NUMBER: /[0-9]|[1-9][0-9]*/ |
| 48 |
""" |
| 49 |
|
| 50 |
|
| 51 |
@given(from_lark(Lark(EBNF_GRAMMAR, start="value"))) |
| 52 |
def test_generates_valid_json(string): |
| 53 |
json.loads(string) |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.parametrize( |
| 57 |
"start, type_", |
| 58 |
[ |
| 59 |
("dict", dict), |
| 60 |
("list", list), |
| 61 |
("STRING", str), |
| 62 |
("NUMBER", (int, float)), |
| 63 |
("TRUE", bool), |
| 64 |
("FALSE", bool), |
| 65 |
("NULL", type(None)), |
| 66 |
], |
| 67 |
) |
| 68 |
@given(data=data()) |
| 69 |
def test_can_specify_start_rule(data, start, type_): |
| 70 |
string = data.draw(from_lark(Lark(EBNF_GRAMMAR, start="value"), start=start)) |
| 71 |
value = json.loads(string) |
| 72 |
assert isinstance(value, type_) |
| 73 |
|
| 74 |
|
| 75 |
def test_can_generate_ignored_tokens(): |
| 76 |
list_grammar = r""" |
| 77 |
list : "[" [STRING ("," STRING)*] "]" |
| 78 |
STRING : /"[a-z]*"/ |
| 79 |
WS : /[ \t\r\n]+/ |
| 80 |
%ignore WS |
| 81 |
""" |
| 82 |
strategy = from_lark(Lark(list_grammar, start="list")) |
| 83 |
# A JSON list of strings in canonical form which does not round-trip, |
| 84 |
# must contain ignorable whitespace in the initial string. |
| 85 |
find_any(strategy, lambda s: "\t" in s) |
| 86 |
|
| 87 |
|
| 88 |
def test_generation_without_whitespace(): |
| 89 |
find_any(from_lark(Lark(LIST_GRAMMAR, start="list")), lambda g: " " not in g) |
| 90 |
|
| 91 |
|
| 92 |
def test_cannot_convert_EBNF_to_strategy_directly(): |
| 93 |
with pytest.raises(InvalidArgument): |
| 94 |
# Not a Lark object |
| 95 |
check_can_generate_examples(from_lark(EBNF_GRAMMAR)) |
| 96 |
with pytest.raises(TypeError): |
| 97 |
# Not even the right number of arguments |
| 98 |
check_can_generate_examples(from_lark(EBNF_GRAMMAR, start="value")) |
| 99 |
with pytest.raises(InvalidArgument): |
| 100 |
# Wrong type for explicit_strategies |
| 101 |
check_can_generate_examples( |
| 102 |
from_lark(Lark(LIST_GRAMMAR, start="list"), explicit=[]) |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
def test_required_undefined_terminals_require_explicit_strategies(): |
| 107 |
elem_grammar = r""" |
| 108 |
list : "[" ELEMENT ("," ELEMENT)* "]" |
| 109 |
%declare ELEMENT |
| 110 |
""" |
| 111 |
with pytest.raises(InvalidArgument, match=r"%declare"): |
| 112 |
check_can_generate_examples(from_lark(Lark(elem_grammar, start="list"))) |
| 113 |
strategy = {"ELEMENT": just("200")} |
| 114 |
check_can_generate_examples( |
| 115 |
from_lark(Lark(elem_grammar, start="list"), explicit=strategy) |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
def test_cannot_use_explicit_strategies_for_unknown_terminals(): |
| 120 |
with pytest.raises(InvalidArgument): |
| 121 |
check_can_generate_examples( |
| 122 |
from_lark( |
| 123 |
Lark(LIST_GRAMMAR, start="list"), explicit={"unused_name": just("")} |
| 124 |
) |
| 125 |
) |
| 126 |
|
| 127 |
|
| 128 |
def test_non_string_explicit_strategies_are_invalid(): |
| 129 |
with pytest.raises(InvalidArgument): |
| 130 |
check_can_generate_examples( |
| 131 |
from_lark(Lark(LIST_GRAMMAR, start="list"), explicit={"NUMBER": just(0)}) |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
@given( |
| 136 |
string=from_lark(Lark(LIST_GRAMMAR, start="list"), explicit={"NUMBER": just("0")}) |
| 137 |
) |
| 138 |
def test_can_override_defined_terminal(string): |
| 139 |
assert sum(json.loads(string)) == 0 |
| 140 |
|
| 141 |
|
| 142 |
@given(string=from_lark(Lark(LIST_GRAMMAR, start="list"), alphabet="[0,]")) |
| 143 |
def test_can_generate_from_limited_alphabet(string): |
| 144 |
assert sum(json.loads(string)) == 0 |
| 145 |
|
| 146 |
|
| 147 |
@given(string=from_lark(Lark(LIST_GRAMMAR, start="list"), alphabet="[9]")) |
| 148 |
def test_can_generate_from_limited_alphabet_no_comma(string): |
| 149 |
assert len(json.loads(string)) <= 1 |
| 150 |
|
| 151 |
|
| 152 |
@given( |
| 153 |
string=from_lark( |
| 154 |
Lark(EBNF_GRAMMAR, start="value"), |
| 155 |
alphabet=characters(codec="ascii", exclude_characters=","), |
| 156 |
) |
| 157 |
) |
| 158 |
def test_can_generate_from_limited_alphabet_no_comma_json(string): |
| 159 |
assert "," not in string |
| 160 |
|
| 161 |
|
| 162 |
def test_error_if_alphabet_bans_all_start_rules(): |
| 163 |
with pytest.raises( |
| 164 |
InvalidArgument, match=r"No start rule .+ is allowed by alphabet=" |
| 165 |
): |
| 166 |
check_can_generate_examples( |
| 167 |
from_lark(Lark(LIST_GRAMMAR, start="list"), alphabet="abc") |
| 168 |
) |