spec/packets/structure_spec.rb in cosmos-3.0.1 vs spec/packets/structure_spec.rb in cosmos-3.1.0
- old
+ new
@@ -1,419 +1,419 @@
-# encoding: ascii-8bit
-
-# Copyright 2014 Ball Aerospace & Technologies Corp.
-# All Rights Reserved.
-#
-# This program is free software; you can modify and/or redistribute it
-# under the terms of the GNU General Public License
-# as published by the Free Software Foundation; version 3 with
-# attribution addendums as found in the LICENSE.txt
-
-require 'spec_helper'
-require 'cosmos'
-require 'cosmos/packets/structure'
-
-module Cosmos
-
- describe Structure do
-
- describe "initialize" do
- it "should complain about non string buffers" do
- expect { Structure.new(:BIG_ENDIAN, Array.new) }.to raise_error(TypeError, "wrong argument type Array (expected String)")
- end
-
- it "should complain about unrecognized data types" do
- expect { Structure.new(:BLAH) }.to raise_error(ArgumentError, "Unrecognized endianness: BLAH - Must be :BIG_ENDIAN or :LITTLE_ENDIAN")
- end
-
- it "should create BIG_ENDIAN structures" do
- Structure.new(:BIG_ENDIAN).default_endianness.should eql :BIG_ENDIAN
- end
-
- it "should create LITTLE_ENDIAN structures" do
- Structure.new(:LITTLE_ENDIAN).default_endianness.should eql :LITTLE_ENDIAN
- end
- end # describe "initialize"
-
- describe "define_item" do
- before(:each) do
- @s = Structure.new
- end
-
- it "should add item to items and sorted_items" do
- @s.items["test1"].should be_nil
- @s.sorted_items[0].should be_nil
- @s.define_item("test1", 0, 8, :UINT)
- @s.items["TEST1"].should_not be_nil
- @s.sorted_items[0].should_not be_nil
- @s.sorted_items[0].name.should eql "TEST1"
- @s.defined_length.should eql 1
- @s.fixed_size.should be_truthy
- end
-
- it "should add items with negative offsets" do
- @s.define_item("test1", -8, 8, :UINT)
- @s.defined_length.should eql 1
- @s.define_item("test2", 0, 4, :UINT)
- @s.defined_length.should eql 2
- @s.define_item("test3", 4, 4, :UINT)
- @s.defined_length.should eql 2
- @s.define_item("test4", 8, 0, :BLOCK)
- @s.defined_length.should eql 2
- @s.define_item("test5", -16, 8, :UINT)
- @s.defined_length.should eql 3
- @s.fixed_size.should be_falsey
- end
-
- it "should add item with negative offset" do
- expect { @s.define_item("test11", -64, 8, :UINT, 128) }.to raise_error(ArgumentError, "TEST11: Can't define an item with array_size 128 greater than negative bit_offset -64")
- expect { @s.define_item("test10", -64, 8, :UINT, -64) }.to raise_error(ArgumentError, "TEST10: Can't define an item with negative array_size -64 and negative bit_offset -64")
- expect { @s.define_item("test9", -64, -64, :BLOCK) }.to raise_error(ArgumentError, "TEST9: Can't define an item with negative bit_size -64 and negative bit_offset -64")
- expect { @s.define_item("test8", 0, -32, :BLOCK, 64) }.to raise_error(ArgumentError, "TEST8: bit_size cannot be negative or zero for array items")
- expect { @s.define_item("test7", 0, 0, :BLOCK, 64) }.to raise_error(ArgumentError, "TEST7: bit_size cannot be negative or zero for array items")
- expect { @s.define_item("test6", -24, 32, :UINT) }.to raise_error(ArgumentError, "TEST6: Can't define an item with bit_size 32 greater than negative bit_offset -24")
- @s.define_item("test5", -16, 8, :UINT)
- @s.defined_length.should eql 2
- @s.define_item("test1", -8, 8, :UINT)
- @s.defined_length.should eql 2
- @s.define_item("test2", 0, 4, :UINT)
- @s.defined_length.should eql 3
- @s.define_item("test3", 4, 4, :UINT)
- @s.defined_length.should eql 3
- @s.define_item("test4", 8, 0, :BLOCK)
- @s.defined_length.should eql 3
- @s.fixed_size.should be_falsey
- end
-
- it "should recalulate sorted_items when adding multiple items" do
- @s.define_item("test1", 8, 32, :UINT)
- @s.sorted_items[0].name.should eql "TEST1"
- @s.defined_length.should eql 5
- @s.define_item("test2", 0, 8, :UINT)
- @s.sorted_items[0].name.should eql "TEST2"
- @s.defined_length.should eql 5
- @s.define_item("test3", 16, 8, :UINT)
- @s.sorted_items[-1].name.should eql "TEST3"
- @s.defined_length.should eql 5
- @s.fixed_size.should be_truthy
- end
-
- it "should overwrite existing items" do
- @s.define_item("test1", 0, 8, :UINT)
- @s.sorted_items[0].name.should eql "TEST1"
- @s.items["TEST1"].bit_size.should eql 8
- @s.items["TEST1"].data_type.should eql :UINT
- @s.defined_length.should eql 1
- @s.define_item("test1", 0, 16, :INT)
- @s.sorted_items[0].name.should eql "TEST1"
- @s.items["TEST1"].bit_size.should eql 16
- @s.items["TEST1"].data_type.should eql :INT
- @s.defined_length.should eql 2
- @s.fixed_size.should be_truthy
- end
- end # describe "define_item"
-
- describe "append_item" do
- before(:each) do
- @s = Structure.new
- end
-
- it "should append an item to items" do
- @s.define_item("test1", 0, 8, :UINT)
- @s.items["TEST1"].bit_size.should eql 8
- @s.sorted_items[0].name.should eql "TEST1"
- @s.sorted_items[1].should be_nil
- @s.defined_length.should eql 1
- @s.append_item("test2", 16, :UINT)
- @s.items["TEST2"].bit_size.should eql 16
- @s.sorted_items[0].name.should eql "TEST1"
- @s.sorted_items[1].name.should eql "TEST2"
- @s.defined_length.should eql 3
- end
-
- it "should append an item after an array item " do
- @s.define_item("test1", 0, 8, :UINT, 16)
- @s.items["TEST1"].bit_size.should eql 8
- @s.sorted_items[0].name.should eql "TEST1"
- @s.sorted_items[1].should be_nil
- @s.defined_length.should eql 2
- @s.append_item("test2", 16, :UINT)
- @s.items["TEST2"].bit_size.should eql 16
- @s.sorted_items[0].name.should eql "TEST1"
- @s.sorted_items[1].name.should eql "TEST2"
- @s.defined_length.should eql 4
- end
-
- it "should complain if appending after a variably sized item" do
- @s.define_item("test1", 0, 0, :BLOCK)
- expect { @s.append_item("test2", 8, :UINT) }.to raise_error(ArgumentError, "Can't append an item after a variably sized item")
- end
-
- it "should complain if appending after a variably sized array" do
- @s.define_item("test1", 0, 8, :UINT, -8)
- expect { @s.append_item("test2", 8, :UINT) }.to raise_error(ArgumentError, "Can't append an item after a variably sized item")
- end
- end # describe "define_item"
-
- describe "get_item" do
- before(:each) do
- @s = Structure.new
- @s.define_item("test1", 0, 8, :UINT)
- end
-
- it "should return a defined item" do
- @s.get_item("test1").should_not be_nil
- end
-
- it "should complain if an item doesn't exist" do
- expect { @s.get_item("test2") }.to raise_error(ArgumentError, "Unknown item: test2")
- end
- end
-
- describe "set_item" do
- before(:each) do
- @s = Structure.new
- @s.define_item("test1", 0, 8, :UINT)
- end
-
- it "should set a defined item" do
- item = @s.get_item("test1")
- item.bit_size.should eql 8
- item.bit_size = 16
- @s.set_item(item)
- @s.get_item("test1").bit_size.should eql 16
- end
-
- it "should complain if an item doesn't exist" do
- item = @s.get_item("test1")
- item.name = "TEST2"
- expect { @s.set_item(item) }.to raise_error(ArgumentError, "Unknown item: TEST2 - Ensure item name is uppercase")
- end
- end
-
- describe "read_item" do
- it "should complain if no buffer given" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT)
- expect { s.read_item(s.get_item("test1"), :RAW, nil) }.to raise_error(RuntimeError, "No buffer given to read_item")
- end
-
- it "should read data from the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT)
- buffer = "\x01"
- s.read_item(s.get_item("test1"), :RAW, buffer).should eql 1
- end
-
- it "should read array data from the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT, 16)
- buffer = "\x01\x02"
- s.read_item(s.get_item("test1"), :RAW, buffer).should eql [1,2]
- end
- end
-
- describe "write_item" do
- it "should complain if no buffer given" do
- expect { Structure.new.write_item(nil, nil, nil, nil) }.to raise_error(RuntimeError, "No buffer given to write_item")
- end
-
- it "should write data to the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT)
- buffer = "\x01"
- s.read_item(s.get_item("test1"), :RAW, buffer).should eql 1
- s.write_item(s.get_item("test1"), 2, :RAW, buffer)
- s.read_item(s.get_item("test1"), :RAW, buffer).should eql 2
- end
-
- it "should write array data to the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT, 16)
- buffer = "\x01\x02"
- s.read_item(s.get_item("test1"), :RAW, buffer).should eql [1,2]
- s.write_item(s.get_item("test1"), [3,4], :RAW, buffer)
- s.read_item(s.get_item("test1"), :RAW, buffer).should eql [3,4]
- end
- end
-
- describe "read" do
- it "should complain if item doesn't exist" do
- expect { Structure.new.read("BLAH") }.to raise_error(ArgumentError, "Unknown item: BLAH")
- end
-
- it "should read data from the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT)
- buffer = "\x01"
- s.read("test1", :RAW, buffer).should eql 1
- end
-
- it "should read array data from the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT, 16)
- buffer = "\x01\x02"
- s.read("test1", :RAW, buffer).should eql [1,2]
- end
- end
-
- describe "write" do
- it "should complain if item doesn't exist" do
- expect { Structure.new.write("BLAH", 0) }.to raise_error(ArgumentError, "Unknown item: BLAH")
- end
-
- it "should write data to the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT)
- buffer = "\x01"
- s.read("test1", :RAW, buffer).should eql 1
- s.write("test1", 2, :RAW, buffer)
- s.read("test1", :RAW, buffer).should eql 2
- end
-
- it "should write array data to the buffer" do
- s = Structure.new
- s.define_item("test1", 0, 8, :UINT, 16)
- buffer = "\x01\x02"
- s.read("test1", :RAW, buffer).should eql [1,2]
- s.write("test1", [3,4], :RAW, buffer)
- s.read("test1", :RAW, buffer).should eql [3,4]
- end
- end
-
- describe "read_all" do
- it "should read all defined items" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT, 16)
- s.append_item("test2", 16, :UINT)
- s.append_item("test3", 32, :UINT)
-
- buffer = "\x01\x02\x03\x04\x05\x06\x07\x08"
- vals = s.read_all(:RAW, buffer)
- vals[0][0].should eql "TEST1"
- vals[1][0].should eql "TEST2"
- vals[2][0].should eql "TEST3"
- vals[0][1].should eql [1,2]
- vals[1][1].should eql 0x0304
- vals[2][1].should eql 0x05060708
- end
- end
-
- describe "formatted" do
- it "should print out all the items and values" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT, 16)
- s.write("test1", [1,2])
- s.append_item("test2", 16, :UINT)
- s.write("test2", 3456)
- s.append_item("test3", 32, :BLOCK)
- s.write("test3", "\x07\x08\x09\x0A")
- s.formatted.should include("TEST1: [1, 2]")
- s.formatted.should include("TEST2: 3456")
- s.formatted.should include("TEST3")
- s.formatted.should include("00000000: 07 08 09 0A")
- end
- end
-
- describe "buffer" do
- it "should return the buffer" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT, 16)
- s.write("test1", [1,2])
- s.append_item("test2", 16, :UINT)
- s.write("test2", 0x0304)
- s.append_item("test3", 32, :UINT)
- s.write("test3", 0x05060708)
- s.buffer.should eql "\x01\x02\x03\x04\x05\x06\x07\x08"
- end
- end
-
- describe "buffer=" do
- it "should complain if the given buffer is too small" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 16, :UINT)
- expect { s.buffer = "\x00" }.to raise_error(RuntimeError, "Buffer length less than defined length")
- end
-
- it "should complain if the given buffer is too big" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 16, :UINT)
- expect { s.buffer = "\x00\x00\x00" }.to raise_error(RuntimeError, "Buffer length greater than defined length")
- end
-
- it "shouldn't complain if the given buffer is too big and we're not fixed length" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT)
- s.append_item("test2", 0, :BLOCK)
- s.buffer = "\x01\x02\x03"
- s.read("test1").should eql 1
- s.read("test2").should eql "\x02\x03"
- end
-
- it "should set the buffer" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT, 16)
- s.write("test1", [1,2])
- s.append_item("test2", 16, :UINT)
- s.write("test2", 0x0304)
- s.append_item("test3", 32, :UINT)
- s.write("test3", 0x05060708)
- s.read("test1").should eql [1,2]
- s.read("test2").should eql 0x0304
- s.read("test3").should eql 0x05060708
- s.buffer = "\x00\x01\x02\x03\x04\x05\x06\x07"
- s.read("test1").should eql [0,1]
- s.read("test2").should eql 0x0203
- s.read("test3").should eql 0x04050607
- end
- end
-
- describe "clone" do
- it "should duplicate the structure with a new buffer" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT, 16)
- s.write("test1", [1,2])
- s.append_item("test2", 16, :UINT)
- s.write("test2", 0x0304)
- s.append_item("test3", 32, :UINT)
- s.write("test3", 0x05060708)
-
- s2 = s.clone
- s2.read("test1").should eql [1,2]
- s2.read("test2").should eql 0x0304
- s2.read("test3").should eql 0x05060708
- s2.write("test1", [0,0])
- s2.read("test1").should eql [0,0]
- # Ensure we didn't change the original
- s.read("test1").should eql [1,2]
- end
- end
-
- describe "enable_method_missing" do
- it "should enable reading by name" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT, 16)
- s.write("test1", [1,2])
- s.enable_method_missing
- s.test1.should eql [1,2]
- end
-
- it "should enable writing by name" do
- s = Structure.new(:BIG_ENDIAN)
- s.append_item("test1", 8, :UINT, 16)
- s.write("test1", [1,2])
- s.enable_method_missing
- s.test1.should eql [1,2]
- s.test1 = [3,4]
- s.test1.should eql [3,4]
- end
-
- it "should complain if it can't find an item" do
- s = Structure.new(:BIG_ENDIAN)
- s.enable_method_missing
- expect { s.test1 }.to raise_error(ArgumentError, "Unknown item: test1")
- end
- end
-
- end # describe Structure
-
-end
+# encoding: ascii-8bit
+
+# Copyright 2014 Ball Aerospace & Technologies Corp.
+# All Rights Reserved.
+#
+# This program is free software; you can modify and/or redistribute it
+# under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 3 with
+# attribution addendums as found in the LICENSE.txt
+
+require 'spec_helper'
+require 'cosmos'
+require 'cosmos/packets/structure'
+
+module Cosmos
+
+ describe Structure do
+
+ describe "initialize" do
+ it "should complain about non string buffers" do
+ expect { Structure.new(:BIG_ENDIAN, Array.new) }.to raise_error(TypeError, "wrong argument type Array (expected String)")
+ end
+
+ it "should complain about unrecognized data types" do
+ expect { Structure.new(:BLAH) }.to raise_error(ArgumentError, "Unrecognized endianness: BLAH - Must be :BIG_ENDIAN or :LITTLE_ENDIAN")
+ end
+
+ it "should create BIG_ENDIAN structures" do
+ Structure.new(:BIG_ENDIAN).default_endianness.should eql :BIG_ENDIAN
+ end
+
+ it "should create LITTLE_ENDIAN structures" do
+ Structure.new(:LITTLE_ENDIAN).default_endianness.should eql :LITTLE_ENDIAN
+ end
+ end # describe "initialize"
+
+ describe "define_item" do
+ before(:each) do
+ @s = Structure.new
+ end
+
+ it "should add item to items and sorted_items" do
+ @s.items["test1"].should be_nil
+ @s.sorted_items[0].should be_nil
+ @s.define_item("test1", 0, 8, :UINT)
+ @s.items["TEST1"].should_not be_nil
+ @s.sorted_items[0].should_not be_nil
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.defined_length.should eql 1
+ @s.fixed_size.should be_truthy
+ end
+
+ it "should add items with negative offsets" do
+ @s.define_item("test1", -8, 8, :UINT)
+ @s.defined_length.should eql 1
+ @s.define_item("test2", 0, 4, :UINT)
+ @s.defined_length.should eql 2
+ @s.define_item("test3", 4, 4, :UINT)
+ @s.defined_length.should eql 2
+ @s.define_item("test4", 8, 0, :BLOCK)
+ @s.defined_length.should eql 2
+ @s.define_item("test5", -16, 8, :UINT)
+ @s.defined_length.should eql 3
+ @s.fixed_size.should be_falsey
+ end
+
+ it "should add item with negative offset" do
+ expect { @s.define_item("test11", -64, 8, :UINT, 128) }.to raise_error(ArgumentError, "TEST11: Can't define an item with array_size 128 greater than negative bit_offset -64")
+ expect { @s.define_item("test10", -64, 8, :UINT, -64) }.to raise_error(ArgumentError, "TEST10: Can't define an item with negative array_size -64 and negative bit_offset -64")
+ expect { @s.define_item("test9", -64, -64, :BLOCK) }.to raise_error(ArgumentError, "TEST9: Can't define an item with negative bit_size -64 and negative bit_offset -64")
+ expect { @s.define_item("test8", 0, -32, :BLOCK, 64) }.to raise_error(ArgumentError, "TEST8: bit_size cannot be negative or zero for array items")
+ expect { @s.define_item("test7", 0, 0, :BLOCK, 64) }.to raise_error(ArgumentError, "TEST7: bit_size cannot be negative or zero for array items")
+ expect { @s.define_item("test6", -24, 32, :UINT) }.to raise_error(ArgumentError, "TEST6: Can't define an item with bit_size 32 greater than negative bit_offset -24")
+ @s.define_item("test5", -16, 8, :UINT)
+ @s.defined_length.should eql 2
+ @s.define_item("test1", -8, 8, :UINT)
+ @s.defined_length.should eql 2
+ @s.define_item("test2", 0, 4, :UINT)
+ @s.defined_length.should eql 3
+ @s.define_item("test3", 4, 4, :UINT)
+ @s.defined_length.should eql 3
+ @s.define_item("test4", 8, 0, :BLOCK)
+ @s.defined_length.should eql 3
+ @s.fixed_size.should be_falsey
+ end
+
+ it "should recalulate sorted_items when adding multiple items" do
+ @s.define_item("test1", 8, 32, :UINT)
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.defined_length.should eql 5
+ @s.define_item("test2", 0, 8, :UINT)
+ @s.sorted_items[0].name.should eql "TEST2"
+ @s.defined_length.should eql 5
+ @s.define_item("test3", 16, 8, :UINT)
+ @s.sorted_items[-1].name.should eql "TEST3"
+ @s.defined_length.should eql 5
+ @s.fixed_size.should be_truthy
+ end
+
+ it "should overwrite existing items" do
+ @s.define_item("test1", 0, 8, :UINT)
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.items["TEST1"].bit_size.should eql 8
+ @s.items["TEST1"].data_type.should eql :UINT
+ @s.defined_length.should eql 1
+ @s.define_item("test1", 0, 16, :INT)
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.items["TEST1"].bit_size.should eql 16
+ @s.items["TEST1"].data_type.should eql :INT
+ @s.defined_length.should eql 2
+ @s.fixed_size.should be_truthy
+ end
+ end # describe "define_item"
+
+ describe "append_item" do
+ before(:each) do
+ @s = Structure.new
+ end
+
+ it "should append an item to items" do
+ @s.define_item("test1", 0, 8, :UINT)
+ @s.items["TEST1"].bit_size.should eql 8
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.sorted_items[1].should be_nil
+ @s.defined_length.should eql 1
+ @s.append_item("test2", 16, :UINT)
+ @s.items["TEST2"].bit_size.should eql 16
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.sorted_items[1].name.should eql "TEST2"
+ @s.defined_length.should eql 3
+ end
+
+ it "should append an item after an array item " do
+ @s.define_item("test1", 0, 8, :UINT, 16)
+ @s.items["TEST1"].bit_size.should eql 8
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.sorted_items[1].should be_nil
+ @s.defined_length.should eql 2
+ @s.append_item("test2", 16, :UINT)
+ @s.items["TEST2"].bit_size.should eql 16
+ @s.sorted_items[0].name.should eql "TEST1"
+ @s.sorted_items[1].name.should eql "TEST2"
+ @s.defined_length.should eql 4
+ end
+
+ it "should complain if appending after a variably sized item" do
+ @s.define_item("test1", 0, 0, :BLOCK)
+ expect { @s.append_item("test2", 8, :UINT) }.to raise_error(ArgumentError, "Can't append an item after a variably sized item")
+ end
+
+ it "should complain if appending after a variably sized array" do
+ @s.define_item("test1", 0, 8, :UINT, -8)
+ expect { @s.append_item("test2", 8, :UINT) }.to raise_error(ArgumentError, "Can't append an item after a variably sized item")
+ end
+ end # describe "define_item"
+
+ describe "get_item" do
+ before(:each) do
+ @s = Structure.new
+ @s.define_item("test1", 0, 8, :UINT)
+ end
+
+ it "should return a defined item" do
+ @s.get_item("test1").should_not be_nil
+ end
+
+ it "should complain if an item doesn't exist" do
+ expect { @s.get_item("test2") }.to raise_error(ArgumentError, "Unknown item: test2")
+ end
+ end
+
+ describe "set_item" do
+ before(:each) do
+ @s = Structure.new
+ @s.define_item("test1", 0, 8, :UINT)
+ end
+
+ it "should set a defined item" do
+ item = @s.get_item("test1")
+ item.bit_size.should eql 8
+ item.bit_size = 16
+ @s.set_item(item)
+ @s.get_item("test1").bit_size.should eql 16
+ end
+
+ it "should complain if an item doesn't exist" do
+ item = @s.get_item("test1")
+ item.name = "TEST2"
+ expect { @s.set_item(item) }.to raise_error(ArgumentError, "Unknown item: TEST2 - Ensure item name is uppercase")
+ end
+ end
+
+ describe "read_item" do
+ it "should complain if no buffer given" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT)
+ expect { s.read_item(s.get_item("test1"), :RAW, nil) }.to raise_error(RuntimeError, "No buffer given to read_item")
+ end
+
+ it "should read data from the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT)
+ buffer = "\x01"
+ s.read_item(s.get_item("test1"), :RAW, buffer).should eql 1
+ end
+
+ it "should read array data from the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT, 16)
+ buffer = "\x01\x02"
+ s.read_item(s.get_item("test1"), :RAW, buffer).should eql [1,2]
+ end
+ end
+
+ describe "write_item" do
+ it "should complain if no buffer given" do
+ expect { Structure.new.write_item(nil, nil, nil, nil) }.to raise_error(RuntimeError, "No buffer given to write_item")
+ end
+
+ it "should write data to the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT)
+ buffer = "\x01"
+ s.read_item(s.get_item("test1"), :RAW, buffer).should eql 1
+ s.write_item(s.get_item("test1"), 2, :RAW, buffer)
+ s.read_item(s.get_item("test1"), :RAW, buffer).should eql 2
+ end
+
+ it "should write array data to the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT, 16)
+ buffer = "\x01\x02"
+ s.read_item(s.get_item("test1"), :RAW, buffer).should eql [1,2]
+ s.write_item(s.get_item("test1"), [3,4], :RAW, buffer)
+ s.read_item(s.get_item("test1"), :RAW, buffer).should eql [3,4]
+ end
+ end
+
+ describe "read" do
+ it "should complain if item doesn't exist" do
+ expect { Structure.new.read("BLAH") }.to raise_error(ArgumentError, "Unknown item: BLAH")
+ end
+
+ it "should read data from the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT)
+ buffer = "\x01"
+ s.read("test1", :RAW, buffer).should eql 1
+ end
+
+ it "should read array data from the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT, 16)
+ buffer = "\x01\x02"
+ s.read("test1", :RAW, buffer).should eql [1,2]
+ end
+ end
+
+ describe "write" do
+ it "should complain if item doesn't exist" do
+ expect { Structure.new.write("BLAH", 0) }.to raise_error(ArgumentError, "Unknown item: BLAH")
+ end
+
+ it "should write data to the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT)
+ buffer = "\x01"
+ s.read("test1", :RAW, buffer).should eql 1
+ s.write("test1", 2, :RAW, buffer)
+ s.read("test1", :RAW, buffer).should eql 2
+ end
+
+ it "should write array data to the buffer" do
+ s = Structure.new
+ s.define_item("test1", 0, 8, :UINT, 16)
+ buffer = "\x01\x02"
+ s.read("test1", :RAW, buffer).should eql [1,2]
+ s.write("test1", [3,4], :RAW, buffer)
+ s.read("test1", :RAW, buffer).should eql [3,4]
+ end
+ end
+
+ describe "read_all" do
+ it "should read all defined items" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT, 16)
+ s.append_item("test2", 16, :UINT)
+ s.append_item("test3", 32, :UINT)
+
+ buffer = "\x01\x02\x03\x04\x05\x06\x07\x08"
+ vals = s.read_all(:RAW, buffer)
+ vals[0][0].should eql "TEST1"
+ vals[1][0].should eql "TEST2"
+ vals[2][0].should eql "TEST3"
+ vals[0][1].should eql [1,2]
+ vals[1][1].should eql 0x0304
+ vals[2][1].should eql 0x05060708
+ end
+ end
+
+ describe "formatted" do
+ it "should print out all the items and values" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT, 16)
+ s.write("test1", [1,2])
+ s.append_item("test2", 16, :UINT)
+ s.write("test2", 3456)
+ s.append_item("test3", 32, :BLOCK)
+ s.write("test3", "\x07\x08\x09\x0A")
+ s.formatted.should include("TEST1: [1, 2]")
+ s.formatted.should include("TEST2: 3456")
+ s.formatted.should include("TEST3")
+ s.formatted.should include("00000000: 07 08 09 0A")
+ end
+ end
+
+ describe "buffer" do
+ it "should return the buffer" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT, 16)
+ s.write("test1", [1,2])
+ s.append_item("test2", 16, :UINT)
+ s.write("test2", 0x0304)
+ s.append_item("test3", 32, :UINT)
+ s.write("test3", 0x05060708)
+ s.buffer.should eql "\x01\x02\x03\x04\x05\x06\x07\x08"
+ end
+ end
+
+ describe "buffer=" do
+ it "should complain if the given buffer is too small" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 16, :UINT)
+ expect { s.buffer = "\x00" }.to raise_error(RuntimeError, "Buffer length less than defined length")
+ end
+
+ it "should complain if the given buffer is too big" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 16, :UINT)
+ expect { s.buffer = "\x00\x00\x00" }.to raise_error(RuntimeError, "Buffer length greater than defined length")
+ end
+
+ it "shouldn't complain if the given buffer is too big and we're not fixed length" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT)
+ s.append_item("test2", 0, :BLOCK)
+ s.buffer = "\x01\x02\x03"
+ s.read("test1").should eql 1
+ s.read("test2").should eql "\x02\x03"
+ end
+
+ it "should set the buffer" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT, 16)
+ s.write("test1", [1,2])
+ s.append_item("test2", 16, :UINT)
+ s.write("test2", 0x0304)
+ s.append_item("test3", 32, :UINT)
+ s.write("test3", 0x05060708)
+ s.read("test1").should eql [1,2]
+ s.read("test2").should eql 0x0304
+ s.read("test3").should eql 0x05060708
+ s.buffer = "\x00\x01\x02\x03\x04\x05\x06\x07"
+ s.read("test1").should eql [0,1]
+ s.read("test2").should eql 0x0203
+ s.read("test3").should eql 0x04050607
+ end
+ end
+
+ describe "clone" do
+ it "should duplicate the structure with a new buffer" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT, 16)
+ s.write("test1", [1,2])
+ s.append_item("test2", 16, :UINT)
+ s.write("test2", 0x0304)
+ s.append_item("test3", 32, :UINT)
+ s.write("test3", 0x05060708)
+
+ s2 = s.clone
+ s2.read("test1").should eql [1,2]
+ s2.read("test2").should eql 0x0304
+ s2.read("test3").should eql 0x05060708
+ s2.write("test1", [0,0])
+ s2.read("test1").should eql [0,0]
+ # Ensure we didn't change the original
+ s.read("test1").should eql [1,2]
+ end
+ end
+
+ describe "enable_method_missing" do
+ it "should enable reading by name" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT, 16)
+ s.write("test1", [1,2])
+ s.enable_method_missing
+ s.test1.should eql [1,2]
+ end
+
+ it "should enable writing by name" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.append_item("test1", 8, :UINT, 16)
+ s.write("test1", [1,2])
+ s.enable_method_missing
+ s.test1.should eql [1,2]
+ s.test1 = [3,4]
+ s.test1.should eql [3,4]
+ end
+
+ it "should complain if it can't find an item" do
+ s = Structure.new(:BIG_ENDIAN)
+ s.enable_method_missing
+ expect { s.test1 }.to raise_error(ArgumentError, "Unknown item: test1")
+ end
+ end
+
+ end # describe Structure
+
+end