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