1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
from json import dumps
from hypothesis import given, settings
from hypothesis import strategies as st
from tqdm import tqdm
from partial_json_parser.core.api import parse_json
json = st.recursive(
st.none() | st.booleans() | st.floats() | st.text(),
lambda children: st.lists(children) | st.dictionaries(st.text(), children),
)
bar = tqdm(ascii=True, ncols=200, leave=False)
FINE_JSON_EXAMPLES = 333
PARTIAL_JSON_EXAMPLES = 333
@settings(max_examples=FINE_JSON_EXAMPLES, deadline=None)
@given(json)
def test_fine_json(anything):
assert str(anything) == str(parse_json(dumps(anything, ensure_ascii=False)))
bar.update()
@settings(max_examples=PARTIAL_JSON_EXAMPLES, deadline=None)
@given(json)
def test_partial_json(anything):
json_string = dumps(anything, ensure_ascii=False)
for i in range(1, len(json_string), max(1, len(json_string) // 10)):
if json_string.startswith("-", 0, i):
continue
parse_json(json_string[:i])
bar.update()
def main():
bar.set_description(" Testing Partial JSON ", False)
bar.clear()
bar.reset(PARTIAL_JSON_EXAMPLES)
test_partial_json()
bar.set_description(" Testing F i n e JSON ", False)
bar.clear()
bar.reset(FINE_JSON_EXAMPLES)
test_fine_json()
bar.close()
|