|
2 | 2 |
|
3 | 3 | require_relative "test_helper" |
4 | 4 |
|
5 | | -return if Prism::BACKEND == :FFI |
6 | | - |
7 | 5 | module Prism |
8 | 6 | class RegexpTest < TestCase |
9 | | - ############################################################################## |
| 7 | + ############################################################################ |
10 | 8 | # These tests test the actual use case of extracting named capture groups |
11 | | - ############################################################################## |
| 9 | + ############################################################################ |
12 | 10 |
|
13 | 11 | def test_named_captures_with_arrows |
14 | | - assert_equal(["foo"], named_captures("(?<foo>bar)")) |
| 12 | + assert_equal([:foo], named_captures("(?<foo>bar)")) |
15 | 13 | end |
16 | 14 |
|
17 | 15 | def test_named_captures_with_single_quotes |
18 | | - assert_equal(["foo"], named_captures("(?'foo'bar)")) |
| 16 | + assert_equal([:foo], named_captures("(?'foo'bar)")) |
19 | 17 | end |
20 | 18 |
|
21 | 19 | def test_nested_named_captures_with_arrows |
22 | | - assert_equal(["foo", "bar"], named_captures("(?<foo>(?<bar>baz))")) |
| 20 | + assert_equal([:foo, :bar], named_captures("(?<foo>(?<bar>baz))")) |
23 | 21 | end |
24 | 22 |
|
25 | 23 | def test_nested_named_captures_with_single_quotes |
26 | | - assert_equal(["foo", "bar"], named_captures("(?'foo'(?'bar'baz))")) |
| 24 | + assert_equal([:foo, :bar], named_captures("(?'foo'(?'bar'baz))")) |
27 | 25 | end |
28 | 26 |
|
29 | 27 | def test_allows_duplicate_named_captures |
30 | | - assert_equal(["foo", "foo"], named_captures("(?<foo>bar)(?<foo>baz)")) |
| 28 | + assert_equal([:foo], named_captures("(?<foo>bar)(?<foo>baz)")) |
31 | 29 | end |
32 | 30 |
|
33 | 31 | def test_named_capture_inside_fake_range_quantifier |
34 | | - assert_equal(["foo"], named_captures("foo{1, (?<foo>2)}")) |
| 32 | + assert_equal([:foo], named_captures("foo{1, (?<foo>2)}")) |
| 33 | + end |
| 34 | + |
| 35 | + def test_fake_named_captures_inside_character_sets |
| 36 | + assert_equal([], named_captures("[a-z(?<foo>)]")) |
35 | 37 | end |
36 | 38 |
|
37 | | - ############################################################################## |
| 39 | + def test_fake_named_capture_inside_character_set_with_escaped_ending |
| 40 | + assert_equal([], named_captures("[a-z\\](?<foo>)]")) |
| 41 | + end |
| 42 | + |
| 43 | + ############################################################################ |
38 | 44 | # These tests test the rest of the AST. They are not exhaustive, but they |
39 | 45 | # should cover the most common cases. We test these to make sure we don't |
40 | 46 | # accidentally regress and stop being able to extract named captures. |
41 | | - ############################################################################## |
| 47 | + ############################################################################ |
42 | 48 |
|
43 | 49 | def test_alternation |
44 | | - refute_nil(named_captures("foo|bar")) |
| 50 | + assert_valid_regexp("foo|bar") |
45 | 51 | end |
46 | 52 |
|
47 | 53 | def test_anchors |
48 | | - refute_nil(named_captures("^foo$")) |
| 54 | + assert_valid_regexp("^foo$") |
49 | 55 | end |
50 | 56 |
|
51 | 57 | def test_any |
52 | | - refute_nil(named_captures(".")) |
| 58 | + assert_valid_regexp(".") |
53 | 59 | end |
54 | 60 |
|
55 | 61 | def test_posix_character_classes |
56 | | - refute_nil(named_captures("[[:digit:]]")) |
| 62 | + assert_valid_regexp("[[:digit:]]") |
57 | 63 | end |
58 | 64 |
|
59 | 65 | def test_negated_posix_character_classes |
60 | | - refute_nil(named_captures("[[:^digit:]]")) |
| 66 | + assert_valid_regexp("[[:^digit:]]") |
61 | 67 | end |
62 | 68 |
|
63 | 69 | def test_invalid_posix_character_classes_should_fall_back_to_regular_classes |
64 | | - refute_nil(named_captures("[[:foo]]")) |
| 70 | + assert_valid_regexp("[[:foo]]") |
65 | 71 | end |
66 | 72 |
|
67 | 73 | def test_character_sets |
68 | | - refute_nil(named_captures("[abc]")) |
| 74 | + assert_valid_regexp("[abc]") |
69 | 75 | end |
70 | 76 |
|
71 | 77 | def test_nested_character_sets |
72 | | - refute_nil(named_captures("[[abc]]")) |
| 78 | + assert_valid_regexp("[[abc]]") |
73 | 79 | end |
74 | 80 |
|
75 | 81 | def test_nested_character_sets_with_operators |
76 | | - refute_nil(named_captures("[[abc] && [def]]")) |
| 82 | + assert_valid_regexp("[[abc] && [def]]") |
77 | 83 | end |
78 | 84 |
|
79 | 85 | def test_named_capture_inside_nested_character_set |
80 | 86 | assert_equal([], named_captures("[foo (?<foo>bar)]")) |
81 | 87 | end |
82 | 88 |
|
83 | 89 | def test_negated_character_sets |
84 | | - refute_nil(named_captures("[^abc]")) |
| 90 | + assert_valid_regexp("[^abc]") |
85 | 91 | end |
86 | 92 |
|
87 | 93 | def test_character_ranges |
88 | | - refute_nil(named_captures("[a-z]")) |
| 94 | + assert_valid_regexp("[a-z]") |
89 | 95 | end |
90 | 96 |
|
91 | 97 | def test_negated_character_ranges |
92 | | - refute_nil(named_captures("[^a-z]")) |
93 | | - end |
94 | | - |
95 | | - def test_fake_named_captures_inside_character_sets |
96 | | - assert_equal([], named_captures("[a-z(?<foo>)]")) |
97 | | - end |
98 | | - |
99 | | - def test_fake_named_capture_inside_character_set_with_escaped_ending |
100 | | - assert_equal([], named_captures("[a-z\\](?<foo>)]")) |
| 98 | + assert_valid_regexp("[^a-z]") |
101 | 99 | end |
102 | 100 |
|
103 | 101 | def test_comments |
104 | | - refute_nil(named_captures("(?#foo)")) |
| 102 | + assert_valid_regexp("(?#foo)") |
105 | 103 | end |
106 | 104 |
|
107 | 105 | def test_comments_with_escaped_parentheses |
108 | | - refute_nil(named_captures("(?#foo\\)\\))")) |
| 106 | + assert_valid_regexp("(?#foo\\)\\))") |
109 | 107 | end |
110 | 108 |
|
111 | 109 | def test_non_capturing_groups |
112 | | - refute_nil(named_captures("(?:foo)")) |
| 110 | + assert_valid_regexp("(?:foo)") |
113 | 111 | end |
114 | 112 |
|
115 | 113 | def test_positive_lookaheads |
116 | | - refute_nil(named_captures("(?=foo)")) |
| 114 | + assert_valid_regexp("(?=foo)") |
117 | 115 | end |
118 | 116 |
|
119 | 117 | def test_negative_lookaheads |
120 | | - refute_nil(named_captures("(?!foo)")) |
| 118 | + assert_valid_regexp("(?!foo)") |
121 | 119 | end |
122 | 120 |
|
123 | 121 | def test_positive_lookbehinds |
124 | | - refute_nil(named_captures("(?<=foo)")) |
| 122 | + assert_valid_regexp("(?<=foo)") |
125 | 123 | end |
126 | 124 |
|
127 | 125 | def test_negative_lookbehinds |
128 | | - refute_nil(named_captures("(?<!foo)")) |
| 126 | + assert_valid_regexp("(?<!foo)") |
129 | 127 | end |
130 | 128 |
|
131 | 129 | def test_atomic_groups |
132 | | - refute_nil(named_captures("(?>foo)")) |
| 130 | + assert_valid_regexp("(?>foo)") |
133 | 131 | end |
134 | 132 |
|
135 | 133 | def test_absence_operator |
136 | | - refute_nil(named_captures("(?~foo)")) |
| 134 | + assert_valid_regexp("(?~foo)") |
137 | 135 | end |
138 | 136 |
|
139 | 137 | def test_conditional_expression_with_index |
140 | | - refute_nil(named_captures("(?(1)foo)")) |
| 138 | + assert_valid_regexp("(?(1)foo)") |
141 | 139 | end |
142 | 140 |
|
143 | 141 | def test_conditional_expression_with_name |
144 | | - refute_nil(named_captures("(?(foo)bar)")) |
| 142 | + assert_valid_regexp("(?(foo)bar)") |
145 | 143 | end |
146 | 144 |
|
147 | 145 | def test_conditional_expression_with_group |
148 | | - refute_nil(named_captures("(?(<foo>)bar)")) |
| 146 | + assert_valid_regexp("(?(<foo>)bar)") |
149 | 147 | end |
150 | 148 |
|
151 | 149 | def test_options_on_groups |
152 | | - refute_nil(named_captures("(?imxdau:foo)")) |
153 | | - end |
154 | | - |
155 | | - def test_options_on_groups_with_invalid_options |
156 | | - assert_nil(named_captures("(?z:bar)")) |
| 150 | + assert_valid_regexp("(?imxdau:foo)") |
157 | 151 | end |
158 | 152 |
|
159 | 153 | def test_options_on_groups_getting_turned_off |
160 | | - refute_nil(named_captures("(?-imx:foo)")) |
| 154 | + assert_valid_regexp("(?-imx:foo)") |
161 | 155 | end |
162 | 156 |
|
163 | 157 | def test_options_on_groups_some_getting_turned_on_some_getting_turned_off |
164 | | - refute_nil(named_captures("(?im-x:foo)")) |
| 158 | + assert_valid_regexp("(?im-x:foo)") |
165 | 159 | end |
166 | 160 |
|
167 | 161 | def test_star_quantifier |
168 | | - refute_nil(named_captures("foo*")) |
| 162 | + assert_valid_regexp("foo*") |
169 | 163 | end |
170 | 164 |
|
171 | 165 | def test_plus_quantifier |
172 | | - refute_nil(named_captures("foo+")) |
| 166 | + assert_valid_regexp("foo+") |
173 | 167 | end |
174 | 168 |
|
175 | 169 | def test_question_mark_quantifier |
176 | | - refute_nil(named_captures("foo?")) |
| 170 | + assert_valid_regexp("foo?") |
177 | 171 | end |
178 | 172 |
|
179 | 173 | def test_endless_range_quantifier |
180 | | - refute_nil(named_captures("foo{1,}")) |
| 174 | + assert_valid_regexp("foo{1,}") |
181 | 175 | end |
182 | 176 |
|
183 | 177 | def test_beginless_range_quantifier |
184 | | - refute_nil(named_captures("foo{,1}")) |
| 178 | + assert_valid_regexp("foo{,1}") |
185 | 179 | end |
186 | 180 |
|
187 | 181 | def test_range_quantifier |
188 | | - refute_nil(named_captures("foo{1,2}")) |
| 182 | + assert_valid_regexp("foo{1,2}") |
189 | 183 | end |
190 | 184 |
|
191 | 185 | def test_fake_range_quantifier_because_of_spaces |
192 | | - refute_nil(named_captures("foo{1, 2}")) |
| 186 | + assert_valid_regexp("foo{1, 2}") |
193 | 187 | end |
194 | 188 |
|
195 | | - ############################################################################## |
| 189 | + ############################################################################ |
196 | 190 | # These test that flag values are correct. |
197 | | - ############################################################################## |
| 191 | + ############################################################################ |
198 | 192 |
|
199 | 193 | def test_flag_ignorecase |
200 | 194 | assert_equal(Regexp::IGNORECASE, options("i")) |
@@ -241,8 +235,12 @@ def test_last_encoding_option_wins |
241 | 235 |
|
242 | 236 | private |
243 | 237 |
|
| 238 | + def assert_valid_regexp(source) |
| 239 | + assert Prism.parse_success?("/#{source}/ =~ \"\"") |
| 240 | + end |
| 241 | + |
244 | 242 | def named_captures(source) |
245 | | - Debug.named_captures(source) |
| 243 | + Prism.parse("/#{source}/ =~ \"\"").value.locals |
246 | 244 | end |
247 | 245 |
|
248 | 246 | def options(flags) |
|
0 commit comments