diff --git a/tests/test_aiff.cpp b/tests/test_aiff.cpp index be94b07f..54f93702 100644 --- a/tests/test_aiff.cpp +++ b/tests/test_aiff.cpp @@ -19,12 +19,11 @@ public: void testReading() { - string filename = copyFile("empty", ".aiff"); + ScopedFileCopy copy("empty", ".aiff"); + string filename = copy.fileName(); RIFF::AIFF::File *f = new RIFF::AIFF::File(filename.c_str()); CPPUNIT_ASSERT_EQUAL(689, f->audioProperties()->bitrate()); - - deleteFile(filename); } }; diff --git a/tests/test_asf.cpp b/tests/test_asf.cpp index 2b1e7aa4..8de44ea4 100644 --- a/tests/test_asf.cpp +++ b/tests/test_asf.cpp @@ -39,7 +39,8 @@ public: void testSaveMultipleValues() { - string newname = copyFile("silence-1", ".wma"); + ScopedFileCopy copy("silence-1", ".wma"); + string newname = copy.fileName(); ASF::File *f = new ASF::File(newname.c_str()); ASF::AttributeList values; @@ -52,13 +53,12 @@ public: f = new ASF::File(newname.c_str()); CPPUNIT_ASSERT_EQUAL(2, (int)f->tag()->attributeListMap()["WM/AlbumTitle"].size()); delete f; - - deleteFile(newname); } void testSaveStream() { - string newname = copyFile("silence-1", ".wma"); + ScopedFileCopy copy("silence-1", ".wma"); + string newname = copy.fileName(); ASF::File *f = new ASF::File(newname.c_str()); ASF::AttributeList values; @@ -72,13 +72,12 @@ public: f = new ASF::File(newname.c_str()); CPPUNIT_ASSERT_EQUAL(43, f->tag()->attributeListMap()["WM/AlbumTitle"][0].stream()); delete f; - - deleteFile(newname); } void testSaveLanguage() { - string newname = copyFile("silence-1", ".wma"); + ScopedFileCopy copy("silence-1", ".wma"); + string newname = copy.fileName(); ASF::File *f = new ASF::File(newname.c_str()); ASF::AttributeList values; @@ -94,8 +93,6 @@ public: CPPUNIT_ASSERT_EQUAL(32, f->tag()->attributeListMap()["WM/AlbumTitle"][0].stream()); CPPUNIT_ASSERT_EQUAL(56, f->tag()->attributeListMap()["WM/AlbumTitle"][0].language()); delete f; - - deleteFile(newname); } }; diff --git a/tests/test_fileref.cpp b/tests/test_fileref.cpp index b0247a39..bc0c5fe1 100644 --- a/tests/test_fileref.cpp +++ b/tests/test_fileref.cpp @@ -38,7 +38,8 @@ public: void fileRefSave(const string &filename, const string &ext) { - string newname = copyFile(filename, ext); + ScopedFileCopy copy(filename, ext); + string newname = copy.fileName(); FileRef *f = new FileRef(newname.c_str()); CPPUNIT_ASSERT(!f->isNull()); @@ -77,8 +78,6 @@ public: CPPUNIT_ASSERT_EQUAL(f->tag()->track(), TagLib::uint(7)); CPPUNIT_ASSERT_EQUAL(f->tag()->year(), TagLib::uint(2080)); delete f; - - deleteFile(newname); } void testMusepack() diff --git a/tests/test_flac.cpp b/tests/test_flac.cpp index acccc871..0901c69f 100644 --- a/tests/test_flac.cpp +++ b/tests/test_flac.cpp @@ -20,7 +20,8 @@ public: void testMultipleCommentBlocks() { - string newname = copyFile("multiple-vc", ".flac"); + ScopedFileCopy copy("multiple-vc", ".flac"); + string newname = copy.fileName(); FLAC::File *f = new FLAC::File(newname.c_str()); CPPUNIT_ASSERT_EQUAL(String("Artist 1"), f->tag()->artist()); @@ -31,8 +32,6 @@ public: f = new FLAC::File(newname.c_str()); CPPUNIT_ASSERT_EQUAL(String("The Artist"), f->tag()->artist()); delete f; - - deleteFile(newname); } }; diff --git a/tests/test_id3v2.cpp b/tests/test_id3v2.cpp index 0a105069..64f6461f 100644 --- a/tests/test_id3v2.cpp +++ b/tests/test_id3v2.cpp @@ -242,7 +242,8 @@ public: void testPOPMFromFile() { - string newname = copyFile("xing", ".mp3"); + ScopedFileCopy copy("xing", ".mp3"); + string newname = copy.fileName(); ID3v2::PopularimeterFrame *f = new ID3v2::PopularimeterFrame(); f->setEmail("email@example.com"); @@ -256,7 +257,6 @@ public: MPEG::File bar(newname.c_str()); CPPUNIT_ASSERT_EQUAL(String("email@example.com"), dynamic_cast(bar.ID3v2Tag()->frameList("POPM").front())->email()); CPPUNIT_ASSERT_EQUAL(200, dynamic_cast(bar.ID3v2Tag()->frameList("POPM").front())->rating()); - deleteFile(newname); } // http://bugs.kde.org/show_bug.cgi?id=150481 @@ -369,7 +369,8 @@ public: void testSaveUTF16Comment() { String::Type defaultEncoding = ID3v2::FrameFactory::instance()->defaultTextEncoding(); - string newname = copyFile("xing", ".mp3"); + ScopedFileCopy copy("xing", ".mp3"); + string newname = copy.fileName(); ID3v2::FrameFactory::instance()->setDefaultTextEncoding(String::UTF16); MPEG::File foo(newname.c_str()); foo.strip(); @@ -377,7 +378,6 @@ public: foo.save(); MPEG::File bar(newname.c_str()); CPPUNIT_ASSERT_EQUAL(String("Test comment!"), bar.tag()->comment()); - deleteFile(newname); ID3v2::FrameFactory::instance()->setDefaultTextEncoding(defaultEncoding); } diff --git a/tests/test_mp4.cpp b/tests/test_mp4.cpp index ad1195df..a5b301bc 100644 --- a/tests/test_mp4.cpp +++ b/tests/test_mp4.cpp @@ -38,7 +38,8 @@ public: void testUpdateStco() { - string filename = copyFile("no-tags", ".3g2"); + ScopedFileCopy copy("no-tags", ".3g2"); + string filename = copy.fileName(); MP4::File *f = new MP4::File(filename.c_str()); f->tag()->setArtist(ByteVector(3000, 'x')); @@ -80,13 +81,12 @@ public: } delete f; - - deleteFile(filename); } void testFreeForm() { - string filename = copyFile("has-tags", ".m4a"); + ScopedFileCopy copy("has-tags", ".m4a"); + string filename = copy.fileName(); MP4::File *f = new MP4::File(filename.c_str()); CPPUNIT_ASSERT(f->tag()->itemListMap().contains("----:com.apple.iTunes:iTunNORM")); @@ -99,13 +99,12 @@ public: CPPUNIT_ASSERT_EQUAL(String("Bar"), f->tag()->itemListMap()["----:org.kde.TagLib:Foo"].toStringList()[0]); f->save(); delete f; - - deleteFile(filename); } void testSaveExisingWhenIlstIsLast() { - string filename = copyFile("ilst-is-last", ".m4a"); + ScopedFileCopy copy("ilst-is-last", ".m4a"); + string filename = copy.fileName(); MP4::File *f = new MP4::File(filename.c_str()); CPPUNIT_ASSERT_EQUAL(String("82,164"), f->tag()->itemListMap()["----:com.apple.iTunes:replaygain_track_minmax"].toStringList()[0]); @@ -118,13 +117,12 @@ public: CPPUNIT_ASSERT_EQUAL(String("82,164"), f->tag()->itemListMap()["----:com.apple.iTunes:replaygain_track_minmax"].toStringList()[0]); CPPUNIT_ASSERT_EQUAL(String("Pearl Jam"), f->tag()->artist()); CPPUNIT_ASSERT_EQUAL(String("foo"), f->tag()->comment()); - - deleteFile(filename); } void test64BitAtom() { - string filename = copyFile("64bit", ".mp4"); + ScopedFileCopy copy("64bit", ".mp4"); + string filename = copy.fileName(); MP4::File *f = new MP4::File(filename.c_str()); CPPUNIT_ASSERT_EQUAL(true, f->tag()->itemListMap()["cpil"].toBool()); @@ -144,8 +142,6 @@ public: moov = atoms->atoms[0]; // original size + 'pgap' size + padding CPPUNIT_ASSERT_EQUAL(long(77 + 25 + 974), moov->length); - - deleteFile(filename); } void testGnre() @@ -168,7 +164,8 @@ public: void testCovrWrite() { - string filename = copyFile("has-tags", ".m4a"); + ScopedFileCopy copy("has-tags", ".m4a"); + string filename = copy.fileName(); MP4::File *f = new MP4::File(filename.c_str()); CPPUNIT_ASSERT(f->tag()->itemListMap().contains("covr")); @@ -189,8 +186,6 @@ public: CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::PNG, l[2].format()); CPPUNIT_ASSERT_EQUAL(TagLib::uint(3), l[2].data().size()); delete f; - - deleteFile(filename); } }; diff --git a/tests/test_ogg.cpp b/tests/test_ogg.cpp index be81877e..9e845096 100644 --- a/tests/test_ogg.cpp +++ b/tests/test_ogg.cpp @@ -23,7 +23,8 @@ public: void testSimple() { - string newname = copyFile("empty", ".ogg"); + ScopedFileCopy copy("empty", ".ogg"); + string newname = copy.fileName(); Vorbis::File *f = new Vorbis::File(newname.c_str()); f->tag()->setArtist("The Artist"); @@ -33,13 +34,12 @@ public: f = new Vorbis::File(newname.c_str()); CPPUNIT_ASSERT_EQUAL(String("The Artist"), f->tag()->artist()); delete f; - - deleteFile(newname); } void testSplitPackets() { - string newname = copyFile("empty", ".ogg"); + ScopedFileCopy copy("empty", ".ogg"); + string newname = copy.fileName(); Vorbis::File *f = new Vorbis::File(newname.c_str()); f->tag()->addField("test", ByteVector(128 * 1024, 'x') + ByteVector(1, '\0')); @@ -49,8 +49,6 @@ public: f = new Vorbis::File(newname.c_str()); CPPUNIT_ASSERT_EQUAL(19, f->lastPageHeader()->pageSequenceNumber()); delete f; - - deleteFile(newname); } }; diff --git a/tests/test_oggflac.cpp b/tests/test_oggflac.cpp index a29a27a6..c1837175 100644 --- a/tests/test_oggflac.cpp +++ b/tests/test_oggflac.cpp @@ -21,7 +21,8 @@ public: void testFramingBit() { - string newname = copyFile("empty_flac", ".oga"); + ScopedFileCopy copy("empty_flac", ".oga"); + string newname = copy.fileName(); Ogg::FLAC::File *f = new Ogg::FLAC::File(newname.c_str()); f->tag()->setArtist("The Artist"); @@ -36,7 +37,6 @@ public: CPPUNIT_ASSERT_EQUAL(9134, size); delete f; - //deleteFile(newname); } }; diff --git a/tests/test_riff.cpp b/tests/test_riff.cpp index 34ab84ec..ac7f40d7 100644 --- a/tests/test_riff.cpp +++ b/tests/test_riff.cpp @@ -35,7 +35,8 @@ public: void testPadding() { - string filename = copyFile("empty", ".aiff"); + ScopedFileCopy copy("empty", ".aiff"); + string filename = copy.fileName(); PublicRIFF *f = new PublicRIFF(filename.c_str()); CPPUNIT_ASSERT_EQUAL(ByteVector("TEST"), f->chunkName(2)); @@ -72,8 +73,6 @@ public: CPPUNIT_ASSERT_EQUAL(ByteVector("TEST"), f->chunkName(2)); CPPUNIT_ASSERT_EQUAL(ByteVector("foo"), f->chunkData(2)); - - deleteFile(filename); } }; diff --git a/tests/utils.h b/tests/utils.h index edcd3c3d..bc566d2d 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -25,3 +25,25 @@ inline void deleteFile(const string &filename) { remove(filename.c_str()); } + +class ScopedFileCopy +{ +public: + ScopedFileCopy(const string &filename, const string &ext) + { + m_filename = copyFile(filename, ext); + } + + ~ScopedFileCopy() + { + deleteFile(m_filename); + } + + string fileName() + { + return m_filename; + } + +private: + string m_filename; +};