#!/usr/bin/ruby if File.exists? File.dirname(__FILE__)+"/taglib.so" require File.dirname(__FILE__)+'/taglib' BASE_DATA=File.dirname(__FILE__)+"/../test/data/" else require File.dirname(__FILE__)+'/../ext/taglib/taglib' BASE_DATA=File.dirname(__FILE__)+"/data/" end require 'fileutils' require 'test/unit' class RtaglibReadTestCase < Test::Unit::TestCase def setup @data_dir=BASE_DATA @temp_dir="/tmp/rtaglib" FileUtils.mkdir(@temp_dir) if !File.exists? @temp_dir end def teardown Dir.glob(@temp_dir+"/*").each{|f| FileUtils.rm(f) } end def get_copy(original) original=~/.*\.(.+)/ ext=$1 copy=@temp_dir+"/test_"+sprintf("%06d",rand(10000))+"."+ext FileUtils.copy(original, copy) copy end # Taglib::File test for # - writable? # - open? # - valid? # - seek (with 1 and 2 arguments) # - readBlock # - find (with 1 and 2 arguments) # - lenght def test_file mp3=@data_dir+"440Hz-5sec.mp3" copy=get_copy(mp3) file=Taglib::MPEG_File.new(mp3) assert(file.writable?) assert(file.open?) assert(file.valid?) file.seek(0); expected=sprintf("%c%c%c%c%c%c%c%c%c%c",0x49,0x44,0x33,0x04,0x00,0x80, 0x00,0x00,0x07,0x76) assert_equal(expected,file.readBlock(10)) expected=sprintf("%c%c%c%c%c%c%c%c%c%c",0x54,0x49,0x54,0x32,0x00,0x00, 0x00,0x10,0x00,0x00) assert_equal(expected,file.readBlock(10)) file.seek(-6, Taglib::File::End) expected=sprintf("%c%c%c%c%c%c",0x77,0x61,0x72,0x00,0x01,0x47) assert_equal(expected,file.readBlock(6)) needle=sprintf("%c%c%c%c%c",0x20,0xF1,0x8A,0xE6,0x63) assert_equal(0x40D,file.find1(needle)) needle=sprintf("%c%c%c%c%c",0x22,0x22,0x22,0xE6,0x63) assert_equal(-1,file.find2(needle,0)) assert_equal(0x7B46,file.length()) end # Taglib::FileRef test for # - ::defaultFileExtensions() # - length # - save # - tag: getters and setters for title, artist, album, year, track, genre, comment # - audioproperties: bitrate,channels,sampleRate and channels # - isNull (null?) def test_fileref assert_equal(%w{ogg flac oga mp3 mpc wv spx tta}, Taglib::FileRef.defaultFileExtensions()) bitrate={'flac'=>168,'wv'=>235,'mp3'=>48,'mpc'=>41,'ogg'=>80} channels={'flac'=>1,'wv'=>1,'mp3'=>1,'mpc'=>2,'ogg'=>1} Dir.glob(@data_dir+"/*").each{|f| fr=Taglib::FileRef.new(f) f=~/.+\.(.+)/ ext=$1 assert_equal(5,fr.audioProperties.length, "Error on file #{f}") assert_equal(bitrate[ext],fr.audioProperties.bitrate, "Error on file #{f}") assert_equal(44100,fr.audioProperties.sampleRate, "Error on file #{f}") assert_equal(channels[ext],fr.audioProperties.channels, "Error on file #{f}") assert_equal("440Hz Sine Wave",fr.tag().title,"Error on file #{f}") assert_equal("Dr. Lex",fr.tag().artist,"Error on file #{f}") assert_equal("http://www.dr-lex.be/",fr.tag().album,"Error on file #{f}") assert_equal(2008,fr.tag().year,"Error on file #{f}") assert_equal(1,fr.tag().track,"Error on file #{f}") assert_equal("Lo-Fi",fr.tag().genre, "Error on file #{f}") assert_equal("http://www.dr-lex.be/software/testsounds.html", fr.tag().comment,"Error on file #{f}") assert(!fr.null?) } mp3=@data_dir+"440Hz-5sec.mp3" copy=get_copy(mp3) fr2=Taglib::FileRef.new(copy) fr2.tag().title="new title"; fr2.tag().artist="new artist"; fr2.tag().album="new album"; fr2.tag().year=2000; fr2.tag().track=10; fr2.tag().genre="Rock"; fr2.tag().comment="new comment"; fr2.save() copy2=get_copy(copy) fr2.tag().title="sfsd" fr2.save() fr3=Taglib::FileRef.new(copy2) assert_equal("new title",fr3.tag.title) assert_equal("new artist",fr3.tag.artist) assert_equal("new album",fr3.tag.album) assert_equal(2000,fr3.tag.year) assert_equal(10,fr3.tag.track) assert_equal("Rock",fr3.tag.genre) assert_equal("new comment",fr3.tag.comment) end def test_flac original=@data_dir+"440Hz-5sec.flac" copy_xiph=get_copy(original) copy_id1=get_copy(original) copy_id2=get_copy(original) flac_xiph=Taglib::FLAC_File.new(copy_xiph,true) flac_id1=Taglib::FLAC_File.new(copy_id1,true) flac_id2=Taglib::FLAC_File.new(copy_id2,true) # file assert_equal(34,flac_xiph.streamInfoData.length) assert_equal(105299, flac_xiph.streamLength) # audioproperties ap=flac_xiph.audioProperties assert_equal(5,ap.length) assert_equal(168,ap.bitrate) assert_equal(44100,ap.sampleRate) assert_equal(1,ap.channels) assert_equal(16,ap.sampleWidth) # comments xiph=flac_xiph.xiphComment() xiph.setTitle("xiph") flac_xiph.save() assert_equal("xiph",flac_xiph.tag().title) assert_nil(flac_xiph.ID3v1Tag()) assert_nil(flac_xiph.ID3v2Tag()) id1=flac_id1.ID3v1Tag(true) id1.setTitle("id1") flac_id1.save() assert_equal("id1",id1.title) assert_not_nil(flac_id1.xiphComment()) assert_nil(flac_id1.ID3v2Tag()) id2=flac_id2.ID3v2Tag(true) id2.setTitle("id2") flac_id2.save() assert_equal("id2",id2.title) assert_not_nil(flac_id2.xiphComment()) assert_nil(flac_id2.ID3v1Tag()) end # def test_mpeg original=@data_dir+"440Hz-5sec.mp3" copy=get_copy(original) mp3=Taglib::MPEG_File.new(copy) %w{tag ID3v1Tag ID3v2Tag}.each {|f| assert_equal("440Hz Sine Wave",mp3.send(f).title) } # file # strip only id3v1 mp3.strip(Taglib::MPEG_File::ID3v1) assert_nil(mp3.ID3v1Tag) assert_not_nil(mp3.ID3v2Tag) copy=get_copy(original) mp3=Taglib::MPEG_File.new(copy) # strip all mp3.strip() assert_nil(mp3.ID3v1Tag) assert_nil(mp3.ID3v2Tag) # properties assert_equal(0,mp3.audioProperties.version) assert_equal(3,mp3.audioProperties.layer) assert(!mp3.audioProperties.protectionEnabled) assert_equal(Taglib::MPEG_Header::SingleChannel, mp3.audioProperties.channelMode) assert(!mp3.audioProperties.isCopyrighted) assert(mp3.audioProperties.isOriginal) end # BAD: strip doesn't work def _test_mpc original=@data_dir+"440Hz-5sec.mpc" copy=get_copy(original) fr=Taglib::FileRef.new(copy) fr.tag.title="440Hz Sine Wave" fr.save mpc=Taglib::MPEG_File.new(copy) %w{tag APETag}.each {|f| assert_equal("440Hz Sine Wave",mpc.send(f).title, "#{f} tag") } # file # strip only id3v1 mpc.strip(Taglib::MPC_File::ID3v1) mpc.save mpc1=Taglib::MPEG_File.new(copy) # assert_nil(mpc1.ID3v1Tag) assert_not_nil(mpc1.APETag) copy=get_copy(original) mpc=Taglib::MPC_File.new(copy) # strip all mpc.strip() mpc.save mpc1=Taglib::MPEG_File.new(copy) # assert_nil(mpc1.ID3v1Tag) assert_nil(mpc1.APETag) # properties assert_equal(0,mp3.audioProperties.version) assert_equal(3,mp3.audioProperties.layer) assert(!mp3.audioProperties.protectionEnabled) assert_equal(Taglib::MPEG_Header::SingleChannel, mp3.audioProperties.channelMode) assert(!mp3.audioProperties.isCopyrighted) assert(mp3.audioProperties.isOriginal) end def test_fieldmap fl=Taglib::FieldListMap.new() fl.insert("key",["val1","val2"]) assert_equal(["val1","val2"],fl["key"]) assert_nil(fl["2"]) fl.insert("key2",["val3","val4"]) assert_equal({'key'=>["val1","val2"],"key2"=>["val3","val4"]}, fl.hash) assert_equal(fl.size,fl.length) end def test_xiph_comment original=@data_dir+"440Hz-5sec.flac" copy_xiph=get_copy(original) flac_xiph=Taglib::FLAC_File.new(copy_xiph,true) xc=flac_xiph.xiphComment fl=xc.fieldListMap fl.hash.each {|k,v| xc.removeField(k) } assert(xc.empty?) xc.setTitle("T") xc.setArtist("A") xc.addField("c","C1",false) xc.addField("c","C2",false) expected={"TITLE"=>["T"],"ARTIST"=>["A"],"C"=>["C1","C2"]} assert_equal(expected,fl.hash) end end