summaryrefslogtreecommitdiff
path: root/test/ruby/test_string.rb
diff options
context:
space:
mode:
authorShugo Maeda <shugo@ruby-lang.org>2022-02-22 10:41:56 +0900
committerShugo Maeda <shugo@ruby-lang.org>2022-03-18 11:51:03 +0900
commit1107839a7fed31339fc947995b7b45b8eaf4041b (patch)
treed96cac32a605f259f5abfe5f70ec35086042da84 /test/ruby/test_string.rb
parentb8e72bd2e96c1ff9b25bea848abdfd0494c40b19 (diff)
Add String#bytesplice
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5584
Diffstat (limited to 'test/ruby/test_string.rb')
-rw-r--r--test/ruby/test_string.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 1d0a68038a..d96e8dff22 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -3395,6 +3395,50 @@ CODE
assert_nil(S("こ").byterindex(S("こんにちは")))
assert_nil(S("").byterindex(S("こんにちは")))
end
+
+ def test_bytesplice
+ assert_bytesplice_raise(IndexError, S("hello"), -6, 0, "xxx")
+ assert_bytesplice_result("xxxhello", S("hello"), -5, 0, "xxx")
+ assert_bytesplice_result("xxxhello", S("hello"), 0, 0, "xxx")
+ assert_bytesplice_result("xxxello", S("hello"), 0, 1, "xxx")
+ assert_bytesplice_result("xxx", S("hello"), 0, 5, "xxx")
+ assert_bytesplice_result("xxx", S("hello"), 0, 6, "xxx")
+
+ assert_bytesplice_raise(RangeError, S("hello"), -6...-6, "xxx")
+ assert_bytesplice_result("xxxhello", S("hello"), -5...-5, "xxx")
+ assert_bytesplice_result("xxxhello", S("hello"), 0...0, "xxx")
+ assert_bytesplice_result("xxxello", S("hello"), 0..0, "xxx")
+ assert_bytesplice_result("xxxello", S("hello"), 0...1, "xxx")
+ assert_bytesplice_result("xxxllo", S("hello"), 0..1, "xxx")
+ assert_bytesplice_result("xxx", S("hello"), 0..-1, "xxx")
+ assert_bytesplice_result("xxx", S("hello"), 0...5, "xxx")
+ assert_bytesplice_result("xxx", S("hello"), 0...6, "xxx")
+
+ assert_bytesplice_raise(TypeError, S("hello"), 0, "xxx")
+
+ assert_bytesplice_raise(IndexError, S("こんにちは"), -16, 0, "xxx")
+ assert_bytesplice_result("xxxこんにちは", S("こんにちは"), -15, 0, "xxx")
+ assert_bytesplice_result("xxxこんにちは", S("こんにちは"), 0, 0, "xxx")
+ assert_bytesplice_raise(IndexError, S("こんにちは"), 1, 0, "xxx")
+ assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 1, "xxx")
+ assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 2, "xxx")
+ assert_bytesplice_result("xxxんにちは", S("こんにちは"), 0, 3, "xxx")
+ assert_bytesplice_result("こんにちはxxx", S("こんにちは"), 15, 0, "xxx")
+
+ assert_bytesplice_result("", S(""), 0, 0, "")
+ assert_bytesplice_result("xxx", S(""), 0, 0, "xxx")
+ end
+
+ private
+
+ def assert_bytesplice_result(expected, s, *args)
+ assert_equal(args.last, s.send(:bytesplice, *args))
+ assert_equal(expected, s)
+ end
+
+ def assert_bytesplice_raise(e, s, *args)
+ assert_raise(e) { s.send(:bytesplice, *args) }
+ end
end
class TestString2 < TestString