Debian Codegraph @

hypothesis-python/tests/cover/test_simple_strings.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 from hypothesis import given
12 from hypothesis.strategies import binary, characters, text, tuples
13
14 from tests.common.debug import minimal
15
16
17 def test_can_minimize_up_to_zero():
18 s = minimal(text(), lambda x: any(t <= "0" for t in x))
19 assert s == "0"
20
21
22 def test_minimizes_towards_ascii_zero():
23 s = minimal(text(), lambda x: any(t < "0" for t in x))
24 assert s == chr(ord("0") - 1)
25
26
27 def test_can_handle_large_codepoints():
28 s = minimal(text(), lambda x: x >= "☃")
29 assert s == "☃"
30
31
32 def test_can_find_mixed_ascii_and_non_ascii_strings():
33 s = minimal(
34 text(), lambda x: (any(t >= "☃" for t in x) and any(ord(t) <= 127 for t in x))
35 )
36 assert len(s) == 2
37 assert sorted(s) == ["0", "☃"]
38
39
40 def test_will_find_ascii_examples_given_the_chance():
41 s = minimal(
42 tuples(text(max_size=1), text(max_size=1)), lambda x: x[0] and (x[0] < x[1])
43 )
44 assert ord(s[1]) == ord(s[0]) + 1
45 assert "0" in s
46
47
48 def test_minimisation_consistent_with_characters():
49 s = minimal(text("FEDCBA", min_size=3))
50 assert s == "AAA"
51
52
53 def test_finds_single_element_strings():
54 assert minimal(text(), bool) == "0"
55
56
57 @given(binary(max_size=5))
58 def test_binary_respects_max_size(x):
59 assert len(x) <= 5
60
61
62 def test_does_not_simplify_into_surrogates():
63 f = minimal(text(), lambda x: x >= "\udfff")
64 assert f == "\ue000"
65
66 size = 5
67
68 f = minimal(text(min_size=size), lambda x: sum(t >= "\udfff" for t in x) >= size)
69 assert f == "\ue000" * size
70
71
72 @given(text(alphabet=["a", "b"]))
73 def test_respects_alphabet_if_list(xs):
74 assert set(xs).issubset(set("ab"))
75
76
77 @given(text(alphabet="cdef"))
78 def test_respects_alphabet_if_string(xs):
79 assert set(xs).issubset(set("cdef"))
80
81
82 @given(text())
83 def test_can_encode_as_utf8(s):
84 s.encode()
85
86
87 @given(text(characters(exclude_characters="\n")))
88 def test_can_blacklist_newlines(s):
89 assert "\n" not in s
90
91
92 @given(text(characters(exclude_categories=("Cc", "Cs"))))
93 def test_can_exclude_newlines_by_category(s):
94 assert "\n" not in s
95
96
97 @given(text(characters(max_codepoint=127)))
98 def test_can_restrict_to_ascii_only(s):
99 s.encode("ascii")
100
101
102 def test_fixed_size_bytes_just_draw_bytes():
103 from hypothesis.internal.conjecture.data import ConjectureData
104
105 x = ConjectureData.for_choices([b"foo"])
106 assert x.draw(binary(min_size=3, max_size=3)) == b"foo"
107
108
109 @given(text(max_size=10**6))
110 def test_can_set_max_size_large(s):
111 pass