diff options
author | Shugo Maeda <[email protected]> | 2022-02-19 19:10:00 +0900 |
---|---|---|
committer | GitHub <[email protected]> | 2022-02-19 19:10:00 +0900 |
commit | c8817d6a3ebc9bbc151625bca198b8f327d1d68f (patch) | |
tree | 8e147d1ec055f668f123a87fd979946206fd2ee4 /test | |
parent | db6b23c76cbc7888cd9a9912790c2068703afdd0 (diff) |
Add String#byteindex, String#byterindex, and MatchData#byteoffset (#5518)
* Add String#byteindex, String#byterindex, and MatchData#byteoffset [Feature #13110]
Co-authored-by: NARUSE, Yui <[email protected]>
Notes
Notes:
Merged-By: shugo <[email protected]>
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_regexp.rb | 21 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 140 |
2 files changed, 161 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) diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 95fbf63702..0e2a484478 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1340,6 +1340,15 @@ CODE assert_nil($~) assert_equal(2, S("abcdbce").index(/b\Kc/)) + + assert_equal(0, S("こんにちは").index(?こ)) + assert_equal(1, S("こんにちは").index(S("んにち"))) + assert_equal(2, S("こんにちは").index(/にち./)) + + assert_equal(0, S("にんにちは").index(?に, 0)) + assert_equal(2, S("にんにちは").index(?に, 1)) + assert_equal(2, S("にんにちは").index(?に, 2)) + assert_nil(S("にんにちは").index(?に, 3)) end def test_insert @@ -1502,6 +1511,11 @@ CODE assert_nil(S("hello").rindex(S("z"))) assert_nil(S("hello").rindex(/z./)) + assert_equal(5, S("hello").rindex(S(""))) + assert_equal(5, S("hello").rindex(S(""), 5)) + assert_equal(4, S("hello").rindex(S(""), 4)) + assert_equal(0, S("hello").rindex(S(""), 0)) + o = Object.new def o.to_str; "bar"; end assert_equal(6, S("foobarbarbaz").rindex(o)) @@ -1514,6 +1528,24 @@ CODE assert_equal([3, 3], $~.offset(0)) assert_equal(5, S("abcdbce").rindex(/b\Kc/)) + + assert_equal(2, S("こんにちは").rindex(?に)) + assert_equal(6, S("にちは、こんにちは").rindex(S("にちは"))) + assert_equal(6, S("にちは、こんにちは").rindex(/にち./)) + + assert_equal(6, S("にちは、こんにちは").rindex(S("にちは"), 7)) + assert_equal(6, S("にちは、こんにちは").rindex(S("にちは"), -2)) + assert_equal(6, S("にちは、こんにちは").rindex(S("にちは"), 6)) + assert_equal(6, S("にちは、こんにちは").rindex(S("にちは"), -3)) + assert_equal(0, S("にちは、こんにちは").rindex(S("にちは"), 5)) + assert_equal(0, S("にちは、こんにちは").rindex(S("にちは"), -4)) + assert_equal(0, S("にちは、こんにちは").rindex(S("にちは"), 1)) + assert_equal(0, S("にちは、こんにちは").rindex(S("にちは"), 0)) + + assert_equal(0, S("こんにちは").rindex(S("こんにちは"))) + assert_nil(S("こんにち").rindex(S("こんにちは"))) + assert_nil(S("こ").rindex(S("こんにちは"))) + assert_nil(S("").rindex(S("こんにちは"))) end def test_rjust @@ -3254,6 +3286,114 @@ CODE assert_not_predicate(data, :valid_encoding?) assert_predicate(data[100..-1], :valid_encoding?) end + + def test_byteindex + assert_equal(0, S("hello").byteindex(?h)) + assert_equal(1, S("hello").byteindex(S("ell"))) + assert_equal(2, S("hello").byteindex(/ll./)) + + assert_equal(3, S("hello").byteindex(?l, 3)) + assert_equal(3, S("hello").byteindex(S("l"), 3)) + assert_equal(3, S("hello").byteindex(/l./, 3)) + + assert_nil(S("hello").byteindex(?z, 3)) + assert_nil(S("hello").byteindex(S("z"), 3)) + assert_nil(S("hello").byteindex(/z./, 3)) + + assert_nil(S("hello").byteindex(?z)) + assert_nil(S("hello").byteindex(S("z"))) + assert_nil(S("hello").byteindex(/z./)) + + assert_equal(0, S("").byteindex(S(""))) + assert_equal(0, S("").byteindex(//)) + assert_nil(S("").byteindex(S("hello"))) + assert_nil(S("").byteindex(/hello/)) + assert_equal(0, S("hello").byteindex(S(""))) + assert_equal(0, S("hello").byteindex(//)) + + s = S("long") * 1000 << "x" + assert_nil(s.byteindex(S("y"))) + assert_equal(4 * 1000, s.byteindex(S("x"))) + s << "yx" + assert_equal(4 * 1000, s.byteindex(S("x"))) + assert_equal(4 * 1000, s.byteindex(S("xyx"))) + + o = Object.new + def o.to_str; "bar"; end + assert_equal(3, S("foobarbarbaz").byteindex(o)) + assert_raise(TypeError) { S("foo").byteindex(Object.new) } + + assert_nil(S("foo").byteindex(//, -100)) + assert_nil($~) + + assert_equal(2, S("abcdbce").byteindex(/b\Kc/)) + + assert_equal(0, S("こんにちは").byteindex(?こ)) + assert_equal(3, S("こんにちは").byteindex(S("んにち"))) + assert_equal(6, S("こんにちは").byteindex(/にち./)) + + assert_equal(0, S("にんにちは").byteindex(?に, 0)) + assert_raise(IndexError) { S("にんにちは").byteindex(?に, 1) } + assert_raise(IndexError) { S("にんにちは").byteindex(?に, 5) } + assert_equal(6, S("にんにちは").byteindex(?に, 6)) + assert_equal(6, S("にんにちは").byteindex(S("に"), 6)) + assert_equal(6, S("にんにちは").byteindex(/に./, 6)) + assert_raise(IndexError) { S("にんにちは").byteindex(?に, 7) } + end + + def test_byterindex + assert_equal(3, S("hello").byterindex(?l)) + assert_equal(6, S("ell, hello").byterindex(S("ell"))) + assert_equal(7, S("ell, hello").byterindex(/ll./)) + + assert_equal(3, S("hello,lo").byterindex(?l, 3)) + assert_equal(3, S("hello,lo").byterindex(S("l"), 3)) + assert_equal(3, S("hello,lo").byterindex(/l./, 3)) + + assert_nil(S("hello").byterindex(?z, 3)) + assert_nil(S("hello").byterindex(S("z"), 3)) + assert_nil(S("hello").byterindex(/z./, 3)) + + assert_nil(S("hello").byterindex(?z)) + assert_nil(S("hello").byterindex(S("z"))) + assert_nil(S("hello").byterindex(/z./)) + + assert_equal(5, S("hello").byterindex(S(""))) + assert_equal(5, S("hello").byterindex(S(""), 5)) + assert_equal(4, S("hello").byterindex(S(""), 4)) + assert_equal(0, S("hello").byterindex(S(""), 0)) + + o = Object.new + def o.to_str; "bar"; end + assert_equal(6, S("foobarbarbaz").byterindex(o)) + assert_raise(TypeError) { S("foo").byterindex(Object.new) } + + assert_nil(S("foo").byterindex(//, -100)) + assert_nil($~) + + assert_equal(3, S("foo").byterindex(//)) + assert_equal([3, 3], $~.offset(0)) + + assert_equal(5, S("abcdbce").byterindex(/b\Kc/)) + + assert_equal(6, S("こんにちは").byterindex(?に)) + assert_equal(18, S("にちは、こんにちは").byterindex(S("にちは"))) + assert_equal(18, S("にちは、こんにちは").byterindex(/にち./)) + + assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), 19) } + assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), -2) } + assert_equal(18, S("にちは、こんにちは").byterindex(S("にちは"), 18)) + assert_equal(18, S("にちは、こんにちは").byterindex(S("にちは"), -3)) + assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), 17) } + assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), -4) } + assert_raise(IndexError) { S("にちは、こんにちは").byterindex(S("にちは"), 1) } + assert_equal(0, S("にちは、こんにちは").byterindex(S("にちは"), 0)) + + assert_equal(0, S("こんにちは").byterindex(S("こんにちは"))) + assert_nil(S("こんにち").byterindex(S("こんにちは"))) + assert_nil(S("こ").byterindex(S("こんにちは"))) + assert_nil(S("").byterindex(S("こんにちは"))) + end end class TestString2 < TestString |