Skip to content

Commit 5050dfb

Browse files
committed
Remove Debug#named_captures
1 parent 75fabf7 commit 5050dfb

File tree

2 files changed

+59
-89
lines changed

2 files changed

+59
-89
lines changed

ext/prism/extension.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,33 +1093,6 @@ parse_file_failure_p(int argc, VALUE *argv, VALUE self) {
10931093
/* Utility functions exposed to make testing easier */
10941094
/******************************************************************************/
10951095

1096-
/**
1097-
* call-seq:
1098-
* Debug::named_captures(source) -> Array
1099-
*
1100-
* Returns an array of strings corresponding to the named capture groups in the
1101-
* given source string. If prism was unable to parse the regular expression,
1102-
* this function returns nil.
1103-
*/
1104-
static VALUE
1105-
named_captures(VALUE self, VALUE source) {
1106-
pm_string_list_t string_list = { 0 };
1107-
1108-
if (!pm_regexp_named_capture_group_names((const uint8_t *) RSTRING_PTR(source), RSTRING_LEN(source), &string_list, false, PM_ENCODING_UTF_8_ENTRY)) {
1109-
pm_string_list_free(&string_list);
1110-
return Qnil;
1111-
}
1112-
1113-
VALUE names = rb_ary_new();
1114-
for (size_t index = 0; index < string_list.length; index++) {
1115-
const pm_string_t *string = &string_list.strings[index];
1116-
rb_ary_push(names, rb_str_new((const char *) pm_string_source(string), pm_string_length(string)));
1117-
}
1118-
1119-
pm_string_list_free(&string_list);
1120-
return names;
1121-
}
1122-
11231096
#ifndef PRISM_EXCLUDE_PRETTYPRINT
11241097

11251098
/**
@@ -1336,7 +1309,6 @@ Init_prism(void) {
13361309
// Next, the functions that will be called by the parser to perform various
13371310
// internal tasks. We expose these to make them easier to test.
13381311
VALUE rb_cPrismDebug = rb_define_module_under(rb_cPrism, "Debug");
1339-
rb_define_singleton_method(rb_cPrismDebug, "named_captures", named_captures, 1);
13401312
rb_define_singleton_method(rb_cPrismDebug, "format_errors", format_errors, 2);
13411313

13421314
#ifndef PRISM_EXCLUDE_PRETTYPRINT

test/prism/regexp_test.rb

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,199 +2,193 @@
22

33
require_relative "test_helper"
44

5-
return if Prism::BACKEND == :FFI
6-
75
module Prism
86
class RegexpTest < TestCase
9-
##############################################################################
7+
############################################################################
108
# These tests test the actual use case of extracting named capture groups
11-
##############################################################################
9+
############################################################################
1210

1311
def test_named_captures_with_arrows
14-
assert_equal(["foo"], named_captures("(?<foo>bar)"))
12+
assert_equal([:foo], named_captures("(?<foo>bar)"))
1513
end
1614

1715
def test_named_captures_with_single_quotes
18-
assert_equal(["foo"], named_captures("(?'foo'bar)"))
16+
assert_equal([:foo], named_captures("(?'foo'bar)"))
1917
end
2018

2119
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))"))
2321
end
2422

2523
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))"))
2725
end
2826

2927
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)"))
3129
end
3230

3331
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>)]"))
3537
end
3638

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+
############################################################################
3844
# These tests test the rest of the AST. They are not exhaustive, but they
3945
# should cover the most common cases. We test these to make sure we don't
4046
# accidentally regress and stop being able to extract named captures.
41-
##############################################################################
47+
############################################################################
4248

4349
def test_alternation
44-
refute_nil(named_captures("foo|bar"))
50+
assert_valid_regexp("foo|bar")
4551
end
4652

4753
def test_anchors
48-
refute_nil(named_captures("^foo$"))
54+
assert_valid_regexp("^foo$")
4955
end
5056

5157
def test_any
52-
refute_nil(named_captures("."))
58+
assert_valid_regexp(".")
5359
end
5460

5561
def test_posix_character_classes
56-
refute_nil(named_captures("[[:digit:]]"))
62+
assert_valid_regexp("[[:digit:]]")
5763
end
5864

5965
def test_negated_posix_character_classes
60-
refute_nil(named_captures("[[:^digit:]]"))
66+
assert_valid_regexp("[[:^digit:]]")
6167
end
6268

6369
def test_invalid_posix_character_classes_should_fall_back_to_regular_classes
64-
refute_nil(named_captures("[[:foo]]"))
70+
assert_valid_regexp("[[:foo]]")
6571
end
6672

6773
def test_character_sets
68-
refute_nil(named_captures("[abc]"))
74+
assert_valid_regexp("[abc]")
6975
end
7076

7177
def test_nested_character_sets
72-
refute_nil(named_captures("[[abc]]"))
78+
assert_valid_regexp("[[abc]]")
7379
end
7480

7581
def test_nested_character_sets_with_operators
76-
refute_nil(named_captures("[[abc] && [def]]"))
82+
assert_valid_regexp("[[abc] && [def]]")
7783
end
7884

7985
def test_named_capture_inside_nested_character_set
8086
assert_equal([], named_captures("[foo (?<foo>bar)]"))
8187
end
8288

8389
def test_negated_character_sets
84-
refute_nil(named_captures("[^abc]"))
90+
assert_valid_regexp("[^abc]")
8591
end
8692

8793
def test_character_ranges
88-
refute_nil(named_captures("[a-z]"))
94+
assert_valid_regexp("[a-z]")
8995
end
9096

9197
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]")
10199
end
102100

103101
def test_comments
104-
refute_nil(named_captures("(?#foo)"))
102+
assert_valid_regexp("(?#foo)")
105103
end
106104

107105
def test_comments_with_escaped_parentheses
108-
refute_nil(named_captures("(?#foo\\)\\))"))
106+
assert_valid_regexp("(?#foo\\)\\))")
109107
end
110108

111109
def test_non_capturing_groups
112-
refute_nil(named_captures("(?:foo)"))
110+
assert_valid_regexp("(?:foo)")
113111
end
114112

115113
def test_positive_lookaheads
116-
refute_nil(named_captures("(?=foo)"))
114+
assert_valid_regexp("(?=foo)")
117115
end
118116

119117
def test_negative_lookaheads
120-
refute_nil(named_captures("(?!foo)"))
118+
assert_valid_regexp("(?!foo)")
121119
end
122120

123121
def test_positive_lookbehinds
124-
refute_nil(named_captures("(?<=foo)"))
122+
assert_valid_regexp("(?<=foo)")
125123
end
126124

127125
def test_negative_lookbehinds
128-
refute_nil(named_captures("(?<!foo)"))
126+
assert_valid_regexp("(?<!foo)")
129127
end
130128

131129
def test_atomic_groups
132-
refute_nil(named_captures("(?>foo)"))
130+
assert_valid_regexp("(?>foo)")
133131
end
134132

135133
def test_absence_operator
136-
refute_nil(named_captures("(?~foo)"))
134+
assert_valid_regexp("(?~foo)")
137135
end
138136

139137
def test_conditional_expression_with_index
140-
refute_nil(named_captures("(?(1)foo)"))
138+
assert_valid_regexp("(?(1)foo)")
141139
end
142140

143141
def test_conditional_expression_with_name
144-
refute_nil(named_captures("(?(foo)bar)"))
142+
assert_valid_regexp("(?(foo)bar)")
145143
end
146144

147145
def test_conditional_expression_with_group
148-
refute_nil(named_captures("(?(<foo>)bar)"))
146+
assert_valid_regexp("(?(<foo>)bar)")
149147
end
150148

151149
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)")
157151
end
158152

159153
def test_options_on_groups_getting_turned_off
160-
refute_nil(named_captures("(?-imx:foo)"))
154+
assert_valid_regexp("(?-imx:foo)")
161155
end
162156

163157
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)")
165159
end
166160

167161
def test_star_quantifier
168-
refute_nil(named_captures("foo*"))
162+
assert_valid_regexp("foo*")
169163
end
170164

171165
def test_plus_quantifier
172-
refute_nil(named_captures("foo+"))
166+
assert_valid_regexp("foo+")
173167
end
174168

175169
def test_question_mark_quantifier
176-
refute_nil(named_captures("foo?"))
170+
assert_valid_regexp("foo?")
177171
end
178172

179173
def test_endless_range_quantifier
180-
refute_nil(named_captures("foo{1,}"))
174+
assert_valid_regexp("foo{1,}")
181175
end
182176

183177
def test_beginless_range_quantifier
184-
refute_nil(named_captures("foo{,1}"))
178+
assert_valid_regexp("foo{,1}")
185179
end
186180

187181
def test_range_quantifier
188-
refute_nil(named_captures("foo{1,2}"))
182+
assert_valid_regexp("foo{1,2}")
189183
end
190184

191185
def test_fake_range_quantifier_because_of_spaces
192-
refute_nil(named_captures("foo{1, 2}"))
186+
assert_valid_regexp("foo{1, 2}")
193187
end
194188

195-
##############################################################################
189+
############################################################################
196190
# These test that flag values are correct.
197-
##############################################################################
191+
############################################################################
198192

199193
def test_flag_ignorecase
200194
assert_equal(Regexp::IGNORECASE, options("i"))
@@ -241,8 +235,12 @@ def test_last_encoding_option_wins
241235

242236
private
243237

238+
def assert_valid_regexp(source)
239+
assert Prism.parse_success?("/#{source}/ =~ \"\"")
240+
end
241+
244242
def named_captures(source)
245-
Debug.named_captures(source)
243+
Prism.parse("/#{source}/ =~ \"\"").value.locals
246244
end
247245

248246
def options(flags)

0 commit comments

Comments
 (0)