Debian Codegraph @

hypothesis-python/tests/cover/test_simple_characters.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 unicodedata
12
13 import pytest
14
15 from hypothesis.errors import InvalidArgument
16 from hypothesis.strategies import characters
17
18 from tests.common.debug import (
19 assert_no_examples,
20 check_can_generate_examples,
21 find_any,
22 minimal,
23 )
24 from tests.common.utils import fails_with
25
26
27 @fails_with(InvalidArgument)
28 def test_nonexistent_category_argument():
29 check_can_generate_examples(characters(exclude_categories=["foo"]))
30
31
32 def test_bad_codepoint_arguments():
33 with pytest.raises(InvalidArgument):
34 check_can_generate_examples(characters(min_codepoint=42, max_codepoint=24))
35
36
37 def test_exclude_all_available_range():
38 with pytest.raises(InvalidArgument):
39 check_can_generate_examples(
40 characters(
41 min_codepoint=ord("0"), max_codepoint=ord("0"), exclude_characters="0"
42 )
43 )
44
45
46 def test_when_nothing_could_be_produced():
47 with pytest.raises(InvalidArgument):
48 check_can_generate_examples(
49 characters(
50 categories=["Cc"], min_codepoint=ord("0"), max_codepoint=ord("9")
51 )
52 )
53
54
55 def test_include_exclude_with_multiple_chars_is_invalid():
56 with pytest.raises(InvalidArgument, match="required to be a single character"):
57 check_can_generate_examples(
58 characters(codec="ascii", exclude_characters=["a", "morethanonechar"])
59 )
60
61 with pytest.raises(InvalidArgument, match="required to be a single character"):
62 check_can_generate_examples(
63 characters(codec="ascii", include_characters=["a", "morethanonechar"])
64 )
65
66
67 def test_characters_of_specific_groups():
68 st = characters(categories=("Lu", "Nd"))
69
70 find_any(st, lambda c: unicodedata.category(c) == "Lu")
71 find_any(st, lambda c: unicodedata.category(c) == "Nd")
72
73 assert_no_examples(st, lambda c: unicodedata.category(c) not in ("Lu", "Nd"))
74
75
76 def test_characters_of_major_categories():
77 st = characters(categories=("L", "N"))
78 find_any(st, lambda c: unicodedata.category(c).startswith("L"))
79 find_any(st, lambda c: unicodedata.category(c).startswith("N"))
80 assert_no_examples(st, lambda c: unicodedata.category(c)[0] not in ("L", "N"))
81
82
83 def test_exclude_characters_of_specific_groups():
84 st = characters(exclude_categories=("Lu", "Nd"))
85
86 find_any(st, lambda c: unicodedata.category(c) != "Lu")
87 find_any(st, lambda c: unicodedata.category(c) != "Nd")
88
89 assert_no_examples(st, lambda c: unicodedata.category(c) in ("Lu", "Nd"))
90
91
92 def test_exclude_characters_of_major_categories():
93 st = characters(exclude_categories=("L", "N"))
94 find_any(st, lambda c: not unicodedata.category(c).startswith("L"))
95 find_any(st, lambda c: not unicodedata.category(c).startswith("N"))
96 assert_no_examples(st, lambda c: unicodedata.category(c)[0] in ("L", "N"))
97
98
99 def test_find_one():
100 char = minimal(characters(min_codepoint=48, max_codepoint=48))
101 assert char == "0"
102
103
104 def test_find_something_rare():
105 st = characters(categories=["Zs"], min_codepoint=12288)
106
107 find_any(st, lambda c: unicodedata.category(c) == "Zs")
108
109 assert_no_examples(st, lambda c: unicodedata.category(c) != "Zs")
110
111
112 def test_whitelisted_characters_alone():
113 with pytest.raises(InvalidArgument):
114 check_can_generate_examples(characters(include_characters="te02тест49st"))
115
116
117 def test_whitelisted_characters_overlap_blacklisted_characters():
118 good_chars = "te02тест49st"
119 bad_chars = "ts94тсет"
120 with pytest.raises(InvalidArgument) as exc:
121 check_can_generate_examples(
122 characters(
123 min_codepoint=ord("0"),
124 max_codepoint=ord("9"),
125 include_characters=good_chars,
126 exclude_characters=bad_chars,
127 )
128 )
129 assert repr(good_chars) in str(exc)
130 assert repr(bad_chars) in str(exc)
131
132
133 def test_whitelisted_characters_override():
134 good_characters = "teтестst"
135 st = characters(
136 min_codepoint=ord("0"),
137 max_codepoint=ord("9"),
138 include_characters=good_characters,
139 )
140
141 find_any(st, lambda c: c in good_characters)
142 find_any(st, lambda c: c in "0123456789")
143
144 assert_no_examples(st, lambda c: c not in good_characters + "0123456789")
145
146
147 def test_blacklisted_characters():
148 bad_chars = "te02тест49st"
149 st = characters(
150 min_codepoint=ord("0"), max_codepoint=ord("9"), exclude_characters=bad_chars
151 )
152
153 assert "1" == minimal(st)
154
155 assert_no_examples(st, lambda c: c in bad_chars)
156
157
158 def test_whitelist_characters_disjoint_blacklist_characters():
159 good_chars = "123abc"
160 bad_chars = "456def"
161 st = characters(
162 min_codepoint=ord("0"),
163 max_codepoint=ord("9"),
164 exclude_characters=bad_chars,
165 include_characters=good_chars,
166 )
167
168 assert_no_examples(st, lambda c: c in bad_chars)