Skip to content

Commit da45ef7

Browse files
committed
Add spec for a complex Regexp with comments
* See #2412
1 parent 0c3745e commit da45ef7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

spec/ruby/language/regexp/grouping_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,32 @@
3232
b # there is no capture group on this line (not even here)
3333
$/x.match("ab").to_a.should == [ "ab", "a" ]
3434
end
35+
36+
it "does not consider # inside a character class as a comment" do
37+
# From https://github.com/rubocop/rubocop/blob/39fcf1c568/lib/rubocop/cop/utils/format_string.rb#L18
38+
regexp = /
39+
% (?<type>%) # line comment
40+
| % (?<flags>(?-mix:[ #0+-]|(?-mix:(\d+)\$))*) (?#group comment)
41+
(?:
42+
(?: (?-mix:(?<width>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))? (?-mix:\.(?<precision>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))? (?-mix:<(?<name>\w+)>)?
43+
| (?-mix:(?<width>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))? (?-mix:<(?<name>\w+)>) (?-mix:\.(?<precision>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))?
44+
| (?-mix:<(?<name>\w+)>) (?<more_flags>(?-mix:[ #0+-]|(?-mix:(\d+)\$))*) (?-mix:(?<width>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))? (?-mix:\.(?<precision>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))?
45+
) (?-mix:(?<type>[bBdiouxXeEfgGaAcps]))
46+
| (?-mix:(?<width>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))? (?-mix:\.(?<precision>(?-mix:\d+|(?-mix:\*(?-mix:(\d+)\$)?))))? (?-mix:\{(?<name>\w+)\})
47+
)
48+
/x
49+
regexp.named_captures.should == {
50+
"type" => [1, 13],
51+
"flags" => [2],
52+
"width" => [3, 6, 11, 14],
53+
"precision" => [4, 8, 12, 15],
54+
"name" => [5, 7, 9, 16],
55+
"more_flags" => [10]
56+
}
57+
match = regexp.match("%6.3f")
58+
match[:width].should == '6'
59+
match[:precision].should == '3'
60+
match[:type].should == 'f'
61+
match.to_a.should == [ "%6.3f", nil, "", "6", "3"] + [nil] * 8 + ["f"] + [nil] * 3
62+
end
3563
end

0 commit comments

Comments
 (0)