13 class MiscTest < Test::Unit::TestCase
15 @keys = ["key_foo", "key_bar", "key_baz"].sort
16 @dest = __FILE__.sub(/\.rb$/, ".eet")
17 @foo_data = Foo.new.to_eet
19 assert_not_nil(@foo_data)
23 Eet::File.open(@dest, "w") do |ef|
25 assert(ef.write(k, @foo_data) > 0)
30 def test_stream_chunks
33 assert_nothing_raised do
34 stream = Eet::Stream.deserialize(@foo_data)
37 assert_equal(1, stream.length)
38 assert_equal("Foo", stream.first.tag)
40 assert_nothing_raised do
41 stream = Eet::Stream.deserialize(stream.first.data)
44 assert_equal(3, stream.length)
45 ["foo", "bar", "baz"].each do |tag|
46 found = stream.find { |c| c.tag == tag }
52 assert_raise(RuntimeError) do
53 Eet::File.open("/foo/bar/baz/xyzzy", "w") { |ef| }
57 def test_catch_invalid_open_mode
58 assert_raise(ArgumentError) do
59 Eet::File.open(@dest, @keys[0]) { |ef| }
64 assert_raise(IOError) do
65 Eet::File.open(@dest, "w") do |ef|
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")
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"))
91 def test_accessing_closed_file
92 ef = Eet::File.open(@dest, "r")
95 assert_raise(IOError) do
100 def test_delete_entry_ro
101 assert_raise(IOError) do
102 Eet::File.open(@dest, "r") do |ef|
108 def test_delete_nonexisting_entry
109 assert_raise(IOError) do
110 Eet::File.open(@dest, "w") do |ef|
111 ef.delete("nonexistingkey")
117 Eet::File.open(@dest, "r") do |ef|
119 assert_equal(@foo_data, ef.read(k))
123 assert_raise(IOError) do
124 Eet::File.open(@dest, "w") do |ef|
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]])
139 assert_raise(IOError) do
140 Eet::File.open(@dest, "w") do |ef|
147 assert_nothing_raised do
148 Eet::File.open(@dest, "r+") do |ef|
153 Eet::File.open(@dest) do |ef|
155 assert_equal(@keys, ef.entries.sort)
161 Eet::File.open(@dest, "r+") do |ef|
162 assert_raise(ArgumentError) do
166 assert_raise(ArgumentError) do
167 ef.write("key_\0foo", "data")
170 assert_raise(ArgumentError) do
171 ef.delete("key_\0foo")