Removed the ancient Rake version test.
[ruby-eet.git] / test / test_misc.rb
1 require "eet"
2 require "test/unit"
3 require "ftools"
4
5 class Foo
6         def initialize
7                 @foo = "foo"
8                 @bar = true
9                 @baz = 512
10         end
11 end
12
13 class MiscTest < Test::Unit::TestCase
14         def setup
15                 @keys = ["key_foo", "key_bar", "key_baz"].sort
16                 @dest = __FILE__.sub(/\.rb$/, ".eet")
17                 @foo_data = Foo.new.to_eet
18
19                 assert_not_nil(@foo_data)
20
21                 File.rm_f(@dest)
22
23                 Eet::File.open(@dest, "w") do |ef|
24                         @keys.each do |k|
25                                 assert(ef.write(k, @foo_data) > 0)
26                         end
27                 end
28         end
29
30         def test_stream_chunks
31                 stream = nil
32
33                 assert_nothing_raised do
34                         stream = Eet::Stream.deserialize(@foo_data)
35                 end
36
37                 assert_equal(1, stream.length)
38                 assert_equal("Foo", stream.first.tag)
39
40                 assert_nothing_raised do
41                         stream = Eet::Stream.deserialize(stream.first.data)
42                 end
43
44                 assert_equal(3, stream.length)
45                 ["foo", "bar", "baz"].each do |tag|
46                         found = stream.find { |c| c.tag == tag }
47                         assert_not_nil(found)
48                 end
49         end
50
51         def test_create_new
52                 assert_raise(RuntimeError) do
53                         Eet::File.open("/foo/bar/baz/xyzzy", "w") { |ef| }
54                 end
55         end
56
57         def test_catch_invalid_open_mode
58                 assert_raise(ArgumentError) do
59                         Eet::File.open(@dest, @keys[0]) { |ef| }
60                 end
61         end
62
63         def test_zero_write
64                 assert_raise(IOError) do
65                         Eet::File.open(@dest, "w") do |ef|
66                                 ef.write("empty", "")
67                         end
68                 end
69         end
70
71         def test_open_existing_ro
72                 # if mode isn't specified, the file must be opened read-only.
73                 # assert that we cannot write in read-only mode.
74                 Eet::File.open(@dest) do |ef|
75                         assert_raise(IOError) do
76                                 ef.write("xyzzy", "data")
77                         end
78                 end
79         end
80
81         def test_open_existing_w
82                 # if the file is opened in write-only or read-write mode,
83                 # we must be able to write to it.
84                 ["w", "r+"].each do |mode|
85                         Eet::File.open(@dest, mode) do |ef|
86                                 assert_equal(4, ef.write("xyzzy", "data"))
87                         end
88                 end
89         end
90
91         def test_accessing_closed_file
92                 ef = Eet::File.open(@dest, "r")
93                 ef.close
94
95                 assert_raise(IOError) do
96                         ef.read(@keys[0])
97                 end
98         end
99
100         def test_delete_entry_ro
101                 assert_raise(IOError) do
102                         Eet::File.open(@dest, "r") do |ef|
103                                 ef.delete(@keys[0])
104                         end
105                 end
106         end
107
108         def test_delete_nonexisting_entry
109                 assert_raise(IOError) do
110                         Eet::File.open(@dest, "w") do |ef|
111                                 ef.delete("nonexistingkey")
112                         end
113                 end
114         end
115
116         def test_read
117                 Eet::File.open(@dest, "r") do |ef|
118                         @keys.each do |k|
119                                 assert_equal(@foo_data, ef.read(k))
120                         end
121                 end
122
123                 assert_raise(IOError) do
124                         Eet::File.open(@dest, "w") do |ef|
125                                 ef.read(@keys[0])
126                         end
127                 end
128         end
129
130         def test_entries_glob
131                 ["r", "r+"].each do |mode|
132                         Eet::File.open(@dest, mode) do |ef|
133                                 assert_equal(@keys, ef.entries.sort)
134                                 assert_equal(@keys, ef["*"].sort)
135                                 assert_equal([@keys[0]], ef[@keys[0]])
136                         end
137                 end
138
139                 assert_raise(IOError) do
140                         Eet::File.open(@dest, "w") do |ef|
141                                 ef.entries
142                         end
143                 end
144         end
145
146         def test_delete
147                 assert_nothing_raised do
148                         Eet::File.open(@dest, "r+") do |ef|
149                                 ef.delete(@keys[0])
150                         end
151                 end
152
153                 Eet::File.open(@dest) do |ef|
154                         tmp = @keys.shift
155                         assert_equal(@keys, ef.entries.sort)
156                         @keys.unshift(tmp)
157                 end
158         end
159
160         def test_invalid_key
161                 Eet::File.open(@dest, "r+") do |ef|
162                         assert_raise(ArgumentError) do
163                                 ef.read("key_\0foo")
164                         end
165
166                         assert_raise(ArgumentError) do
167                                 ef.write("key_\0foo", "data")
168                         end
169
170                         assert_raise(ArgumentError) do
171                                 ef.delete("key_\0foo")
172                         end
173                 end
174         end
175 end