Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include negated assertion_result messages: #395

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion include/boost/test/tools/assertion_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ class BOOST_TEST_DECL assertion_result {
assertion_result( BoolConvertable const& pv_ ) : p_predicate_value( !!pv_ ) {}

// Access methods
bool operator!() const { return !p_predicate_value; }
assertion_result operator!() const
{
assertion_result negatedResult = !p_predicate_value;
negatedResult.message() << "NOT(" << ( !m_message ? std::string() : m_message->str() ) << ")";
return negatedResult;
}
void operator=( bool pv_ ) { p_predicate_value.value = pv_; }
operator safe_bool() const { return !!p_predicate_value ? &dummy::nonnull : 0; }

Expand All @@ -75,6 +80,12 @@ class BOOST_TEST_DECL assertion_result {
}
const_string message() const { return !m_message ? const_string() : const_string( m_message->str() ); }

// Operators
friend bool operator==(bool left, const assertion_result& right) { return left == static_cast<bool>(right); }
friend bool operator==(const assertion_result& left, bool right) { return static_cast<bool>(left) == right; }
friend bool operator!=(bool left, const assertion_result& right) { return left == static_cast<bool>(right); }
friend bool operator!=(const assertion_result& left, bool right) { return static_cast<bool>(left) == right; }

private:
// Data members
shared_ptr<wrap_stringstream> m_message;
Expand Down
31 changes: 31 additions & 0 deletions test/writing-test-ts/assertion-construction-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <boost/noncopyable.hpp>

#include <locale>
#include <map>
#include <set>

Expand Down Expand Up @@ -405,6 +406,15 @@ class NC : boost::noncopyable {
}
};

boost::test_tools::predicate_result isEqualCaseInsensitive(char left, char right)
{
boost::test_tools::predicate_result result(
std::tolower(left, std::locale::classic()) == std::tolower(right, std::locale::classic()));
result.message() << "[" << left << " isEqualCaseInsensitiveTo " << right << ']';
return result;
}


BOOST_AUTO_TEST_CASE( test_objects )
{
using namespace boost::test_tools;
Expand Down Expand Up @@ -469,6 +479,27 @@ BOOST_AUTO_TEST_CASE( test_objects )
BOOST_TEST( !res );
BOOST_TEST( res.message() == " [NC != NC]" );
}

{
predicate_result const& res = EXPR_TYPE( isEqualCaseInsensitive('z', 'Z') ).evaluate();
BOOST_TEST( res );
BOOST_TEST( res.message() == "[z isEqualCaseInsensitiveTo Z]" );
}

{
predicate_result const& res = !EXPR_TYPE( isEqualCaseInsensitive('z', 'Z') ).evaluate();
BOOST_TEST( !res );
BOOST_TEST( res.message() == "NOT([z isEqualCaseInsensitiveTo Z])" );
}

{
NC nc1;
NC nc2;

predicate_result const& res = !EXPR_TYPE( nc1 == nc2 ).evaluate();
BOOST_TEST( res );
BOOST_TEST( res.message() == "NOT( [NC != NC])" );
}
}

//____________________________________________________________________________//
Expand Down