Skip to content

Commit

Permalink
Extra specs for String#append_as_bytes
Browse files Browse the repository at this point in the history
Describe behaviour for negative numbers and numbers outside the byte
range.
  • Loading branch information
herwinw committed Jan 9, 2025
1 parent b9c5d34 commit 4c35b41
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/string/append_as_bytes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
str.should == "hello\xE2\x82\f+\xAC".b
end

it "truncates integers to the least significant byte" do
str = +""
str.append_as_bytes(0x131, 0x232, 0x333, bignum_value, bignum_value(1))
str.bytes.should == [0x31, 0x32, 0x33, 0, 1]
end

it "wraps negative integers" do
str = "".b
str.append_as_bytes(-1, -bignum_value, -bignum_value(1))
str.bytes.should == [0xFF, 0, 0xFF]
end

it "only accepts strings or integers, and doesn't attempt to cast with #to_str or #to_int" do
to_str = mock("to_str")
to_str.should_not_receive(:to_str)
Expand Down

0 comments on commit 4c35b41

Please sign in to comment.