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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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 <typename T>
|
||||
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;
|
||||
|
@@ -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_
|
||||
|
||||
|
Reference in New Issue
Block a user