2010-06-13 10:30:55 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/file/filefn.cpp
|
|
|
|
// Purpose: generic file functions unit test
|
|
|
|
// Author: Francesco Montorsi (extracted from console sample)
|
|
|
|
// Created: 2010-06-13
|
|
|
|
// Copyright: (c) 2010 wxWidgets team
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
#if wxUSE_FILE
|
|
|
|
|
|
|
|
#include "wx/ffile.h"
|
|
|
|
#include "wx/filefn.h"
|
2015-04-22 15:38:09 -04:00
|
|
|
#include "wx/textfile.h"
|
|
|
|
#include "wx/filesys.h"
|
2010-06-13 10:30:55 -04:00
|
|
|
|
|
|
|
#include "testfile.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
class FileFunctionsTestCase
|
2010-06-13 10:30:55 -04:00
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
protected:
|
|
|
|
FileFunctionsTestCase();
|
|
|
|
~FileFunctionsTestCase();
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Helper methods
|
|
|
|
void DoCreateFile(const wxString& filePath);
|
|
|
|
void DoFileExists(const wxString& filePath);
|
|
|
|
void DoFindFile(const wxString& filePath);
|
|
|
|
void DoRemoveFile(const wxString& filePath);
|
|
|
|
void DoRenameFile(const wxString& oldFilePath,
|
|
|
|
const wxString& newFilePath,
|
|
|
|
bool overwrite,
|
|
|
|
bool withNew);
|
|
|
|
void DoConcatFile(const wxString& filePath1,
|
|
|
|
const wxString& filePath2,
|
|
|
|
const wxString& destFilePath);
|
|
|
|
|
|
|
|
wxString m_fileNameASCII;
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
wxString m_fileNameNonASCII;
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
wxString m_fileNameWork;
|
2010-06-13 10:30:55 -04:00
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(FileFunctionsTestCase);
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2021-07-04 08:59:35 -04:00
|
|
|
// test fixture implementation
|
2010-06-13 10:30:55 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
FileFunctionsTestCase::FileFunctionsTestCase()
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Initialize local data
|
|
|
|
|
|
|
|
wxFileName fn1(wxFileName::GetTempDir(), wxT("wx_file_mask.txt"));
|
|
|
|
m_fileNameASCII = fn1.GetFullPath();
|
|
|
|
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
// This file name is 'wx_file_mask.txt' in Russian.
|
|
|
|
wxFileName fn2(wxFileName::GetTempDir(),
|
|
|
|
wxT("wx_\u043C\u0430\u0441\u043A\u0430_\u0444\u0430\u0439\u043B\u0430.txt"));
|
|
|
|
m_fileNameNonASCII = fn2.GetFullPath();
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
wxFileName fn3(wxFileName::GetTempDir(), wxT("wx_test_copy"));
|
|
|
|
m_fileNameWork = fn3.GetFullPath();
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
FileFunctionsTestCase::~FileFunctionsTestCase()
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Remove all remaining temporary files
|
|
|
|
if ( wxFileExists(m_fileNameASCII) )
|
|
|
|
{
|
|
|
|
wxRemoveFile(m_fileNameASCII);
|
|
|
|
}
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
if ( wxFileExists(m_fileNameNonASCII) )
|
|
|
|
{
|
|
|
|
wxRemoveFile(m_fileNameNonASCII);
|
|
|
|
}
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
if ( wxFileExists(m_fileNameWork) )
|
|
|
|
{
|
|
|
|
wxRemoveFile(m_fileNameWork);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// tests themselves start here
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::GetTempFolder",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Verify that obtained temporary folder is not empty.
|
|
|
|
wxString tmpDir = wxFileName::GetTempDir();
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !tmpDir.IsEmpty() );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::CopyFile",
|
|
|
|
"[filefn]")
|
2010-06-13 10:30:55 -04:00
|
|
|
{
|
2017-10-01 12:17:49 -04:00
|
|
|
const wxString filename1(wxS("horse.xpm"));
|
2015-04-22 15:38:09 -04:00
|
|
|
const wxString& filename2 = m_fileNameWork;
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File 1: " << filename1 << " File 2: " << filename2);
|
2015-04-22 15:38:09 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( wxCopyFile(filename1, filename2) );
|
2010-06-13 10:30:55 -04:00
|
|
|
|
|
|
|
// verify that the two files have the same contents!
|
|
|
|
wxFFile f1(filename1, wxT("rb")),
|
|
|
|
f2(filename2, wxT("rb"));
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( f1.IsOpened() );
|
|
|
|
REQUIRE( f2.IsOpened() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
2010-06-13 10:30:55 -04:00
|
|
|
wxString s1, s2;
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( f1.ReadAll(&s1) );
|
|
|
|
REQUIRE( f2.ReadAll(&s2) );
|
|
|
|
CHECK( s1 == s2 );
|
2010-06-13 10:30:55 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( f1.Close() );
|
|
|
|
CHECK( f2.Close() );
|
|
|
|
CHECK( wxRemoveFile(filename2) );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::CreateFile",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Create file name containing ASCII characters only.
|
|
|
|
DoCreateFile(m_fileNameASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
// Create file name containing non-ASCII characters.
|
|
|
|
DoCreateFile(m_fileNameNonASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileFunctionsTestCase::DoCreateFile(const wxString& filePath)
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File: " << filePath);
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Create temporary file.
|
|
|
|
wxTextFile file;
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( file.Create(filePath) );
|
|
|
|
CHECK( file.Close() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
wxRemoveFile(filePath);
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::FileExists",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxFileExists(wxT("horse.png")) );
|
|
|
|
CHECK( !wxFileExists(wxT("horse.___")) );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Check file name containing ASCII characters only.
|
|
|
|
DoFileExists(m_fileNameASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
// Check file name containing non-ASCII characters.
|
|
|
|
DoFileExists(m_fileNameNonASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileFunctionsTestCase::DoFileExists(const wxString& filePath)
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File: " << filePath);
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Create temporary file.
|
|
|
|
wxTextFile file;
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( file.Create(filePath) );
|
|
|
|
CHECK( file.Close() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Verify that file exists with 2 methods.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( file.Exists() );
|
|
|
|
CHECK( wxFileExists(filePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
wxRemoveFile(filePath);
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::FindFile",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Find file name containing ASCII characters only.
|
|
|
|
DoFindFile(m_fileNameASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
// Find file name containing non-ASCII characters.
|
|
|
|
DoFindFile(m_fileNameNonASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileFunctionsTestCase::DoFindFile(const wxString& filePath)
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File: " << filePath);
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Create temporary file.
|
|
|
|
wxTextFile file;
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( file.Create(filePath) );
|
|
|
|
CHECK( file.Close() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Check if file can be found (method 1).
|
|
|
|
wxString foundFile = wxFindFirstFile(filePath, wxFILE);
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( foundFile == filePath );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Check if file can be found (method 2).
|
|
|
|
wxFileSystem fs;
|
|
|
|
wxString furl = fs.FindFirst(filePath, wxFILE);
|
2019-08-24 01:49:25 -04:00
|
|
|
wxFileName fname = wxFileName::URLToFileName(furl);
|
2015-04-22 15:38:09 -04:00
|
|
|
foundFile = fname.GetFullPath();
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( foundFile == filePath );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
wxRemoveFile(filePath);
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::FindFileNext",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Construct file name containing ASCII characters only.
|
|
|
|
const wxString fileMask(wxT("horse.*"));
|
|
|
|
|
|
|
|
// Check using method 1.
|
|
|
|
wxString foundFile1 = wxFindFirstFile(fileMask, wxFILE);
|
|
|
|
wxString foundFile2 = wxFindNextFile();
|
|
|
|
wxFileName fn1(foundFile1);
|
|
|
|
wxFileName fn2(foundFile2);
|
|
|
|
// Full names must be different.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( foundFile1 != foundFile2 );
|
2015-04-22 15:38:09 -04:00
|
|
|
// Base names must be the same.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( fn1.GetName() == fn2.GetName() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Check using method 2.
|
|
|
|
wxFileSystem fs;
|
|
|
|
wxString furl = fs.FindFirst(fileMask, wxFILE);
|
2019-08-24 01:49:25 -04:00
|
|
|
fn1 = wxFileName::URLToFileName(furl);
|
2015-04-22 15:38:09 -04:00
|
|
|
foundFile1 = fn1.GetFullPath();
|
|
|
|
furl = fs.FindNext();
|
2019-08-24 01:49:25 -04:00
|
|
|
fn2 = wxFileName::URLToFileName(furl);
|
2015-04-22 15:38:09 -04:00
|
|
|
foundFile2 = fn2.GetFullPath();
|
|
|
|
// Full names must be different.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( fn1.GetFullPath() != fn2.GetFullPath() );
|
2015-04-22 15:38:09 -04:00
|
|
|
// Base names must be the same.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( fn1.GetName() == fn2.GetName() );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::RemoveFile",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Create & remove file with name containing ASCII characters only.
|
|
|
|
DoRemoveFile(m_fileNameASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
// Create & remove file with name containing non-ASCII characters.
|
|
|
|
DoRemoveFile(m_fileNameNonASCII);
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2010-06-13 10:30:55 -04:00
|
|
|
}
|
|
|
|
|
2015-04-22 15:38:09 -04:00
|
|
|
void FileFunctionsTestCase::DoRemoveFile(const wxString& filePath)
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File: " << filePath);
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Create temporary file.
|
|
|
|
wxTextFile file;
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( file.Create(filePath) );
|
|
|
|
CHECK( file.Close() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( file.Exists() );
|
|
|
|
CHECK( wxRemoveFile(filePath) );
|
|
|
|
CHECK( !file.Exists() );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::RenameFile",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Verify renaming file with/without overwriting
|
|
|
|
// when new file already exist/don't exist.
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
DoRenameFile(m_fileNameASCII, m_fileNameNonASCII, false, false);
|
|
|
|
DoRenameFile(m_fileNameASCII, m_fileNameNonASCII, false, true);
|
|
|
|
DoRenameFile(m_fileNameASCII, m_fileNameNonASCII, true, false);
|
|
|
|
DoRenameFile(m_fileNameASCII, m_fileNameNonASCII, true, true);
|
|
|
|
DoRenameFile(m_fileNameNonASCII, m_fileNameASCII, false, false);
|
|
|
|
DoRenameFile(m_fileNameNonASCII, m_fileNameASCII, false, true);
|
|
|
|
DoRenameFile(m_fileNameNonASCII, m_fileNameASCII, true, false);
|
|
|
|
DoRenameFile(m_fileNameNonASCII, m_fileNameASCII, true, true);
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileFunctionsTestCase::DoRenameFile(const wxString& oldFilePath,
|
|
|
|
const wxString& newFilePath,
|
|
|
|
bool overwrite,
|
|
|
|
bool withNew)
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File 1:" << oldFilePath << " File 2: " << newFilePath);
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Create temporary source file.
|
|
|
|
wxTextFile file;
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( file.Create(oldFilePath) );
|
|
|
|
CHECK( file.Close() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
if ( withNew )
|
|
|
|
{
|
|
|
|
// Create destination file to test overwriting.
|
|
|
|
wxTextFile file2;
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( file2.Create(newFilePath) );
|
|
|
|
CHECK( file2.Close() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxFileExists(newFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Remove destination file
|
|
|
|
if ( wxFileExists(newFilePath) )
|
|
|
|
{
|
|
|
|
wxRemoveFile(newFilePath);
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !wxFileExists(newFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxFileExists(oldFilePath) );
|
|
|
|
CHECK( wxFileExists(oldFilePath) );
|
|
|
|
CHECK( wxFileExists(oldFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
bool shouldFail = !overwrite && withNew;
|
|
|
|
if ( shouldFail )
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !wxRenameFile(oldFilePath, newFilePath, overwrite));
|
2015-04-22 15:38:09 -04:00
|
|
|
// Verify that file has not been renamed.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxFileExists(oldFilePath) );
|
|
|
|
CHECK( wxFileExists(newFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
wxRemoveFile(oldFilePath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxRenameFile(oldFilePath, newFilePath, overwrite) );
|
2015-04-22 15:38:09 -04:00
|
|
|
// Verify that file has been renamed.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !wxFileExists(oldFilePath) );
|
|
|
|
CHECK( wxFileExists(newFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
wxRemoveFile(newFilePath);
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::ConcatenateFiles",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
DoConcatFile(m_fileNameASCII, m_fileNameNonASCII, m_fileNameWork);
|
|
|
|
DoConcatFile(m_fileNameNonASCII, m_fileNameASCII, m_fileNameWork);
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileFunctionsTestCase::DoConcatFile(const wxString& filePath1,
|
|
|
|
const wxString& filePath2,
|
|
|
|
const wxString& destFilePath)
|
|
|
|
{
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File 1:" << filePath1
|
|
|
|
<< " File 2: " << filePath2
|
|
|
|
<< " File 3: " << destFilePath);
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Prepare source data
|
|
|
|
wxFFile f1(filePath1, wxT("wb")),
|
|
|
|
f2(filePath2, wxT("wb"));
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( f1.IsOpened() );
|
|
|
|
REQUIRE( f2.IsOpened() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
wxString s1(wxT("1234567890"));
|
|
|
|
wxString s2(wxT("abcdefghij"));
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( f1.Write(s1) );
|
|
|
|
CHECK( f2.Write(s2) );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( f1.Close() );
|
|
|
|
CHECK( f2.Close() );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Concatenate source files
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxConcatFiles(filePath1, filePath2, destFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
|
|
|
|
// Verify content of destination file
|
2021-07-04 08:59:35 -04:00
|
|
|
REQUIRE( wxFileExists(destFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
wxString sSrc = s1 + s2;
|
|
|
|
wxString s3;
|
|
|
|
wxFFile f3(destFilePath, wxT("rb"));
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( f3.ReadAll(&s3) );
|
|
|
|
CHECK( sSrc.length() == s3.length() );
|
|
|
|
CHECK( memcmp(sSrc.c_str(), s3.c_str(), sSrc.length()) == 0 );
|
|
|
|
CHECK( f3.Close() );
|
|
|
|
|
|
|
|
CHECK( wxRemoveFile(filePath1) );
|
|
|
|
CHECK( wxRemoveFile(filePath2) );
|
|
|
|
CHECK( wxRemoveFile(destFilePath) );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::GetCwd",
|
|
|
|
"[filefn]")
|
2015-04-22 15:38:09 -04:00
|
|
|
{
|
|
|
|
// Verify that obtained working directory is not empty.
|
|
|
|
wxString cwd = wxGetCwd();
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !cwd.IsEmpty() );
|
2015-04-22 15:38:09 -04:00
|
|
|
}
|
2010-06-13 10:30:55 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::Eof",
|
|
|
|
"[filefn]")
|
2017-04-01 12:38:14 -04:00
|
|
|
{
|
|
|
|
const wxString filename(wxT("horse.bmp"));
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File: " << filename);
|
2017-04-01 13:07:26 -04:00
|
|
|
|
2017-04-01 12:38:14 -04:00
|
|
|
wxFFile file(filename, wxT("r"));
|
|
|
|
// wxFFile::Eof must be false at start
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !file.Eof() );
|
|
|
|
CHECK( file.SeekEnd() );
|
2017-04-01 12:38:14 -04:00
|
|
|
// wxFFile::Eof returns true only after attempt to read last byte
|
|
|
|
char array[1];
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( file.Read(array, 1) == 0 );
|
|
|
|
CHECK( file.Eof() );
|
2017-04-01 12:38:14 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( file.Close() );
|
2017-04-01 12:38:14 -04:00
|
|
|
// wxFFile::Eof after close should not cause crash but fail instead
|
2021-07-04 08:56:24 -04:00
|
|
|
WX_ASSERT_FAILS_WITH_ASSERT( file.Eof() );
|
2017-04-01 12:38:14 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::Error",
|
|
|
|
"[filefn]")
|
2017-04-01 12:38:14 -04:00
|
|
|
{
|
|
|
|
const wxString filename(wxT("horse.bmp"));
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File: " << filename);
|
2017-04-01 13:07:26 -04:00
|
|
|
|
2017-04-01 12:38:14 -04:00
|
|
|
wxFFile file(filename, wxT("r"));
|
|
|
|
// wxFFile::Error must be false at start assuming file "horse.bmp" exists.
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !file.Error() );
|
2017-04-01 12:38:14 -04:00
|
|
|
// Attempt to write to file opened in readonly mode should cause error
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !file.Write(filename) );
|
|
|
|
CHECK( file.Error() );
|
2017-04-01 12:38:14 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( file.Close() );
|
2017-04-01 12:38:14 -04:00
|
|
|
// wxFFile::Error after close should not cause crash but fail instead
|
2021-07-04 08:56:24 -04:00
|
|
|
WX_ASSERT_FAILS_WITH_ASSERT( file.Error() );
|
2017-04-01 12:38:14 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::DirExists",
|
|
|
|
"[filefn]")
|
2017-04-01 12:47:16 -04:00
|
|
|
{
|
|
|
|
wxString cwd = wxGetCwd();
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("CWD: " << cwd);
|
2017-04-01 13:07:26 -04:00
|
|
|
|
2017-04-01 12:47:16 -04:00
|
|
|
// Current working directory must exist
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK(wxDirExists(cwd));
|
2017-04-01 12:47:16 -04:00
|
|
|
}
|
2010-06-13 10:30:55 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::IsAbsolutePath",
|
|
|
|
"[filefn]")
|
2017-04-01 12:47:16 -04:00
|
|
|
{
|
|
|
|
wxString name = wxT("horse.bmp");
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("File: " << name);
|
2017-04-01 13:07:26 -04:00
|
|
|
|
2017-04-01 12:47:16 -04:00
|
|
|
// File name is given as relative path
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( !wxIsAbsolutePath(name) );
|
2017-04-01 12:47:16 -04:00
|
|
|
|
|
|
|
wxFileName filename(name);
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( filename.MakeAbsolute() );
|
2017-04-01 12:47:16 -04:00
|
|
|
// wxFileName::GetFullPath returns absolute path
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxIsAbsolutePath(filename.GetFullPath()));
|
2021-03-01 16:02:20 -05:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxIsAbsolutePath("\\"));
|
|
|
|
CHECK( wxIsAbsolutePath("c:"));
|
|
|
|
CHECK( !wxIsAbsolutePath("c"));
|
2021-03-01 16:02:20 -05:00
|
|
|
#endif
|
2017-04-01 12:47:16 -04:00
|
|
|
}
|
2010-06-13 10:30:55 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::PathOnly",
|
|
|
|
"[filefn]")
|
2017-04-01 12:47:16 -04:00
|
|
|
{
|
|
|
|
wxString name = wxT("horse.bmp");
|
|
|
|
// Get absolute path to horse.bmp
|
2017-04-08 03:44:02 -04:00
|
|
|
wxFileName filename(name);
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( filename.MakeAbsolute() );
|
2017-04-01 12:47:16 -04:00
|
|
|
|
|
|
|
wxString pathOnly = wxPathOnly(filename.GetFullPath());
|
Replace CppUnit with Catch for unit tests
Drop the legacy CppUnit testing framework used for the unit tests.
Replacing it with Catch has the advantage of not requiring CppUnit
libraries to be installed on the system in order to be able to run
tests (Catch is header-only and a copy of it is now included in the
main repository itself) and, in the future, of being able to write
the tests in a much more natural way.
For now, however, avoid changing the existing tests code as much as
[reasonably] possible to avoid introducing bugs in them and provide
the CppUnit compatibility macros in the new wx/catch_cppunit.h header
which allow to preserve the 99% of the existing code unchanged. Some
of the required changes are:
- Decompose asserts using "a && b" conditions into multiple asserts
checking "a" and "b" independently. This would have been better
even with CppUnit (to know which part of condition exactly failed)
and is required with Catch.
- Use extra parentheses around such conditions when they can't be
easily decomposed in the arrays test, due to the use of macros.
This is not ideal from the point of view of messages given when
the tests fail but will do for now.
- Rewrite asserts using "a || b" as a combination of condition
checks and assert macros. Again, this is better anyhow, and is
required with Catch. Incidentally, this allowed to fix a bug in
the "exec" unit test which didn't leave enough time for the new
process to be launched before trying to kill it.
- Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros,
our emulation of this macro can be used only once.
- Provide string conversions using Catch-specific StringMaker for
a couple of types.
- Replace custom wxImage comparison with a Catch-specific matcher
class.
- Remove most of test running logic from test.cpp, in particular don't
parse command line ourselves any longer but use Catch built-in
command line parser. This is a source of a minor regression:
previously, both "Foo" and "FooTestCase" could be used as the name of
the test to run, but now only the latter is accepted.
2017-11-01 14:15:24 -04:00
|
|
|
if ( !wxDirExists(pathOnly) )
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( pathOnly == wxString() );
|
2017-04-01 12:47:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unit tests for Mkdir and Rmdir doesn't cover non-ASCII directory names.
|
|
|
|
// Rmdir fails on them on Linux. See ticket #17644.
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::Mkdir",
|
|
|
|
"[filefn]")
|
2017-04-01 12:47:16 -04:00
|
|
|
{
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2017-04-01 13:12:27 -04:00
|
|
|
wxString dirname = wxString::FromUTF8("__wxMkdir_test_dir_with_\xc3\xb6");
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("Dir: " << dirname);
|
|
|
|
|
|
|
|
CHECK( wxMkdir(dirname) );
|
|
|
|
CHECK( wxDirExists(dirname) );
|
|
|
|
CHECK( wxRmdir(dirname) );
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2017-04-01 12:47:16 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
TEST_CASE_METHOD(FileFunctionsTestCase,
|
|
|
|
"FileFunctions::Rmdir",
|
|
|
|
"[filefn]")
|
2017-04-01 12:47:16 -04:00
|
|
|
{
|
2020-10-18 17:47:29 -04:00
|
|
|
#if wxUSE_UNICODE
|
2017-04-01 13:12:27 -04:00
|
|
|
wxString dirname = wxString::FromUTF8("__wxRmdir_test_dir_with_\xc3\xb6");
|
2021-07-04 08:59:35 -04:00
|
|
|
INFO("Dir: " << dirname);
|
2017-04-01 13:07:26 -04:00
|
|
|
|
2021-07-04 08:59:35 -04:00
|
|
|
CHECK( wxMkdir(dirname) );
|
|
|
|
CHECK( wxRmdir(dirname) );
|
|
|
|
CHECK( !wxDirExists(dirname) );
|
2020-10-18 17:47:29 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2017-04-01 12:47:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: other file functions to test:
|
2010-06-13 10:30:55 -04:00
|
|
|
|
|
|
|
wxChar* wxFileNameFromPath(wxChar *path);
|
|
|
|
wxString wxFileNameFromPath(const wxString& path);
|
|
|
|
|
|
|
|
bool wxIsWild(const wxString& pattern);
|
|
|
|
|
|
|
|
bool wxMatchWild(const wxString& pattern, const wxString& text, bool dot_special = true);
|
|
|
|
|
|
|
|
bool wxSetWorkingDirectory(const wxString& d);
|
|
|
|
|
|
|
|
wxFileKind wxGetFileKind(int fd);
|
|
|
|
wxFileKind wxGetFileKind(FILE *fp);
|
|
|
|
|
|
|
|
bool wxIsWritable(const wxString &path);
|
|
|
|
bool wxIsReadable(const wxString &path);
|
|
|
|
bool wxIsExecutable(const wxString &path);
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endif // wxUSE_FILE
|