add testStrEq()

This commit is contained in:
Michael Davidsaver
2020-03-17 14:33:30 -07:00
parent 9e79e50fdc
commit 6bba99fce1
2 changed files with 65 additions and 0 deletions
+9
View File
@@ -122,6 +122,9 @@ testCase testNotEq(const char *sLHS, const LHS& lhs, const char *sRHS, const RHS
return ret;
}
PVXS_API
testCase _testStrEq(const char *sLHS, const std::string& lhs, const char *sRHS, const std::string& rhs);
} // namespace detail
/** Assert that an exception is thrown.
@@ -175,6 +178,12 @@ testCase testThrows(FN fn)
//! Roughly equivalent to @code testOk((LHS)!=(RHS), "..."); @endcode
#define testNotEq(LHS, RHS) ::pvxs::detail::testNotEq(#LHS, LHS, #RHS, RHS)
//! Macro which asserts equality between LHS and RHS.
//! Evaluates to a pvxs::testCase
//! Functionally equivalent to testEq() with two std::string instances.
//! Prints diff-like output which is friendlier to multi-line strings.
#define testStrEq(LHS, RHS) ::pvxs::detail::_testStrEq(#LHS, LHS, #RHS, RHS)
//! Macro which prints diagnostic (non-test) lines.
//! Evaluates to a pvxs::testCase
//! Roughly equivalent to @code testDiag("..."); @endcode
+56
View File
@@ -7,6 +7,7 @@
#include <epicsUnitTest.h>
#include "pvxs/unittest.h"
#include "utilpvt.h"
namespace pvxs {
@@ -63,4 +64,59 @@ testCase::~testCase()
}
}
namespace detail {
size_t findNextLine(const std::string& s, size_t pos=0u)
{
size_t next = s.find_first_of('\n', pos);
if(next!=std::string::npos)
next++;
return next;
}
testCase _testStrEq(const char *sLHS, const std::string& lhs, const char *sRHS, const std::string& rhs)
{
testCase ret(lhs==rhs);
ret<<sLHS<<" == "<<sRHS<<"\n";
size_t posL=0u, posR=0u;
while(posL<lhs.size() && posR<rhs.size()) {
size_t eolL = findNextLine(lhs, posL);
size_t eolR = findNextLine(rhs, posR);
auto L = lhs.substr(posL, eolL-posL);
auto R = rhs.substr(posR, eolR-posR);
if(L==R) {
ret<<" \""<<escape(L)<<"\"\n";
} else {
ret<<"+ \""<<escape(R)<<"\"\n";
ret<<"- \""<<escape(L)<<"\"\n";
}
posL = eolL;
posR = eolR;
}
while(posR<rhs.size()) {
size_t eol = findNextLine(rhs, posR);
auto line = rhs.substr(posR, eol-posR);
ret<<"+ \""<<escape(line)<<"\"\n";
posR = eol;
}
while(posL<lhs.size()) {
size_t eol = findNextLine(lhs, posL);
auto line = lhs.substr(posL, eol-posL);
ret<<"- \""<<escape(line)<<"\"\n";
posL = eol;
}
return ret;
}
} // namespace detail
} // namespace pvxs