Added two tests that ensure that writing comments doesn't corrupt files.
[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_append
81                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
82                         t.comments["genre"] = "Death Metal"
83                         assert_equal(4, t.write)
84                 end
85
86                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
87                         assert_equal("Death Metal", t.comments["genre"])
88                 end
89         end
90
91         def test_delete
92                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
93                         assert_equal("...For Victory", t.comments.delete("album"))
94                         assert_nil(t.comments.delete("foo"))
95                         assert_equal(2, t.write)
96                 end
97
98                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
99                         assert_equal(["artist", "date"], t.comments.keys)
100                 end
101         end
102
103         def test_clear
104                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
105                         t.comments.clear
106                         assert_equal(0, t.write)
107                 end
108
109                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
110                         assert(t.comments.empty?)
111                 end
112         end
113
114         def test_empty
115                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
116                         assert(!t.comments.empty?)
117
118                         t.comments.delete("artist")
119                         t.comments.delete("album")
120                         t.comments.delete("date")
121
122                         assert_equal(0, t.write)
123                 end
124
125                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
126                         assert(t.comments.empty?)
127                 end
128         end
129
130         def test_each
131                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
132                         a = {
133                                 "artist" => "Bolt Thrower",
134                                 "album" => "...For Victory",
135                                 "date" => "1994"
136                         }
137                         b = {}
138
139                         t.comments.each do |k, v|
140                                 b[k] = v
141                         end
142
143                         assert_equal(a, b)
144                 end
145         end
146
147         def test_each_key
148                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
149                         b = []
150
151                         t.comments.each_key do |k|
152                                 b << k
153                         end
154
155                         assert_equal(["artist", "album", "date"], b)
156                 end
157         end
158
159         def test_each_value
160                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
161                         b = []
162
163                         t.comments.each_value do |v|
164                                 b << v
165                         end
166
167                         assert_equal(["Bolt Thrower", "...For Victory", "1994"], b)
168                 end
169         end
170
171         def test_inspect
172                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
173                         tmp=<<EOF
174 {"artist"=>"Bolt Thrower", "album"=>"...For Victory", "date"=>"1994"}
175 EOF
176                         assert_equal(tmp.strip, t.comments.inspect)
177                 end
178         end
179
180         def test_has_key
181                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
182                         assert(t.comments.has_key?("artist"))
183                         assert(!t.comments.has_key?("foo"))
184
185                         assert(t.comments.key?("artist"))
186                         assert(t.comments.include?("artist"))
187                         assert(t.comments.member?("artist"))
188                 end
189         end
190
191         def test_compare
192                 a = nil
193                 b = nil
194
195                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
196                         a = t.comments
197                 end
198
199                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
200                         b = t.comments
201                 end
202
203                 assert_equal(0, a <=> b)
204                 b["artist"] = "Foo"
205                 assert_equal(-1, a <=> b)
206         end
207
208         def test_modify_existing_key
209                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
210                         assert_raises(TypeError) do
211                                 t.comments.keys.first.replace("new")
212                         end
213                 end
214         end
215
216         def test_modify_added_key
217                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
218                         t.comments["Foo"] = "Bar"
219
220                         assert_raises(TypeError) do
221                                 t.comments.keys.last.replace("new")
222                         end
223                 end
224         end
225
226         def test_merge
227                 repl = {
228                         "artist" => "Ballista",
229                         "genre" => "Death Metal",
230                 }
231
232                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
233                         t.comments.merge!(repl)
234
235                         assert_equal(["artist", "album", "date", "genre"],
236                                      t.comments.keys)
237                         assert_equal(["Ballista", "...For Victory", "1994",
238                                       "Death Metal"],
239                                      t.comments.values)
240                 end
241         end
242
243         def test_shift
244                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
245                         assert_equal(["artist", "Bolt Thrower"], t.comments.shift)
246                         assert_equal(["album", "...For Victory"], t.comments.shift)
247                         assert_equal(["date", "1994"], t.comments.shift)
248                         assert_equal(nil, t.comments.shift)
249                 end
250         end
251
252         def test_close
253                 Ogg::Vorbis::Tagger.new(OGG_FILE).close
254         end
255 end