Skip to content

Commit

Permalink
Add SCOPE_EXPECT_EQUAL() support comparing std::strings and C-style s…
Browse files Browse the repository at this point in the history
…trings, and for comparing pointer values to nullptr.
  • Loading branch information
Jon Stewart committed Apr 2, 2021
1 parent 2c1336f commit 6da270d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
40 changes: 37 additions & 3 deletions scope/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,24 @@ namespace scope {
is an int, the sequence version of evalEqual() is preferred. But if the types
do not satisfy std::begin() and std::end(), then, due to SFINAE, the evalEqualImpl
that takes a long will be selected, which simply calls op== on the two arguments.
`
TO-DO:
- provide specialization for comparison of std::tuple args (see bottom of header)
- provide specialization for epsilon-comparison of floating point scalars
*/
template<typename ExceptionType, typename ActualT>
void evalEqualImpl(nullptr_t e, ActualT&& a, long, const char* const file, int line, const char* msg = "") {
if (!(ActualT(e) == a)) {
std::ostringstream buf;
if (*msg) {
buf << msg << " ";
}
buf << "Expected: null, Actual: " << a;
throw ExceptionType(file, line, buf.str().c_str());
}
}

template<typename ExceptionType, typename ExpectedT, typename ActualT>
auto evalEqualImpl(ExpectedT&& e, ActualT&& a, long, const char* const file, int line, const char* msg = "")
-> decltype((e == a), std::declval<std::ostringstream&>() << e, std::declval<std::ostringstream&>() << a, void())
Expand Down Expand Up @@ -133,13 +145,35 @@ namespace scope {
}
}

template<typename ExceptionType>
void evalEqual(const char* const file, int line, const char* e, const char* a, const char* msg = "") {
evalEqualImpl<ExceptionType>(std::string(e), std::string(a), 0, file, line, msg);
}

template<typename ExceptionType>
void evalEqual(const char* const file, int line, char* e, const char* a, const char* msg = "") {
evalEqualImpl<ExceptionType>(std::string(e), std::string(a), 0, file, line, msg);
}

template<typename ExceptionType>
void evalEqual(const char* const file, int line, const char* e, char* a, const char* msg = "") {
evalEqualImpl<ExceptionType>(std::string(e), std::string(a), 0, file, line, msg);
}

template<typename ExceptionType>
void evalEqual(const char* const file, int line, char* e, char* a, const char* msg = "") {
evalEqualImpl<ExceptionType>(std::string(e), std::string(a), 0, file, line, msg);
}

// policy for passing arguments to evalEqual by value
template <typename L, typename R>
using pass_by_value = std::integral_constant<bool,
(!std::is_class<L>::value &&
std::is_null_pointer<L>::value || std::is_null_pointer<R>::value ||
std::is_pointer<L>::value || std::is_pointer<R>::value ||
((!std::is_class<L>::value &&
std::is_convertible<const L, const R>::value) ||
(!std::is_class<R>::value &&
std::is_convertible<const R, const L>::value)
std::is_convertible<const R, const L>::value))
>;

// evalEqual for floating points
Expand Down
37 changes: 37 additions & 0 deletions test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,40 @@ SCOPE_TEST(pairSequence) {

SCOPE_ASSERT_EQUAL({std::make_pair(std::string("hello"), 5u)}, actual);
}

SCOPE_TEST(strings) {
std::string a("a");
std::string a2("a");

char cs[2];
cs[0] = 'a';
cs[1] = '\0';

char cs2[2];
cs2[0] = 'a';
cs2[1] = '\0';

char* cp = &cs2[0];

SCOPE_ASSERT_EQUAL(a, "a");
SCOPE_ASSERT_EQUAL("a", a);
SCOPE_ASSERT_EQUAL(a, a2);

SCOPE_ASSERT_EQUAL(a, cs);
SCOPE_ASSERT_EQUAL(cs, a);
SCOPE_ASSERT_EQUAL("a", cp);
}

SCOPE_TEST(nulls) {
void* v = 0;
char* x = 0;
int* z = nullptr;

SCOPE_ASSERT_EQUAL(nullptr, v);
SCOPE_ASSERT_EQUAL(nullptr, x);
SCOPE_ASSERT_EQUAL(nullptr, z);
// SCOPE_ASSERT_EQUAL(0, v);
// SCOPE_ASSERT_EQUAL(0, x);
// SCOPE_ASSERT_EQUAL(0, z);
// SCOPE_ASSERT_EQUAL(null, x);
}

0 comments on commit 6da270d

Please sign in to comment.