Skip to content

Commit

Permalink
Add specs for String#bytesplice
Browse files Browse the repository at this point in the history
  • Loading branch information
AI-Mozi committed May 18, 2023
1 parent 0006d19 commit d448851
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions core/string/bytesplice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require_relative '../../spec_helper'

describe "String#bytesplice" do
ruby_version_is "3.2" do
it "accepts index, length and replacement string as arguments and returns replaced string" do
"string".bytesplice(0, 3, "xxx").should == "xxxing"
"string".bytesplice(-3, 3, "xxx").should == "strxxx"
"string".bytesplice(-6, 0, "xxx").should == "xxxstring"
"string".bytesplice(3, 0, "xxx").should == "strxxxing"
end

it "accepts range and replacement string as arguments and returns replaced string" do
"string".bytesplice(0...3, "xxx").should == "xxxing"
"string".bytesplice(-3...3, "xxx").should == "strxxxing"
"string".bytesplice(-6...0, "xxx").should == "xxxstring"
"string".bytesplice(3..6, "xxx").should == "strxxx"
end

it "replaces whole string with replacement string" do
"".bytesplice(0, 0, "xxx").should == "xxx"
"".bytesplice(0, 0, "").should == ""
end

it "adjust string length if is not the same as replacement string" do
"string".bytesplice(0..-1, "xxx").should == "xxx"
"string".bytesplice(0, 6, "xxx").should == "xxx"
end

it "raises IndexError if argument is out of range" do
-> {
"string".bytesplice(-7, 0, "xxx")
}.should raise_error(IndexError, "index -7 out of string")
end

it "raises RangeError if argument is out of range" do
-> {
"string".bytesplice(-7...-7, "xxx")
}.should raise_error(RangeError, "-7...-7 out of range")
end

it "raises IndexError if offset does not land on character boundary" do
-> {
"こんにちは".bytesplice(1, 0, "xxx")
}.should raise_error(IndexError, "offset 1 does not land on character boundary")
end

it "raises TypeError if argument is an Integer instead of Range" do
-> {
"string".bytesplice(0, "xxx")
}.should raise_error(TypeError, "wrong argument type Integer (expected Range)")
end
end
end

0 comments on commit d448851

Please sign in to comment.