-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |