|
| 1 | +require_relative '../../spec_helper' |
| 2 | + |
| 3 | +describe "String#bytesplice" do |
| 4 | + ruby_version_is "3.2" do |
| 5 | + it "accepts index, length and replacement string as arguments and returns replaced string" do |
| 6 | + "string".bytesplice(0, 3, "xxx").should == "xxxing" |
| 7 | + "string".bytesplice(-3, 3, "xxx").should == "strxxx" |
| 8 | + "string".bytesplice(-6, 0, "xxx").should == "xxxstring" |
| 9 | + "string".bytesplice(3, 0, "xxx").should == "strxxxing" |
| 10 | + end |
| 11 | + |
| 12 | + it "accepts range and replacement string as arguments and returns replaced string" do |
| 13 | + "string".bytesplice(0...3, "xxx").should == "xxxing" |
| 14 | + "string".bytesplice(-3...3, "xxx").should == "strxxxing" |
| 15 | + "string".bytesplice(-6...0, "xxx").should == "xxxstring" |
| 16 | + "string".bytesplice(3..6, "xxx").should == "strxxx" |
| 17 | + end |
| 18 | + |
| 19 | + it "replaces on an empty string" do |
| 20 | + "".bytesplice(0, 0, "xxx").should == "xxx" |
| 21 | + "".bytesplice(0, 0, "").should == "" |
| 22 | + end |
| 23 | + |
| 24 | + it "adjust string length if is not the same as replacement string" do |
| 25 | + "string".bytesplice(0..-1, "xxx").should == "xxx" |
| 26 | + "string".bytesplice(0, 6, "xxx").should == "xxx" |
| 27 | + end |
| 28 | + |
| 29 | + it "mutates self" do |
| 30 | + s = "string" |
| 31 | + s.bytesplice(2, 1, "xxx").should == "stxxxing" |
| 32 | + s.should == "stxxxing" |
| 33 | + end |
| 34 | + |
| 35 | + it "raises IndexError if index argument is out of range" do |
| 36 | + -> { |
| 37 | + "string".bytesplice(-7, 0, "xxx") |
| 38 | + }.should raise_error(IndexError, "index -7 out of string") |
| 39 | + |
| 40 | + -> { |
| 41 | + "string".bytesplice(7, 0, "xxx") |
| 42 | + }.should raise_error(IndexError, "index 7 out of string") |
| 43 | + end |
| 44 | + |
| 45 | + it "raises RangeError if range argument is out of range" do |
| 46 | + -> { |
| 47 | + "string".bytesplice(-7...-7, "xxx") |
| 48 | + }.should raise_error(RangeError, "-7...-7 out of range") |
| 49 | + end |
| 50 | + |
| 51 | + it "raises TypeError if index is provided withoput length argument" do |
| 52 | + -> { |
| 53 | + "string".bytesplice(0, "xxx") |
| 54 | + }.should raise_error(TypeError, "wrong argument type Integer (expected Range)") |
| 55 | + end |
| 56 | + |
| 57 | + it "raises FrozenError when string is frozen" do |
| 58 | + s = "string".freeze |
| 59 | + |
| 60 | + -> { |
| 61 | + s.bytesplice(2, 1, "xxx") |
| 62 | + }.should raise_error(FrozenError, "can't modify frozen String: \"string\"") |
| 63 | + end |
| 64 | + |
| 65 | + context "with multibyte characters" do |
| 66 | + it "accepts index, length and replacement string as arguments and returns replaced string" do |
| 67 | + "こんにちは".bytesplice(0, 3, "xxx").should == "xxxんにちは" |
| 68 | + "こんにちは".bytesplice(-3, 3, "xxx").should == "こんにちxxx" |
| 69 | + "こんにちは".bytesplice(-6, 0, "xxx").should == "こんにxxxちは" |
| 70 | + "こんにちは".bytesplice(3, 0, "xxx").should == "こxxxんにちは" |
| 71 | + end |
| 72 | + |
| 73 | + it "accepts range and replacement string as arguments and returns replaced string" do |
| 74 | + "こんにちは".bytesplice(0...3, "xxx").should == "xxxんにちは" |
| 75 | + "こんにちは".bytesplice(-3...3, "xxx").should == "こんにちxxxは" |
| 76 | + "こんにちは".bytesplice(-6...0, "xxx").should == "こんにxxxちは" |
| 77 | + end |
| 78 | + |
| 79 | + it "raises IndexError if index is out of byte size boundary" do |
| 80 | + -> { |
| 81 | + "こんにちは".bytesplice(-16, 0, "xxx") |
| 82 | + }.should raise_error(IndexError, "index -16 out of string") |
| 83 | + end |
| 84 | + |
| 85 | + it "raises IndexError if index is not on a codepoint boundary" do |
| 86 | + -> { |
| 87 | + "こんにちは".bytesplice(1, 0, "xxx") |
| 88 | + }.should raise_error(IndexError, "offset 1 does not land on character boundary") |
| 89 | + end |
| 90 | + |
| 91 | + it "raises IndexError when length is not matching the codepoint boundary" do |
| 92 | + -> { |
| 93 | + "こんにちは".bytesplice(0, 1, "xxx") |
| 94 | + }.should raise_error(IndexError, "offset 1 does not land on character boundary") |
| 95 | + |
| 96 | + -> { |
| 97 | + "こんにちは".bytesplice(0, 2, "xxx") |
| 98 | + }.should raise_error(IndexError, "offset 2 does not land on character boundary") |
| 99 | + end |
| 100 | + |
| 101 | + it "treats negative length for range as 0" do |
| 102 | + "こんにちは".bytesplice(0...-100, "xxx").should == "xxxこんにちは" |
| 103 | + "こんにちは".bytesplice(3...-100, "xxx").should == "こxxxんにちは" |
| 104 | + "こんにちは".bytesplice(-15...-100, "xxx").should == "xxxこんにちは" |
| 105 | + end |
| 106 | + |
| 107 | + it "deals with a different encoded argument" do |
| 108 | + s = "こんにちは" |
| 109 | + s.encoding.should == Encoding::UTF_8 |
| 110 | + sub = "xxxxxx" |
| 111 | + sub.force_encoding(Encoding::US_ASCII) |
| 112 | + |
| 113 | + result = s.bytesplice(0, 3, sub) |
| 114 | + result.should == "xxxxxxんにちは" |
| 115 | + result.encoding.should == Encoding::UTF_8 |
| 116 | + |
| 117 | + s = "xxxxxx" |
| 118 | + s.force_encoding(Encoding::US_ASCII) |
| 119 | + sub = "こんにちは" |
| 120 | + sub.encoding.should == Encoding::UTF_8 |
| 121 | + |
| 122 | + result = s.bytesplice(0, 3, sub) |
| 123 | + result.should == "こんにちはxxx" |
| 124 | + result.encoding.should == Encoding::UTF_8 |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments