Skip to content

Commit

Permalink
TestSuite: create a non-templated base for Comparator.
Browse files Browse the repository at this point in the history
Majority of the work done in this class is template-independent, so this
avoid a lot of needless template instantiations.
  • Loading branch information
mosra committed Jun 1, 2022
1 parent c5102b3 commit ca98065
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
19 changes: 19 additions & 0 deletions src/Corrade/TestSuite/Comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Comparator.h"

#include "Corrade/Containers/EnumSet.hpp"
#include "Corrade/Containers/StringView.h"

namespace Corrade { namespace TestSuite {

Expand Down Expand Up @@ -57,4 +58,22 @@ Utility::Debug& operator<<(Utility::Debug& debug, const ComparisonStatusFlags va
ComparisonStatusFlag::VerboseDiagnostic});
}

namespace Implementation {

void ComparatorBase::printMessage(ComparisonStatusFlags, Utility::Debug& out, const char* const actual, const char* const expected, void(*printer)(Utility::Debug&, const void*)) {
CORRADE_INTERNAL_ASSERT(actualValue && expectedValue);
out << "Values" << actual << "and" << expected << "are not the same, actual is\n ";
printer(out, actualValue);
out << Utility::Debug::newline << " but expected\n ";
printer(out, expectedValue);
}

/* LCOV_EXCL_START */
void ComparatorBase::saveDiagnostic(ComparisonStatusFlags, Utility::Debug&, Containers::StringView) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}
/* LCOV_EXCL_STOP */

}

}}
48 changes: 25 additions & 23 deletions src/Corrade/TestSuite/Comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ CORRADE_TESTSUITE_EXPORT Utility::Debug& operator<<(Utility::Debug& debug, Compa
/** @debugoperatorenum{ComparisonStatusFlags} */
CORRADE_TESTSUITE_EXPORT Utility::Debug& operator<<(Utility::Debug& debug, ComparisonStatusFlags value);

namespace Implementation {

class CORRADE_TESTSUITE_EXPORT ComparatorBase {
public:
explicit ComparatorBase(): actualValue{}, expectedValue{} {}

void saveDiagnostic(ComparisonStatusFlags status, Utility::Debug& out, Containers::StringView path);

protected:
void printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected, void(*printer)(Utility::Debug&, const void*));

const void* actualValue;
const void* expectedValue;
};

}

/**
@brief Default comparator implementation
Expand Down Expand Up @@ -202,10 +219,8 @@ In the above case, the message will look for example like this:
@include testsuite-save-diagnostic.ansi
*/
template<class T> class Comparator {
template<class T> class Comparator: public Implementation::ComparatorBase {
public:
explicit Comparator();

/**
* @brief Compare two values
*
Expand All @@ -230,6 +245,8 @@ template<class T> class Comparator {
*/
void printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected);

/* Defined in the base class already */
#ifdef DOXYGEN_GENERATING_OUTPUT
/**
* @brief Save a diagnostic
*
Expand All @@ -247,20 +264,10 @@ template<class T> class Comparator {
* (and the function not being called at all if
* @ref ComparisonStatusFlag::Diagnostic is not present as well).
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
void saveDiagnostic(ComparisonStatusFlags status, Utility::Debug& out, Containers::StringView path);
#else
/* using const& to avoid having to include StringView.h */
void saveDiagnostic(ComparisonStatusFlags status, Utility::Debug& out, const Containers::StringView& path);
#endif

private:
const T* actualValue;
const T* expectedValue;
};

template<class T> Comparator<T>::Comparator(): actualValue(), expectedValue() {}

template<class T> ComparisonStatusFlags Comparator<T>::operator()(const T& actual, const T& expected) {
if(actual == expected) return {};

Expand All @@ -269,17 +276,12 @@ template<class T> ComparisonStatusFlags Comparator<T>::operator()(const T& actua
return ComparisonStatusFlag::Failed;
}

template<class T> void Comparator<T>::printMessage(ComparisonStatusFlags, Utility::Debug& out, const char* actual, const char* expected) {
CORRADE_INTERNAL_ASSERT(actualValue && expectedValue);
out << "Values" << actual << "and" << expected << "are not the same, actual is\n "
<< *actualValue << Utility::Debug::newline << " but expected\n " << *expectedValue;
}

/* LCOV_EXCL_START */
template<class T> void Comparator<T>::saveDiagnostic(ComparisonStatusFlags, Utility::Debug&, const Containers::StringView&) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
template<class T> void Comparator<T>::printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected) {
Implementation::ComparatorBase::printMessage(status, out, actual, expected,
[](Utility::Debug& out, const void* value) {
out << *static_cast<const T*>(value);
});
}
/* LCOV_EXCL_STOP */

namespace Implementation {

Expand Down

0 comments on commit ca98065

Please sign in to comment.