File: byteoffset_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (95 lines) | stat: -rw-r--r-- 2,896 bytes parent folder | download
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require_relative '../../spec_helper'

describe "MatchData#byteoffset" do
  ruby_version_is "3.2" do
    it "returns beginning and ending byte-based offset of whole matched substring for 0 element" do
      m = /(.)(.)(\d+)(\d)/.match("THX1138.")
      m.byteoffset(0).should == [1, 7]
    end

    it "returns beginning and ending byte-based offset of n-th match, all the subsequent elements are capturing groups" do
      m = /(.)(.)(\d+)(\d)/.match("THX1138.")

      m.byteoffset(2).should == [2, 3]
      m.byteoffset(3).should == [3, 6]
      m.byteoffset(4).should == [6, 7]
    end

    it "accepts String as a reference to a named capture" do
      m = /(?<f>foo)(?<b>bar)/.match("foobar")

      m.byteoffset("f").should == [0, 3]
      m.byteoffset("b").should == [3, 6]
    end

    it "accepts Symbol as a reference to a named capture" do
      m = /(?<f>foo)(?<b>bar)/.match("foobar")

      m.byteoffset(:f).should == [0, 3]
      m.byteoffset(:b).should == [3, 6]
    end

    it "returns [nil, nil] if a capturing group is optional and doesn't match" do
      m = /(?<x>q..)?/.match("foobarbaz")

      m.byteoffset("x").should == [nil, nil]
      m.byteoffset(1).should == [nil, nil]
    end

    it "returns correct beginning and ending byte-based offset for multi-byte strings" do
      m = /\A\u3042(.)(.)?(.)\z/.match("\u3042\u3043\u3044")

      m.byteoffset(1).should == [3, 6]
      m.byteoffset(3).should == [6, 9]
    end

    it "returns [nil, nil] if a capturing group is optional and doesn't match for multi-byte string" do
      m = /\A\u3042(.)(.)?(.)\z/.match("\u3042\u3043\u3044")

      m.byteoffset(2).should == [nil, nil]
    end

    it "converts argument into integer if is not String nor Symbol" do
      m = /(?<f>foo)(?<b>bar)/.match("foobar")

      obj = Object.new
      def obj.to_int; 2; end

      m.byteoffset(1r).should == [0, 3]
      m.byteoffset(1.1).should == [0, 3]
      m.byteoffset(obj).should == [3, 6]
    end

    it "raises IndexError if there is no group with provided name" do
      m = /(?<f>foo)(?<b>bar)/.match("foobar")

      -> {
        m.byteoffset("y")
      }.should raise_error(IndexError, "undefined group name reference: y")

      -> {
        m.byteoffset(:y)
      }.should raise_error(IndexError, "undefined group name reference: y")
    end

    it "raises IndexError if index is out of matches" do
      m = /(?<f>foo)(?<b>bar)/.match("foobar")

      -> {
        m.byteoffset(-1)
      }.should raise_error(IndexError, "index -1 out of matches")

      -> {
        m.byteoffset(3)
      }.should raise_error(IndexError, "index 3 out of matches")
    end

    it "raises TypeError if can't convert argument into Integer" do
      m = /(?<f>foo)(?<b>bar)/.match("foobar")

      -> {
        m.byteoffset([])
      }.should raise_error(TypeError, "no implicit conversion of Array into Integer")
    end
  end
end