Added two tests that ensure that writing comments doesn't corrupt files.
[ruby-vorbistagger.git] / test / test_main.rb
index cdcf0d8f8a7a13b601502afc34e63741fd40f9ec..bbc5b0023a2987e31af883c4573dde96f4605dd5 100644 (file)
@@ -1,39 +1,21 @@
 require "test/unit"
 require "ogg/vorbis/tagger"
 require "fileutils"
-require "stringio"
-require "open3"
+require "digest/md5"
 
 class MainTest < Test::Unit::TestCase
        OGG_FILE = "test/test.ogg"
 
        def setup
-               tmp =<<EOF
--c "artist=Bolt Thrower" -c "album=...For Victory" -c "date=1994"
-EOF
-
-               @ogg_buf = StringIO.new
-               cmd = "oggenc -q 0 #{tmp.strip} -r -"
-
-               Open3.popen3(cmd) do |sin, sout, _|
-                       sin.write("\0\0\0\0")
-                       sin.close
-
-                       begin
-                               tmp = sout.read
-                               @ogg_buf << tmp
-                       end while !tmp.length.zero?
-               end
-
-               @ogg_buf.seek(0)
+               FileUtils.cp("test/sample.ogg", OGG_FILE)
        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"],
@@ -48,73 +30,89 @@ EOF
                end
        end
 
+       def test_write_is_non_destructive
+               a = Digest::MD5.hexdigest(File.read(OGG_FILE))
+
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                       t.write
+               end
+
+               b = Digest::MD5.hexdigest(File.read(OGG_FILE))
+
+               assert_equal(a, b)
+       end
+
+       def test_multiple_writes
+               a = Digest::MD5.hexdigest(File.read(OGG_FILE))
+
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                       1.upto(10) do
+                               t.write
+                       end
+               end
+
+               b = Digest::MD5.hexdigest(File.read(OGG_FILE))
+
+               assert_equal(a, b)
+       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 +122,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 +145,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 +157,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 +169,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 +178,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 +192,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
 
@@ -207,12 +205,16 @@ EOF
                assert_equal(-1, a <=> b)
        end
 
-       def test_modify_key
-               Ogg::Vorbis::Tagger.open(@ogg_buf) do |t|
+       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
@@ -220,4 +222,34 @@ EOF
                        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
+
+       def test_shift
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                       assert_equal(["artist", "Bolt Thrower"], t.comments.shift)
+                       assert_equal(["album", "...For Victory"], t.comments.shift)
+                       assert_equal(["date", "1994"], t.comments.shift)
+                       assert_equal(nil, t.comments.shift)
+               end
+       end
+
+       def test_close
+               Ogg::Vorbis::Tagger.new(OGG_FILE).close
+       end
 end