3003783554337a3bd3b2b05722ea3d29d414038a
[ruby-vorbistagger.git] / test / test_main.rb
1 require "test/unit"
2 require "ogg/vorbis/tagger"
3 require "fileutils"
4
5 class MainTest < Test::Unit::TestCase
6         OGG_FILE = "test/test.ogg"
7
8         def setup
9                 FileUtils.cp("test/sample.ogg", OGG_FILE)
10         end
11
12         def teardown
13                 FileUtils.rm_f(OGG_FILE)
14         end
15
16         def test_read
17                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
18                         # make sure the keys are returned in the correct order
19                         assert_equal(["artist", "album", "date"], t.comments.keys)
20                         assert_equal(["Bolt Thrower", "...For Victory", "1994"],
21                                      t.comments.values)
22
23                         assert_equal(3, t.comments.length)
24                         assert_equal(3, t.comments.size)
25
26                         assert_equal("Bolt Thrower", t.comments["artist"])
27                         assert_equal("...For Victory", t.comments["album"])
28                         assert_equal("1994", t.comments["date"])
29                 end
30         end
31
32         def test_write_stable_order
33                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
34                         assert_equal(3, t.write)
35                 end
36
37                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
38                         assert_equal(["artist", "album", "date"], t.comments.keys)
39                 end
40         end
41
42         def test_write_stable_order_change
43                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
44                         t.comments["artist"] = "Ballista"
45                         assert_equal(3, t.write)
46                 end
47
48                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
49                         assert_equal(["artist", "album", "date"], t.comments.keys)
50                 end
51         end
52
53         def test_append
54                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
55                         t.comments["genre"] = "Death Metal"
56                         assert_equal(4, t.write)
57                 end
58
59                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
60                         assert_equal("Death Metal", t.comments["genre"])
61                 end
62         end
63
64         def test_delete
65                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
66                         assert_equal("...For Victory", t.comments.delete("album"))
67                         assert_nil(t.comments.delete("foo"))
68                         assert_equal(2, t.write)
69                 end
70
71                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
72                         assert_equal(["artist", "date"], t.comments.keys)
73                 end
74         end
75
76         def test_clear
77                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
78                         t.comments.clear
79                         assert_equal(0, t.write)
80                 end
81
82                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
83                         assert(t.comments.empty?)
84                 end
85         end
86
87         def test_empty
88                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
89                         assert(!t.comments.empty?)
90
91                         t.comments.delete("artist")
92                         t.comments.delete("album")
93                         t.comments.delete("date")
94
95                         assert_equal(0, t.write)
96                 end
97
98                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
99                         assert(t.comments.empty?)
100                 end
101         end
102
103         def test_each
104                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
105                         a = {
106                                 "artist" => "Bolt Thrower",
107                                 "album" => "...For Victory",
108                                 "date" => "1994"
109                         }
110                         b = {}
111
112                         t.comments.each do |k, v|
113                                 b[k] = v
114                         end
115
116                         assert_equal(a, b)
117                 end
118         end
119
120         def test_each_key
121                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
122                         b = []
123
124                         t.comments.each_key do |k|
125                                 b << k
126                         end
127
128                         assert_equal(["artist", "album", "date"], b)
129                 end
130         end
131
132         def test_each_value
133                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
134                         b = []
135
136                         t.comments.each_value do |v|
137                                 b << v
138                         end
139
140                         assert_equal(["Bolt Thrower", "...For Victory", "1994"], b)
141                 end
142         end
143
144         def test_inspect
145                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
146                         tmp=<<EOF
147 {"artist"=>"Bolt Thrower", "album"=>"...For Victory", "date"=>"1994"}
148 EOF
149                         assert_equal(tmp.strip, t.comments.inspect)
150                 end
151         end
152
153         def test_has_key
154                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
155                         assert(t.comments.has_key?("artist"))
156                         assert(!t.comments.has_key?("foo"))
157
158                         assert(t.comments.key?("artist"))
159                         assert(t.comments.include?("artist"))
160                         assert(t.comments.member?("artist"))
161                 end
162         end
163
164         def test_compare
165                 a = nil
166                 b = nil
167
168                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
169                         a = t.comments
170                 end
171
172                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
173                         b = t.comments
174                 end
175
176                 assert_equal(0, a <=> b)
177                 b["artist"] = "Foo"
178                 assert_equal(-1, a <=> b)
179         end
180
181         def test_modify_existing_key
182                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
183                         assert_raises(TypeError) do
184                                 t.comments.keys.first.replace("new")
185                         end
186                 end
187         end
188
189         def test_modify_added_key
190                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
191                         t.comments["Foo"] = "Bar"
192
193                         assert_raises(TypeError) do
194                                 t.comments.keys.last.replace("new")
195                         end
196                 end
197         end
198
199         def test_merge
200                 repl = {
201                         "artist" => "Ballista",
202                         "genre" => "Death Metal",
203                 }
204
205                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
206                         t.comments.merge!(repl)
207
208                         assert_equal(["artist", "album", "date", "genre"],
209                                      t.comments.keys)
210                         assert_equal(["Ballista", "...For Victory", "1994",
211                                       "Death Metal"],
212                                      t.comments.values)
213                 end
214         end
215
216         def test_shift
217                 Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
218                         assert_equal(["artist", "Bolt Thrower"], t.comments.shift)
219                         assert_equal(["album", "...For Victory"], t.comments.shift)
220                         assert_equal(["date", "1994"], t.comments.shift)
221                         assert_equal(nil, t.comments.shift)
222                 end
223         end
224
225         def test_close
226                 Ogg::Vorbis::Tagger.new(OGG_FILE).close
227         end
228 end