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