Skip to content

Commit c3036c3

Browse files
committed
🧪 Integrate Hypothesis in tests
1 parent 5e12b4c commit c3036c3

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

requirements/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
hypothesis >= 6.0
12
idna==3.4
23
multidict==6.0.4
34
pytest==7.4.3

tests/test_quoting.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import pytest
2+
from hypothesis import HealthCheck, assume, example, given, note, settings
3+
from hypothesis import strategies as st
24

35
from yarl._quoting import NO_EXTENSIONS
46
from yarl._quoting_py import _Quoter as _PyQuoter
@@ -448,3 +450,82 @@ def test_quoter_path_with_plus(quoter):
448450
def test_unquoter_path_with_plus(unquoter):
449451
s = "/test/x+y%2Bz/:+%2B/"
450452
assert "/test/x+y+z/:++/" == unquoter(unsafe="+")(s)
453+
454+
455+
@example(
456+
# quoter=_PyQuoter,
457+
# unquoter=_PyUnquoter,
458+
text_input="0",
459+
safe="",
460+
unsafe="0",
461+
protected="",
462+
qs=False,
463+
requote=False,
464+
)
465+
@settings(suppress_health_check=(HealthCheck.function_scoped_fixture,))
466+
@given(
467+
text_input=st.text(),
468+
safe=st.text(alphabet=st.characters(max_codepoint=127)),
469+
unsafe=st.text(),
470+
protected=st.text(alphabet=st.characters(max_codepoint=127)),
471+
qs=st.booleans(),
472+
requote=st.booleans(),
473+
)
474+
def test_quote_unquote_parameter(
475+
quoter: _PyQuoter,
476+
unquoter: _PyUnquoter,
477+
safe: str,
478+
unsafe: str,
479+
protected: str,
480+
qs: bool,
481+
requote: bool,
482+
text_input: str,
483+
) -> None:
484+
quote = quoter(safe=safe, protected=protected, qs=qs, requote=requote)
485+
unquote = unquoter(unsafe=unsafe, qs=qs)
486+
487+
text_quoted = quote(text_input)
488+
assume(qs and all(unsafe_char not in text_quoted for unsafe_char in unsafe))
489+
note(f"{text_quoted=}")
490+
text_output = unquote(text_quoted)
491+
assume(qs or all(unsafe_char not in text_output for unsafe_char in unsafe))
492+
# assume(
493+
# qs and
494+
# (unsafe in text_quoted and text_input != text_output) or
495+
# (unsafe not in text_quoted)
496+
# )
497+
498+
assert text_input == text_output
499+
500+
501+
if not NO_EXTENSIONS and False:
502+
test_quote_unquote_parameter = example(
503+
quoter=_PyQuoter,
504+
unquoter=_CUnquoter,
505+
text_input="0",
506+
safe="",
507+
unsafe="0",
508+
protected="",
509+
qs=False,
510+
requote=False,
511+
)(test_quote_unquote_parameter)
512+
test_quote_unquote_parameter = example(
513+
quoter=_CQuoter,
514+
unquoter=_PyUnquoter,
515+
text_input="0",
516+
safe="",
517+
unsafe="0",
518+
protected="",
519+
qs=False,
520+
requote=False,
521+
)(test_quote_unquote_parameter)
522+
test_quote_unquote_parameter = example(
523+
quoter=_CQuoter,
524+
unquoter=_CUnquoter,
525+
text_input="0",
526+
safe="",
527+
unsafe="0",
528+
protected="",
529+
qs=False,
530+
requote=False,
531+
)(test_quote_unquote_parameter)

0 commit comments

Comments
 (0)