2017-05-07 08:04:49 -04:00
|
|
|
# -*- encoding: utf-8 -*-
|
2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe "String#setbyte" do
|
|
|
|
it "returns an Integer" do
|
|
|
|
"a".setbyte(0,1).should be_kind_of(Integer)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "modifies the receiver" do
|
|
|
|
str = "glark"
|
|
|
|
old_id = str.object_id
|
|
|
|
str.setbyte(0,88)
|
|
|
|
str.object_id.should == old_id
|
|
|
|
end
|
|
|
|
|
|
|
|
it "changes the byte at the given index to the new byte" do
|
|
|
|
str = "a"
|
|
|
|
str.setbyte(0,98)
|
|
|
|
str.should == 'b'
|
|
|
|
|
|
|
|
# copy-on-write case
|
|
|
|
str1, str2 = "fooXbar".split("X")
|
|
|
|
str2.setbyte(0, 50)
|
|
|
|
str2.should == "2ar"
|
|
|
|
str1.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows changing bytes in multi-byte characters" do
|
|
|
|
str = "\u{915}"
|
|
|
|
str.setbyte(1,254)
|
|
|
|
str.getbyte(1).should == 254
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can invalidate a String's encoding" do
|
|
|
|
str = "glark"
|
|
|
|
str.valid_encoding?.should be_true
|
|
|
|
str.setbyte(2,253)
|
|
|
|
str.valid_encoding?.should be_false
|
2022-07-27 11:18:25 -04:00
|
|
|
|
|
|
|
str = "ABC"
|
|
|
|
str.setbyte(0, 0x20) # ' '
|
|
|
|
str.should.valid_encoding?
|
|
|
|
str.setbyte(0, 0xE3)
|
|
|
|
str.should_not.valid_encoding?
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "regards a negative index as counting from the end of the String" do
|
|
|
|
str = "hedgehog"
|
|
|
|
str.setbyte(-3, 108)
|
|
|
|
str.should == "hedgelog"
|
|
|
|
|
|
|
|
# copy-on-write case
|
|
|
|
str1, str2 = "fooXbar".split("X")
|
|
|
|
str2.setbyte(-1, 50)
|
|
|
|
str2.should == "ba2"
|
|
|
|
str1.should == "foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an IndexError if the index is greater than the String bytesize" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { "?".setbyte(1, 97) }.should raise_error(IndexError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2019-02-07 11:35:33 -05:00
|
|
|
it "raises an IndexError if the negative index is greater magnitude than the String bytesize" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { "???".setbyte(-5, 97) }.should raise_error(IndexError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets a byte at an index greater than String size" do
|
|
|
|
chr = "\u{998}"
|
|
|
|
chr.bytesize.should == 3
|
|
|
|
chr.setbyte(2, 150)
|
|
|
|
chr.should == "\xe0\xa6\x96"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not modify the original string when using String.new" do
|
|
|
|
str1 = "hedgehog"
|
|
|
|
str2 = String.new(str1)
|
|
|
|
str2.setbyte(0, 108)
|
|
|
|
str2.should == "ledgehog"
|
|
|
|
str2.should_not == "hedgehog"
|
|
|
|
str1.should == "hedgehog"
|
|
|
|
str1.should_not == "ledgehog"
|
|
|
|
end
|
|
|
|
|
2020-02-08 21:07:01 -05:00
|
|
|
it "raises a FrozenError if self is frozen" do
|
2017-05-07 08:04:49 -04:00
|
|
|
str = "cold".freeze
|
|
|
|
str.frozen?.should be_true
|
2020-02-08 21:07:01 -05:00
|
|
|
-> { str.setbyte(3,96) }.should raise_error(FrozenError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a TypeError unless the second argument is an Integer" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { "a".setbyte(0,'a') }.should raise_error(TypeError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls #to_int to convert the index" do
|
|
|
|
index = mock("setbyte index")
|
|
|
|
index.should_receive(:to_int).and_return(1)
|
|
|
|
|
|
|
|
str = "hat"
|
|
|
|
str.setbyte(index, "i".ord)
|
|
|
|
str.should == "hit"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls to_int to convert the value" do
|
|
|
|
value = mock("setbyte value")
|
|
|
|
value.should_receive(:to_int).and_return("i".ord)
|
|
|
|
|
|
|
|
str = "hat"
|
|
|
|
str.setbyte(1, value)
|
|
|
|
str.should == "hit"
|
|
|
|
end
|
|
|
|
end
|