aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_regexp.rb
diff options
context:
space:
mode:
authorShugo Maeda <shugo@ruby-lang.org>2022-02-19 19:10:00 +0900
committerGitHub <noreply@github.com>2022-02-19 19:10:00 +0900
commitc8817d6a3ebc9bbc151625bca198b8f327d1d68f (patch)
tree8e147d1ec055f668f123a87fd979946206fd2ee4 /test/ruby/test_regexp.rb
parentdb6b23c76cbc7888cd9a9912790c2068703afdd0 (diff)
downloadruby-c8817d6a3ebc9bbc151625bca198b8f327d1d68f.tar.gz
Add String#byteindex, String#byterindex, and MatchData#byteoffset (#5518)
* Add String#byteindex, String#byterindex, and MatchData#byteoffset [Feature #13110] Co-authored-by: NARUSE, Yui <naruse@airemix.jp>
Diffstat (limited to 'test/ruby/test_regexp.rb')
-rw-r--r--test/ruby/test_regexp.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index 2bf4649f14..80caa879e4 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -424,6 +424,27 @@ class TestRegexp < Test::Unit::TestCase
assert_equal([2, 3], m.offset(3))
end
+ def test_match_byteoffset_begin_end
+ m = /(?<x>b..)/.match("foobarbaz")
+ assert_equal([3, 6], m.byteoffset("x"))
+ assert_equal(3, m.begin("x"))
+ assert_equal(6, m.end("x"))
+ assert_raise(IndexError) { m.byteoffset("y") }
+ assert_raise(IndexError) { m.byteoffset(2) }
+ assert_raise(IndexError) { m.begin(2) }
+ assert_raise(IndexError) { m.end(2) }
+
+ m = /(?<x>q..)?/.match("foobarbaz")
+ assert_equal([nil, nil], m.byteoffset("x"))
+ assert_equal(nil, m.begin("x"))
+ assert_equal(nil, m.end("x"))
+
+ m = /\A\u3042(.)(.)?(.)\z/.match("\u3042\u3043\u3044")
+ assert_equal([3, 6], m.byteoffset(1))
+ assert_equal([nil, nil], m.byteoffset(2))
+ assert_equal([6, 9], m.byteoffset(3))
+ end
+
def test_match_to_s
m = /(?<x>b..)/.match("foobarbaz")
assert_equal("bar", m.to_s)