Added Ogg::Vorbis::Comments#merge!.
[ruby-vorbistagger.git] / test / test_main.rb
index da3621cc3ef3c159164ea2d811775a6ac9bca0dd..be81bb87048ca263bf456854eb0b627553db0f70 100644 (file)
@@ -1,7 +1,6 @@
 require "test/unit"
 require "ogg/vorbis/tagger"
 require "fileutils"
-require "stringio"
 require "open3"
 
 class MainTest < Test::Unit::TestCase
@@ -12,7 +11,7 @@ class MainTest < Test::Unit::TestCase
 -c "artist=Bolt Thrower" -c "album=...For Victory" -c "date=1994"
 EOF
 
-               @ogg_buf = StringIO.new
+               f = File.open(OGG_FILE, "w")
                cmd = "oggenc -q 0 #{tmp.strip} -r -"
 
                Open3.popen3(cmd) do |sin, sout, _|
@@ -21,19 +20,19 @@ EOF
 
                        begin
                                tmp = sout.read
-                               @ogg_buf << tmp
+                               f << tmp
                        end while !tmp.length.zero?
                end
 
-               @ogg_buf.seek(0)
+               f.close
        end
 
        def teardown
-               @ogg_buf.close
+               FileUtils.rm_f(OGG_FILE)
        end
 
        def test_read
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        # make sure the keys are returned in the correct order
                        assert_equal(["artist", "album", "date"], t.comments.keys)
                        assert_equal(["Bolt Thrower", "...For Victory", "1994"],
@@ -49,72 +48,62 @@ EOF
        end
 
        def test_write_stable_order
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert_equal(3, t.write)
                end
 
-               @ogg_buf.seek(0)
-
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert_equal(["artist", "album", "date"], t.comments.keys)
                end
        end
 
        def test_write_stable_order_change
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        t.comments["artist"] = "Ballista"
                        assert_equal(3, t.write)
                end
 
-               @ogg_buf.seek(0)
-
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert_equal(["artist", "album", "date"], t.comments.keys)
                end
        end
 
        def test_append
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        t.comments["genre"] = "Death Metal"
                        assert_equal(4, t.write)
                end
 
-               @ogg_buf.seek(0)
-
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert_equal("Death Metal", t.comments["genre"])
                end
        end
 
        def test_delete
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert_equal("...For Victory", t.comments.delete("album"))
                        assert_nil(t.comments.delete("foo"))
                        assert_equal(2, t.write)
                end
 
-               @ogg_buf.seek(0)
-
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert_equal(["artist", "date"], t.comments.keys)
                end
        end
 
        def test_clear
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        t.comments.clear
                        assert_equal(0, t.write)
                end
 
-               @ogg_buf.seek(0)
-
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert(t.comments.empty?)
                end
        end
 
        def test_empty
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert(!t.comments.empty?)
 
                        t.comments.delete("artist")
@@ -124,15 +113,13 @@ EOF
                        assert_equal(0, t.write)
                end
 
-               @ogg_buf.seek(0)
-
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert(t.comments.empty?)
                end
        end
 
        def test_each
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        a = {
                                "artist" => "Bolt Thrower",
                                "album" => "...For Victory",
@@ -149,7 +136,7 @@ EOF
        end
 
        def test_each_key
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        b = []
 
                        t.comments.each_key do |k|
@@ -161,7 +148,7 @@ EOF
        end
 
        def test_each_value
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        b = []
 
                        t.comments.each_value do |v|
@@ -173,7 +160,7 @@ EOF
        end
 
        def test_inspect
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        tmp=<<EOF
 {"artist"=>"Bolt Thrower", "album"=>"...For Victory", "date"=>"1994"}
 EOF
@@ -182,9 +169,13 @@ EOF
        end
 
        def test_has_key
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        assert(t.comments.has_key?("artist"))
                        assert(!t.comments.has_key?("foo"))
+
+                       assert(t.comments.key?("artist"))
+                       assert(t.comments.include?("artist"))
+                       assert(t.comments.member?("artist"))
                end
        end
 
@@ -192,13 +183,11 @@ EOF
                a = nil
                b = nil
 
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        a = t.comments
                end
 
-               @ogg_buf.seek(0)
-
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
                        b = t.comments
                end
 
@@ -206,4 +195,39 @@ EOF
                b["artist"] = "Foo"
                assert_equal(-1, a <=> b)
        end
+
+       def test_modify_existing_key
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                       assert_raises(TypeError) do
+                               t.comments.keys.first.replace("new")
+                       end
+               end
+       end
+
+       def test_modify_added_key
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                       t.comments["Foo"] = "Bar"
+
+                       assert_raises(TypeError) do
+                               t.comments.keys.last.replace("new")
+                       end
+               end
+       end
+
+       def test_merge
+               repl = {
+                       "artist" => "Ballista",
+                       "genre" => "Death Metal",
+               }
+
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                       t.comments.merge!(repl)
+
+                       assert_equal(["artist", "album", "date", "genre"],
+                                    t.comments.keys)
+                       assert_equal(["Ballista", "...For Victory", "1994",
+                                     "Death Metal"],
+                                    t.comments.values)
+               end
+       end
 end