782ba974e671507a0aa107d160a8cd75abbb0a4b
[ruby-eet.git] / test / test_list.rb
1 # $Id: test_list.rb 1 2005-03-26 01:45:38Z tilman $
2
3 require "eet"
4 require "test/unit"
5 require "common"
6
7 class ListTestData
8         def initialize
9                 @strings = {}
10                 @strings2 = []
11
12                 5.times do |i|
13                         @strings[i] = WrappedString.new("rubyrocks%i" % i)
14                 end
15
16                 #(6..10).each do |i|
17                 5.times do |i|
18                         @strings2 << WrappedString.new("rubyrocks%i" % i)
19                 end
20         end
21
22         private
23         def to_eet_name
24                 "ListTest"
25         end
26
27         def to_eet_properties
28                 {"stringlist_a" => [@strings],
29                  "stringlist_h" => [@strings2]}
30         end
31 end
32
33 class ListTest < Test::Unit::TestCase
34         def test_list
35                 data = ListTestData.new.to_eet
36                 assert_not_nil(data)
37
38                 stream = nil
39
40                 assert_nothing_raised do
41                         stream = Eet::Stream.deserialize(data)
42                 end
43
44                 assert_equal(1, stream.length)
45                 assert_equal("ListTest", stream.first.tag)
46
47                 assert_nothing_raised do
48                         stream = Eet::Stream.deserialize(stream.first.data)
49                 end
50
51                 assert_equal(10, stream.length)
52
53                 ["stringlist_a", "stringlist_h"].each do |tag|
54                         stringlist = stream.find_all { |c| c.tag == tag }
55                         assert_equal(5, stringlist.length)
56
57                         (0..4).each do |i|
58                                 str_stream = nil
59
60                                 assert_nothing_raised do
61                                         d = stringlist.shift.data
62                                         str_stream = Eet::Stream.deserialize(d)
63                                 end
64
65                                 assert_equal(1, str_stream.length)
66                                 assert_equal("String", 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(1, foo.length)
75                                 assert_equal("buf", foo.first.tag)
76                                 assert_equal("rubyrocks%i\0" % i, foo.first.data)
77                         end
78
79                         assert_equal([], stringlist)
80                 end
81         end
82 end