Prefer rb_funcall2() over rb_funcall().
[ruby-vorbistagger.git] / test / test_main.rb
1 require "test/unit"
2 require "ogg/vorbis/tagger"
3 require "fileutils"
4 require "digest/md5"
5
6 class MainTest < Test::Unit::TestCase
7         OGG_FILE = "test/test.ogg"
8
9         def setup
10                 FileUtils.cp("test/sample.ogg", OGG_FILE)
11         end
12
13         def teardown
14                 FileUtils.rm_f(OGG_FILE)
15         end
16
17         def test_read
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"],
22                                      t.comments.values)
23
24                         assert_equal(3, t.comments.length)
25                         assert_equal(3, t.comments.size)
26
27                         assert_equal("Bolt Thrower", t.comments["artist"])
28                         assert_equal("...For Victory", t.comments["album"])
29                         assert_equal("1994", t.comments["date"])
30                 end
31         end
32
33         def test_write_is_non_destructive
34                 a = Digest::MD5.hexdigest(File.read(OGG_FILE))
35
36                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
37                         t.write
38                 end
39
40                 b = Digest::MD5.hexdigest(File.read(OGG_FILE))
41
42                 assert_equal(a, b)
43         end
44
45         def test_multiple_writes
46                 a = Digest::MD5.hexdigest(File.read(OGG_FILE))
47
48                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
49                         1.upto(10) do
50                                 t.write
51                         end
52                 end
53
54                 b = Digest::MD5.hexdigest(File.read(OGG_FILE))
55
56                 assert_equal(a, b)
57         end
58
59         def test_write_stable_order
60                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
61                         assert_equal(3, t.write)
62                 end
63
64                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
65                         assert_equal(["artist", "album", "date"], t.comments.keys)
66                 end
67         end
68
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)
73                 end
74
75                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
76                         assert_equal(["artist", "album", "date"], t.comments.keys)
77                 end
78         end
79
80         def test_write_keeps_file_mode
81                 mode = File.stat(OGG_FILE).mode
82
83                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
84                         t.write
85                 end
86
87                 assert_equal(mode, File.stat(OGG_FILE).mode)
88         end
89
90         def test_append
91                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
92                         t.comments["genre"] = "Death Metal"
93                         assert_equal(4, t.write)
94                 end
95
96                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
97                         assert_equal("Death Metal", t.comments["genre"])
98                 end
99         end
100
101         def test_delete
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)
106                 end
107
108                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
109                         assert_equal(["artist", "date"], t.comments.keys)
110                 end
111         end
112
113         def test_clear
114                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
115                         t.comments.clear
116                         assert_equal(0, t.write)
117                 end
118
119                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
120                         assert(t.comments.empty?)
121                 end
122         end
123
124         def test_empty
125                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
126                         assert(!t.comments.empty?)
127
128                         t.comments.delete("artist")
129                         t.comments.delete("album")
130                         t.comments.delete("date")
131
132                         assert_equal(0, t.write)
133                 end
134
135                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
136                         assert(t.comments.empty?)
137                 end
138         end
139
140         def test_each
141                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
142                         a = {
143                                 "artist" => "Bolt Thrower",
144                                 "album" => "...For Victory",
145                                 "date" => "1994"
146                         }
147                         b = {}
148
149                         t.comments.each do |k, v|
150                                 b[k] = v
151                         end
152
153                         assert_equal(a, b)
154                 end
155         end
156
157         def test_each_key
158                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
159                         b = []
160
161                         t.comments.each_key do |k|
162                                 b << k
163                         end
164
165                         assert_equal(["artist", "album", "date"], b)
166                 end
167         end
168
169         def test_each_value
170                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
171                         b = []
172
173                         t.comments.each_value do |v|
174                                 b << v
175                         end
176
177                         assert_equal(["Bolt Thrower", "...For Victory", "1994"], b)
178                 end
179         end
180
181         def test_inspect
182                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
183                         tmp=<<EOF
184 {"artist"=>"Bolt Thrower", "album"=>"...For Victory", "date"=>"1994"}
185 EOF
186                         assert_equal(tmp.strip, t.comments.inspect)
187                 end
188         end
189
190         def test_has_key
191                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
192                         assert(t.comments.has_key?("artist"))
193                         assert(!t.comments.has_key?("foo"))
194
195                         assert(t.comments.key?("artist"))
196                         assert(t.comments.include?("artist"))
197                         assert(t.comments.member?("artist"))
198                 end
199         end
200
201         def test_compare
202                 a = nil
203                 b = nil
204
205                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
206                         a = t.comments
207                 end
208
209                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
210                         b = t.comments
211                 end
212
213                 assert_equal(0, a <=> b)
214                 b["artist"] = "Foo"
215                 assert_equal(-1, a <=> b)
216         end
217
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")
222                         end
223                 end
224         end
225
226         def test_modify_added_key
227                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
228                         t.comments["Foo"] = "Bar"
229
230                         assert_raises(TypeError) do
231                                 t.comments.keys.last.replace("new")
232                         end
233                 end
234         end
235
236         def test_merge
237                 repl = {
238                         "artist" => "Ballista",
239                         "genre" => "Death Metal",
240                 }
241
242                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
243                         t.comments.merge!(repl)
244
245                         assert_equal(["artist", "album", "date", "genre"],
246                                      t.comments.keys)
247                         assert_equal(["Ballista", "...For Victory", "1994",
248                                       "Death Metal"],
249                                      t.comments.values)
250                 end
251         end
252
253         def test_merge_bad_arg
254                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
255                         assert_raises(ArgumentError) do
256                                 t.comments.merge!(42)
257                         end
258                 end
259         end
260
261         def test_shift
262                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
263                         assert_equal(["artist", "Bolt Thrower"], t.comments.shift)
264                         assert_equal(["album", "...For Victory"], t.comments.shift)
265                         assert_equal(["date", "1994"], t.comments.shift)
266                         assert_equal(nil, t.comments.shift)
267                 end
268         end
269
270         def test_close
271                 Ogg::Vorbis::Tagger.new(OGG_FILE).close
272         end
273
274         def test_close2
275                 t = Ogg::Vorbis::Tagger.new(OGG_FILE)
276                 t.close
277
278                 assert_raise(Ogg::Vorbis::Tagger::ClosedStreamError) do
279                         t.close
280                 end
281
282                 assert_raise(Ogg::Vorbis::Tagger::ClosedStreamError) do
283                         Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
284                                 t.close
285                         end
286                 end
287         end
288
289         def test_open_non_existing_file
290                 assert_raises(Ogg::Vorbis::Tagger::OpenError) do
291                         Ogg::Vorbis::Tagger.new("foo.bar")
292                 end
293         end
294
295         def test_open_non_ogg_file
296                 File.open("test/foo.bar", "w") do |f|
297                         f << "foobarbazxyzzy"
298                 end
299
300                 assert_raises(Ogg::Vorbis::Tagger::InvalidDataError) do
301                         Ogg::Vorbis::Tagger.new("test/foo.bar")
302                 end
303         ensure
304                 FileUtils.rm_f("test/foo.bar")
305         end
306
307         def test_standalone_comments_object
308                 o = Ogg::Vorbis::Comments.new
309                 assert_equal(o.length, 0);
310         end
311 end