Remove test input file in file stream unit tests only once

Don't remove the files in the test cases classes dtors, this was
inconsistent with how they were created: we either need to create the
file in the ctor and destroy it in the dtor or do both only once
globally.

Implement the second solution using the helper AutoRemoveFile instead of
a simple static bool in GetInFileName() implementations.
This commit is contained in:
Vadim Zeitlin 2017-11-01 14:45:06 +01:00
parent c54b611093
commit 5520d56222
2 changed files with 58 additions and 24 deletions

View File

@ -37,7 +37,6 @@ class ffileStream : public BaseStreamTestCase<wxFFileInputStream, wxFFileOutputS
{
public:
ffileStream();
virtual ~ffileStream();
CPPUNIT_TEST_SUITE(ffileStream);
// Base class stream tests the ffileStream supports.
@ -80,13 +79,6 @@ ffileStream::ffileStream()
m_bEofAtLastRead = false;
}
ffileStream::~ffileStream()
{
// Remove the temp test file...
::wxRemoveFile(FILENAME_FFILEINSTREAM);
::wxRemoveFile(FILENAME_FFILEOUTSTREAM);
}
wxFFileInputStream *ffileStream::DoCreateInStream()
{
wxFFileInputStream *pFileInStream = new wxFFileInputStream(GetInFileName());
@ -107,12 +99,37 @@ void ffileStream::DoDeleteOutStream()
wxString ffileStream::GetInFileName() const
{
static bool bFileCreated = false;
if (!bFileCreated)
class AutoRemoveFile
{
// Create the file only once
bFileCreated = true;
public:
AutoRemoveFile()
{
m_created = false;
}
~AutoRemoveFile()
{
if ( m_created )
wxRemoveFile(FILENAME_FFILEINSTREAM);
}
bool ShouldCreate()
{
if ( m_created )
return false;
m_created = true;
return true;
}
private:
bool m_created;
};
static AutoRemoveFile autoFile;
if ( autoFile.ShouldCreate() )
{
// Make sure we have a input file...
char buf[DATABUFFER_SIZE];
wxFFileOutputStream out(FILENAME_FFILEINSTREAM);

View File

@ -37,7 +37,6 @@ class fileStream : public BaseStreamTestCase<wxFileInputStream, wxFileOutputStre
{
public:
fileStream();
virtual ~fileStream();
CPPUNIT_TEST_SUITE(fileStream);
// Base class stream tests the fileStream supports.
@ -79,13 +78,6 @@ fileStream::fileStream()
m_bSeekInvalidBeyondEnd = false;
}
fileStream::~fileStream()
{
// Remove the temp test file...
::wxRemoveFile(FILENAME_FILEINSTREAM);
::wxRemoveFile(FILENAME_FILEOUTSTREAM);
}
wxFileInputStream *fileStream::DoCreateInStream()
{
wxFileInputStream *pFileInStream = new wxFileInputStream(GetInFileName());
@ -106,12 +98,37 @@ void fileStream::DoDeleteOutStream()
wxString fileStream::GetInFileName() const
{
static bool bFileCreated = false;
if (!bFileCreated)
class AutoRemoveFile
{
// Create the file only once
bFileCreated = true;
public:
AutoRemoveFile()
{
m_created = false;
}
~AutoRemoveFile()
{
if ( m_created )
wxRemoveFile(FILENAME_FILEINSTREAM);
}
bool ShouldCreate()
{
if ( m_created )
return false;
m_created = true;
return true;
}
private:
bool m_created;
};
static AutoRemoveFile autoFile;
if ( autoFile.ShouldCreate() )
{
// Make sure we have a input file...
char buf[DATABUFFER_SIZE];
wxFileOutputStream out(FILENAME_FILEINSTREAM);