From b514a217e8e2496ee5e1015c617fc9252b8b2c60 Mon Sep 17 00:00:00 2001 From: Herwin Date: Thu, 9 Jan 2025 08:12:34 +0100 Subject: [PATCH] Extra specs for String#append_as_bytes Describe behaviour for negative numbers and numbers outside the byte range. --- core/string/append_as_bytes_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/string/append_as_bytes_spec.rb b/core/string/append_as_bytes_spec.rb index 0e1d09558..b1703e5f8 100644 --- a/core/string/append_as_bytes_spec.rb +++ b/core/string/append_as_bytes_spec.rb @@ -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)