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