Debian Codegraph @

hypothesis-python/tests/cover/test_provisional_strategies.py

raw
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 re
12 import string
13
14 import pytest
15
16 from hypothesis import given, settings
17 from hypothesis.errors import InvalidArgument
18 from hypothesis.provisional import (
19 FRAGMENT_SAFE_CHARACTERS,
20 _url_fragments_strategy,
21 domains,
22 urls,
23 )
24
25 from tests.common.debug import check_can_generate_examples, find_any
26
27
28 @given(urls())
29 def test_is_URL(url):
30 allowed_chars = set(string.ascii_letters + string.digits + "$-_.+!*'(),~%/")
31 url_schemeless = url.split("://", 1)[1]
32 components = url_schemeless.split("#", 1)
33
34 domain_path = components[0]
35 path = domain_path.split("/", 1)[1] if "/" in domain_path else ""
36 assert all(c in allowed_chars for c in path)
37 assert all(
38 re.match("^[0-9A-Fa-f]{2}", after_perc) for after_perc in path.split("%")[1:]
39 )
40
41 fragment = components[1] if "#" in url_schemeless else ""
42 fragment_allowed_chars = allowed_chars | {"?"}
43 assert all(c in fragment_allowed_chars for c in fragment)
44 assert all(
45 re.match("^[0-9A-Fa-f]{2}", after_perc)
46 for after_perc in fragment.split("%")[1:]
47 )
48
49
50 @pytest.mark.parametrize("max_length", [-1, 0, 3, 4.0, 256])
51 @pytest.mark.parametrize("max_element_length", [-1, 0, 4.0, 64, 128])
52 def test_invalid_domain_arguments(max_length, max_element_length):
53 with pytest.raises(InvalidArgument):
54 check_can_generate_examples(
55 domains(max_length=max_length, max_element_length=max_element_length)
56 )
57
58
59 @pytest.mark.skipif(
60 settings.get_current_profile_name() == "crosshair",
61 reason="takes ~300s each; the `sampled_from(get_top_level_domains())` decision is realized via iterative comparisons https://github.com/pschanely/CrossHair/issues/332",
62 )
63 @pytest.mark.parametrize("max_length", [None, 4, 8, 255])
64 @pytest.mark.parametrize("max_element_length", [None, 1, 2, 4, 8, 63])
65 def test_valid_domains_arguments(max_length, max_element_length):
66 check_can_generate_examples(
67 domains(max_length=max_length, max_element_length=max_element_length)
68 )
69
70
71 @pytest.mark.parametrize("strategy", [domains(), urls()])
72 def test_find_any_non_empty(strategy):
73 find_any(strategy, lambda s: len(s) > 0)
74
75
76 @given(_url_fragments_strategy)
77 # There's a lambda in the implementation that only gets run if we generate at
78 # least one percent-escape sequence, so we derandomize to ensure that coverage
79 # isn't flaky.
80 @settings(derandomize=True)
81 def test_url_fragments_contain_legal_chars(fragment):
82 assert fragment.startswith("#")
83
84 # Strip all legal escape sequences. Any remaining % characters were not
85 # part of a legal escape sequence.
86 without_escapes = re.sub(r"(?ai)%[0-9a-f][0-9a-f]", "", fragment[1:])
87
88 assert set(without_escapes).issubset(FRAGMENT_SAFE_CHARACTERS)