diff options
Diffstat (limited to 'spec/ruby/core/array/pack')
24 files changed, 328 insertions, 188 deletions
diff --git a/spec/ruby/core/array/pack/a_spec.rb b/spec/ruby/core/array/pack/a_spec.rb index 7af7a16c68..03bfd8214c 100644 --- a/spec/ruby/core/array/pack/a_spec.rb +++ b/spec/ruby/core/array/pack/a_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -12,11 +12,22 @@ 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(TypeError) + -> { [0].pack('a') }.should.raise(TypeError) + end + it "adds all the bytes to the output when passed the '*' modifier" do ["abc"].pack("A*").should == "abc" end - it "padds the output with spaces when the count exceeds the size of the String" do + it "pads the output with spaces when the count exceeds the size of the String" do ["abc"].pack("A6").should == "abc " end @@ -44,7 +55,7 @@ describe "Array#pack with format 'a'" do ["abc"].pack("a*").should == "abc" end - it "padds the output with NULL bytes when the count exceeds the size of the String" do + it "pads the output with NULL bytes when the count exceeds the size of the String" do ["abc"].pack("a6").should == "abc\x00\x00\x00" end diff --git a/spec/ruby/core/array/pack/at_spec.rb b/spec/ruby/core/array/pack/at_spec.rb index 3942677913..bb9801440a 100644 --- a/spec/ruby/core/array/pack/at_spec.rb +++ b/spec/ruby/core/array/pack/at_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' diff --git a/spec/ruby/core/array/pack/b_spec.rb b/spec/ruby/core/array/pack/b_spec.rb index 872c1b88d5..f7576846ef 100644 --- a/spec/ruby/core/array/pack/b_spec.rb +++ b/spec/ruby/core/array/pack/b_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -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(TypeError) + -> { [0].pack('b') }.should.raise(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/buffer_spec.rb b/spec/ruby/core/array/pack/buffer_spec.rb index ecb40bfd06..d104c80186 100644 --- a/spec/ruby/core/array/pack/buffer_spec.rb +++ b/spec/ruby/core/array/pack/buffer_spec.rb @@ -7,43 +7,53 @@ describe "Array#pack with :buffer option" do n = [ 65, 66, 67 ] buffer = " "*3 result = n.pack("ccc", buffer: buffer) #=> "ABC" - result.should equal(buffer) + result.should.equal?(buffer) end it "adds result at the end of buffer content" do n = [ 65, 66, 67 ] # result without buffer is "ABC" - buffer = "" + buffer = +"" n.pack("ccc", buffer: buffer).should == "ABC" - buffer = "123" + buffer = +"123" n.pack("ccc", buffer: buffer).should == "123ABC" - buffer = "12345" + buffer = +"12345" n.pack("ccc", buffer: buffer).should == "12345ABC" end it "raises TypeError exception if buffer is not String" do - -> { [65].pack("ccc", buffer: []) }.should raise_error( + -> { [65].pack("ccc", buffer: []) }.should.raise( TypeError, "buffer must be String, not Array") end + it "raise FrozenError if buffer is frozen" do + -> { [65].pack("c", buffer: "frozen-string".freeze) }.should.raise(FrozenError) + end + + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + [65, 66, 67].pack("ccc", buffer: buffer) + buffer.encoding.should == Encoding::ISO_8859_1 + end + context "offset (@) is specified" do it 'keeps buffer content if it is longer than offset' do n = [ 65, 66, 67 ] - buffer = "123456" + buffer = +"123456" n.pack("@3ccc", buffer: buffer).should == "123ABC" end it "fills the gap with \\0 if buffer content is shorter than offset" do n = [ 65, 66, 67 ] - buffer = "123" + buffer = +"123" n.pack("@6ccc", buffer: buffer).should == "123\0\0\0ABC" end it 'does not keep buffer content if it is longer than offset + result' do n = [ 65, 66, 67 ] - buffer = "1234567890" + buffer = +"1234567890" n.pack("@3ccc", buffer: buffer).should == "123ABC" end end diff --git a/spec/ruby/core/array/pack/c_spec.rb b/spec/ruby/core/array/pack/c_spec.rb index 7200830331..de06207a23 100644 --- a/spec/ruby/core/array/pack/c_spec.rb +++ b/spec/ruby/core/array/pack/c_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' @@ -45,8 +45,10 @@ describe :array_pack_8bit, shared: true do [1, 2, 3, 4, 5].pack(pack_format('*')).should == "\x01\x02\x03\x04\x05" end - it "ignores NULL bytes between directives" do - [1, 2, 3].pack(pack_format("\000", 2)).should == "\x01\x02" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [1, 2, 3].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/array/pack/comment_spec.rb b/spec/ruby/core/array/pack/comment_spec.rb index 254c827ccc..daf1cff06a 100644 --- a/spec/ruby/core/array/pack/comment_spec.rb +++ b/spec/ruby/core/array/pack/comment_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' diff --git a/spec/ruby/core/array/pack/h_spec.rb b/spec/ruby/core/array/pack/h_spec.rb index 85a875fc8b..1492d02b1f 100644 --- a/spec/ruby/core/array/pack/h_spec.rb +++ b/spec/ruby/core/array/pack/h_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -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(TypeError) + -> { [0].pack('h') }.should.raise(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/l_spec.rb b/spec/ruby/core/array/pack/l_spec.rb index b446a7a36a..f6dfb1da83 100644 --- a/spec/ruby/core/array/pack/l_spec.rb +++ b/spec/ruby/core/array/pack/l_spec.rb @@ -29,7 +29,7 @@ describe "Array#pack with format 'L'" do it_behaves_like :array_pack_32bit_be, 'L>' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_32bit_le, 'L<_' it_behaves_like :array_pack_32bit_le, 'L_<' @@ -51,7 +51,7 @@ describe "Array#pack with format 'L'" do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_64bit_le, 'L<_' it_behaves_like :array_pack_64bit_le, 'L_<' @@ -83,7 +83,7 @@ describe "Array#pack with format 'l'" do it_behaves_like :array_pack_32bit_be, 'l>' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_32bit_le, 'l<_' it_behaves_like :array_pack_32bit_le, 'l_<' @@ -105,7 +105,7 @@ describe "Array#pack with format 'l'" do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_64bit_le, 'l<_' it_behaves_like :array_pack_64bit_le, 'l_<' @@ -137,7 +137,7 @@ little_endian do it_behaves_like :array_pack_32bit_le, 'l' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_32bit_le, 'L_' end @@ -155,7 +155,7 @@ little_endian do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_64bit_le, 'L_' end @@ -183,7 +183,7 @@ big_endian do it_behaves_like :array_pack_32bit_be, 'l' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_32bit_be, 'L_' end @@ -201,7 +201,7 @@ big_endian do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_64bit_be, 'L_' end diff --git a/spec/ruby/core/array/pack/m_spec.rb b/spec/ruby/core/array/pack/m_spec.rb index 2b1a84abca..fb670d120e 100644 --- a/spec/ruby/core/array/pack/m_spec.rb +++ b/spec/ruby/core/array/pack/m_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -80,8 +80,16 @@ describe "Array#pack with format 'M'" do ].should be_computed_by(:pack, "M") end - it "encodes a tab followed by a newline with an encoded newline" do + it "encodes a tab at the end of a line with an encoded newline" do + ["\t"].pack("M").should == "\t=\n" ["\t\n"].pack("M").should == "\t=\n\n" + ["abc\t\nxyz"].pack("M").should == "abc\t=\n\nxyz=\n" + end + + it "encodes a space at the end of a line with an encoded newline" do + [" "].pack("M").should == " =\n" + [" \n"].pack("M").should == " =\n\n" + ["abc \nxyz"].pack("M").should == "abc =\n\nxyz=\n" end it "encodes 127..255 in hex format" do @@ -147,7 +155,7 @@ describe "Array#pack with format 'M'" do it "encodes a recursive array" do empty = ArraySpecs.empty_recursive_array - empty.pack('M').should be_an_instance_of(String) + empty.pack('M').should.instance_of?(String) array = ArraySpecs.recursive_array array.pack('M').should == "1=\n" @@ -164,7 +172,7 @@ describe "Array#pack with format 'M'" do obj = mock("pack M non-string") obj.should_receive(:to_s).and_return(2) - [obj].pack("M").should be_an_instance_of(String) + [obj].pack("M").should.instance_of?(String) end it "encodes a Symbol as a String" do @@ -285,16 +293,16 @@ describe "Array#pack with format 'm'" do it "raises a TypeError if #to_str does not return a String" do obj = mock("pack m non-string") - -> { [obj].pack("m") }.should raise_error(TypeError) + -> { [obj].pack("m") }.should.raise(TypeError) end it "raises a TypeError if passed nil" do - -> { [nil].pack("m") }.should raise_error(TypeError) + -> { [nil].pack("m") }.should.raise(TypeError) end it "raises a TypeError if passed an Integer" do - -> { [0].pack("m") }.should raise_error(TypeError) - -> { [bignum_value].pack("m") }.should raise_error(TypeError) + -> { [0].pack("m") }.should.raise(TypeError) + -> { [bignum_value].pack("m") }.should.raise(TypeError) end it "does not emit a newline if passed zero as the count modifier" do diff --git a/spec/ruby/core/array/pack/p_spec.rb b/spec/ruby/core/array/pack/p_spec.rb index d7dff8a4da..b023bf9110 100644 --- a/spec/ruby/core/array/pack/p_spec.rb +++ b/spec/ruby/core/array/pack/p_spec.rb @@ -15,18 +15,6 @@ describe "Array#pack with format 'P'" do ["hello"].pack("P").unpack("P5").should == ["hello"] end - ruby_version_is ''...'2.7' do - it "taints the input string" do - input_string = "hello" - [input_string].pack("P") - input_string.tainted?.should be_true - end - - it "does not taint the output string in normal cases" do - ["hello"].pack("P").tainted?.should be_false - end - end - it "with nil gives a null pointer" do [nil].pack("P").unpack("J").should == [0] end @@ -44,18 +32,6 @@ describe "Array#pack with format 'p'" do ["hello"].pack("p").unpack("p").should == ["hello"] end - ruby_version_is ''...'2.7' do - it "taints the input string" do - input_string = "hello" - [input_string].pack("p") - input_string.tainted?.should be_true - end - - it "does not taint the output string in normal cases" do - ["hello"].pack("p").tainted?.should be_false - end - end - it "with nil gives a null pointer" do [nil].pack("p").unpack("J").should == [0] end diff --git a/spec/ruby/core/array/pack/percent_spec.rb b/spec/ruby/core/array/pack/percent_spec.rb index 5d56dea5fe..29b119732a 100644 --- a/spec/ruby/core/array/pack/percent_spec.rb +++ b/spec/ruby/core/array/pack/percent_spec.rb @@ -2,6 +2,6 @@ require_relative '../../../spec_helper' describe "Array#pack with format '%'" do it "raises an Argument Error" do - -> { [1].pack("%") }.should raise_error(ArgumentError) + -> { [1].pack("%") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/pack/r_spec.rb b/spec/ruby/core/array/pack/r_spec.rb new file mode 100644 index 0000000000..cefe1388d2 --- /dev/null +++ b/spec/ruby/core/array/pack/r_spec.rb @@ -0,0 +1,89 @@ +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/numeric_basic' +require_relative 'shared/integer' + +ruby_version_is "4.1" do + describe "Array#pack with format 'R'" do + it_behaves_like :array_pack_basic, 'R' + it_behaves_like :array_pack_basic_non_float, 'R' + it_behaves_like :array_pack_arguments, 'R' + it_behaves_like :array_pack_numeric_basic, 'R' + it_behaves_like :array_pack_integer, 'R' + + it "encodes a ULEB128 integer" do + [ [[0], "\x00"], + [[1], "\x01"], + [[127], "\x7f"], + [[128], "\x80\x01"], + [[0x3fff], "\xff\x7f"], + [[0x4000], "\x80\x80\x01"], + [[0xffffffff], "\xff\xff\xff\xff\x0f"], + [[0x100000000], "\x80\x80\x80\x80\x10"], + [[0xffff_ffff_ffff_ffff], "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"], + [[0xffff_ffff_ffff_ffff_ffff_ffff], "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1f"], + ].should be_computed_by(:pack, "R") + end + + it "encodes multiple values with '*' modifier" do + [1, 2].pack("R*").should == "\x01\x02" + [127, 128].pack("R*").should == "\x7f\x80\x01" + end + + it "raises an ArgumentError when passed a negative value" do + -> { [-1].pack("R") }.should.raise(ArgumentError) + -> { [-100].pack("R") }.should.raise(ArgumentError) + end + + it "round-trips values through pack and unpack" do + values = [0, 1, 127, 128, 0x3fff, 0x4000, 0xffffffff, 0x100000000] + values.pack("R*").unpack("R*").should == values + end + end + + describe "Array#pack with format 'r'" do + it_behaves_like :array_pack_basic, 'r' + it_behaves_like :array_pack_basic_non_float, 'r' + it_behaves_like :array_pack_arguments, 'r' + it_behaves_like :array_pack_numeric_basic, 'r' + it_behaves_like :array_pack_integer, 'r' + + it "encodes a SLEB128 integer" do + [ [[0], "\x00"], + [[1], "\x01"], + [[-1], "\x7f"], + [[-2], "\x7e"], + [[127], "\xff\x00"], + [[128], "\x80\x01"], + [[-127], "\x81\x7f"], + [[-128], "\x80\x7f"], + ].should be_computed_by(:pack, "r") + end + + it "encodes larger positive numbers" do + [0x3fff].pack("r").should == "\xff\xff\x00" + [0x4000].pack("r").should == "\x80\x80\x01" + end + + it "encodes larger negative numbers" do + [-0x3fff].pack("r").should == "\x81\x80\x7f" + [-0x4000].pack("r").should == "\x80\x80\x7f" + end + + it "encodes very large numbers" do + [0xffff_ffff_ffff_ffff_ffff_ffff].pack("r").should == "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1F" + [-0xffff_ffff_ffff_ffff_ffff_ffff].pack("r").should == "\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x60" + end + + it "encodes multiple values with '*' modifier" do + [0, 1, -1].pack("r*").should == "\x00\x01\x7f" + end + + it "round-trips values through pack and unpack" do + values = [0, 1, -1, 127, -127, 128, -128, 0x3fff, -0x3fff, 0x4000, -0x4000] + values.pack("r*").unpack("r*").should == values + end + end +end diff --git a/spec/ruby/core/array/pack/shared/basic.rb b/spec/ruby/core/array/pack/shared/basic.rb index 9061273ad6..2894369c71 100644 --- a/spec/ruby/core/array/pack/shared/basic.rb +++ b/spec/ruby/core/array/pack/shared/basic.rb @@ -1,6 +1,6 @@ describe :array_pack_arguments, shared: true do it "raises an ArgumentError if there are fewer elements than the format requires" do - -> { [].pack(pack_format(1)) }.should raise_error(ArgumentError) + -> { [].pack(pack_format(1)) }.should.raise(ArgumentError) end end @@ -10,11 +10,11 @@ describe :array_pack_basic, shared: true do end it "raises a TypeError when passed nil" do - -> { [@obj].pack(nil) }.should raise_error(TypeError) + -> { [@obj].pack(nil) }.should.raise(TypeError) end it "raises a TypeError when passed an Integer" do - -> { [@obj].pack(1) }.should raise_error(TypeError) + -> { [@obj].pack(1) }.should.raise(TypeError) end end @@ -24,46 +24,50 @@ describe :array_pack_basic_non_float, shared: true do end it "ignores whitespace in the format string" do - [@obj, @obj].pack("a \t\n\v\f\r"+pack_format).should be_an_instance_of(String) + [@obj, @obj].pack("a \t\n\v\f\r"+pack_format).should.instance_of?(String) + end + + it "ignores comments in the format string" do + # 2 additional directives ('a') are required for the X directive + [@obj, @obj, @obj, @obj].pack("aa #{pack_format} # some comment \n#{pack_format}").should.instance_of?(String) + end + + it "raise ArgumentError when a directive is unknown" do + # additional directive ('a') is required for the X directive + -> { [@obj, @obj].pack("a K" + pack_format) }.should.raise(ArgumentError, /unknown pack directive 'K'/) + -> { [@obj, @obj].pack("a 0" + pack_format) }.should.raise(ArgumentError, /unknown pack directive '0'/) + -> { [@obj, @obj].pack("a :" + pack_format) }.should.raise(ArgumentError, /unknown pack directive ':'/) end it "calls #to_str to coerce the directives string" do d = mock("pack directive") d.should_receive(:to_str).and_return("x"+pack_format) - [@obj, @obj].pack(d).should be_an_instance_of(String) - end - - ruby_version_is ''...'2.7' do - it "taints the output string if the format string is tainted" do - [@obj, @obj].pack("x"+pack_format.taint).tainted?.should be_true - end + [@obj, @obj].pack(d).should.instance_of?(String) end end describe :array_pack_basic_float, shared: true do it "ignores whitespace in the format string" do - [9.3, 4.7].pack(" \t\n\v\f\r"+pack_format).should be_an_instance_of(String) + [9.3, 4.7].pack(" \t\n\v\f\r"+pack_format).should.instance_of?(String) + end + + it "ignores comments in the format string" do + [9.3, 4.7].pack(pack_format + "# some comment \n" + pack_format).should.instance_of?(String) end it "calls #to_str to coerce the directives string" do d = mock("pack directive") d.should_receive(:to_str).and_return("x"+pack_format) - [1.2, 4.7].pack(d).should be_an_instance_of(String) - end - - ruby_version_is ''...'2.7' do - it "taints the output string if the format string is tainted" do - [3.2, 2.8].pack("x"+pack_format.taint).tainted?.should be_true - end + [1.2, 4.7].pack(d).should.instance_of?(String) end end describe :array_pack_no_platform, shared: true do it "raises ArgumentError when the format modifier is '_'" do - ->{ [1].pack(pack_format("_")) }.should raise_error(ArgumentError) + ->{ [1].pack(pack_format("_")) }.should.raise(ArgumentError) end it "raises ArgumentError when the format modifier is '!'" do - ->{ [1].pack(pack_format("!")) }.should raise_error(ArgumentError) + ->{ [1].pack(pack_format("!")) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/pack/shared/encodings.rb b/spec/ruby/core/array/pack/shared/encodings.rb index 6b7ffac764..0b5a5cc8a0 100644 --- a/spec/ruby/core/array/pack/shared/encodings.rb +++ b/spec/ruby/core/array/pack/shared/encodings.rb @@ -5,12 +5,12 @@ describe :array_pack_hex, shared: true do it "raises a TypeError if the object does not respond to #to_str" do obj = mock("pack hex non-string") - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError if #to_str does not return a String" do obj = mock("pack hex non-string") obj.should_receive(:to_str).and_return(1) - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/array/pack/shared/float.rb b/spec/ruby/core/array/pack/shared/float.rb index c6b194007f..c1efcd7677 100644 --- a/spec/ruby/core/array/pack/shared/float.rb +++ b/spec/ruby/core/array/pack/shared/float.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :array_pack_float_le, shared: true do it "encodes a positive Float" do @@ -14,7 +14,7 @@ describe :array_pack_float_le, shared: true do end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -25,8 +25,10 @@ describe :array_pack_float_le, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "\x9a\x999@33\xb3?33\x03A" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "\x9a\x99\xa9@33\x13A" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -43,7 +45,7 @@ describe :array_pack_float_le, shared: true do it "encodes NaN" do nans = ["\x00\x00\xc0\xff", "\x00\x00\xc0\x7f", "\xFF\xFF\xFF\x7F"] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do @@ -53,6 +55,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 @@ -66,10 +76,15 @@ describe :array_pack_float_be, shared: true do it "converts an Integer to a Float" do [8].pack(pack_format).should == "A\x00\x00\x00" + [bignum_value].pack(pack_format).should == "_\x80\x00\x00" + end + + it "converts a Rational to a Float" do + [Rational(8)].pack(pack_format).should == "A\x00\x00\x00" end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -80,8 +95,10 @@ describe :array_pack_float_be, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "@9\x99\x9a?\xb333A\x0333" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "@\xa9\x99\x9aA\x1333" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -98,7 +115,7 @@ describe :array_pack_float_be, shared: true do it "encodes NaN" do nans = ["\xff\xc0\x00\x00", "\x7f\xc0\x00\x00", "\x7F\xFF\xFF\xFF"] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do @@ -121,10 +138,15 @@ describe :array_pack_double_le, shared: true do it "converts an Integer to a Float" do [8].pack(pack_format).should == "\x00\x00\x00\x00\x00\x00\x20@" + [bignum_value].pack(pack_format).should == "\x00\x00\x00\x00\x00\x00\xF0C" + end + + it "converts a Rational to a Float" do + [Rational(8)].pack(pack_format).should == "\x00\x00\x00\x00\x00\x00 @" end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -135,8 +157,10 @@ describe :array_pack_double_le, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "333333\x07@ffffff\xf6?ffffff\x20@" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "333333\x15@ffffff\x22@" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -157,7 +181,7 @@ describe :array_pack_double_le, shared: true do "\x00\x00\x00\x00\x00\x00\xf8\x7f", "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F" ] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do @@ -183,7 +207,7 @@ describe :array_pack_double_be, shared: true do end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -194,8 +218,10 @@ describe :array_pack_double_be, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "@\x07333333?\xf6ffffff@\x20ffffff" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "@\x15333333@\x22ffffff" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -216,7 +242,7 @@ describe :array_pack_double_be, shared: true do "\x7f\xf8\x00\x00\x00\x00\x00\x00", "\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do diff --git a/spec/ruby/core/array/pack/shared/integer.rb b/spec/ruby/core/array/pack/shared/integer.rb index 6592f85022..1cdd386cc1 100644 --- a/spec/ruby/core/array/pack/shared/integer.rb +++ b/spec/ruby/core/array/pack/shared/integer.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :array_pack_16bit_le, shared: true do it "encodes the least significant 16 bits of a positive number" do @@ -41,9 +41,10 @@ describe :array_pack_16bit_le, shared: true do str.should == "\x78\x65\xcd\xab\x21\x43" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x78\x65\xcd\xab" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -93,9 +94,10 @@ describe :array_pack_16bit_be, shared: true do str.should == "\x65\x78\xab\xcd\x43\x21" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x65\x78\xab\xcd" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -145,9 +147,10 @@ describe :array_pack_32bit_le, shared: true do str.should == "\x78\x65\x43\x12\xcd\xab\xf0\xde\x21\x43\x65\x78" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x78\x65\x43\x12\xcd\xab\xf0\xde" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -197,9 +200,10 @@ describe :array_pack_32bit_be, shared: true do str.should == "\x12\x43\x65\x78\xde\xf0\xab\xcd\x78\x65\x43\x21" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x12\x43\x65\x78\xde\xf0\xab\xcd" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -225,7 +229,7 @@ describe :array_pack_32bit_le_platform, shared: true do str.should == "\x78\x65\x43\x12\xcd\xab\xf0\xde\x21\x43\x65\x78" end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do it "encodes the least significant 32 bits of a number that is greater than 32 bits" do [ [[0xff_7865_4321], "\x21\x43\x65\x78"], [[-0xff_7865_4321], "\xdf\xbc\x9a\x87"] @@ -251,7 +255,7 @@ describe :array_pack_32bit_be_platform, shared: true do str.should == "\x12\x43\x65\x78\xde\xf0\xab\xcd\x78\x65\x43\x21" end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do it "encodes the least significant 32 bits of a number that is greater than 32 bits" do [ [[0xff_7865_4321], "\x78\x65\x43\x21"], [[-0xff_7865_4321], "\x87\x9a\xbc\xdf"] @@ -309,9 +313,10 @@ describe :array_pack_64bit_le, shared: true do str.should == "\x56\x78\x12\x34\xcd\xab\xf0\xde\xf0\xde\xba\xdc\x21\x43\x65\x78" end - it "ignores NULL bytes between directives" do - str = [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) - str.should == "\x56\x78\x12\x34\xcd\xab\xf0\xde\xf0\xde\xba\xdc\x21\x43\x65\x78" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -369,9 +374,10 @@ describe :array_pack_64bit_be, shared: true do str.should == "\xde\xf0\xab\xcd\x34\x12\x78\x56\x78\x65\x43\x21\xdc\xba\xde\xf0" end - it "ignores NULL bytes between directives" do - str = [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) - str.should == "\xde\xf0\xab\xcd\x34\x12\x78\x56\x78\x65\x43\x21\xdc\xba\xde\xf0" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/array/pack/shared/numeric_basic.rb b/spec/ruby/core/array/pack/shared/numeric_basic.rb index 7c36ba4a32..6594914933 100644 --- a/spec/ruby/core/array/pack/shared/numeric_basic.rb +++ b/spec/ruby/core/array/pack/shared/numeric_basic.rb @@ -4,15 +4,15 @@ describe :array_pack_numeric_basic, shared: true do end it "raises a TypeError when passed nil" do - -> { [nil].pack(pack_format) }.should raise_error(TypeError) + -> { [nil].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError when passed true" do - -> { [true].pack(pack_format) }.should raise_error(TypeError) + -> { [true].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError when passed false" do - -> { [false].pack(pack_format) }.should raise_error(TypeError) + -> { [false].pack(pack_format) }.should.raise(TypeError) end it "returns a binary string" do @@ -24,21 +24,27 @@ end describe :array_pack_integer, shared: true do it "raises a TypeError when the object does not respond to #to_int" do obj = mock('not an integer') - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError when passed a String" do - -> { ["5"].pack(pack_format) }.should raise_error(TypeError) + -> { ["5"].pack(pack_format) }.should.raise(TypeError) end end describe :array_pack_float, shared: true do it "raises a TypeError if a String does not represent a floating point number" do - -> { ["a"].pack(pack_format) }.should raise_error(TypeError) + -> { ["a"].pack(pack_format) }.should.raise(TypeError) end - it "raises a TypeError when the object does not respond to #to_f" do - obj = mock('not an float') - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + it "raises a TypeError when the object is not Numeric" do + obj = Object.new + -> { [obj].pack(pack_format) }.should.raise(TypeError, /can't convert Object into Float/) + end + + it "raises a TypeError when the Numeric object does not respond to #to_f" do + klass = Class.new(Numeric) + obj = klass.new + -> { [obj].pack(pack_format) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/array/pack/shared/string.rb b/spec/ruby/core/array/pack/shared/string.rb index 8c82e8c617..b02257059f 100644 --- a/spec/ruby/core/array/pack/shared/string.rb +++ b/spec/ruby/core/array/pack/shared/string.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :array_pack_string, shared: true do it "adds count bytes of a String to the output" do ["abc"].pack(pack_format(2)).should == "ab" @@ -17,11 +17,11 @@ describe :array_pack_string, shared: true do end it "raises an ArgumentError when the Array is empty" do - -> { [].pack(pack_format) }.should raise_error(ArgumentError) + -> { [].pack(pack_format) }.should.raise(ArgumentError) end it "raises an ArgumentError when the Array has too few elements" do - -> { ["a"].pack(pack_format(nil, 2)) }.should raise_error(ArgumentError) + -> { ["a"].pack(pack_format(nil, 2)) }.should.raise(ArgumentError) end it "calls #to_str to convert the element to a String" do @@ -33,14 +33,14 @@ describe :array_pack_string, shared: true do it "raises a TypeError when the object does not respond to #to_str" do obj = mock("not a string") - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end it "returns a string in encoding of common to the concatenated results" do f = pack_format("*") [ [["\u{3042 3044 3046 3048}", 0x2000B].pack(f+"U"), Encoding::BINARY], [["abcde\xd1", "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY], - [["a".force_encoding("ascii"), "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY], + [["a".dup.force_encoding("ascii"), "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY], # under discussion [ruby-dev:37294] [["\u{3042 3044 3046 3048}", 1].pack(f+"N"), Encoding::BINARY] ].should be_computed_by(:encoding) diff --git a/spec/ruby/core/array/pack/shared/taint.rb b/spec/ruby/core/array/pack/shared/taint.rb index 565f04b8b9..2c2b011c34 100644 --- a/spec/ruby/core/array/pack/shared/taint.rb +++ b/spec/ruby/core/array/pack/shared/taint.rb @@ -1,35 +1,2 @@ describe :array_pack_taint, shared: true do - ruby_version_is ''...'2.7' do - it "returns a tainted string when a pack argument is tainted" do - ["abcd".taint, 0x20].pack(pack_format("3C")).tainted?.should be_true - end - - it "does not return a tainted string when the array is tainted" do - ["abcd", 0x20].taint.pack(pack_format("3C")).tainted?.should be_false - end - - it "returns a tainted string when the format is tainted" do - ["abcd", 0x20].pack(pack_format("3C").taint).tainted?.should be_true - end - - it "returns a tainted string when an empty format is tainted" do - ["abcd", 0x20].pack("".taint).tainted?.should be_true - end - - it "returns a untrusted string when the format is untrusted" do - ["abcd", 0x20].pack(pack_format("3C").untrust).untrusted?.should be_true - end - - it "returns a untrusted string when the empty format is untrusted" do - ["abcd", 0x20].pack("".untrust).untrusted?.should be_true - end - - it "returns a untrusted string when a pack argument is untrusted" do - ["abcd".untrust, 0x20].pack(pack_format("3C")).untrusted?.should be_true - end - - it "returns a trusted string when the array is untrusted" do - ["abcd", 0x20].untrust.pack(pack_format("3C")).untrusted?.should be_false - end - end end diff --git a/spec/ruby/core/array/pack/shared/unicode.rb b/spec/ruby/core/array/pack/shared/unicode.rb index dd0f8b38aa..58ba8a8b23 100644 --- a/spec/ruby/core/array/pack/shared/unicode.rb +++ b/spec/ruby/core/array/pack/shared/unicode.rb @@ -26,7 +26,7 @@ describe :array_pack_unicode, shared: true do it "constructs strings with valid encodings" do str = [0x85].pack("U*") str.should == "\xc2\x85" - str.valid_encoding?.should be_true + str.valid_encoding?.should == true end it "encodes values larger than UTF-8 max codepoints" do @@ -64,11 +64,13 @@ describe :array_pack_unicode, shared: true do it "raises a TypeError if #to_int does not return an Integer" do obj = mock('to_int') obj.should_receive(:to_int).and_return("5") - -> { [obj].pack("U") }.should raise_error(TypeError) + -> { [obj].pack("U") }.should.raise(TypeError) end - it "ignores NULL bytes between directives" do - [1, 2, 3].pack("U\x00U").should == "\x01\x02" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [1, 2, 3].pack("U\x00U") + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -76,11 +78,11 @@ describe :array_pack_unicode, shared: true do end it "raises a RangeError if passed a negative number" do - -> { [-1].pack("U") }.should raise_error(RangeError) + -> { [-1].pack("U") }.should.raise(RangeError) end it "raises a RangeError if passed a number larger than an unsigned 32-bit integer" do - -> { [2**32].pack("U") }.should raise_error(RangeError) + -> { [2**32].pack("U") }.should.raise(RangeError) end it "sets the output string to UTF-8 encoding" do diff --git a/spec/ruby/core/array/pack/u_spec.rb b/spec/ruby/core/array/pack/u_spec.rb index fe969cbb2d..c6a0d77eb2 100644 --- a/spec/ruby/core/array/pack/u_spec.rb +++ b/spec/ruby/core/array/pack/u_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -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(TypeError) + end + it "encodes an empty string as an empty string" do [""].pack("u").should == "" end @@ -112,16 +122,16 @@ describe "Array#pack with format 'u'" do it "raises a TypeError if #to_str does not return a String" do obj = mock("pack m non-string") - -> { [obj].pack("u") }.should raise_error(TypeError) + -> { [obj].pack("u") }.should.raise(TypeError) end it "raises a TypeError if passed nil" do - -> { [nil].pack("u") }.should raise_error(TypeError) + -> { [nil].pack("u") }.should.raise(TypeError) end it "raises a TypeError if passed an Integer" do - -> { [0].pack("u") }.should raise_error(TypeError) - -> { [bignum_value].pack("u") }.should raise_error(TypeError) + -> { [0].pack("u") }.should.raise(TypeError) + -> { [bignum_value].pack("u") }.should.raise(TypeError) end it "sets the output string to US-ASCII encoding" do diff --git a/spec/ruby/core/array/pack/w_spec.rb b/spec/ruby/core/array/pack/w_spec.rb index 439fa02198..263e2a2288 100644 --- a/spec/ruby/core/array/pack/w_spec.rb +++ b/spec/ruby/core/array/pack/w_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -24,8 +24,10 @@ describe "Array#pack with format 'w'" do [obj].pack("w").should == "\x05" end - it "ignores NULL bytes between directives" do - [1, 2, 3].pack("w\x00w").should == "\x01\x02" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [1, 2, 3].pack("w\x00w") + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -33,7 +35,7 @@ describe "Array#pack with format 'w'" do end it "raises an ArgumentError when passed a negative value" do - -> { [-1].pack("w") }.should raise_error(ArgumentError) + -> { [-1].pack("w") }.should.raise(ArgumentError) end it "returns a binary string" do diff --git a/spec/ruby/core/array/pack/x_spec.rb b/spec/ruby/core/array/pack/x_spec.rb index a28dd0bf21..7ff587a01e 100644 --- a/spec/ruby/core/array/pack/x_spec.rb +++ b/spec/ruby/core/array/pack/x_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -30,6 +30,7 @@ describe "Array#pack with format 'x'" do it "does not add a NULL byte when passed the '*' modifier" do [].pack("x*").should == "" + [1, 2].pack("Cx*C").should == "\x01\x02" end end @@ -55,10 +56,10 @@ describe "Array#pack with format 'X'" do end it "raises an ArgumentError if the output string is empty" do - -> { [1, 2, 3].pack("XC") }.should raise_error(ArgumentError) + -> { [1, 2, 3].pack("XC") }.should.raise(ArgumentError) end it "raises an ArgumentError if the count modifier is greater than the bytes in the string" do - -> { [1, 2, 3].pack("C2X3") }.should raise_error(ArgumentError) + -> { [1, 2, 3].pack("C2X3") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/pack/z_spec.rb b/spec/ruby/core/array/pack/z_spec.rb index 82ce7b4a1c..5cd084c825 100644 --- a/spec/ruby/core/array/pack/z_spec.rb +++ b/spec/ruby/core/array/pack/z_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -12,11 +12,21 @@ 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(TypeError) + end + it "adds all the bytes and appends a NULL byte when passed the '*' modifier" do ["abc"].pack("Z*").should == "abc\x00" end - it "padds the output with NULL bytes when the count exceeds the size of the String" do + it "pads the output with NULL bytes when the count exceeds the size of the String" do ["abc"].pack("Z6").should == "abc\x00\x00\x00" end |
