test/test_marshal_structure.rb in marshal-structure-1.1.1 vs test/test_marshal_structure.rb in marshal-structure-2.0

- old
+ new

@@ -1,171 +1,37 @@ -require 'minitest/autorun' -require 'marshal/structure' -require 'ben_string' -require 'openssl' -require 'pp' +require 'marshal/structure/test_case' -class OpenSSL::X509::Name - alias _dump_data to_a +class TestMarshalStructure < Marshal::Structure::TestCase - def _load_data ary - ary.each do |entry| - add_entry(*entry) + def test_class_load + ary = %W[\x04 \x08 T] + def ary.getc + shift end - end -end -class B; end + result = @MS.load ary -module C; end - -module E; end - -class M - def marshal_dump - 'marshal_dump' + assert_equal :true, result end - def marshal_load o + def test_count_allocations + assert_equal 1, @MS.new("\x04\x08[\x06T").count_allocations end -end -class U - def self._load str - new + def test_load + assert_equal [true], @MS.new("\x04\x08[\x06T").load end - def _dump limit - s = '_dump' - s.instance_variable_set :@ivar_on_dump_str, 'value on ivar on dump str' - s + def test_structure + assert_equal [:array, 0, 1, :true], @MS.new("\x04\x08[\x06T").structure end -end -S = Struct.new :f + def test_token_stream + stream = @MS.new("\x04\x08[\x06T").token_stream -class TestMarshalStructure < MiniTest::Unit::TestCase + assert_kind_of Enumerator, stream - def mu_pp obj - s = '' - s = PP.pp obj, s - s.chomp - end - - def setup - @MS = Marshal::Structure - end - - def test_construct - str = - "\004\b{\006:\006a[\031c\006Bm\006C\"\006d/\006e\000i\006" \ - "f\0322.2999999999999998\000ff" \ - "l+\n\000\000\000\000\000\000\000\000\001\0000TF}\000i\000" \ - "S:\006S\006:\006fi\000o:\vObject\000@\017" \ - "U:\006M\"\021marshal_dump" \ - "Iu:\006U\n_dump\006" \ - ":\026@ivar_on_dump_str\"\036value on ivar on dump str" \ - ";\000e:\006Eo;\b\000" \ - "I\"\025string with ivar\006:\v@value\"\017some value" \ - "C:\016BenString\"\000" - - structure = @MS.load str - - expected = [ - :hash, - 0, - 1, - [:symbol, 0, "a"], - [:array, - 1, - 20, - [:class, 2, "B"], - [:module, 3, "C"], - [:string, 4, "d"], - [:regexp, 5, "e", 0], - [:fixnum, 1], - [:float, 6, "2.2999999999999998\000ff"], - [:bignum, 7, 1, 10, 18446744073709551616], - :nil, - :true, - :false, - [:hash_default, 8, 0, [:fixnum, 0]], - [:struct, 9, [:symbol, 1, "S"], 1, [:symbol, 2, "f"], [:fixnum, 0]], - [:object, 10, [:symbol, 3, "Object"], [0]], - [:link, 10], - [:user_marshal, 11, [:symbol, 4, "M"], [:string, 12, "marshal_dump"]], - [:instance_variables, - [:user_defined, 13, [:symbol, 5, "U"], "_dump"], - 1, - [:symbol, 6, "@ivar_on_dump_str"], - [:string, 14, "value on ivar on dump str"]], - [:symbol_link, 0], - [:extended, [:symbol, 7, "E"], [:object, 15, [:symbol_link, 3], [0]]], - [:instance_variables, - [:string, 16, "string with ivar"], - 1, - [:symbol, 8, "@value"], - [:string, 17, "some value"]], - [:user_class, [:symbol, 9, "BenString"], [:string, 18, ""]]]] - - assert_equal expected, structure - end - - def test_construct_data - name = OpenSSL::X509::Name.parse 'CN=nobody/DC=example' - str = Marshal.dump name - - expected = [ - :data, - 0, - [:symbol, 0, "OpenSSL::X509::Name"], - [:array, - 1, - 2, - [:array, 2, 3, - [:string, 3, "CN"], - [:string, 4, "nobody"], - [:fixnum, 12]], - [:array, 5, 3, - [:string, 6, "DC"], - [:string, 7, "example"], - [:fixnum, 22]]]] - - assert_equal expected, @MS.load(str) - end - - def test_construct_module_old - assert_equal [:module, 0, "M"], @MS.load("\x04\x08M\x06M") - end - - def test_consume - ms = @MS.new "\x04\x08\x06M" - - assert_equal "\x06M", ms.consume(2) - end - - def test_consume_bytes - ms = @MS.new "\x04\x08\x06M" - - assert_equal [6, 77], ms.consume_bytes(2) - end - - def test_consume_byte - ms = @MS.new "\x04\x08M" - - assert_equal 77, ms.consume_byte - end - - def test_consume_character - ms = @MS.new "\x04\x08M" - - assert_equal 'M', ms.consume_character - end - - def test_get_byte_sequence - ms = @MS.new "\x04\x08\x06M" - - assert_equal "M", ms.get_byte_sequence + assert_equal [:array, 1, :true], stream.to_a end end