initial commit
Signed-off-by: Peter Siegmund <mars3142@noreply.mars3142.dev>
This commit is contained in:
301
libs/wxWidgets-3.3.1/tests/file/dir.cpp
Normal file
301
libs/wxWidgets-3.3.1/tests/file/dir.cpp
Normal file
@@ -0,0 +1,301 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/file/dir.cpp
|
||||
// Purpose: wxDir unit test
|
||||
// Author: Francesco Montorsi (extracted from console sample)
|
||||
// Created: 2010-06-19
|
||||
// Copyright: (c) 2010 wxWidgets team
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
|
||||
#include "wx/dir.h"
|
||||
#include "wx/filename.h"
|
||||
#include "wx/stdpaths.h"
|
||||
|
||||
#define DIRTEST_FOLDER wxString("dirTest_folder")
|
||||
#define SEP wxFileName::GetPathSeparator()
|
||||
|
||||
// We can't use wxFileSelectorDefaultWildcardStr from wxCore here, so define
|
||||
// our own.
|
||||
const wxString WILDCARD_ALL =
|
||||
#if defined(__WXMSW__)
|
||||
"*.*"
|
||||
#else // Unix/Mac
|
||||
"*"
|
||||
#endif
|
||||
;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test fixture
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class DirTestCase
|
||||
{
|
||||
public:
|
||||
DirTestCase();
|
||||
~DirTestCase();
|
||||
|
||||
protected:
|
||||
void CreateTempFile(const wxString& path);
|
||||
wxArrayString DirEnumHelper(wxDir& dir,
|
||||
int flags = wxDIR_DEFAULT,
|
||||
const wxString& filespec = wxEmptyString);
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(DirTestCase);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test fixture implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void DirTestCase::CreateTempFile(const wxString& path)
|
||||
{
|
||||
wxFile f(path, wxFile::write);
|
||||
f.Write("dummy test file");
|
||||
f.Close();
|
||||
}
|
||||
|
||||
DirTestCase::DirTestCase()
|
||||
{
|
||||
// create a test directory hierarchy
|
||||
wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
|
||||
wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
|
||||
wxDir::Make(DIRTEST_FOLDER + SEP + "folder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
|
||||
wxDir::Make(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
|
||||
|
||||
CreateTempFile(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
|
||||
CreateTempFile(DIRTEST_FOLDER + SEP + "dummy");
|
||||
CreateTempFile(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo");
|
||||
CreateTempFile(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo.bar");
|
||||
}
|
||||
|
||||
DirTestCase::~DirTestCase()
|
||||
{
|
||||
wxRemove(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
|
||||
wxRemove(DIRTEST_FOLDER + SEP + "dummy");
|
||||
wxRemove(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo");
|
||||
wxRemove(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo.bar");
|
||||
wxDir::Remove(DIRTEST_FOLDER, wxPATH_RMDIR_RECURSIVE);
|
||||
}
|
||||
|
||||
wxArrayString DirTestCase::DirEnumHelper(wxDir& dir,
|
||||
int flags,
|
||||
const wxString& filespec)
|
||||
{
|
||||
wxArrayString ret;
|
||||
CHECK( dir.IsOpened() );
|
||||
|
||||
wxString filename;
|
||||
bool cont = dir.GetFirst(&filename, filespec, flags);
|
||||
while ( cont )
|
||||
{
|
||||
ret.push_back(filename);
|
||||
cont = dir.GetNext(&filename);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// tests themselves start here
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE_METHOD(DirTestCase, "Dir::Enum", "[dir]")
|
||||
{
|
||||
wxDir dir(DIRTEST_FOLDER);
|
||||
CHECK( dir.IsOpened() );
|
||||
|
||||
// enumerating everything in test directory
|
||||
CHECK( DirEnumHelper(dir).size() == 4 );
|
||||
|
||||
// enumerating really everything in test directory recursively
|
||||
CHECK( DirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT).size() == 6 );
|
||||
|
||||
// enumerating object files in test directory
|
||||
CHECK( DirEnumHelper(dir, wxDIR_DEFAULT, "*.o*").size() == 0 );
|
||||
|
||||
// enumerating directories in test directory
|
||||
CHECK( DirEnumHelper(dir, wxDIR_DIRS).size() == 3 );
|
||||
|
||||
// enumerating files in test directory
|
||||
CHECK( DirEnumHelper(dir, wxDIR_FILES).size() == 1 );
|
||||
|
||||
// enumerating files including hidden in test directory
|
||||
CHECK( DirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN).size() == 1 );
|
||||
|
||||
// enumerating files and folders in test directory
|
||||
CHECK( DirEnumHelper(dir, wxDIR_FILES | wxDIR_DIRS).size() == 4 );
|
||||
}
|
||||
|
||||
class TestDirTraverser : public wxDirTraverser
|
||||
{
|
||||
public:
|
||||
wxArrayString dirs;
|
||||
|
||||
virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename)) override
|
||||
{
|
||||
return wxDIR_CONTINUE;
|
||||
}
|
||||
|
||||
virtual wxDirTraverseResult OnDir(const wxString& dirname) override
|
||||
{
|
||||
dirs.push_back(dirname);
|
||||
return wxDIR_CONTINUE;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE_METHOD(DirTestCase, "Dir::Traverse", "[dir]")
|
||||
{
|
||||
// enum all files
|
||||
wxArrayString files;
|
||||
CHECK( wxDir::GetAllFiles(DIRTEST_FOLDER, &files) == 4 );
|
||||
|
||||
// enum all files using an explicit wildcard
|
||||
CHECK(wxDir::GetAllFiles(DIRTEST_FOLDER, &files, WILDCARD_ALL) == 4);
|
||||
|
||||
// enum all files using an explicit wildcard different from WILDCARD_ALL
|
||||
//
|
||||
// broken under Wine, see https://bugs.winehq.org/show_bug.cgi?id=55677
|
||||
if ( !wxIsRunningUnderWine() )
|
||||
{
|
||||
CHECK(wxDir::GetAllFiles(DIRTEST_FOLDER, &files, "d" + WILDCARD_ALL) == 4);
|
||||
}
|
||||
else if (wxDir::GetAllFiles(DIRTEST_FOLDER, &files, "d" + WILDCARD_ALL) == 4)
|
||||
{
|
||||
WARN("PathMatchSpec() seems to work under Wine now");
|
||||
}
|
||||
|
||||
// enum all files according to the filter
|
||||
CHECK( wxDir::GetAllFiles(DIRTEST_FOLDER, &files, "*.foo") == 1 );
|
||||
|
||||
// enum again with custom traverser
|
||||
wxDir dir(DIRTEST_FOLDER);
|
||||
TestDirTraverser traverser;
|
||||
dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
|
||||
CHECK( traverser.dirs.size() == 6 );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(DirTestCase, "Dir::Exists", "[dir]")
|
||||
{
|
||||
struct
|
||||
{
|
||||
const char *dirname;
|
||||
bool shouldExist;
|
||||
} testData[] =
|
||||
{
|
||||
{ ".", true },
|
||||
{ "..", true },
|
||||
{ "$USER_DOCS_DIR", true },
|
||||
#if defined(__WINDOWS__)
|
||||
{ "$USER_DOCS_DIR\\", true },
|
||||
{ "$USER_DOCS_DIR\\\\", true },
|
||||
{ "..\\..", true },
|
||||
{ "$MSW_DRIVE", true },
|
||||
{ "$MSW_DRIVE\\", true },
|
||||
{ "$MSW_DRIVE\\\\", true },
|
||||
{ "\\\\non_existent_share\\file", false },
|
||||
{ "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist", false },
|
||||
{ "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\", false },
|
||||
{ "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\\\", false },
|
||||
{ "test.exe", false } // not a directory!
|
||||
#elif defined(__UNIX__)
|
||||
{ "../..", true },
|
||||
{ "/", true },
|
||||
{ "//", true },
|
||||
{ "/usr/bin", true },
|
||||
{ "/usr//bin", true },
|
||||
{ "/usr///bin", true },
|
||||
{ "/tmp/a/directory/which/does/not/exist", false },
|
||||
{ "/bin/ls", false } // not a directory!
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
wxString homedrive = wxGetenv("HOMEDRIVE");
|
||||
if ( homedrive.empty() )
|
||||
homedrive = "c:";
|
||||
#endif // __WINDOWS__
|
||||
|
||||
for ( size_t n = 0; n < WXSIZEOF(testData); n++ )
|
||||
{
|
||||
wxString dirname = testData[n].dirname;
|
||||
dirname.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
dirname.Replace("$MSW_DRIVE", homedrive);
|
||||
#endif // __WINDOWS__
|
||||
|
||||
const bool shouldExist = testData[n].shouldExist;
|
||||
|
||||
INFO("Directory " << dirname << ", should exist: " << shouldExist);
|
||||
CHECK( wxDir::Exists(dirname) == shouldExist );
|
||||
|
||||
wxDir d(dirname);
|
||||
CHECK( d.IsOpened() == shouldExist );
|
||||
}
|
||||
|
||||
CHECK( wxDir::Exists(wxGetCwd()) );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(DirTestCase, "Dir::GetName", "[dir]")
|
||||
{
|
||||
wxDir d;
|
||||
|
||||
CHECK( d.Open(".") );
|
||||
CHECK( d.GetName().Last() != wxFILE_SEP_PATH );
|
||||
CHECK( d.GetNameWithSep().Last() == wxFILE_SEP_PATH );
|
||||
CHECK( d.GetNameWithSep() == d.GetName() + wxFILE_SEP_PATH );
|
||||
|
||||
#ifdef __UNIX__
|
||||
CHECK( d.Open("/") );
|
||||
CHECK( d.GetName() == "/" );
|
||||
CHECK( d.GetNameWithSep() == "/" );
|
||||
#endif
|
||||
}
|
||||
|
||||
// Disabled by default test allowing to check the result of matching against
|
||||
// the given filter.
|
||||
#ifdef __WXMSW__
|
||||
|
||||
#include "wx/msw/wrapwin.h"
|
||||
#include <shlwapi.h>
|
||||
#ifdef __VISUALC__
|
||||
#pragma comment(lib, "shlwapi")
|
||||
#endif
|
||||
|
||||
#include "wx/crt.h"
|
||||
#include "wx/filefn.h"
|
||||
|
||||
TEST_CASE("Dir::Match", "[.]")
|
||||
{
|
||||
wxString filter;
|
||||
REQUIRE( wxGetEnv("WX_TEST_DIR_FILTER", &filter) );
|
||||
|
||||
static const wxString filenames[] =
|
||||
{
|
||||
"foo",
|
||||
"foo.bar",
|
||||
"foo.bar.baz",
|
||||
".hidden",
|
||||
".hidden.ext",
|
||||
};
|
||||
|
||||
// Show the results of matching the pattern using various functions.
|
||||
wxPrintf("%-15s %20s %20s %20s\n",
|
||||
"File", "wxString::Matches", "wxMatchWild", "PathMatchSpec");
|
||||
for ( const auto& fn : filenames )
|
||||
{
|
||||
wxPrintf("%-15s %20d %20d %20d\n",
|
||||
fn,
|
||||
fn.Matches(filter),
|
||||
wxMatchWild(filter, fn),
|
||||
PathMatchSpec(fn.wc_str(), filter.wc_str()) == TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __WXMSW__
|
||||
561
libs/wxWidgets-3.3.1/tests/file/filefn.cpp
Normal file
561
libs/wxWidgets-3.3.1/tests/file/filefn.cpp
Normal file
@@ -0,0 +1,561 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// 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"
|
||||
#include "wx/textfile.h"
|
||||
#include "wx/filesys.h"
|
||||
|
||||
#include "testfile.h"
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test class
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class FileFunctionsTestCase
|
||||
{
|
||||
protected:
|
||||
FileFunctionsTestCase();
|
||||
~FileFunctionsTestCase();
|
||||
|
||||
// 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;
|
||||
wxString m_fileNameNonASCII;
|
||||
wxString m_fileNameWork;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(FileFunctionsTestCase);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test fixture implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
FileFunctionsTestCase::FileFunctionsTestCase()
|
||||
{
|
||||
// Initialize local data
|
||||
|
||||
wxFileName fn1(wxFileName::GetTempDir(), wxT("wx_file_mask.txt"));
|
||||
m_fileNameASCII = fn1.GetFullPath();
|
||||
|
||||
// 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();
|
||||
|
||||
wxFileName fn3(wxFileName::GetTempDir(), wxT("wx_test_copy"));
|
||||
m_fileNameWork = fn3.GetFullPath();
|
||||
}
|
||||
|
||||
FileFunctionsTestCase::~FileFunctionsTestCase()
|
||||
{
|
||||
// Remove all remaining temporary files
|
||||
if ( wxFileExists(m_fileNameASCII) )
|
||||
{
|
||||
wxRemoveFile(m_fileNameASCII);
|
||||
}
|
||||
if ( wxFileExists(m_fileNameNonASCII) )
|
||||
{
|
||||
wxRemoveFile(m_fileNameNonASCII);
|
||||
}
|
||||
if ( wxFileExists(m_fileNameWork) )
|
||||
{
|
||||
wxRemoveFile(m_fileNameWork);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// tests themselves start here
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::GetTempFolder",
|
||||
"[filefn]")
|
||||
{
|
||||
// Verify that obtained temporary folder is not empty.
|
||||
wxString tmpDir = wxFileName::GetTempDir();
|
||||
|
||||
CHECK( !tmpDir.IsEmpty() );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::CopyFile",
|
||||
"[filefn]")
|
||||
{
|
||||
const wxString filename1(wxS("horse.xpm"));
|
||||
const wxString& filename2 = m_fileNameWork;
|
||||
|
||||
INFO("File 1: " << filename1 << " File 2: " << filename2);
|
||||
|
||||
REQUIRE( wxCopyFile(filename1, filename2) );
|
||||
|
||||
// verify that the two files have the same contents!
|
||||
wxFFile f1(filename1, wxT("rb")),
|
||||
f2(filename2, wxT("rb"));
|
||||
|
||||
REQUIRE( f1.IsOpened() );
|
||||
REQUIRE( f2.IsOpened() );
|
||||
|
||||
wxString s1, s2;
|
||||
REQUIRE( f1.ReadAll(&s1) );
|
||||
REQUIRE( f2.ReadAll(&s2) );
|
||||
CHECK( s1 == s2 );
|
||||
|
||||
CHECK( f1.Close() );
|
||||
CHECK( f2.Close() );
|
||||
CHECK( wxRemoveFile(filename2) );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::CreateFile",
|
||||
"[filefn]")
|
||||
{
|
||||
// Create file name containing ASCII characters only.
|
||||
DoCreateFile(m_fileNameASCII);
|
||||
// Create file name containing non-ASCII characters.
|
||||
DoCreateFile(m_fileNameNonASCII);
|
||||
}
|
||||
|
||||
void FileFunctionsTestCase::DoCreateFile(const wxString& filePath)
|
||||
{
|
||||
INFO("File: " << filePath);
|
||||
|
||||
// Create temporary file.
|
||||
wxTextFile file;
|
||||
REQUIRE( file.Create(filePath) );
|
||||
CHECK( file.Close() );
|
||||
|
||||
wxRemoveFile(filePath);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::FileExists",
|
||||
"[filefn]")
|
||||
{
|
||||
CHECK( wxFileExists(wxT("horse.png")) );
|
||||
CHECK( !wxFileExists(wxT("horse.___")) );
|
||||
|
||||
// Check file name containing ASCII characters only.
|
||||
DoFileExists(m_fileNameASCII);
|
||||
// Check file name containing non-ASCII characters.
|
||||
DoFileExists(m_fileNameNonASCII);
|
||||
}
|
||||
|
||||
void FileFunctionsTestCase::DoFileExists(const wxString& filePath)
|
||||
{
|
||||
INFO("File: " << filePath);
|
||||
|
||||
// Create temporary file.
|
||||
wxTextFile file;
|
||||
REQUIRE( file.Create(filePath) );
|
||||
CHECK( file.Close() );
|
||||
|
||||
// Verify that file exists with 2 methods.
|
||||
CHECK( file.Exists() );
|
||||
CHECK( wxFileExists(filePath) );
|
||||
|
||||
wxRemoveFile(filePath);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::FindFile",
|
||||
"[filefn]")
|
||||
{
|
||||
// Find file name containing ASCII characters only.
|
||||
DoFindFile(m_fileNameASCII);
|
||||
// Find file name containing non-ASCII characters.
|
||||
DoFindFile(m_fileNameNonASCII);
|
||||
}
|
||||
|
||||
void FileFunctionsTestCase::DoFindFile(const wxString& filePath)
|
||||
{
|
||||
INFO("File: " << filePath);
|
||||
|
||||
// Create temporary file.
|
||||
wxTextFile file;
|
||||
REQUIRE( file.Create(filePath) );
|
||||
CHECK( file.Close() );
|
||||
|
||||
// Check if file can be found (method 1).
|
||||
wxString foundFile = wxFindFirstFile(filePath, wxFILE);
|
||||
CHECK( foundFile == filePath );
|
||||
|
||||
// Check if file can be found (method 2).
|
||||
wxFileSystem fs;
|
||||
wxString furl = fs.FindFirst(filePath, wxFILE);
|
||||
wxFileName fname = wxFileName::URLToFileName(furl);
|
||||
foundFile = fname.GetFullPath();
|
||||
CHECK( foundFile == filePath );
|
||||
|
||||
wxRemoveFile(filePath);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::FindFileNext",
|
||||
"[filefn]")
|
||||
{
|
||||
// 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.
|
||||
CHECK( foundFile1 != foundFile2 );
|
||||
// Base names must be the same.
|
||||
CHECK( fn1.GetName() == fn2.GetName() );
|
||||
|
||||
// Check using method 2.
|
||||
wxFileSystem fs;
|
||||
wxString furl = fs.FindFirst(fileMask, wxFILE);
|
||||
fn1 = wxFileName::URLToFileName(furl);
|
||||
foundFile1 = fn1.GetFullPath();
|
||||
furl = fs.FindNext();
|
||||
fn2 = wxFileName::URLToFileName(furl);
|
||||
foundFile2 = fn2.GetFullPath();
|
||||
// Full names must be different.
|
||||
CHECK( fn1.GetFullPath() != fn2.GetFullPath() );
|
||||
// Base names must be the same.
|
||||
CHECK( fn1.GetName() == fn2.GetName() );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::RemoveFile",
|
||||
"[filefn]")
|
||||
{
|
||||
// Create & remove file with name containing ASCII characters only.
|
||||
DoRemoveFile(m_fileNameASCII);
|
||||
// Create & remove file with name containing non-ASCII characters.
|
||||
DoRemoveFile(m_fileNameNonASCII);
|
||||
}
|
||||
|
||||
void FileFunctionsTestCase::DoRemoveFile(const wxString& filePath)
|
||||
{
|
||||
INFO("File: " << filePath);
|
||||
|
||||
// Create temporary file.
|
||||
wxTextFile file;
|
||||
REQUIRE( file.Create(filePath) );
|
||||
CHECK( file.Close() );
|
||||
|
||||
CHECK( file.Exists() );
|
||||
CHECK( wxRemoveFile(filePath) );
|
||||
CHECK( !file.Exists() );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::RenameFile",
|
||||
"[filefn]")
|
||||
{
|
||||
// Verify renaming file with/without overwriting
|
||||
// when new file already exist/don't exist.
|
||||
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);
|
||||
}
|
||||
|
||||
void
|
||||
FileFunctionsTestCase::DoRenameFile(const wxString& oldFilePath,
|
||||
const wxString& newFilePath,
|
||||
bool overwrite,
|
||||
bool withNew)
|
||||
{
|
||||
INFO("File 1:" << oldFilePath << " File 2: " << newFilePath);
|
||||
|
||||
// Create temporary source file.
|
||||
wxTextFile file;
|
||||
REQUIRE( file.Create(oldFilePath) );
|
||||
CHECK( file.Close() );
|
||||
|
||||
if ( withNew )
|
||||
{
|
||||
// Create destination file to test overwriting.
|
||||
wxTextFile file2;
|
||||
REQUIRE( file2.Create(newFilePath) );
|
||||
CHECK( file2.Close() );
|
||||
|
||||
CHECK( wxFileExists(newFilePath) );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Remove destination file
|
||||
if ( wxFileExists(newFilePath) )
|
||||
{
|
||||
wxRemoveFile(newFilePath);
|
||||
}
|
||||
|
||||
CHECK( !wxFileExists(newFilePath) );
|
||||
}
|
||||
|
||||
CHECK( wxFileExists(oldFilePath) );
|
||||
CHECK( wxFileExists(oldFilePath) );
|
||||
CHECK( wxFileExists(oldFilePath) );
|
||||
bool shouldFail = !overwrite && withNew;
|
||||
if ( shouldFail )
|
||||
{
|
||||
CHECK( !wxRenameFile(oldFilePath, newFilePath, overwrite));
|
||||
// Verify that file has not been renamed.
|
||||
CHECK( wxFileExists(oldFilePath) );
|
||||
CHECK( wxFileExists(newFilePath) );
|
||||
|
||||
// Cleanup.
|
||||
wxRemoveFile(oldFilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
CHECK( wxRenameFile(oldFilePath, newFilePath, overwrite) );
|
||||
// Verify that file has been renamed.
|
||||
CHECK( !wxFileExists(oldFilePath) );
|
||||
CHECK( wxFileExists(newFilePath) );
|
||||
}
|
||||
|
||||
// Cleanup.
|
||||
wxRemoveFile(newFilePath);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::ConcatenateFiles",
|
||||
"[filefn]")
|
||||
{
|
||||
DoConcatFile(m_fileNameASCII, m_fileNameNonASCII, m_fileNameWork);
|
||||
DoConcatFile(m_fileNameNonASCII, m_fileNameASCII, m_fileNameWork);
|
||||
}
|
||||
|
||||
void FileFunctionsTestCase::DoConcatFile(const wxString& filePath1,
|
||||
const wxString& filePath2,
|
||||
const wxString& destFilePath)
|
||||
{
|
||||
INFO("File 1:" << filePath1
|
||||
<< " File 2: " << filePath2
|
||||
<< " File 3: " << destFilePath);
|
||||
|
||||
// Prepare source data
|
||||
wxFFile f1(filePath1, wxT("wb")),
|
||||
f2(filePath2, wxT("wb"));
|
||||
REQUIRE( f1.IsOpened() );
|
||||
REQUIRE( f2.IsOpened() );
|
||||
|
||||
wxString s1(wxT("1234567890"));
|
||||
wxString s2(wxT("abcdefghij"));
|
||||
CHECK( f1.Write(s1) );
|
||||
CHECK( f2.Write(s2) );
|
||||
|
||||
CHECK( f1.Close() );
|
||||
CHECK( f2.Close() );
|
||||
|
||||
// Concatenate source files
|
||||
CHECK( wxConcatFiles(filePath1, filePath2, destFilePath) );
|
||||
|
||||
// Verify content of destination file
|
||||
REQUIRE( wxFileExists(destFilePath) );
|
||||
wxString sSrc = s1 + s2;
|
||||
wxString s3;
|
||||
wxFFile f3(destFilePath, wxT("rb"));
|
||||
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) );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::GetCwd",
|
||||
"[filefn]")
|
||||
{
|
||||
// Verify that obtained working directory is not empty.
|
||||
wxString cwd = wxGetCwd();
|
||||
|
||||
CHECK( !cwd.IsEmpty() );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::Eof",
|
||||
"[filefn]")
|
||||
{
|
||||
const wxString filename(wxT("horse.bmp"));
|
||||
INFO("File: " << filename);
|
||||
|
||||
wxFFile file(filename, wxT("r"));
|
||||
// wxFFile::Eof must be false at start
|
||||
CHECK( !file.Eof() );
|
||||
CHECK( file.SeekEnd() );
|
||||
// wxFFile::Eof returns true only after attempt to read last byte
|
||||
char array[1];
|
||||
CHECK( file.Read(array, 1) == 0 );
|
||||
CHECK( file.Eof() );
|
||||
|
||||
CHECK( file.Close() );
|
||||
// wxFFile::Eof after close should not cause crash but fail instead
|
||||
WX_ASSERT_FAILS_WITH_ASSERT( file.Eof() );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::Error",
|
||||
"[filefn]")
|
||||
{
|
||||
const wxString filename(wxT("horse.bmp"));
|
||||
INFO("File: " << filename);
|
||||
|
||||
wxFFile file(filename, wxT("r"));
|
||||
// wxFFile::Error must be false at start assuming file "horse.bmp" exists.
|
||||
CHECK( !file.Error() );
|
||||
// Attempt to write to file opened in readonly mode should cause error
|
||||
CHECK( !file.Write(filename) );
|
||||
CHECK( file.Error() );
|
||||
|
||||
CHECK( file.Close() );
|
||||
// wxFFile::Error after close should not cause crash but fail instead
|
||||
WX_ASSERT_FAILS_WITH_ASSERT( file.Error() );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::DirExists",
|
||||
"[filefn]")
|
||||
{
|
||||
wxString cwd = wxGetCwd();
|
||||
INFO("CWD: " << cwd);
|
||||
|
||||
// Current working directory must exist
|
||||
CHECK(wxDirExists(cwd));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::IsAbsolutePath",
|
||||
"[filefn]")
|
||||
{
|
||||
wxString name = wxT("horse.bmp");
|
||||
INFO("File: " << name);
|
||||
|
||||
// File name is given as relative path
|
||||
CHECK( !wxIsAbsolutePath(name) );
|
||||
|
||||
wxFileName filename(name);
|
||||
CHECK( filename.MakeAbsolute() );
|
||||
// wxFileName::GetFullPath returns absolute path
|
||||
CHECK( wxIsAbsolutePath(filename.GetFullPath()));
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
CHECK( wxIsAbsolutePath("\\"));
|
||||
CHECK( wxIsAbsolutePath("c:"));
|
||||
CHECK( !wxIsAbsolutePath("c"));
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::PathOnly",
|
||||
"[filefn]")
|
||||
{
|
||||
wxString name = wxT("horse.bmp");
|
||||
// Get absolute path to horse.bmp
|
||||
wxFileName filename(name);
|
||||
CHECK( filename.MakeAbsolute() );
|
||||
|
||||
wxString pathOnly = wxPathOnly(filename.GetFullPath());
|
||||
if ( !wxDirExists(pathOnly) )
|
||||
CHECK( pathOnly == wxString() );
|
||||
}
|
||||
|
||||
// Unit tests for Mkdir and Rmdir doesn't cover non-ASCII directory names.
|
||||
// Rmdir fails on them on Linux. See ticket #17644.
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::Mkdir",
|
||||
"[filefn]")
|
||||
{
|
||||
wxString dirname = wxString::FromUTF8("__wxMkdir_test_dir_with_ö");
|
||||
INFO("Dir: " << dirname);
|
||||
|
||||
CHECK( wxMkdir(dirname) );
|
||||
CHECK( wxDirExists(dirname) );
|
||||
CHECK( wxRmdir(dirname) );
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(FileFunctionsTestCase,
|
||||
"FileFunctions::Rmdir",
|
||||
"[filefn]")
|
||||
{
|
||||
wxString dirname = wxString::FromUTF8("__wxRmdir_test_dir_with_ö");
|
||||
INFO("Dir: " << dirname);
|
||||
|
||||
CHECK( wxMkdir(dirname) );
|
||||
CHECK( wxRmdir(dirname) );
|
||||
CHECK( !wxDirExists(dirname) );
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: other file functions to test:
|
||||
|
||||
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
|
||||
|
||||
#if wxUSE_FSVOLUME
|
||||
|
||||
#include "wx/volume.h"
|
||||
|
||||
TEST_CASE("FSVolume", "[fs][volume]")
|
||||
{
|
||||
wxFSVolumeBase vol;
|
||||
CHECK( !vol.IsOk() );
|
||||
|
||||
const auto& volumes = wxFSVolumeBase::GetVolumes();
|
||||
if ( volumes.empty() )
|
||||
{
|
||||
WARN("No volumes found, skipping wxFSVolume tests.");
|
||||
return;
|
||||
}
|
||||
|
||||
vol.Create(volumes[0]);
|
||||
REQUIRE( vol.IsOk() );
|
||||
}
|
||||
|
||||
#endif // wxUSE_FSVOLUME
|
||||
192
libs/wxWidgets-3.3.1/tests/file/filetest.cpp
Normal file
192
libs/wxWidgets-3.3.1/tests/file/filetest.cpp
Normal file
@@ -0,0 +1,192 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: tests/file/filetest.cpp
|
||||
// Purpose: wxFile unit test
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2009-09-12
|
||||
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "testprec.h"
|
||||
|
||||
|
||||
#if wxUSE_FILE
|
||||
|
||||
#include "wx/file.h"
|
||||
|
||||
#include "testfile.h"
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// tests implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("wxFile::ReadAll", "[file]")
|
||||
{
|
||||
TestFile tf;
|
||||
|
||||
const char* text = "Ream\nde";
|
||||
|
||||
{
|
||||
wxFile fout(tf.GetName(), wxFile::write);
|
||||
CHECK( fout.IsOpened() );
|
||||
fout.Write(text, strlen(text));
|
||||
CHECK( fout.Close() );
|
||||
}
|
||||
|
||||
{
|
||||
wxFile fin(tf.GetName(), wxFile::read);
|
||||
CHECK( fin.IsOpened() );
|
||||
|
||||
wxString s;
|
||||
CHECK( fin.ReadAll(&s) );
|
||||
CHECK( s == text );
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("wxFile::RoundTrip", "[file]")
|
||||
{
|
||||
std::unique_ptr<wxMBConv> conv;
|
||||
|
||||
SECTION("UTF-8") { conv.reset(new wxMBConvStrictUTF8); }
|
||||
SECTION("UTF-16") { conv.reset(new wxMBConvUTF16); }
|
||||
SECTION("UTF-32") { conv.reset(new wxMBConvUTF32); }
|
||||
|
||||
TestFile tf;
|
||||
|
||||
// Explicit length is needed because of the embedded NUL.
|
||||
const wxString data("Hello\0UTF!", 10);
|
||||
|
||||
{
|
||||
wxFile fout(tf.GetName(), wxFile::write);
|
||||
CHECK( fout.IsOpened() );
|
||||
|
||||
CHECK( fout.Write(data, *conv) );
|
||||
}
|
||||
|
||||
{
|
||||
wxFile fin(tf.GetName(), wxFile::read);
|
||||
CHECK( fin.IsOpened() );
|
||||
|
||||
const ssize_t len = fin.Length();
|
||||
wxCharBuffer buf(len);
|
||||
CHECK( fin.Read(buf.data(), len) == len );
|
||||
|
||||
wxString dataReadBack(buf, *conv, len);
|
||||
CHECK( dataReadBack == data );
|
||||
}
|
||||
|
||||
{
|
||||
wxFile fin(tf.GetName(), wxFile::read);
|
||||
CHECK( fin.IsOpened() );
|
||||
|
||||
wxString dataReadBack;
|
||||
CHECK( fin.ReadAll(&dataReadBack, *conv) );
|
||||
|
||||
CHECK( dataReadBack == data );
|
||||
}
|
||||
}
|
||||
|
||||
static void CheckFileContents(const wxString& name, const wxString& data)
|
||||
{
|
||||
// Check that the file exists with the expected contents.
|
||||
wxFile f(name);
|
||||
REQUIRE( f.IsOpened() );
|
||||
|
||||
wxString s;
|
||||
CHECK( f.ReadAll(&s) );
|
||||
CHECK( s == data );
|
||||
}
|
||||
|
||||
TEST_CASE("wxTempFile", "[file][temp]")
|
||||
{
|
||||
constexpr const char* name = "wxtemp_test";
|
||||
const wxString dataOld("what is the meaning of life?");
|
||||
const wxString dataNew("the answer is 42");
|
||||
|
||||
// Ensure that it will be removed at the end of the test in any case.
|
||||
TempFile tf(name);
|
||||
|
||||
bool hasOldFile = false;
|
||||
|
||||
SECTION("New")
|
||||
{
|
||||
wxRemoveFile(name);
|
||||
}
|
||||
|
||||
SECTION("Existing")
|
||||
{
|
||||
wxFile f(name, wxFile::write);
|
||||
CHECK( f.IsOpened() );
|
||||
CHECK( f.Write(dataOld) );
|
||||
CHECK( f.Close() );
|
||||
|
||||
hasOldFile = true;
|
||||
}
|
||||
|
||||
// First check that not committing the file doesn't do anything.
|
||||
{
|
||||
wxTempFile discarded(name);
|
||||
CHECK( discarded.IsOpened() );
|
||||
CHECK( discarded.Write(dataNew) );
|
||||
}
|
||||
|
||||
if ( !hasOldFile )
|
||||
{
|
||||
// The file shouldn't have been created.
|
||||
CHECK( !wxFile::Exists(name) );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check that the old file is still there with the old contents.
|
||||
CheckFileContents(name, dataOld);
|
||||
}
|
||||
|
||||
// Next check that committing it does.
|
||||
wxTempFile tmpFile;
|
||||
CHECK( tmpFile.Open(name) );
|
||||
CHECK( tmpFile.Write(dataNew) );
|
||||
CHECK( tmpFile.Commit() );
|
||||
|
||||
CheckFileContents(name, dataNew);
|
||||
}
|
||||
|
||||
#ifdef __LINUX__
|
||||
|
||||
// Check that GetSize() works correctly for special files.
|
||||
TEST_CASE("wxFile::Special", "[file][linux][special-file]")
|
||||
{
|
||||
// We can't test /proc/kcore here, unlike in the similar
|
||||
// wxFileName::GetSize() test, as wxFile must be able to open it (at least
|
||||
// for reading) and usually we don't have the permissions to do it.
|
||||
|
||||
// This file is not seekable and has 0 size, but can still be read.
|
||||
wxFile fileProc("/proc/cpuinfo");
|
||||
CHECK( fileProc.IsOpened() );
|
||||
|
||||
wxString s;
|
||||
CHECK( fileProc.ReadAll(&s) );
|
||||
CHECK( !s.empty() );
|
||||
|
||||
// All files in /sys have the size of one kernel page, even if they don't
|
||||
// have that much data in them.
|
||||
const long pageSize = sysconf(_SC_PAGESIZE);
|
||||
|
||||
wxFile fileSys("/sys/power/state");
|
||||
if ( !fileSys.IsOpened() )
|
||||
{
|
||||
WARN("/sys/power/state can't be opened, skipping test");
|
||||
return;
|
||||
}
|
||||
|
||||
CHECK( fileSys.Length() == pageSize );
|
||||
CHECK( fileSys.ReadAll(&s) );
|
||||
CHECK( !s.empty() );
|
||||
CHECK( s.length() < static_cast<unsigned long>(pageSize) );
|
||||
}
|
||||
|
||||
#endif // __LINUX__
|
||||
|
||||
#endif // wxUSE_FILE
|
||||
Reference in New Issue
Block a user