spec/wrapper_spec.rb in bindata-0.11.0 vs spec/wrapper_spec.rb in bindata-0.11.1

- old
+ new

@@ -1,33 +1,26 @@ #!/usr/bin/env ruby require File.expand_path(File.join(File.dirname(__FILE__), "spec_common")) require 'bindata' -require 'bindata/wrapper' describe BinData::Wrapper, "with errors" do it "should not wrap more than one type" do lambda { - eval <<-END - class WrappedMultipleTypes < BinData::Wrapper - uint8 - uint8 - end - END + class WrappedMultipleTypes < BinData::Wrapper + uint8 + uint8 + end }.should raise_error(SyntaxError) end end describe BinData::Wrapper, "around a Primitive" do - before(:all) do - eval <<-END - class WrappedPrimitive < BinData::Wrapper - default_parameter :a => 3 + class WrappedPrimitive < BinData::Wrapper + default_parameter :a => 3 - uint8 :initial_value => :a - end - END + uint8 :initial_value => :a end it "should access custom parameter" do obj = WrappedPrimitive.new obj.value.should == 3 @@ -44,18 +37,14 @@ obj.value.should == 7 end end describe BinData::Wrapper, "around an Array" do - before(:all) do - eval <<-END - class WrappedIntArray < BinData::Wrapper - endian :big - default_parameter :initial_element_value => 0 - array :type => [:uint16, {:initial_value => :initial_element_value}] - end - END + class WrappedIntArray < BinData::Wrapper + endian :big + default_parameter :initial_element_value => 0 + array :type => [:uint16, {:initial_value => :initial_element_value}] end it "should forward parameters" do obj = WrappedIntArray.new(:initial_length => 7) obj.length.should == 7 @@ -66,19 +55,29 @@ obj.to_binary_s.should == "\x00\x05\x00\x05\x00\x05" end end describe BinData::Wrapper, "around a Choice" do - before(:all) do - eval <<-END - class WrappedChoice < BinData::Wrapper - endian :big - choice :choices => { 'a' => :uint8, 'b' => :uint16 } - end - END + class WrappedChoice < BinData::Wrapper + endian :big + choice :choices => { 'a' => :uint8, 'b' => :uint16 } end it "should forward parameters" do obj = WrappedChoice.new(:selection => 'b') obj.num_bytes.should == 2 + end +end + +describe BinData::Wrapper, "inside a struct" do + class WrappedUint32le < BinData::Wrapper + uint32le + end + + it "should handle onlyif" do + field1 = [:wrapped_uint32le, :a, {:onlyif => false, :value => 1 }] + field2 = [:wrapped_uint32le, :b, {:onlyif => true, :value => 2 }] + + obj = BinData::Struct.new(:fields => [field1, field2]) + obj.should == {'b' => 2} end end