2 require "ogg/vorbis/tagger"
6 class MainTest < Test::Unit::TestCase
7 OGG_FILE = "test/test.ogg"
10 FileUtils.cp("test/sample.ogg", OGG_FILE)
14 FileUtils.rm_f(OGG_FILE)
18 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
19 # make sure the keys are returned in the correct order
20 assert_equal(["artist", "album", "date"], t.comments.keys)
21 assert_equal(["Bolt Thrower", "...For Victory", "1994"],
24 assert_equal(3, t.comments.length)
25 assert_equal(3, t.comments.size)
27 assert_equal("Bolt Thrower", t.comments["artist"])
28 assert_equal("...For Victory", t.comments["album"])
29 assert_equal("1994", t.comments["date"])
33 def test_write_is_non_destructive
34 a = Digest::MD5.hexdigest(File.read(OGG_FILE))
36 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
40 b = Digest::MD5.hexdigest(File.read(OGG_FILE))
45 def test_multiple_writes
46 a = Digest::MD5.hexdigest(File.read(OGG_FILE))
48 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
54 b = Digest::MD5.hexdigest(File.read(OGG_FILE))
59 def test_write_stable_order
60 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
61 assert_equal(3, t.write)
64 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
65 assert_equal(["artist", "album", "date"], t.comments.keys)
69 def test_write_stable_order_change
70 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
71 t.comments["artist"] = "Ballista"
72 assert_equal(3, t.write)
75 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
76 assert_equal(["artist", "album", "date"], t.comments.keys)
80 def test_write_keeps_file_mode
81 mode = File.stat(OGG_FILE).mode
83 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
87 assert_equal(mode, File.stat(OGG_FILE).mode)
91 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
92 t.comments["genre"] = "Death Metal"
93 assert_equal(4, t.write)
96 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
97 assert_equal("Death Metal", t.comments["genre"])
102 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
103 assert_equal("...For Victory", t.comments.delete("album"))
104 assert_nil(t.comments.delete("foo"))
105 assert_equal(2, t.write)
108 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
109 assert_equal(["artist", "date"], t.comments.keys)
114 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
116 assert_equal(0, t.write)
119 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
120 assert(t.comments.empty?)
125 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
126 assert(!t.comments.empty?)
128 t.comments.delete("artist")
129 t.comments.delete("album")
130 t.comments.delete("date")
132 assert_equal(0, t.write)
135 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
136 assert(t.comments.empty?)
141 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
143 "artist" => "Bolt Thrower",
144 "album" => "...For Victory",
149 t.comments.each do |k, v|
158 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
161 t.comments.each_key do |k|
165 assert_equal(["artist", "album", "date"], b)
170 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
173 t.comments.each_value do |v|
177 assert_equal(["Bolt Thrower", "...For Victory", "1994"], b)
182 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
184 {"artist"=>"Bolt Thrower", "album"=>"...For Victory", "date"=>"1994"}
186 assert_equal(tmp.strip, t.comments.inspect)
191 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
192 assert(t.comments.has_key?("artist"))
193 assert(!t.comments.has_key?("foo"))
195 assert(t.comments.key?("artist"))
196 assert(t.comments.include?("artist"))
197 assert(t.comments.member?("artist"))
205 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
209 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
213 assert_equal(0, a <=> b)
215 assert_equal(-1, a <=> b)
218 def test_modify_existing_key
219 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
220 assert_raises(TypeError) do
221 t.comments.keys.first.replace("new")
226 def test_modify_added_key
227 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
228 t.comments["Foo"] = "Bar"
230 assert_raises(TypeError) do
231 t.comments.keys.last.replace("new")
238 "artist" => "Ballista",
239 "genre" => "Death Metal",
242 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
243 t.comments.merge!(repl)
245 assert_equal(["artist", "album", "date", "genre"],
247 assert_equal(["Ballista", "...For Victory", "1994",
254 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
255 assert_equal(["artist", "Bolt Thrower"], t.comments.shift)
256 assert_equal(["album", "...For Victory"], t.comments.shift)
257 assert_equal(["date", "1994"], t.comments.shift)
258 assert_equal(nil, t.comments.shift)
263 Ogg::Vorbis::Tagger.new(OGG_FILE).close
267 t = Ogg::Vorbis::Tagger.new(OGG_FILE)
270 assert_raise(Ogg::Vorbis::Tagger::ClosedStreamError) do
274 assert_raise(Ogg::Vorbis::Tagger::ClosedStreamError) do
275 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
281 def test_open_non_existing_file
282 assert_raises(Ogg::Vorbis::Tagger::OpenError) do
283 Ogg::Vorbis::Tagger.new("foo.bar")
287 def test_open_non_ogg_file
288 File.open("test/foo.bar", "w") do |f|
289 f << "foobarbazxyzzy"
292 assert_raises(Ogg::Vorbis::Tagger::InvalidDataError) do
293 Ogg::Vorbis::Tagger.new("test/foo.bar")
296 FileUtils.rm_f("test/foo.bar")
299 def test_standalone_comments_object
300 o = Ogg::Vorbis::Comments.new
301 assert_equal(o.length, 0);