Skip to content

Commit

Permalink
Merge 32edf17 into bc78d78
Browse files Browse the repository at this point in the history
  • Loading branch information
CiderMan committed Sep 21, 2020
2 parents bc78d78 + 32edf17 commit 27e8e09
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -67,6 +67,7 @@ TEST(FirstTestGroup, FirstTest)
* -s# random shuffle the test execution order. # is an integer used for seeding the random number generator. # is optional, and if omitted, the seed value is chosen automatically, which results in a different order every time. The seed value is printed to console to make it possible to reproduce a previously generated execution order. Handy for detecting problems related to dependencies between tests.
* -g group only run test whose group contains the substring group
* -n name only run test whose name contains the substring name
* -f crash on fail, run the tests as normal but, when a test fails, crash rather than report the failure in the normal way
## Test Macros
Expand Down
2 changes: 2 additions & 0 deletions include/CppUTest/CommandLineArguments.h
Expand Up @@ -51,6 +51,7 @@ class CommandLineArguments
size_t getRepeatCount() const;
bool isShuffling() const;
bool isReversing() const;
bool isCrashingOnFail() const;
size_t getShuffleSeed() const;
const TestFilter* getGroupFilters() const;
const TestFilter* getNameFilters() const;
Expand Down Expand Up @@ -81,6 +82,7 @@ class CommandLineArguments
bool listTestGroupAndCaseNames_;
bool runIgnored_;
bool reversing_;
bool crashOnFail_;
bool shuffling_;
bool shufflingPreSeeded_;
size_t repeat_;
Expand Down
2 changes: 2 additions & 0 deletions include/CppUTest/TestRegistry.h
Expand Up @@ -77,6 +77,8 @@ class TestRegistry
int getCurrentRepetition();
void setRunIgnored();

virtual void setCrashOnFail();

private:

bool testShouldRun(UtestShell* test, TestResult& result);
Expand Down
4 changes: 4 additions & 0 deletions include/CppUTest/Utest.h
Expand Up @@ -171,6 +171,10 @@ class UtestShell

virtual SimpleString getMacroName() const;
TestResult *getTestResult();

static const NormalTestTerminator normalTestTerminator_;
static const CrashingTestTerminator crashingTestTerminator_;

private:
const char *group_;
const char *name_;
Expand Down
19 changes: 14 additions & 5 deletions src/CppUTest/CommandLineArguments.cpp
Expand Up @@ -30,7 +30,7 @@
#include "CppUTest/PlatformSpecificFunctions.h"

CommandLineArguments::CommandLineArguments(int ac, const char *const *av) :
ac_(ac), av_(av), needHelp_(false), verbose_(false), veryVerbose_(false), color_(false), runTestsAsSeperateProcess_(false), listTestGroupNames_(false), listTestGroupAndCaseNames_(false), runIgnored_(false), reversing_(false), shuffling_(false), shufflingPreSeeded_(false), repeat_(1), shuffleSeed_(0), groupFilters_(NULLPTR), nameFilters_(NULLPTR), outputType_(OUTPUT_ECLIPSE)
ac_(ac), av_(av), needHelp_(false), verbose_(false), veryVerbose_(false), color_(false), runTestsAsSeperateProcess_(false), listTestGroupNames_(false), listTestGroupAndCaseNames_(false), runIgnored_(false), reversing_(false), crashOnFail_(false), shuffling_(false), shufflingPreSeeded_(false), repeat_(1), shuffleSeed_(0), groupFilters_(NULLPTR), nameFilters_(NULLPTR), outputType_(OUTPUT_ECLIPSE)
{
}

Expand Down Expand Up @@ -66,6 +66,7 @@ bool CommandLineArguments::parse(TestPlugin* plugin)
else if (argument == "-lg") listTestGroupNames_ = true;
else if (argument == "-ln") listTestGroupAndCaseNames_ = true;
else if (argument == "-ri") runIgnored_ = true;
else if (argument == "-f") crashOnFail_ = true;
else if (argument.startsWith("-r")) setRepeatCount(ac_, av_, i);
else if (argument.startsWith("-g")) addGroupFilter(ac_, av_, i);
else if (argument.startsWith("-t")) correctParameters = addGroupDotNameFilter(ac_, av_, i);
Expand Down Expand Up @@ -93,9 +94,11 @@ bool CommandLineArguments::parse(TestPlugin* plugin)

const char* CommandLineArguments::usage() const
{
return "use -h for more extensive help\nusage [-h] [-v] [-vv] [-c] [-p] [-lg] [-ln] [-ri] [-r#]\n"
" [-g|sg|xg|xsg groupName]... [-n|sn|xn|xsn testName]... [-t groupName.testName]...\n"
" [-b] [-s [randomizerSeed>0]] [\"TEST(groupName, testName)\"]... [-o{normal, junit, teamcity}] [-k packageName]\n";
return "use -h for more extensive help\n"
"usage [-h] [-v] [-vv] [-c] [-p] [-lg] [-ln] [-ri] [-r#] [-f]\n"
" [-g|sg|xg|xsg groupName]... [-n|sn|xn|xsn testName]... [-t groupName.testName]...\n"
" [-b] [-s [randomizerSeed>0]] [\"TEST(groupName, testName)\"]...\n"
" [-o{normal, junit, teamcity}] [-k packageName]\n";
}

const char* CommandLineArguments::help() const
Expand Down Expand Up @@ -134,7 +137,8 @@ const char* CommandLineArguments::help() const
" -p - run tests in a separate process.\n"
" -b - run the tests backwards, reversing the normal way\n"
" -s [seed] - shuffle tests randomly. Seed is optional\n"
" -r# - repeat the tests some number (#) of times, or twice if # is not specified.\n";
" -r# - repeat the tests some number (#) of times, or twice if # is not specified.\n"
" -f - Cause the tests to crash on failure (to allow the test to be debugged if necessary)\n";
}

bool CommandLineArguments::needHelp() const
Expand Down Expand Up @@ -188,6 +192,11 @@ bool CommandLineArguments::isReversing() const
return reversing_;
}

bool CommandLineArguments::isCrashingOnFail() const
{
return crashOnFail_;
}

bool CommandLineArguments::isShuffling() const
{
return shuffling_;
Expand Down
1 change: 1 addition & 0 deletions src/CppUTest/CommandLineTestRunner.cpp
Expand Up @@ -95,6 +95,7 @@ void CommandLineTestRunner::initializeTestRun()
if (arguments_->isColor()) output_->color();
if (arguments_->runTestsInSeperateProcess()) registry_->setRunTestsInSeperateProcess();
if (arguments_->isRunIgnored()) registry_->setRunIgnored();
if (arguments_->isCrashingOnFail()) registry_->setCrashOnFail();
}

int CommandLineTestRunner::runAllTests()
Expand Down
5 changes: 5 additions & 0 deletions src/CppUTest/TestRegistry.cpp
Expand Up @@ -172,6 +172,11 @@ void TestRegistry::setRunTestsInSeperateProcess()
runInSeperateProcess_ = true;
}

void TestRegistry::setCrashOnFail()
{
UtestShell::setCrashOnFail();
}

int TestRegistry::getCurrentRepetition()
{
return currentRepetition_;
Expand Down
16 changes: 7 additions & 9 deletions src/CppUTest/Utest.cpp
Expand Up @@ -131,13 +131,6 @@ extern "C" {

/******************************** */

static const NormalTestTerminator normalTestTerminator;
static const CrashingTestTerminator crashingTestTerminator;

const TestTerminator *UtestShell::currentTestTerminator_ = &normalTestTerminator;

/******************************** */

UtestShell::UtestShell() :
group_("UndefinedTestGroup"), name_("UndefinedTest"), file_("UndefinedFile"), lineNumber_(0), next_(NULLPTR), isRunAsSeperateProcess_(false), hasFailed_(false)
{
Expand Down Expand Up @@ -582,19 +575,24 @@ UtestShell* UtestShell::getCurrent()
return currentTest_;
}

const NormalTestTerminator UtestShell::normalTestTerminator_;
const CrashingTestTerminator UtestShell::crashingTestTerminator_;

const TestTerminator *UtestShell::currentTestTerminator_ = &UtestShell::normalTestTerminator_;

const TestTerminator &UtestShell::getCurrentTestTerminator()
{
return *currentTestTerminator_;
}

void UtestShell::setCrashOnFail()
{
currentTestTerminator_ = &crashingTestTerminator;
currentTestTerminator_ = &crashingTestTerminator_;
}

void UtestShell::restoreDefaultTestTerminator()
{
currentTestTerminator_ = &normalTestTerminator;
currentTestTerminator_ = &normalTestTerminator_;
}

ExecFunctionTestShell::~ExecFunctionTestShell()
Expand Down
18 changes: 15 additions & 3 deletions tests/CppUTest/CommandLineArgumentsTest.cpp
Expand Up @@ -464,9 +464,12 @@ TEST(CommandLineArguments, weirdParamatersReturnsFalse)

TEST(CommandLineArguments, printUsage)
{
STRCMP_EQUAL("use -h for more extensive help\nusage [-h] [-v] [-vv] [-c] [-p] [-lg] [-ln] [-ri] [-r#]\n"
" [-g|sg|xg|xsg groupName]... [-n|sn|xn|xsn testName]... [-t groupName.testName]...\n"
" [-b] [-s [randomizerSeed>0]] [\"TEST(groupName, testName)\"]... [-o{normal, junit, teamcity}] [-k packageName]\n",
STRCMP_EQUAL(
"use -h for more extensive help\n"
"usage [-h] [-v] [-vv] [-c] [-p] [-lg] [-ln] [-ri] [-r#] [-f]\n"
" [-g|sg|xg|xsg groupName]... [-n|sn|xn|xsn testName]... [-t groupName.testName]...\n"
" [-b] [-s [randomizerSeed>0]] [\"TEST(groupName, testName)\"]...\n"
" [-o{normal, junit, teamcity}] [-k packageName]\n",
args->usage());
}

Expand Down Expand Up @@ -500,6 +503,7 @@ TEST(CommandLineArguments, checkDefaultArguments)
CHECK(NULLPTR == args->getNameFilters());
CHECK(args->isEclipseOutput());
CHECK(SimpleString("") == args->getPackageName());
CHECK(!args->isCrashingOnFail());
}

TEST(CommandLineArguments, setPackageName)
Expand Down Expand Up @@ -539,3 +543,11 @@ TEST(CommandLineArguments, setOptRun)
CHECK(args->isRunIgnored());
}

TEST(CommandLineArguments, setOptCrashOnFail)
{
int argc = 2;
const char* argv[] = { "tests.exe", "-f"};
CHECK(newArgumentParser(argc, argv));
CHECK(args->isCrashingOnFail());
}

18 changes: 18 additions & 0 deletions tests/CppUTest/TestRegistryTest.cpp
Expand Up @@ -48,6 +48,11 @@ class MockTest: public UtestShell
}

bool hasRun_;

static bool wouldCrash()
{
return &getCurrentTestTerminator() == &crashingTestTerminator_;
}
};

class MockTestResult: public TestResult
Expand Down Expand Up @@ -425,3 +430,16 @@ TEST(TestRegistry, reverseZeroTests)

CHECK(NULLPTR == myRegistry->getFirstTest());
}

TEST(TestRegistry, doesNotCrashIfNotSetToCrash)
{
CHECK_FALSE(MockTest::wouldCrash());
}

TEST(TestRegistry, crashesIfSetToCrash)
{
myRegistry->setCrashOnFail();

CHECK(MockTest::wouldCrash());
UtestShell::restoreDefaultTestTerminator();
}

0 comments on commit 27e8e09

Please sign in to comment.