|
32 | 32 | b # there is no capture group on this line (not even here)
|
33 | 33 | $/x.match("ab").to_a.should == [ "ab", "a" ]
|
34 | 34 | 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 |
35 | 63 | end
|
0 commit comments