Remove CppUnit boilerplate from the file functions test

This cuts down the number of lines and allows to execute, or exclude,
individual tests easily.
This commit is contained in:
Vadim Zeitlin
2021-07-04 13:59:35 +01:00
parent 3e1687c36b
commit bab92b4805

View File

@@ -26,51 +26,11 @@
// test class // test class
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class FileFunctionsTestCase : public CppUnit::TestCase class FileFunctionsTestCase
{ {
public: protected:
FileFunctionsTestCase() { } FileFunctionsTestCase();
void setUp() wxOVERRIDE; ~FileFunctionsTestCase();
void tearDown() wxOVERRIDE;
private:
CPPUNIT_TEST_SUITE( FileFunctionsTestCase );
CPPUNIT_TEST( GetTempFolder );
CPPUNIT_TEST( CopyFile );
CPPUNIT_TEST( CreateFile );
CPPUNIT_TEST( FileExists );
CPPUNIT_TEST( FindFile );
CPPUNIT_TEST( FindFileNext );
CPPUNIT_TEST( RemoveFile );
CPPUNIT_TEST( RenameFile );
CPPUNIT_TEST( ConcatenateFiles );
CPPUNIT_TEST( GetCwd );
CPPUNIT_TEST( FileEof );
CPPUNIT_TEST( FileError );
CPPUNIT_TEST( DirExists );
CPPUNIT_TEST( IsAbsolutePath );
CPPUNIT_TEST( PathOnly );
CPPUNIT_TEST( Mkdir );
CPPUNIT_TEST( Rmdir );
CPPUNIT_TEST_SUITE_END();
void GetTempFolder();
void CopyFile();
void CreateFile();
void FileExists();
void FindFile();
void FindFileNext();
void RemoveFile();
void RenameFile();
void ConcatenateFiles();
void GetCwd();
void FileEof();
void FileError();
void DirExists();
void IsAbsolutePath();
void PathOnly();
void Mkdir();
void Rmdir();
// Helper methods // Helper methods
void DoCreateFile(const wxString& filePath); void DoCreateFile(const wxString& filePath);
@@ -95,17 +55,10 @@ private:
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// CppUnit macros // test fixture implementation
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
CPPUNIT_TEST_SUITE_REGISTRATION( FileFunctionsTestCase ); FileFunctionsTestCase::FileFunctionsTestCase()
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileFunctionsTestCase, "FileFunctionsTestCase" );
// ----------------------------------------------------------------------------
// tests implementation
// ----------------------------------------------------------------------------
void FileFunctionsTestCase::setUp()
{ {
// Initialize local data // Initialize local data
@@ -123,7 +76,7 @@ void FileFunctionsTestCase::setUp()
m_fileNameWork = fn3.GetFullPath(); m_fileNameWork = fn3.GetFullPath();
} }
void FileFunctionsTestCase::tearDown() FileFunctionsTestCase::~FileFunctionsTestCase()
{ {
// Remove all remaining temporary files // Remove all remaining temporary files
if ( wxFileExists(m_fileNameASCII) ) if ( wxFileExists(m_fileNameASCII) )
@@ -142,43 +95,51 @@ void FileFunctionsTestCase::tearDown()
} }
} }
void FileFunctionsTestCase::GetTempFolder() // ----------------------------------------------------------------------------
// tests themselves start here
// ----------------------------------------------------------------------------
TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::GetTempFolder",
"[filefn]")
{ {
// Verify that obtained temporary folder is not empty. // Verify that obtained temporary folder is not empty.
wxString tmpDir = wxFileName::GetTempDir(); wxString tmpDir = wxFileName::GetTempDir();
CPPUNIT_ASSERT( !tmpDir.IsEmpty() ); CHECK( !tmpDir.IsEmpty() );
} }
void FileFunctionsTestCase::CopyFile() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::CopyFile",
"[filefn]")
{ {
const wxString filename1(wxS("horse.xpm")); const wxString filename1(wxS("horse.xpm"));
const wxString& filename2 = m_fileNameWork; const wxString& filename2 = m_fileNameWork;
const std::string msg = INFO("File 1: " << filename1 << " File 2: " << filename2);
wxString::Format("File 1: %s File 2:%s", filename1, filename2)
.ToStdString();
CPPUNIT_ASSERT_MESSAGE( msg, wxCopyFile(filename1, filename2) ); REQUIRE( wxCopyFile(filename1, filename2) );
// verify that the two files have the same contents! // verify that the two files have the same contents!
wxFFile f1(filename1, wxT("rb")), wxFFile f1(filename1, wxT("rb")),
f2(filename2, wxT("rb")); f2(filename2, wxT("rb"));
CPPUNIT_ASSERT_MESSAGE( msg, f1.IsOpened() ); REQUIRE( f1.IsOpened() );
CPPUNIT_ASSERT_MESSAGE( msg, f2.IsOpened() ); REQUIRE( f2.IsOpened() );
wxString s1, s2; wxString s1, s2;
CPPUNIT_ASSERT_MESSAGE( msg, f1.ReadAll(&s1) ); REQUIRE( f1.ReadAll(&s1) );
CPPUNIT_ASSERT_MESSAGE( msg, f2.ReadAll(&s2) ); REQUIRE( f2.ReadAll(&s2) );
CPPUNIT_ASSERT_MESSAGE( msg, s1 == s2 ); CHECK( s1 == s2 );
CPPUNIT_ASSERT_MESSAGE( msg, f1.Close() ); CHECK( f1.Close() );
CPPUNIT_ASSERT_MESSAGE( msg, f2.Close() ); CHECK( f2.Close() );
CPPUNIT_ASSERT_MESSAGE( msg, wxRemoveFile(filename2) ); CHECK( wxRemoveFile(filename2) );
} }
void FileFunctionsTestCase::CreateFile() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::CreateFile",
"[filefn]")
{ {
// Create file name containing ASCII characters only. // Create file name containing ASCII characters only.
DoCreateFile(m_fileNameASCII); DoCreateFile(m_fileNameASCII);
@@ -190,20 +151,22 @@ void FileFunctionsTestCase::CreateFile()
void FileFunctionsTestCase::DoCreateFile(const wxString& filePath) void FileFunctionsTestCase::DoCreateFile(const wxString& filePath)
{ {
const std::string msg = wxString::Format("File: %s", filePath).ToStdString(); INFO("File: " << filePath);
// Create temporary file. // Create temporary file.
wxTextFile file; wxTextFile file;
CPPUNIT_ASSERT_MESSAGE( msg, file.Create(filePath) ); REQUIRE( file.Create(filePath) );
CPPUNIT_ASSERT_MESSAGE( msg, file.Close() ); CHECK( file.Close() );
wxRemoveFile(filePath); wxRemoveFile(filePath);
} }
void FileFunctionsTestCase::FileExists() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::FileExists",
"[filefn]")
{ {
CPPUNIT_ASSERT( wxFileExists(wxT("horse.png")) ); CHECK( wxFileExists(wxT("horse.png")) );
CPPUNIT_ASSERT( !wxFileExists(wxT("horse.___")) ); CHECK( !wxFileExists(wxT("horse.___")) );
// Check file name containing ASCII characters only. // Check file name containing ASCII characters only.
DoFileExists(m_fileNameASCII); DoFileExists(m_fileNameASCII);
@@ -215,21 +178,23 @@ void FileFunctionsTestCase::FileExists()
void FileFunctionsTestCase::DoFileExists(const wxString& filePath) void FileFunctionsTestCase::DoFileExists(const wxString& filePath)
{ {
const std::string msg = wxString::Format("File: %s", filePath).ToStdString(); INFO("File: " << filePath);
// Create temporary file. // Create temporary file.
wxTextFile file; wxTextFile file;
CPPUNIT_ASSERT_MESSAGE( msg, file.Create(filePath) ); REQUIRE( file.Create(filePath) );
CPPUNIT_ASSERT_MESSAGE( msg, file.Close() ); CHECK( file.Close() );
// Verify that file exists with 2 methods. // Verify that file exists with 2 methods.
CPPUNIT_ASSERT_MESSAGE( msg, file.Exists() ); CHECK( file.Exists() );
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(filePath) ); CHECK( wxFileExists(filePath) );
wxRemoveFile(filePath); wxRemoveFile(filePath);
} }
void FileFunctionsTestCase::FindFile() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::FindFile",
"[filefn]")
{ {
// Find file name containing ASCII characters only. // Find file name containing ASCII characters only.
DoFindFile(m_fileNameASCII); DoFindFile(m_fileNameASCII);
@@ -241,28 +206,30 @@ void FileFunctionsTestCase::FindFile()
void FileFunctionsTestCase::DoFindFile(const wxString& filePath) void FileFunctionsTestCase::DoFindFile(const wxString& filePath)
{ {
const std::string msg = wxString::Format("File: %s", filePath).ToStdString(); INFO("File: " << filePath);
// Create temporary file. // Create temporary file.
wxTextFile file; wxTextFile file;
CPPUNIT_ASSERT_MESSAGE( msg, file.Create(filePath) ); REQUIRE( file.Create(filePath) );
CPPUNIT_ASSERT_MESSAGE( msg, file.Close() ); CHECK( file.Close() );
// Check if file can be found (method 1). // Check if file can be found (method 1).
wxString foundFile = wxFindFirstFile(filePath, wxFILE); wxString foundFile = wxFindFirstFile(filePath, wxFILE);
CPPUNIT_ASSERT_MESSAGE( msg, foundFile == filePath ); CHECK( foundFile == filePath );
// Check if file can be found (method 2). // Check if file can be found (method 2).
wxFileSystem fs; wxFileSystem fs;
wxString furl = fs.FindFirst(filePath, wxFILE); wxString furl = fs.FindFirst(filePath, wxFILE);
wxFileName fname = wxFileName::URLToFileName(furl); wxFileName fname = wxFileName::URLToFileName(furl);
foundFile = fname.GetFullPath(); foundFile = fname.GetFullPath();
CPPUNIT_ASSERT_MESSAGE( msg, foundFile == filePath ); CHECK( foundFile == filePath );
wxRemoveFile(filePath); wxRemoveFile(filePath);
} }
void FileFunctionsTestCase::FindFileNext() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::FindFileNext",
"[filefn]")
{ {
// Construct file name containing ASCII characters only. // Construct file name containing ASCII characters only.
const wxString fileMask(wxT("horse.*")); const wxString fileMask(wxT("horse.*"));
@@ -273,9 +240,9 @@ void FileFunctionsTestCase::FindFileNext()
wxFileName fn1(foundFile1); wxFileName fn1(foundFile1);
wxFileName fn2(foundFile2); wxFileName fn2(foundFile2);
// Full names must be different. // Full names must be different.
CPPUNIT_ASSERT( foundFile1 != foundFile2 ); CHECK( foundFile1 != foundFile2 );
// Base names must be the same. // Base names must be the same.
CPPUNIT_ASSERT( fn1.GetName() == fn2.GetName() ); CHECK( fn1.GetName() == fn2.GetName() );
// Check using method 2. // Check using method 2.
wxFileSystem fs; wxFileSystem fs;
@@ -286,12 +253,14 @@ void FileFunctionsTestCase::FindFileNext()
fn2 = wxFileName::URLToFileName(furl); fn2 = wxFileName::URLToFileName(furl);
foundFile2 = fn2.GetFullPath(); foundFile2 = fn2.GetFullPath();
// Full names must be different. // Full names must be different.
CPPUNIT_ASSERT( fn1.GetFullPath() != fn2.GetFullPath() ); CHECK( fn1.GetFullPath() != fn2.GetFullPath() );
// Base names must be the same. // Base names must be the same.
CPPUNIT_ASSERT( fn1.GetName() == fn2.GetName() ); CHECK( fn1.GetName() == fn2.GetName() );
} }
void FileFunctionsTestCase::RemoveFile() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::RemoveFile",
"[filefn]")
{ {
// Create & remove file with name containing ASCII characters only. // Create & remove file with name containing ASCII characters only.
DoRemoveFile(m_fileNameASCII); DoRemoveFile(m_fileNameASCII);
@@ -303,19 +272,21 @@ void FileFunctionsTestCase::RemoveFile()
void FileFunctionsTestCase::DoRemoveFile(const wxString& filePath) void FileFunctionsTestCase::DoRemoveFile(const wxString& filePath)
{ {
const std::string msg = wxString::Format("File: %s", filePath).ToStdString(); INFO("File: " << filePath);
// Create temporary file. // Create temporary file.
wxTextFile file; wxTextFile file;
CPPUNIT_ASSERT_MESSAGE( msg, file.Create(filePath) ); REQUIRE( file.Create(filePath) );
CPPUNIT_ASSERT_MESSAGE( msg, file.Close() ); CHECK( file.Close() );
CPPUNIT_ASSERT_MESSAGE( msg, file.Exists() ); CHECK( file.Exists() );
CPPUNIT_ASSERT_MESSAGE( msg, wxRemoveFile(filePath) ); CHECK( wxRemoveFile(filePath) );
CPPUNIT_ASSERT_MESSAGE( msg, !file.Exists() ); CHECK( !file.Exists() );
} }
void FileFunctionsTestCase::RenameFile() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::RenameFile",
"[filefn]")
{ {
// Verify renaming file with/without overwriting // Verify renaming file with/without overwriting
// when new file already exist/don't exist. // when new file already exist/don't exist.
@@ -337,23 +308,21 @@ FileFunctionsTestCase::DoRenameFile(const wxString& oldFilePath,
bool overwrite, bool overwrite,
bool withNew) bool withNew)
{ {
const std::string msg = INFO("File 1:" << oldFilePath << " File 2: " << newFilePath);
wxString::Format(wxT("File 1: %s File 2:%s"), oldFilePath, newFilePath)
.ToStdString();
// Create temporary source file. // Create temporary source file.
wxTextFile file; wxTextFile file;
CPPUNIT_ASSERT_MESSAGE( msg, file.Create(oldFilePath) ); REQUIRE( file.Create(oldFilePath) );
CPPUNIT_ASSERT_MESSAGE( msg, file.Close() ); CHECK( file.Close() );
if ( withNew ) if ( withNew )
{ {
// Create destination file to test overwriting. // Create destination file to test overwriting.
wxTextFile file2; wxTextFile file2;
CPPUNIT_ASSERT_MESSAGE( msg, file2.Create(newFilePath) ); REQUIRE( file2.Create(newFilePath) );
CPPUNIT_ASSERT_MESSAGE( msg, file2.Close() ); CHECK( file2.Close() );
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(newFilePath) ); CHECK( wxFileExists(newFilePath) );
} }
else else
{ {
@@ -363,36 +332,38 @@ FileFunctionsTestCase::DoRenameFile(const wxString& oldFilePath,
wxRemoveFile(newFilePath); wxRemoveFile(newFilePath);
} }
CPPUNIT_ASSERT_MESSAGE( msg, !wxFileExists(newFilePath) ); CHECK( !wxFileExists(newFilePath) );
} }
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(oldFilePath) ); CHECK( wxFileExists(oldFilePath) );
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(oldFilePath) ); CHECK( wxFileExists(oldFilePath) );
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(oldFilePath) ); CHECK( wxFileExists(oldFilePath) );
bool shouldFail = !overwrite && withNew; bool shouldFail = !overwrite && withNew;
if ( shouldFail ) if ( shouldFail )
{ {
CPPUNIT_ASSERT_MESSAGE( msg, !wxRenameFile(oldFilePath, newFilePath, overwrite)); CHECK( !wxRenameFile(oldFilePath, newFilePath, overwrite));
// Verify that file has not been renamed. // Verify that file has not been renamed.
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(oldFilePath) ); CHECK( wxFileExists(oldFilePath) );
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(newFilePath) ); CHECK( wxFileExists(newFilePath) );
// Cleanup. // Cleanup.
wxRemoveFile(oldFilePath); wxRemoveFile(oldFilePath);
} }
else else
{ {
CPPUNIT_ASSERT_MESSAGE( msg, wxRenameFile(oldFilePath, newFilePath, overwrite) ); CHECK( wxRenameFile(oldFilePath, newFilePath, overwrite) );
// Verify that file has been renamed. // Verify that file has been renamed.
CPPUNIT_ASSERT_MESSAGE( msg, !wxFileExists(oldFilePath) ); CHECK( !wxFileExists(oldFilePath) );
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(newFilePath) ); CHECK( wxFileExists(newFilePath) );
} }
// Cleanup. // Cleanup.
wxRemoveFile(newFilePath); wxRemoveFile(newFilePath);
} }
void FileFunctionsTestCase::ConcatenateFiles() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::ConcatenateFiles",
"[filefn]")
{ {
#if wxUSE_UNICODE #if wxUSE_UNICODE
DoConcatFile(m_fileNameASCII, m_fileNameNonASCII, m_fileNameWork); DoConcatFile(m_fileNameASCII, m_fileNameNonASCII, m_fileNameWork);
@@ -404,151 +375,166 @@ void FileFunctionsTestCase::DoConcatFile(const wxString& filePath1,
const wxString& filePath2, const wxString& filePath2,
const wxString& destFilePath) const wxString& destFilePath)
{ {
const std::string msg = INFO("File 1:" << filePath1
wxString::Format("File 1: %s File 2:%s File 3: %s", << " File 2: " << filePath2
filePath1, filePath2, destFilePath) << " File 3: " << destFilePath);
.ToStdString();
// Prepare source data // Prepare source data
wxFFile f1(filePath1, wxT("wb")), wxFFile f1(filePath1, wxT("wb")),
f2(filePath2, wxT("wb")); f2(filePath2, wxT("wb"));
CPPUNIT_ASSERT_MESSAGE( msg, f1.IsOpened() ); REQUIRE( f1.IsOpened() );
CPPUNIT_ASSERT_MESSAGE( msg, f2.IsOpened() ); REQUIRE( f2.IsOpened() );
wxString s1(wxT("1234567890")); wxString s1(wxT("1234567890"));
wxString s2(wxT("abcdefghij")); wxString s2(wxT("abcdefghij"));
CPPUNIT_ASSERT_MESSAGE( msg, f1.Write(s1) ); CHECK( f1.Write(s1) );
CPPUNIT_ASSERT_MESSAGE( msg, f2.Write(s2) ); CHECK( f2.Write(s2) );
CPPUNIT_ASSERT_MESSAGE( msg, f1.Close() ); CHECK( f1.Close() );
CPPUNIT_ASSERT_MESSAGE( msg, f2.Close() ); CHECK( f2.Close() );
// Concatenate source files // Concatenate source files
CPPUNIT_ASSERT_MESSAGE( msg, wxConcatFiles(filePath1, filePath2, destFilePath) ); CHECK( wxConcatFiles(filePath1, filePath2, destFilePath) );
// Verify content of destination file // Verify content of destination file
CPPUNIT_ASSERT_MESSAGE( msg, wxFileExists(destFilePath) ); REQUIRE( wxFileExists(destFilePath) );
wxString sSrc = s1 + s2; wxString sSrc = s1 + s2;
wxString s3; wxString s3;
wxFFile f3(destFilePath, wxT("rb")); wxFFile f3(destFilePath, wxT("rb"));
CPPUNIT_ASSERT_MESSAGE( msg, f3.ReadAll(&s3) ); CHECK( f3.ReadAll(&s3) );
CPPUNIT_ASSERT_MESSAGE( msg, sSrc.length() == s3.length() ); CHECK( sSrc.length() == s3.length() );
CPPUNIT_ASSERT_MESSAGE( msg, memcmp(sSrc.c_str(), s3.c_str(), sSrc.length()) == 0 ); CHECK( memcmp(sSrc.c_str(), s3.c_str(), sSrc.length()) == 0 );
CPPUNIT_ASSERT_MESSAGE( msg, f3.Close() ); CHECK( f3.Close() );
CPPUNIT_ASSERT_MESSAGE( msg, wxRemoveFile(filePath1) ); CHECK( wxRemoveFile(filePath1) );
CPPUNIT_ASSERT_MESSAGE( msg, wxRemoveFile(filePath2) ); CHECK( wxRemoveFile(filePath2) );
CPPUNIT_ASSERT_MESSAGE( msg, wxRemoveFile(destFilePath) ); CHECK( wxRemoveFile(destFilePath) );
} }
void FileFunctionsTestCase::GetCwd() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::GetCwd",
"[filefn]")
{ {
// Verify that obtained working directory is not empty. // Verify that obtained working directory is not empty.
wxString cwd = wxGetCwd(); wxString cwd = wxGetCwd();
CPPUNIT_ASSERT( !cwd.IsEmpty() ); CHECK( !cwd.IsEmpty() );
} }
void FileFunctionsTestCase::FileEof() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::Eof",
"[filefn]")
{ {
const wxString filename(wxT("horse.bmp")); const wxString filename(wxT("horse.bmp"));
const std::string msg = wxString::Format("File: %s", filename).ToStdString(); INFO("File: " << filename);
wxFFile file(filename, wxT("r")); wxFFile file(filename, wxT("r"));
// wxFFile::Eof must be false at start // wxFFile::Eof must be false at start
CPPUNIT_ASSERT_MESSAGE( msg, !file.Eof() ); CHECK( !file.Eof() );
CPPUNIT_ASSERT_MESSAGE( msg, file.SeekEnd() ); CHECK( file.SeekEnd() );
// wxFFile::Eof returns true only after attempt to read last byte // wxFFile::Eof returns true only after attempt to read last byte
char array[1]; char array[1];
CPPUNIT_ASSERT_MESSAGE( msg, file.Read(array, 1) == 0 ); CHECK( file.Read(array, 1) == 0 );
CPPUNIT_ASSERT_MESSAGE( msg, file.Eof() ); CHECK( file.Eof() );
CPPUNIT_ASSERT_MESSAGE( msg, file.Close() ); CHECK( file.Close() );
// wxFFile::Eof after close should not cause crash but fail instead // wxFFile::Eof after close should not cause crash but fail instead
WX_ASSERT_FAILS_WITH_ASSERT( file.Eof() ); WX_ASSERT_FAILS_WITH_ASSERT( file.Eof() );
} }
void FileFunctionsTestCase::FileError() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::Error",
"[filefn]")
{ {
const wxString filename(wxT("horse.bmp")); const wxString filename(wxT("horse.bmp"));
const std::string msg = wxString::Format("File: %s", filename).ToStdString(); INFO("File: " << filename);
wxFFile file(filename, wxT("r")); wxFFile file(filename, wxT("r"));
// wxFFile::Error must be false at start assuming file "horse.bmp" exists. // wxFFile::Error must be false at start assuming file "horse.bmp" exists.
CPPUNIT_ASSERT_MESSAGE( msg, !file.Error() ); CHECK( !file.Error() );
// Attempt to write to file opened in readonly mode should cause error // Attempt to write to file opened in readonly mode should cause error
CPPUNIT_ASSERT_MESSAGE( msg, !file.Write(filename) ); CHECK( !file.Write(filename) );
CPPUNIT_ASSERT_MESSAGE( msg, file.Error() ); CHECK( file.Error() );
CPPUNIT_ASSERT_MESSAGE( msg, file.Close() ); CHECK( file.Close() );
// wxFFile::Error after close should not cause crash but fail instead // wxFFile::Error after close should not cause crash but fail instead
WX_ASSERT_FAILS_WITH_ASSERT( file.Error() ); WX_ASSERT_FAILS_WITH_ASSERT( file.Error() );
} }
TEST_CASE_METHOD(FileFunctionsTestCase,
void FileFunctionsTestCase::DirExists() "FileFunctions::DirExists",
"[filefn]")
{ {
wxString cwd = wxGetCwd(); wxString cwd = wxGetCwd();
const std::string msg = wxString::Format("CWD: %s", cwd).ToStdString(); INFO("CWD: " << cwd);
// Current working directory must exist // Current working directory must exist
CPPUNIT_ASSERT_MESSAGE( msg, wxDirExists(cwd)); CHECK(wxDirExists(cwd));
} }
void FileFunctionsTestCase::IsAbsolutePath() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::IsAbsolutePath",
"[filefn]")
{ {
wxString name = wxT("horse.bmp"); wxString name = wxT("horse.bmp");
const std::string msg = wxString::Format("File: %s", name).ToStdString(); INFO("File: " << name);
// File name is given as relative path // File name is given as relative path
CPPUNIT_ASSERT_MESSAGE( msg, !wxIsAbsolutePath(name) ); CHECK( !wxIsAbsolutePath(name) );
wxFileName filename(name); wxFileName filename(name);
CPPUNIT_ASSERT( filename.MakeAbsolute() ); CHECK( filename.MakeAbsolute() );
// wxFileName::GetFullPath returns absolute path // wxFileName::GetFullPath returns absolute path
CPPUNIT_ASSERT_MESSAGE( msg, wxIsAbsolutePath(filename.GetFullPath())); CHECK( wxIsAbsolutePath(filename.GetFullPath()));
#ifdef __WINDOWS__ #ifdef __WINDOWS__
CPPUNIT_ASSERT( wxIsAbsolutePath("\\")); CHECK( wxIsAbsolutePath("\\"));
CPPUNIT_ASSERT( wxIsAbsolutePath("c:")); CHECK( wxIsAbsolutePath("c:"));
CPPUNIT_ASSERT( !wxIsAbsolutePath("c")); CHECK( !wxIsAbsolutePath("c"));
#endif #endif
} }
void FileFunctionsTestCase::PathOnly() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::PathOnly",
"[filefn]")
{ {
wxString name = wxT("horse.bmp"); wxString name = wxT("horse.bmp");
// Get absolute path to horse.bmp // Get absolute path to horse.bmp
wxFileName filename(name); wxFileName filename(name);
CPPUNIT_ASSERT( filename.MakeAbsolute() ); CHECK( filename.MakeAbsolute() );
wxString pathOnly = wxPathOnly(filename.GetFullPath()); wxString pathOnly = wxPathOnly(filename.GetFullPath());
if ( !wxDirExists(pathOnly) ) if ( !wxDirExists(pathOnly) )
CPPUNIT_ASSERT( pathOnly == wxString() ); CHECK( pathOnly == wxString() );
} }
// Unit tests for Mkdir and Rmdir doesn't cover non-ASCII directory names. // Unit tests for Mkdir and Rmdir doesn't cover non-ASCII directory names.
// Rmdir fails on them on Linux. See ticket #17644. // Rmdir fails on them on Linux. See ticket #17644.
void FileFunctionsTestCase::Mkdir() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::Mkdir",
"[filefn]")
{ {
#if wxUSE_UNICODE #if wxUSE_UNICODE
wxString dirname = wxString::FromUTF8("__wxMkdir_test_dir_with_\xc3\xb6"); wxString dirname = wxString::FromUTF8("__wxMkdir_test_dir_with_\xc3\xb6");
const std::string msg = wxString::Format("Dir: %s", dirname).ToStdString(); INFO("Dir: " << dirname);
CPPUNIT_ASSERT_MESSAGE( msg, wxMkdir(dirname) );
CPPUNIT_ASSERT_MESSAGE( msg, wxDirExists(dirname) ); CHECK( wxMkdir(dirname) );
CPPUNIT_ASSERT_MESSAGE( msg, wxRmdir(dirname) ); CHECK( wxDirExists(dirname) );
CHECK( wxRmdir(dirname) );
#endif // wxUSE_UNICODE #endif // wxUSE_UNICODE
} }
void FileFunctionsTestCase::Rmdir() TEST_CASE_METHOD(FileFunctionsTestCase,
"FileFunctions::Rmdir",
"[filefn]")
{ {
#if wxUSE_UNICODE #if wxUSE_UNICODE
wxString dirname = wxString::FromUTF8("__wxRmdir_test_dir_with_\xc3\xb6"); wxString dirname = wxString::FromUTF8("__wxRmdir_test_dir_with_\xc3\xb6");
const std::string msg = wxString::Format("Dir: %s", dirname).ToStdString(); INFO("Dir: " << dirname);
CPPUNIT_ASSERT_MESSAGE( msg, wxMkdir(dirname) ); CHECK( wxMkdir(dirname) );
CPPUNIT_ASSERT_MESSAGE( msg, wxRmdir(dirname) ); CHECK( wxRmdir(dirname) );
CPPUNIT_ASSERT_MESSAGE( msg, !wxDirExists(dirname) ); CHECK( !wxDirExists(dirname) );
#endif // wxUSE_UNICODE #endif // wxUSE_UNICODE
} }