|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | +require_relative '../../spec_helper' |
| 3 | + |
| 4 | +describe "String#bytesplice" do |
| 5 | + ruby_version_is "3.2" do |
| 6 | + it "raises IndexError when index is out of boundary" do |
| 7 | + -> { "hello".bytesplice(-6, 0, "xxx") }.should raise_error(IndexError) |
| 8 | + end |
| 9 | + |
| 10 | + it "replaces with integer indices" do |
| 11 | + "hello".bytesplice(-5, 0, "xxx").should == "xxxhello" |
| 12 | + "hello".bytesplice(0, 0, "xxx").should == "xxxhello" |
| 13 | + "hello".bytesplice(0, 1, "xxx").should == "xxxello" |
| 14 | + "hello".bytesplice(0, 5, "xxx").should == "xxx" |
| 15 | + "hello".bytesplice(0, 6, "xxx").should == "xxx" |
| 16 | + end |
| 17 | + |
| 18 | + it "raises RangeError when range is out of boundary" do |
| 19 | + -> { "hello".bytesplice(-6...-6, "xxx") }.should raise_error(RangeError) |
| 20 | + end |
| 21 | + |
| 22 | + it "replaces with ranges" do |
| 23 | + "hello".bytesplice(-5...-5, "xxx").should == "xxxhello" |
| 24 | + "hello".bytesplice(0...0, "xxx").should == "xxxhello" |
| 25 | + "hello".bytesplice(0..0, "xxx").should == "xxxello" |
| 26 | + "hello".bytesplice(0...1, "xxx").should == "xxxello" |
| 27 | + "hello".bytesplice(0..1, "xxx").should == "xxxllo" |
| 28 | + "hello".bytesplice(0..-1, "xxx").should == "xxx" |
| 29 | + "hello".bytesplice(0...5, "xxx").should == "xxx" |
| 30 | + "hello".bytesplice(0...6, "xxx").should == "xxx" |
| 31 | + end |
| 32 | + |
| 33 | + it "raises TypeError when integer index is provided without length argument" do |
| 34 | + -> { "hello".bytesplice(0, "xxx") }.should raise_error(TypeError) |
| 35 | + end |
| 36 | + |
| 37 | + it "replaces on an empty string" do |
| 38 | + "".bytesplice(0, 0, "").should == "" |
| 39 | + "".bytesplice(0, 0, "xxx").should == "xxx" |
| 40 | + end |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +describe "String#bytesplice with multibyte characters" do |
| 45 | + ruby_version_is "3.2" do |
| 46 | + it "raises IndexError when index is out of byte size boundary" do |
| 47 | + -> { "こんにちは".bytesplice(-16, 0, "xxx") }.should raise_error(IndexError) |
| 48 | + end |
| 49 | + |
| 50 | + it "raises IndexError when index is not on a codepoint boundary" do |
| 51 | + -> { "こんにちは".bytesplice(1, 0, "xxx") }.should raise_error(IndexError) |
| 52 | + end |
| 53 | + |
| 54 | + it "raises IndexError when length is not matching the codepoint boundary" do |
| 55 | + -> { "こんにちは".bytesplice(0, 1, "xxx") }.should raise_error(IndexError) |
| 56 | + -> { "こんにちは".bytesplice(0, 2, "xxx") }.should raise_error(IndexError) |
| 57 | + end |
| 58 | + |
| 59 | + it "replaces with integer indices" do |
| 60 | + "こんにちは".bytesplice(-15, 0, "xxx").should == "xxxこんにちは" |
| 61 | + "こんにちは".bytesplice(0, 0, "xxx").should == "xxxこんにちは" |
| 62 | + "こんにちは".bytesplice(0, 3, "xxx").should == "xxxんにちは" |
| 63 | + "こんにちは".bytesplice(3, 3, "はは").should == "こははにちは" |
| 64 | + "こんにちは".bytesplice(15, 0, "xxx").should == "こんにちはxxx" |
| 65 | + end |
| 66 | + |
| 67 | + it "replaces with range" do |
| 68 | + "こんにちは".bytesplice(-15...-16, "xxx").should == "xxxこんにちは" |
| 69 | + "こんにちは".bytesplice(0...0, "xxx").should == "xxxこんにちは" |
| 70 | + "こんにちは".bytesplice(0..2, "xxx").should == "xxxんにちは" |
| 71 | + "こんにちは".bytesplice(0...3, "xxx").should == "xxxんにちは" |
| 72 | + "こんにちは".bytesplice(0..5, "xxx").should == "xxxにちは" |
| 73 | + "こんにちは".bytesplice(0..-1, "xxx").should == "xxx" |
| 74 | + "こんにちは".bytesplice(0...15, "xxx").should == "xxx" |
| 75 | + "こんにちは".bytesplice(0...18, "xxx").should == "xxx" |
| 76 | + end |
| 77 | + |
| 78 | + it "treats negative length for range as 0" do |
| 79 | + "こんにちは".bytesplice(0...-100, "xxx").should == "xxxこんにちは" |
| 80 | + "こんにちは".bytesplice(3...-100, "xxx").should == "こxxxんにちは" |
| 81 | + "こんにちは".bytesplice(-15...-100, "xxx").should == "xxxこんにちは" |
| 82 | + end |
| 83 | + |
| 84 | + it "raises when ranges not match codepoint boundaries" do |
| 85 | + -> { "こんにちは".bytesplice(0..0, "x") }.should raise_error(IndexError) |
| 86 | + -> { "こんにちは".bytesplice(0..1, "x") }.should raise_error(IndexError) |
| 87 | + # Begin is incorrect |
| 88 | + -> { "こんにちは".bytesplice(-4..-1, "x") }.should raise_error(IndexError) |
| 89 | + -> { "こんにちは".bytesplice(-5..-1, "x") }.should raise_error(IndexError) |
| 90 | + # End is incorrect |
| 91 | + -> { "こんにちは".bytesplice(-3..-2, "x") }.should raise_error(IndexError) |
| 92 | + -> { "こんにちは".bytesplice(-3..-3, "x") }.should raise_error(IndexError) |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments