Code cleanup.
[ruby-eet.git] / test / test_hash.rb
1 # $Id: test_hash.rb 1 2005-03-26 01:45:38Z tilman $
2
3 require "eet"
4 require "test/unit"
5 require "common"
6
7 class HashEntry
8         def initialize(key, value)
9                 @key = key.to_str.dup.freeze
10                 @value = value.to_str.dup.freeze
11         end
12 end
13
14 class HashTestData
15         def initialize
16                 @hash = {"artist" => HashEntry.new("artist", "Amon Amarth"),
17                          "title" => HashEntry.new("title", "Death In Fire")}
18         end
19
20         private
21         def to_eet_name
22                 "HashTest"
23         end
24
25         def to_eet_properties
26                 {"hash" => [@hash]}
27         end
28 end
29
30 class HashTest < Test::Unit::TestCase
31         def test_hash
32                 data = HashTestData.new.to_eet
33                 assert_not_nil(data)
34
35                 stream = nil
36
37                 assert_nothing_raised do
38                         stream = Eet::Stream.deserialize(data)
39                 end
40
41                 assert_equal(1, stream.length)
42                 assert_equal("HashTest", stream.first.tag)
43
44
45                 assert_nothing_raised do
46                         stream = Eet::Stream.deserialize(stream.first.data)
47                 end
48
49                 assert_equal(2, stream.length)
50
51                 2.times do
52                         hashlist = stream.find_all { |c| c.tag == "hash" }
53                         assert_equal(2, hashlist.length)
54
55                         hash = {"artist" => "Amon Amarth",
56                                 "title" => "Death In Fire"}
57                         hash.each do |key, value|
58                                 str_stream = nil
59
60                                 assert_nothing_raised do
61                                         d = hashlist.shift.data
62                                         str_stream = Eet::Stream.deserialize(d)
63                                 end
64
65                                 assert_equal(1, str_stream.length)
66                                 assert_equal("HashEntry", str_stream.first.tag)
67
68                                 foo = nil
69
70                                 assert_nothing_raised do
71                                         foo = Eet::Stream.deserialize(str_stream.first.data)
72                                 end
73
74                                 assert_equal(2, foo.length)
75
76                                 {"key" => key, "value" => value}.each do |tag, data|
77                                         found = foo.find { |i| i.tag == tag }
78                                         assert_not_nil(found)
79                                         assert_equal(data + "\0", found.data)
80                                 end
81                         end
82
83                         assert_equal([], hashlist)
84                 end
85         end
86 end