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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
from edify.errors.anchors import (
CannotDefineStartAfterEndError,
EndInputAlreadyDefinedError,
StartInputAlreadyDefinedError,
)
from edify.errors.captures import InvalidTotalCaptureGroupsIndexError
from edify.errors.input import (
MustBeAStringError,
MustBeAtLeastOneLiteralError,
MustBeAtLeastTwoOperandsError,
MustBeInstanceError,
MustBeIntegerGreaterThanZeroError,
MustBeLessThanError,
MustBeOneCharacterError,
MustBePositiveIntegerError,
MustBeSingleCharacterError,
MustHaveASmallerValueError,
)
from edify.errors.internal import (
FailedToCompileRegexError,
NonFusableElementError,
UnexpectedFrameTypeError,
UnknownElementTypeError,
)
from edify.errors.naming import (
CannotCreateDuplicateNamedGroupError,
NamedGroupDoesNotExistError,
NameNotValidError,
)
from edify.errors.structure import (
CannotCallSubexpressionError,
CannotEndWhileBuildingRootExpressionError,
)
def test_start_input_already_defined_outside_subexpression():
error = StartInputAlreadyDefinedError()
text = str(error)
assert "start_of_input has already been added" in text
assert "help: remove the duplicate .start_of_input()" in text
assert "ignore_start_and_end" not in text
def test_start_input_already_defined_in_subexpression():
error = StartInputAlreadyDefinedError(in_subexpression=True)
text = str(error)
assert "start_of_input has already been added" in text
assert "ignore_start_and_end=True" in text
def test_end_input_already_defined_outside_subexpression():
error = EndInputAlreadyDefinedError()
text = str(error)
assert "end_of_input has already been added" in text
assert "help: remove the duplicate .end_of_input()" in text
assert "ignore_start_and_end" not in text
def test_end_input_already_defined_in_subexpression():
error = EndInputAlreadyDefinedError(in_subexpression=True)
text = str(error)
assert "end_of_input has already been added" in text
assert "ignore_start_and_end=True" in text
def test_cannot_define_start_after_end():
error = CannotDefineStartAfterEndError()
text = str(error)
assert "start_of_input cannot follow end_of_input" in text
assert "move .start_of_input() to before" in text
def test_invalid_total_capture_groups_index_reports_out_of_range():
error = InvalidTotalCaptureGroupsIndexError(5, 3)
text = str(error)
assert "back_reference index #5 is out of range" in text
assert "3 capture groups" in text
assert "valid indices are 1 to 3" in text
def test_invalid_total_capture_groups_index_with_no_capture_groups_prompts_add_one():
error = InvalidTotalCaptureGroupsIndexError(1, 0)
text = str(error)
assert "no capture groups yet" in text
assert "add a .capture()" in text
def test_must_be_a_string():
error = MustBeAStringError("Name", "int")
text = str(error)
assert "Name must be a string" in text
assert "int" in text
assert "convert the value with str(...)" in text
def test_must_be_one_character():
error = MustBeOneCharacterError("Value")
text = str(error)
assert "Value must be one character long" in text
def test_must_be_single_character():
error = MustBeSingleCharacterError("Value", "str")
text = str(error)
assert "Value must be a single character" in text
assert "str" in text
def test_must_be_positive_integer():
error = MustBePositiveIntegerError("count")
text = str(error)
assert "count must be a positive integer" in text
def test_must_be_integer_greater_than_zero():
error = MustBeIntegerGreaterThanZeroError("x")
text = str(error)
assert "x must be an integer greater than zero" in text
def test_must_be_instance():
error = MustBeInstanceError("Expression", "str", "RegexBuilder")
text = str(error)
assert "Expression must be an instance of RegexBuilder" in text
assert "str" in text
def test_must_have_a_smaller_value_reports_the_codepoints():
error = MustHaveASmallerValueError("z", "a")
text = str(error)
assert "range bounds are inverted" in text
assert "'z'" in text
assert "'a'" in text
assert "= 122" in text
assert "= 97" in text
def test_must_be_less_than():
error = MustBeLessThanError("X", "Y")
text = str(error)
assert "X must be less than Y" in text
def test_must_be_at_least_two_operands():
error = MustBeAtLeastTwoOperandsError("any_of")
text = str(error)
assert "any_of requires at least two operands" in text
def test_must_be_at_least_one_literal():
error = MustBeAtLeastOneLiteralError("one_of")
text = str(error)
assert "one_of requires at least one literal" in text
def test_name_not_valid():
error = NameNotValidError("bad name")
text = str(error)
assert "'bad name' is not a valid identifier" in text
assert "letters, digits, and underscores" in text
def test_cannot_create_duplicate_named_group():
error = CannotCreateDuplicateNamedGroupError("dup")
text = str(error)
assert "named group 'dup' already exists" in text
assert ".named_back_reference('dup')" in text
def test_named_group_does_not_exist():
error = NamedGroupDoesNotExistError("missing")
text = str(error)
assert "named group 'missing' does not exist" in text
assert ".named_capture('missing')" in text
def test_cannot_end_while_building_root_expression():
error = CannotEndWhileBuildingRootExpressionError()
text = str(error)
assert "cannot .end() while building the root expression" in text
assert "no matching opener" in text or "no frame to close" in text
def test_cannot_call_subexpression():
error = CannotCallSubexpressionError("capture")
text = str(error)
assert "cannot merge a subexpression that has an unclosed frame" in text
assert "capture" in text
def test_unknown_element_type():
error = UnknownElementTypeError("WeirdElement")
text = str(error)
assert "unknown element type 'WeirdElement'" in text
def test_non_fusable_element():
error = NonFusableElementError("DigitElement")
text = str(error)
assert "cannot fuse element 'DigitElement' into a character class" in text
def test_unexpected_frame_type():
error = UnexpectedFrameTypeError("DigitElement")
text = str(error)
assert "stack frame anchored at unexpected element 'DigitElement'" in text
def test_failed_to_compile_regex():
error = FailedToCompileRegexError("missing )")
text = str(error)
assert "rejected by the re engine" in text
assert "missing )" in text
def test_every_annotated_error_message_starts_with_error_prefix():
errors = [
StartInputAlreadyDefinedError(),
CannotDefineStartAfterEndError(),
EndInputAlreadyDefinedError(),
InvalidTotalCaptureGroupsIndexError(1, 0),
MustBeAStringError("X", "int"),
MustBeOneCharacterError("X"),
MustBeSingleCharacterError("X", "int"),
MustBePositiveIntegerError("X"),
MustBeIntegerGreaterThanZeroError("X"),
MustBeInstanceError("X", "int", "Y"),
MustHaveASmallerValueError("z", "a"),
MustBeLessThanError("A", "B"),
MustBeAtLeastTwoOperandsError("f"),
MustBeAtLeastOneLiteralError("f"),
NameNotValidError("x"),
CannotCreateDuplicateNamedGroupError("x"),
NamedGroupDoesNotExistError("x"),
CannotEndWhileBuildingRootExpressionError(),
CannotCallSubexpressionError("capture"),
UnknownElementTypeError("X"),
NonFusableElementError("X"),
UnexpectedFrameTypeError("X"),
FailedToCompileRegexError("boom"),
]
for error in errors:
text = str(error)
assert text.startswith("error:")
assert "help:" in text
assert "= note:" in text
|