spec/array_spec.rb in bindata-0.9.2 vs spec/array_spec.rb in bindata-0.9.3
- old
+ new
@@ -32,11 +32,11 @@
it "should not be a single_value" do
@data.should_not be_single_value
end
- it "should return correct length" do
+ it "should return zero length" do
@data.length.should be_zero
end
it "should be empty" do
@data.should be_empty
@@ -125,11 +125,11 @@
it "should not be empty" do
@data.should_not be_empty
end
- it "should return a nicely formatted array for inspect" do
+ it "should return a nicely formatted array for inspect" do
@data.inspect.should == "[1, 2, 3, 4, 5]"
end
it "should be able to use methods from Enumerable" do
@data.select { |x| (x % 2) == 0 }.should == [2, 4]
@@ -262,10 +262,26 @@
@data.read(io)
@data.length.should == 7
end
end
+describe BinData::Array, "with :read_until => :eof" do
+ it "should read records until eof" do
+ obj = BinData::Array.new(:type => :int8, :read_until => :eof)
+ data = "\x01\x02\x03"
+ obj.read(data)
+ obj.snapshot.should == [1, 2, 3]
+ end
+
+ it "should read records until eof, ignoring partial records" do
+ obj = BinData::Array.new(:type => :int16be, :read_until => :eof)
+ data = "\x00\x01\x00\x02\x03"
+ obj.read(data)
+ obj.snapshot.should == [1, 2]
+ end
+end
+
describe BinData::Array, "of bits" do
before(:each) do
@data = BinData::Array.new(:type => :bit1, :initial_length => 15)
end
@@ -294,8 +310,27 @@
@data.to_s.should == [0b0001_0000, 0b0000_0000].pack("CC")
end
it "should return num_bytes" do
@data.num_bytes.should == 2
+ end
+end
+
+describe BinData::Array, "nested within an Array" do
+ before(:each) do
+ nested_array_params = { :type => [:int8, { :initial_value => :index }],
+ :initial_length => lambda { index + 1 } }
+ @data = BinData::Array.new(:type => [:array, nested_array_params],
+ :initial_length => 3)
+ end
+
+ it "should use correct index" do
+ @data.snapshot.should == [ [0], [0, 1], [0, 1, 2] ]
+ end
+
+ it "should maintain structure when reading" do
+ str = "\x04\x05\x06\x07\x08\x09"
+ @data.read(str)
+ @data.snapshot.should == [ [4], [5, 6], [7, 8, 9] ]
end
end