mirror of
https://github.com/wxWidgets/wxWidgets.git
synced 2026-08-17 17:02:51 +08:00
Get rid of CppUnit boilerplate in wxExecute unit tests
No real changes.
This commit is contained in:
+51
-67
@@ -27,6 +27,10 @@
|
||||
#include "wx/txtstrm.h"
|
||||
#include "wx/timer.h"
|
||||
|
||||
// wxX11 didn't implement some required features. Disable these tests
|
||||
// for now.
|
||||
#if !defined (__WXX11__)
|
||||
|
||||
#ifdef __UNIX__
|
||||
#define COMMAND "echo hi"
|
||||
#define COMMAND_STDERR "cat nonexistentfile"
|
||||
@@ -55,34 +59,15 @@ namespace
|
||||
} // anonymous namespace
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test class
|
||||
// namespace-like class with helpers for the test below
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class ExecTestCase : public CppUnit::TestCase
|
||||
class ExecTestCase
|
||||
{
|
||||
public:
|
||||
ExecTestCase() { }
|
||||
|
||||
private:
|
||||
CPPUNIT_TEST_SUITE( ExecTestCase );
|
||||
// wxX11 didn't implement some required features. Disable these tests
|
||||
// for now.
|
||||
#if !defined (__WXX11__)
|
||||
CPPUNIT_TEST( TestShell );
|
||||
CPPUNIT_TEST( TestExecute );
|
||||
CPPUNIT_TEST( TestProcess );
|
||||
CPPUNIT_TEST( TestAsync );
|
||||
CPPUNIT_TEST( TestAsyncRedirect );
|
||||
CPPUNIT_TEST( TestOverlappedSyncExecute );
|
||||
#endif
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
void TestShell();
|
||||
void TestExecute();
|
||||
void TestProcess();
|
||||
void TestAsync();
|
||||
void TestAsyncRedirect();
|
||||
void TestOverlappedSyncExecute();
|
||||
protected:
|
||||
|
||||
// Helper: create an executable file sleeping for the given amount of
|
||||
// seconds with the specified base name.
|
||||
@@ -160,19 +145,16 @@ private:
|
||||
wxDECLARE_NO_COPY_CLASS(ExecTestCase);
|
||||
};
|
||||
|
||||
// register in the unnamed registry so that these tests are run by default
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( ExecTestCase );
|
||||
// ----------------------------------------------------------------------------
|
||||
// The tests themselves
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// also include in its own registry so that these tests can be run alone
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExecTestCase, "ExecTestCase" );
|
||||
|
||||
|
||||
void ExecTestCase::TestShell()
|
||||
TEST_CASE("wxShell", "[exec]")
|
||||
{
|
||||
CPPUNIT_ASSERT( wxShell(SHELL_COMMAND) );
|
||||
CHECK( wxShell(SHELL_COMMAND) );
|
||||
}
|
||||
|
||||
void ExecTestCase::TestExecute()
|
||||
TEST_CASE_METHOD(ExecTestCase, "wxExecute", "[exec]")
|
||||
{
|
||||
// Launching interactive programs doesn't work without an interactive
|
||||
// session.
|
||||
@@ -191,7 +173,7 @@ void ExecTestCase::TestExecute()
|
||||
long pid = asyncInEventLoop.DoExecute(AsyncExec_ExitLoop, // Force exit of event loop right
|
||||
// after the call to wxExecute()
|
||||
ASYNC_COMMAND, wxEXEC_ASYNC);
|
||||
CPPUNIT_ASSERT( pid != 0 );
|
||||
REQUIRE( pid != 0 );
|
||||
|
||||
// Give the system some time to launch the child.
|
||||
wxMilliSleep(200);
|
||||
@@ -214,24 +196,25 @@ void ExecTestCase::TestExecute()
|
||||
|
||||
if (useNoeventsFlag)
|
||||
{
|
||||
INFO("Testing wxEXEC_SYNC | wxEXEC_NOEVENTS");
|
||||
execFlags |= wxEXEC_NOEVENTS;
|
||||
}
|
||||
|
||||
// test sync exec (with a command not producing any output to avoid
|
||||
// interfering with the test):
|
||||
CPPUNIT_ASSERT( wxExecute(COMMAND_NO_OUTPUT, execFlags) == 0 );
|
||||
CHECK( wxExecute(COMMAND_NO_OUTPUT, execFlags) == 0 );
|
||||
|
||||
// test running COMMAND again, but this time with redirection:
|
||||
// and the expected data is on stdout.
|
||||
wxArrayString stdout_arr;
|
||||
CPPUNIT_ASSERT_EQUAL( 0, wxExecute(COMMAND, stdout_arr, execFlags) );
|
||||
CPPUNIT_ASSERT_EQUAL( "hi", stdout_arr[0] );
|
||||
CHECK( wxExecute(COMMAND, stdout_arr, execFlags) == 0 );
|
||||
CHECK( stdout_arr[0] == "hi" );
|
||||
|
||||
// test running COMMAND_STDERR with redirection and the expected data
|
||||
// is on stderr.
|
||||
wxArrayString stderr_arr;
|
||||
stdout_arr.Empty();
|
||||
CPPUNIT_ASSERT( wxExecute(COMMAND_STDERR, stdout_arr, stderr_arr, execFlags) != 0 );
|
||||
CHECK( wxExecute(COMMAND_STDERR, stdout_arr, stderr_arr, execFlags) != 0 );
|
||||
|
||||
// Check that there is something on stderr.
|
||||
// In Unix systems, the 'cat' command has the name of the file it could not
|
||||
@@ -240,11 +223,11 @@ void ExecTestCase::TestExecute()
|
||||
// a file:
|
||||
// "The system cannot find the file specified"
|
||||
// In both cases, we expect the word 'file' to be in the stderr.
|
||||
CPPUNIT_ASSERT( stderr_arr[0].Contains("file") );
|
||||
CHECK( stderr_arr[0].Contains("file") );
|
||||
}
|
||||
}
|
||||
|
||||
void ExecTestCase::TestProcess()
|
||||
TEST_CASE_METHOD(ExecTestCase, "wxProcess", "[exec]")
|
||||
{
|
||||
if ( IsAutomaticTest() )
|
||||
return;
|
||||
@@ -262,8 +245,8 @@ void ExecTestCase::TestProcess()
|
||||
long pid = asyncInEventLoop.DoExecute(AsyncExec_ExitLoop, // Force exit of event loop right
|
||||
// after the call to wxExecute()
|
||||
ASYNC_COMMAND, wxEXEC_ASYNC, proc);
|
||||
CPPUNIT_ASSERT( proc->GetPid() == pid );
|
||||
CPPUNIT_ASSERT( pid != 0 );
|
||||
CHECK( proc->GetPid() == pid );
|
||||
REQUIRE( pid != 0 );
|
||||
|
||||
// As above, give the system time to launch the process.
|
||||
wxMilliSleep(200);
|
||||
@@ -284,34 +267,33 @@ void ExecTestCase::TestProcess()
|
||||
// depending on this flag, and we want to test both cases.
|
||||
|
||||
// First the default case, dispatching the events while waiting.
|
||||
SECTION("With events")
|
||||
{
|
||||
wxProcess proc2;
|
||||
proc2.Redirect();
|
||||
CPPUNIT_ASSERT_EQUAL( 0, wxExecute(COMMAND, wxEXEC_SYNC, &proc2) );
|
||||
CHECK( wxExecute(COMMAND, wxEXEC_SYNC, &proc2) == 0 );
|
||||
|
||||
wxStringOutputStream procOutput;
|
||||
CPPUNIT_ASSERT( proc2.GetInputStream() );
|
||||
CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF,
|
||||
proc2.GetInputStream()->Read(procOutput).GetLastError() );
|
||||
REQUIRE( proc2.GetInputStream() );
|
||||
CHECK( proc2.GetInputStream()->Read(procOutput).GetLastError() == wxSTREAM_EOF );
|
||||
|
||||
wxString output = procOutput.GetString();
|
||||
CPPUNIT_ASSERT_EQUAL( "hi", output.Trim() );
|
||||
CHECK( output.Trim() == "hi" );
|
||||
}
|
||||
|
||||
// And now without event dispatching.
|
||||
SECTION("Without events")
|
||||
{
|
||||
wxProcess proc2;
|
||||
proc2.Redirect();
|
||||
CPPUNIT_ASSERT_EQUAL( 0,
|
||||
wxExecute(COMMAND, wxEXEC_SYNC | wxEXEC_NOEVENTS, &proc2) );
|
||||
CHECK( wxExecute(COMMAND, wxEXEC_SYNC | wxEXEC_NOEVENTS, &proc2) == 0 );
|
||||
|
||||
wxStringOutputStream procOutput;
|
||||
CPPUNIT_ASSERT( proc2.GetInputStream() );
|
||||
CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF,
|
||||
proc2.GetInputStream()->Read(procOutput).GetLastError() );
|
||||
REQUIRE( proc2.GetInputStream() );
|
||||
CHECK( proc2.GetInputStream()->Read(procOutput).GetLastError() == wxSTREAM_EOF );
|
||||
|
||||
wxString output = procOutput.GetString();
|
||||
CPPUNIT_ASSERT_EQUAL( "hi", output.Trim() );
|
||||
CHECK( output.Trim() == "hi" );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,14 +317,14 @@ private:
|
||||
wxDECLARE_NO_COPY_CLASS(TestAsyncProcess);
|
||||
};
|
||||
|
||||
void ExecTestCase::TestAsync()
|
||||
TEST_CASE_METHOD(ExecTestCase, "wxExecute::Async", "[exec]")
|
||||
{
|
||||
// Test asynchronous execution with no redirection, just to make sure we
|
||||
// get the OnTerminate() call.
|
||||
TestAsyncProcess proc;
|
||||
AsyncInEventLoop asyncInEventLoop;
|
||||
|
||||
CPPUNIT_ASSERT( asyncInEventLoop.DoExecute(
|
||||
CHECK( asyncInEventLoop.DoExecute(
|
||||
AsyncExec_DontExitLoop, // proc is expected (inside of its OnTerminate())
|
||||
// to trigger the exit of the event loop.
|
||||
COMMAND_NO_OUTPUT, wxEXEC_ASYNC, &proc) != 0 );
|
||||
@@ -358,7 +340,7 @@ ExecTestCase::DoTestAsyncRedirect(const wxString& command,
|
||||
|
||||
proc.Redirect();
|
||||
|
||||
CPPUNIT_ASSERT( asyncInEventLoop.DoExecute(
|
||||
CHECK( asyncInEventLoop.DoExecute(
|
||||
AsyncExec_DontExitLoop, // proc is expected (inside of its OnTerminate())
|
||||
// to trigger the exit of the event loop.
|
||||
command, wxEXEC_ASYNC, &proc) != 0 );
|
||||
@@ -378,10 +360,10 @@ ExecTestCase::DoTestAsyncRedirect(const wxString& command,
|
||||
wxTextInputStream tis(*streamToCheck);
|
||||
|
||||
// Check that the first line of output contains what we expect.
|
||||
CPPUNIT_ASSERT( tis.ReadLine().Contains(expectedContaining) );
|
||||
CHECK( tis.ReadLine().Contains(expectedContaining) );
|
||||
}
|
||||
|
||||
void ExecTestCase::TestAsyncRedirect()
|
||||
TEST_CASE_METHOD(ExecTestCase, "wxExecute::AsyncRedirect", "[exec]")
|
||||
{
|
||||
// Test redirection with reading from the input stream after process termination.
|
||||
DoTestAsyncRedirect(COMMAND, Check_Stdout, "hi");
|
||||
@@ -417,7 +399,7 @@ wxString ExecTestCase::CreateSleepFile(const wxString& basename, int seconds)
|
||||
const wxString fnSleep = wxFileName(".", basename, scriptExt).GetFullPath();
|
||||
|
||||
wxFile fileSleep;
|
||||
CPPUNIT_ASSERT
|
||||
REQUIRE
|
||||
(
|
||||
fileSleep.Create(fnSleep, true, wxS_IRUSR | wxS_IWUSR | wxS_IXUSR)
|
||||
);
|
||||
@@ -443,7 +425,7 @@ wxString ExecTestCase::MakeShellCommand(const wxString& filename)
|
||||
return command;
|
||||
}
|
||||
|
||||
void ExecTestCase::TestOverlappedSyncExecute()
|
||||
TEST_CASE_METHOD(ExecTestCase, "wxExecute::Overlapped", "[exec]")
|
||||
{
|
||||
// Windows Synchronous wxExecute implementation does not currently
|
||||
// support overlapped event loops. It is still using wxYield, which is
|
||||
@@ -501,31 +483,33 @@ void ExecTestCase::TestOverlappedSyncExecute()
|
||||
// doesn't return until both process terminate.
|
||||
DelayedExecuteTimer delayLongSleep(longSleepCommand, longSleepOutput);
|
||||
wxExecute(shortSleepCommand, shortSleepOutput);
|
||||
CPPUNIT_ASSERT( !shortSleepOutput.empty() );
|
||||
CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING, shortSleepOutput.Last() );
|
||||
REQUIRE( !shortSleepOutput.empty() );
|
||||
CHECK( shortSleepOutput.Last() == SLEEP_END_STRING );
|
||||
|
||||
CPPUNIT_ASSERT( !longSleepOutput.empty() );
|
||||
CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING, longSleepOutput.Last() );
|
||||
REQUIRE( !longSleepOutput.empty() );
|
||||
CHECK( longSleepOutput.Last() == SLEEP_END_STRING );
|
||||
|
||||
// And also that, vice versa, running a short-lived child process that both
|
||||
// starts and ends while a longer-lived parent process is still running
|
||||
// works too.
|
||||
DelayedExecuteTimer delayShortSleep(shortSleepCommand, shortSleepOutput);
|
||||
wxExecute(longSleepCommand, longSleepOutput);
|
||||
CPPUNIT_ASSERT( !shortSleepOutput.empty() );
|
||||
CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING, shortSleepOutput.Last() );
|
||||
REQUIRE( !shortSleepOutput.empty() );
|
||||
CHECK( shortSleepOutput.Last() == SLEEP_END_STRING );
|
||||
|
||||
CPPUNIT_ASSERT( !longSleepOutput.empty() );
|
||||
CPPUNIT_ASSERT_EQUAL( SLEEP_END_STRING, longSleepOutput.Last() );
|
||||
REQUIRE( !longSleepOutput.empty() );
|
||||
CHECK( longSleepOutput.Last() == SLEEP_END_STRING );
|
||||
#endif // !__WINDOWS__
|
||||
}
|
||||
|
||||
#endif // !__WXX11__
|
||||
|
||||
#ifdef __UNIX__
|
||||
|
||||
// This test is disabled by default because it must be run in French locale,
|
||||
// i.e. with explicit LC_ALL=fr_FR.UTF-8 and only works with GNU ls, which
|
||||
// produces the expected output.
|
||||
TEST_CASE("wxExecute::RedirectUTF8", "[exec][unicode][.]")
|
||||
TEST_CASE("wxExecute::RedirectUTF8", "[.]")
|
||||
{
|
||||
wxArrayString output;
|
||||
REQUIRE( wxExecute("/bin/ls --version", output) == 0 );
|
||||
|
||||
Reference in New Issue
Block a user