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
|
# frozen_string_literal: true
require_relative "test_helper"
return if YARP::BACKEND == :FFI
module YARP
class RegexpTest < TestCase
##############################################################################
# These tests test the actual use case of extracting named capture groups
##############################################################################
def test_named_captures_with_arrows
assert_equal(["foo"], named_captures("(?<foo>bar)"))
end
def test_named_captures_with_single_quotes
assert_equal(["foo"], named_captures("(?'foo'bar)"))
end
def test_nested_named_captures_with_arrows
assert_equal(["foo", "bar"], named_captures("(?<foo>(?<bar>baz))"))
end
def test_nested_named_captures_with_single_quotes
assert_equal(["foo", "bar"], named_captures("(?'foo'(?'bar'baz))"))
end
def test_allows_duplicate_named_captures
assert_equal(["foo", "foo"], named_captures("(?<foo>bar)(?<foo>baz)"))
end
def test_named_capture_inside_fake_range_quantifier
assert_equal(["foo"], named_captures("foo{1, (?<foo>2)}"))
end
##############################################################################
# These tests test the rest of the AST. They are not exhaustive, but they
# should cover the most common cases. We test these to make sure we don't
# accidentally regress and stop being able to extract named captures.
##############################################################################
def test_alternation
refute_nil(named_captures("foo|bar"))
end
def test_anchors
refute_nil(named_captures("^foo$"))
end
def test_any
refute_nil(named_captures("."))
end
def test_posix_character_classes
refute_nil(named_captures("[[:digit:]]"))
end
def test_negated_posix_character_classes
refute_nil(named_captures("[[:^digit:]]"))
end
def test_invalid_posix_character_classes_should_fall_back_to_regular_classes
refute_nil(named_captures("[[:foo]]"))
end
def test_character_sets
refute_nil(named_captures("[abc]"))
end
def test_nested_character_sets
refute_nil(named_captures("[[abc]]"))
end
def test_nested_character_sets_with_operators
refute_nil(named_captures("[[abc] && [def]]"))
end
def test_named_capture_inside_nested_character_set
assert_equal([], named_captures("[foo (?<foo>bar)]"))
end
def test_negated_character_sets
refute_nil(named_captures("[^abc]"))
end
def test_character_ranges
refute_nil(named_captures("[a-z]"))
end
def test_negated_character_ranges
refute_nil(named_captures("[^a-z]"))
end
def test_fake_named_captures_inside_character_sets
assert_equal([], named_captures("[a-z(?<foo>)]"))
end
def test_fake_named_capture_inside_character_set_with_escaped_ending
assert_equal([], named_captures("[a-z\\](?<foo>)]"))
end
def test_comments
refute_nil(named_captures("(?#foo)"))
end
def test_comments_with_escaped_parentheses
refute_nil(named_captures("(?#foo\\)\\))"))
end
def test_non_capturing_groups
refute_nil(named_captures("(?:foo)"))
end
def test_positive_lookaheads
refute_nil(named_captures("(?=foo)"))
end
def test_negative_lookaheads
refute_nil(named_captures("(?!foo)"))
end
def test_positive_lookbehinds
refute_nil(named_captures("(?<=foo)"))
end
def test_negative_lookbehinds
refute_nil(named_captures("(?<!foo)"))
end
def test_atomic_groups
refute_nil(named_captures("(?>foo)"))
end
def test_absence_operator
refute_nil(named_captures("(?~foo)"))
end
def test_conditional_expression_with_index
refute_nil(named_captures("(?(1)foo)"))
end
def test_conditional_expression_with_name
refute_nil(named_captures("(?(foo)bar)"))
end
def test_conditional_expression_with_group
refute_nil(named_captures("(?(<foo>)bar)"))
end
def test_options_on_groups
refute_nil(named_captures("(?imxdau:foo)"))
end
def test_options_on_groups_with_invalid_options
assert_nil(named_captures("(?z:bar)"))
end
def test_options_on_groups_getting_turned_off
refute_nil(named_captures("(?-imx:foo)"))
end
def test_options_on_groups_some_getting_turned_on_some_getting_turned_off
refute_nil(named_captures("(?im-x:foo)"))
end
def test_star_quantifier
refute_nil(named_captures("foo*"))
end
def test_plus_quantifier
refute_nil(named_captures("foo+"))
end
def test_question_mark_quantifier
refute_nil(named_captures("foo?"))
end
def test_endless_range_quantifier
refute_nil(named_captures("foo{1,}"))
end
def test_beginless_range_quantifier
refute_nil(named_captures("foo{,1}"))
end
def test_range_quantifier
refute_nil(named_captures("foo{1,2}"))
end
def test_fake_range_quantifier_because_of_spaces
refute_nil(named_captures("foo{1, 2}"))
end
private
def named_captures(source)
Debug.named_captures(source)
end
end
end
|