summaryrefslogtreecommitdiff
path: root/spec/ruby/library/stringscanner/named_captures_spec.rb
blob: f35051b8cb4790f51925ee84c5131d5602e5d584 (plain)
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
require_relative '../../spec_helper'
require 'strscan'

version_is StringScanner::Version, "3.0.5" do # ruby_version_is "3.2"
  describe "StringScanner#named_captures" do
    before do
      @s = StringScanner.new('Fri Dec 12 1975 14:39')
    end

    it "returns a hash of names and matched substrings for named capturing groups in a regular expression of the most recent matching" do
      @s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\d+)/)
      @s.named_captures.should == {"wday" => "Fri", "month" => "Dec", "day" => "12"}
    end

    it "returns {} if there are no named capturing groups" do
      @s.exist?(/(\w+) (\w+) (\d+)/)
      @s.named_captures.should == {}
    end

    # https://github.com/ruby/strscan/issues/132
    ruby_bug "", "3.2"..."3.3" do # fixed in strscan v3.0.7
      it "returns {} if there is no any matching done" do
        @s.named_captures.should == {}
      end
    end

    it "returns nil for an optional named capturing group if it doesn't match" do
      @s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\s+)?/)
      @s.named_captures.should == {"wday" => "Fri", "month" => "Dec", "day" => nil}
    end
  end
end