From 4053e8ba0d39b688440fedee2ab3fffabcd64312 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 10 Jan 2022 16:29:54 +0100 Subject: Update to ruby/spec@226cfdc --- spec/ruby/core/array/pack/a_spec.rb | 11 +++++++++++ spec/ruby/core/array/pack/b_spec.rb | 7 ++++++- spec/ruby/core/array/pack/h_spec.rb | 5 +++++ spec/ruby/core/array/pack/shared/float.rb | 8 ++++++++ spec/ruby/core/array/pack/u_spec.rb | 10 ++++++++++ spec/ruby/core/array/pack/z_spec.rb | 10 ++++++++++ 6 files changed, 50 insertions(+), 1 deletion(-) (limited to 'spec/ruby/core/array') diff --git a/spec/ruby/core/array/pack/a_spec.rb b/spec/ruby/core/array/pack/a_spec.rb index 7af7a16c68..f4a40502c2 100644 --- a/spec/ruby/core/array/pack/a_spec.rb +++ b/spec/ruby/core/array/pack/a_spec.rb @@ -12,6 +12,17 @@ describe "Array#pack with format 'A'" do it_behaves_like :array_pack_string, 'A' it_behaves_like :array_pack_taint, 'A' + it "calls #to_str to convert an Object to a String" do + obj = mock("pack A string") + obj.should_receive(:to_str).and_return("``abcdef") + [obj].pack("A*").should == "``abcdef" + end + + it "will not implicitly convert a number to a string" do + -> { [0].pack('A') }.should raise_error(TypeError) + -> { [0].pack('a') }.should raise_error(TypeError) + end + it "adds all the bytes to the output when passed the '*' modifier" do ["abc"].pack("A*").should == "abc" end diff --git a/spec/ruby/core/array/pack/b_spec.rb b/spec/ruby/core/array/pack/b_spec.rb index 872c1b88d5..ec82b7d1ab 100644 --- a/spec/ruby/core/array/pack/b_spec.rb +++ b/spec/ruby/core/array/pack/b_spec.rb @@ -13,11 +13,16 @@ describe "Array#pack with format 'B'" do it_behaves_like :array_pack_taint, 'B' it "calls #to_str to convert an Object to a String" do - obj = mock("pack H string") + obj = mock("pack B string") obj.should_receive(:to_str).and_return("``abcdef") [obj].pack("B*").should == "\x2a" end + it "will not implicitly convert a number to a string" do + -> { [0].pack('B') }.should raise_error(TypeError) + -> { [0].pack('b') }.should raise_error(TypeError) + end + it "encodes one bit for each character starting with the most significant bit" do [ [["0"], "\x00"], [["1"], "\x80"] diff --git a/spec/ruby/core/array/pack/h_spec.rb b/spec/ruby/core/array/pack/h_spec.rb index 85a875fc8b..2c1dac8d4a 100644 --- a/spec/ruby/core/array/pack/h_spec.rb +++ b/spec/ruby/core/array/pack/h_spec.rb @@ -18,6 +18,11 @@ describe "Array#pack with format 'H'" do [obj].pack("H").should == "\xa0" end + it "will not implicitly convert a number to a string" do + -> { [0].pack('H') }.should raise_error(TypeError) + -> { [0].pack('h') }.should raise_error(TypeError) + end + it "encodes the first character as the most significant nibble when passed no count modifier" do ["ab"].pack("H").should == "\xa0" end diff --git a/spec/ruby/core/array/pack/shared/float.rb b/spec/ruby/core/array/pack/shared/float.rb index c6b194007f..ba174a071a 100644 --- a/spec/ruby/core/array/pack/shared/float.rb +++ b/spec/ruby/core/array/pack/shared/float.rb @@ -53,6 +53,14 @@ describe :array_pack_float_le, shared: true do it "encodes a negative Float outside the range of a single precision float" do [-1e150].pack(pack_format).should == "\x00\x00\x80\xff" end + + it "encodes a bignum as a float" do + [2 ** 65].pack(pack_format).should == [(2 ** 65).to_f].pack(pack_format) + end + + it "encodes a rational as a float" do + [Rational(3, 4)].pack(pack_format).should == [Rational(3, 4).to_f].pack(pack_format) + end end describe :array_pack_float_be, shared: true do diff --git a/spec/ruby/core/array/pack/u_spec.rb b/spec/ruby/core/array/pack/u_spec.rb index fe969cbb2d..b20093a647 100644 --- a/spec/ruby/core/array/pack/u_spec.rb +++ b/spec/ruby/core/array/pack/u_spec.rb @@ -18,6 +18,16 @@ describe "Array#pack with format 'u'" do it_behaves_like :array_pack_arguments, 'u' it_behaves_like :array_pack_taint, 'u' + it "calls #to_str to convert an Object to a String" do + obj = mock("pack u string") + obj.should_receive(:to_str).and_return("``abcdef") + [obj].pack("u*").should == "(8&!A8F-D968`\n" + end + + it "will not implicitly convert a number to a string" do + -> { [0].pack('u') }.should raise_error(TypeError) + end + it "encodes an empty string as an empty string" do [""].pack("u").should == "" end diff --git a/spec/ruby/core/array/pack/z_spec.rb b/spec/ruby/core/array/pack/z_spec.rb index 82ce7b4a1c..5ad3afd69e 100644 --- a/spec/ruby/core/array/pack/z_spec.rb +++ b/spec/ruby/core/array/pack/z_spec.rb @@ -12,6 +12,16 @@ describe "Array#pack with format 'Z'" do it_behaves_like :array_pack_string, 'Z' it_behaves_like :array_pack_taint, 'Z' + it "calls #to_str to convert an Object to a String" do + obj = mock("pack Z string") + obj.should_receive(:to_str).and_return("``abcdef") + [obj].pack("Z*").should == "``abcdef\x00" + end + + it "will not implicitly convert a number to a string" do + -> { [0].pack('Z') }.should raise_error(TypeError) + end + it "adds all the bytes and appends a NULL byte when passed the '*' modifier" do ["abc"].pack("Z*").should == "abc\x00" end -- cgit v1.2.3