Skip to content

Commit

Permalink
Update doctest with few modifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
MFraters committed Feb 22, 2022
1 parent 5e5af87 commit 2163410
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions tests/doctest.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,6 @@ DOCTEST_MSVC_SUPPRESS_WARNING_POP

namespace doctest {

class Contains;

DOCTEST_INTERFACE extern bool is_running_in_test;

// A 24 byte string class (can be as small as 17 for x64 and 13 for x86) that can hold strings with length
Expand Down Expand Up @@ -581,19 +579,21 @@ class DOCTEST_INTERFACE String

int compare(const char* other, bool no_case = false) const;
int compare(const String& other, bool no_case = false) const;
const char *compare(const Contains& other) const;
};

class DOCTEST_INTERFACE Contains {
public:
Contains(const char* string);
explicit Contains(const char* string);

bool checkWith(const String& other) const;

String string;
};

DOCTEST_INTERFACE String operator+(const String& lhs, const String& rhs);

DOCTEST_INTERFACE bool operator==(const String& lhs, const String& rhs);
DOCTEST_INTERFACE bool operator==(const String& lhs, const Contains& rhs);
DOCTEST_INTERFACE bool operator!=(const String& lhs, const String& rhs);
DOCTEST_INTERFACE bool operator!=(const String& lhs, const Contains& rhs);
DOCTEST_INTERFACE bool operator<(const String& lhs, const String& rhs);
Expand Down Expand Up @@ -739,6 +739,15 @@ struct DOCTEST_INTERFACE TestCaseData

struct DOCTEST_INTERFACE AssertData
{
union StringContains {
String string;
Contains contains;

StringContains()
: string("") {}

~StringContains() {}
};
// common - for all asserts
const TestCaseData* m_test_case;
assertType::Enum m_at;
Expand All @@ -755,10 +764,10 @@ struct DOCTEST_INTERFACE AssertData
String m_decomp;

// for specific exception-related asserts
bool m_threw_as;
const char* m_exception_type;
String m_exception_string;
bool m_contains;
bool m_threw_as;
const char* m_exception_type;
StringContains m_exception_string;
bool m_contains;
};

struct DOCTEST_INTERFACE MessageData
Expand Down Expand Up @@ -1531,10 +1540,10 @@ DOCTEST_CLANG_SUPPRESS_WARNING_POP
struct DOCTEST_INTERFACE ResultBuilder : public AssertData
{
ResultBuilder(assertType::Enum at, const char* file, int line, const char* expr,
const char* exception_type = "", const char* exception_string = "");
const char* exception_type = "", const String& exception_string = "");

ResultBuilder(assertType::Enum at, const char* file, int line, const char* expr,
const char* exception_type, Contains exception_string);
const char* exception_type, const Contains& exception_string);

void setResult(const Result& res);

Expand Down Expand Up @@ -3655,21 +3664,22 @@ int String::compare(const String& other, bool no_case) const {
return compare(other.c_str(), no_case);
}

const char *String::compare(const Contains& other) const {
return strstr(c_str(), other.string.c_str());
}

Contains::Contains(const char* in){
string = String(in);
}
}

bool Contains::checkWith(const String& full_string) const {
return strstr(full_string.c_str(), string.c_str()) != nullptr;
}

// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
String operator+(const String& lhs, const String& rhs) { return String(lhs) += rhs; }

// clang-format off
bool operator==(const String& lhs, const String& rhs) { return lhs.compare(rhs) == 0; }
bool operator==(const String& lhs, const Contains& rhs) { return rhs.checkWith(lhs); }
bool operator!=(const String& lhs, const String& rhs) { return lhs.compare(rhs) != 0; }
bool operator!=(const String& lhs, const Contains& rhs) { return lhs.compare(rhs) == nullptr; }
bool operator!=(const String& lhs, const Contains& rhs) { return !rhs.checkWith(lhs); }
bool operator< (const String& lhs, const String& rhs) { return lhs.compare(rhs) < 0; }
bool operator> (const String& lhs, const String& rhs) { return lhs.compare(rhs) > 0; }
bool operator<=(const String& lhs, const String& rhs) { return (lhs != rhs) ? lhs.compare(rhs) < 0 : true; }
Expand Down Expand Up @@ -4732,7 +4742,7 @@ namespace {
} // namespace
namespace detail {
ResultBuilder::ResultBuilder(assertType::Enum at, const char* file, int line, const char* expr,
const char* exception_type, const char* exception_string) {
const char* exception_type, const String& exception_string) {
m_test_case = g_cs->currentTest;
m_at = at;
m_file = file;
Expand All @@ -4742,7 +4752,7 @@ namespace detail {
m_threw = false;
m_threw_as = false;
m_exception_type = exception_type;
m_exception_string = String(exception_string);
m_exception_string.string = exception_string;
m_contains = false;
#if DOCTEST_MSVC
if(m_expr[0] == ' ') // this happens when variadic macros are disabled under MSVC
Expand All @@ -4751,7 +4761,7 @@ namespace detail {
}

ResultBuilder::ResultBuilder(assertType::Enum at, const char* file, int line, const char* expr,
const char* exception_type, Contains exception_string) {
const char* exception_type, const Contains& exception_string) {
m_test_case = g_cs->currentTest;
m_at = at;
m_file = file;
Expand All @@ -4761,7 +4771,7 @@ namespace detail {
m_threw = false;
m_threw_as = false;
m_exception_type = exception_type;
m_exception_string = exception_string.string;
m_exception_string.contains = exception_string;
m_contains = true;
#if DOCTEST_MSVC
if(m_expr[0] == ' ') // this happens when variadic macros are disabled under MSVC
Expand All @@ -4783,11 +4793,11 @@ namespace detail {
if(m_at & assertType::is_throws) { //!OCLINT bitwise operator in conditional
m_failed = !m_threw;
} else if((m_at & assertType::is_throws_as) && (m_at & assertType::is_throws_with)) { //!OCLINT
m_failed = !m_threw_as || ( m_contains ? m_exception != Contains(m_exception_string.c_str()) : m_exception != String(m_exception_string));
m_failed = !m_threw_as || ( m_contains ? m_exception != m_exception_string.contains : m_exception != m_exception_string.string);
} else if(m_at & assertType::is_throws_as) { //!OCLINT bitwise operator in conditional
m_failed = !m_threw_as;
} else if(m_at & assertType::is_throws_with) { //!OCLINT bitwise operator in conditional
m_failed = ( m_contains ? m_exception != Contains(m_exception_string.c_str()) : m_exception != String(m_exception_string));
m_failed = ( m_contains ? m_exception != m_exception_string.contains : m_exception != m_exception_string.string);
} else if(m_at & assertType::is_nothrow) { //!OCLINT bitwise operator in conditional
m_failed = m_threw;
}
Expand Down Expand Up @@ -5460,7 +5470,7 @@ namespace {
if(rb.m_at & assertType::is_throws_as)
xml.scopedElement("ExpectedException").writeText(rb.m_exception_type);
if(rb.m_at & assertType::is_throws_with)
xml.scopedElement("ExpectedExceptionString").writeText(rb.m_exception_string.c_str());
xml.scopedElement("ExpectedExceptionString").writeText(rb.m_contains ? rb.m_exception_string.contains.string.c_str() : rb.m_exception_string.string.c_str());
if((rb.m_at & assertType::is_normal) && !rb.m_threw)
xml.scopedElement("Expanded").writeText(rb.m_decomp.c_str());

Expand Down Expand Up @@ -5506,7 +5516,8 @@ namespace {
} else if((rb.m_at & assertType::is_throws_as) &&
(rb.m_at & assertType::is_throws_with)) { //!OCLINT
s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \""
<< rb.m_exception_string << "\", " << rb.m_exception_type << " ) " << Color::None;
<< (rb.m_contains ? rb.m_exception_string.contains.string.c_str() : rb.m_exception_string.string.c_str())
<< "\", " << rb.m_exception_type << " ) " << Color::None;
if(rb.m_threw) {
if(!rb.m_failed) {
s << "threw as expected!\n";
Expand All @@ -5527,7 +5538,8 @@ namespace {
} else if(rb.m_at &
assertType::is_throws_with) { //!OCLINT bitwise operator in conditional
s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \""
<< rb.m_exception_string << "\" ) " << Color::None
<< (rb.m_contains ? rb.m_exception_string.contains.string.c_str() : rb.m_exception_string.string.c_str())
<< "\" ) " << Color::None
<< (rb.m_threw ? (!rb.m_failed ? "threw as expected!" :
"threw a DIFFERENT exception: ") :
"did NOT throw at all!")
Expand Down

0 comments on commit 2163410

Please sign in to comment.