From 10b80a16f0bbdbdc9efac66d7eb26389fcc0628b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Nov 2017 17:28:24 +0100 Subject: [PATCH] Factor out TempFile class and reuse it in other tests Ensure we don't leave "mytext.dat" and "test.txt" lying around in any directory the tests are run from by ensuring that these files are destroyed by the test code using them. --- tests/filekind/filekind.cpp | 12 ++++-------- tests/streams/datastreamtest.cpp | 20 ++++++++++++++------ tests/streams/textstreamtest.cpp | 14 ++++++++++---- tests/testfile.h | 25 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/tests/filekind/filekind.cpp b/tests/filekind/filekind.cpp index aa812b0884..fa270897b4 100644 --- a/tests/filekind/filekind.cpp +++ b/tests/filekind/filekind.cpp @@ -37,6 +37,8 @@ #define fileno _fileno #endif +#include "testfile.h" + /////////////////////////////////////////////////////////////////////////////// // The test case @@ -100,23 +102,17 @@ void FileKindTestCase::TestFd(wxFile& file, bool expected) CPPUNIT_ASSERT(outStream.IsSeekable() == expected); } -struct TempFile -{ - ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); } - wxString m_name; -}; - // test with an ordinary file // void FileKindTestCase::File() { TempFile tmp; // put first wxFile file; - tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file); + tmp.Assign(wxFileName::CreateTempFileName(wxT("wxft"), &file)); TestFd(file, true); file.Close(); - wxFFile ffile(tmp.m_name); + wxFFile ffile(tmp.GetName()); TestFILE(ffile, true); } diff --git a/tests/streams/datastreamtest.cpp b/tests/streams/datastreamtest.cpp index ae6c1564ad..ca2456a6c5 100644 --- a/tests/streams/datastreamtest.cpp +++ b/tests/streams/datastreamtest.cpp @@ -26,6 +26,8 @@ #include "wx/wfstream.h" #include "wx/math.h" +#include "testfile.h" + // ---------------------------------------------------------------------------- // test class // ---------------------------------------------------------------------------- @@ -107,8 +109,10 @@ DataStreamTestCase::DataStreamTestCase() wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue) { + TempFile f("mytext.dat"); + { - wxFileOutputStream pFileOutput( wxT("mytext.dat") ); + wxFileOutputStream pFileOutput( f.GetName() ); wxDataOutputStream pDataOutput( pFileOutput ); if ( ms_useBigEndianFormat ) pDataOutput.BigEndianOrdered(true); @@ -121,7 +125,7 @@ wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue) pDataOutput << fValue; } - wxFileInputStream pFileInput( wxT("mytext.dat") ); + wxFileInputStream pFileInput( f.GetName() ); wxDataInputStream pDataInput( pFileInput ); if ( ms_useBigEndianFormat ) pDataInput.BigEndianOrdered(true); @@ -156,15 +160,17 @@ private: { ValueArray InValues(Size); + TempFile f("mytext.dat"); + { - wxFileOutputStream FileOutput( wxT("mytext.dat") ); + wxFileOutputStream FileOutput( f.GetName() ); wxDataOutputStream DataOutput( FileOutput ); (DataOutput.*pfnWriter)(Values, Size); } { - wxFileInputStream FileInput( wxT("mytext.dat") ); + wxFileInputStream FileInput( f.GetName() ); wxDataInputStream DataInput( FileInput ); (DataInput.*pfnReader)(&*InValues.begin(), InValues.size()); @@ -207,15 +213,17 @@ T TestRW(const T &Value) { T InValue; + TempFile f("mytext.dat"); + { - wxFileOutputStream FileOutput( wxT("mytext.dat") ); + wxFileOutputStream FileOutput( f.GetName() ); wxDataOutputStream DataOutput( FileOutput ); DataOutput << Value; } { - wxFileInputStream FileInput( wxT("mytext.dat") ); + wxFileInputStream FileInput( f.GetName() ); wxDataInputStream DataInput( FileInput ); DataInput >> InValue; diff --git a/tests/streams/textstreamtest.cpp b/tests/streams/textstreamtest.cpp index ec88c01ec8..e91cc5a1cf 100644 --- a/tests/streams/textstreamtest.cpp +++ b/tests/streams/textstreamtest.cpp @@ -31,6 +31,8 @@ #include "wx/mstream.h" #endif // wxUSE_UNICODE +#include "testfile.h" + // ---------------------------------------------------------------------------- // test class // ---------------------------------------------------------------------------- @@ -105,7 +107,9 @@ TextStreamTestCase::TextStreamTestCase() void TextStreamTestCase::Endline() { - wxFileOutputStream* pOutFile = new wxFileOutputStream(wxT("test.txt")); + TempFile f("test.txt"); + + wxFileOutputStream* pOutFile = new wxFileOutputStream(f.GetName()); wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile); *pOutText << wxT("Test text") << endl << wxT("More Testing Text (There should be newline before this)"); @@ -113,7 +117,7 @@ void TextStreamTestCase::Endline() delete pOutText; delete pOutFile; - wxFileInputStream* pInFile = new wxFileInputStream(wxT("test.txt")); + wxFileInputStream* pInFile = new wxFileInputStream(f.GetName()); char szIn[9 + NEWLINELEN]; @@ -147,8 +151,10 @@ void TextStreamTestCase::MiscTests() template static void DoTestRoundTrip(const T *values, size_t numValues) { + TempFile f("test.txt"); + { - wxFileOutputStream fileOut(wxT("test.txt")); + wxFileOutputStream fileOut(f.GetName()); wxTextOutputStream textOut(fileOut); for ( size_t n = 0; n < numValues; n++ ) @@ -158,7 +164,7 @@ static void DoTestRoundTrip(const T *values, size_t numValues) } { - wxFileInputStream fileIn(wxT("test.txt")); + wxFileInputStream fileIn(f.GetName()); wxTextInputStream textIn(fileIn); T value; diff --git a/tests/testfile.h b/tests/testfile.h index 519acbe8b2..9c1f5afa3d 100644 --- a/tests/testfile.h +++ b/tests/testfile.h @@ -41,5 +41,30 @@ private: wxString m_name; }; +// ---------------------------------------------------------------------------- +// TempFile: just a self deleting file +// ---------------------------------------------------------------------------- + +class TempFile +{ +public: + explicit TempFile(const wxString& name = wxString()) : m_name(name) { } + + void Assign(const wxString& name) { m_name = name; } + + const wxString& GetName() const { return m_name; } + + ~TempFile() + { + if ( !m_name.empty() ) + wxRemoveFile(m_name); + } + +private: + wxString m_name; + + wxDECLARE_NO_COPY_CLASS(TempFile); +}; + #endif // _WX_TESTS_TEMPFILE_H_