catch2/include/internal/catch_expression_lhs.hpp

98 lines
3.5 KiB
C++
Raw Normal View History

2012-05-11 09:03:05 +02:00
/*
* Created by Phil on 11/5/2012.
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_EXPRESSION_LHS_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_EXPRESSION_LHS_HPP_INCLUDED
2012-05-11 09:03:05 +02:00
#include "catch_expressionresult_builder.h"
2012-05-11 09:03:05 +02:00
#include "catch_evaluate.hpp"
2012-05-15 08:42:26 +02:00
namespace Catch {
2012-05-11 09:03:05 +02:00
2012-10-28 21:57:21 +01:00
struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison;
2012-10-26 10:05:36 +02:00
// Wraps the LHS of an expression and captures the operator and RHS (if any) - wrapping them all
// in an ExpressionResultBuilder object
2012-05-11 09:03:05 +02:00
template<typename T>
class ExpressionLhs {
void operator = ( const ExpressionLhs& );
2012-05-11 09:03:05 +02:00
public:
2012-10-28 21:57:21 +01:00
ExpressionLhs( T lhs ) : m_lhs( lhs ) {}
2012-10-24 22:59:47 +02:00
2012-05-11 09:03:05 +02:00
template<typename RhsT>
ExpressionResultBuilder& operator == ( const RhsT& rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsEqualTo>( rhs );
2012-05-11 09:03:05 +02:00
}
2012-05-11 09:03:05 +02:00
template<typename RhsT>
ExpressionResultBuilder& operator != ( const RhsT& rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsNotEqualTo>( rhs );
2012-05-11 09:03:05 +02:00
}
template<typename RhsT>
ExpressionResultBuilder& operator < ( const RhsT& rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsLessThan>( rhs );
2012-05-11 09:03:05 +02:00
}
template<typename RhsT>
ExpressionResultBuilder& operator > ( const RhsT& rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsGreaterThan>( rhs );
2012-05-11 09:03:05 +02:00
}
template<typename RhsT>
ExpressionResultBuilder& operator <= ( const RhsT& rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsLessThanOrEqualTo>( rhs );
2012-05-11 09:03:05 +02:00
}
template<typename RhsT>
ExpressionResultBuilder& operator >= ( const RhsT& rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsGreaterThanOrEqualTo>( rhs );
2012-05-11 09:03:05 +02:00
}
ExpressionResultBuilder& operator == ( bool rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsEqualTo>( rhs );
2012-05-11 09:03:05 +02:00
}
ExpressionResultBuilder& operator != ( bool rhs ) {
2012-10-09 21:58:33 +02:00
return captureExpression<Internal::IsNotEqualTo>( rhs );
2012-05-11 09:03:05 +02:00
}
2012-10-26 10:05:36 +02:00
ExpressionResultBuilder& negate( bool shouldNegate ) {
2012-10-28 21:57:21 +01:00
bool value = m_lhs ? true : false;
return m_result
.setLhs( Catch::toString( value ) )
.setResultType( value )
.negate( shouldNegate );
2012-05-11 09:03:05 +02:00
}
// Only simple binary expressions are allowed on the LHS.
// If more complex compositions are required then place the sub expression in parentheses
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator + ( const RhsT& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator - ( const RhsT& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator / ( const RhsT& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator * ( const RhsT& );
2012-05-11 09:03:05 +02:00
2012-10-09 21:58:33 +02:00
private:
template<Internal::Operator Op, typename RhsT>
ExpressionResultBuilder& captureExpression( const RhsT& rhs ) {
2012-10-09 21:58:33 +02:00
return m_result
2012-10-24 22:59:47 +02:00
.setResultType( Internal::compare<Op>( m_lhs, rhs ) )
2012-10-28 21:57:21 +01:00
.setLhs( Catch::toString( m_lhs ) )
2012-10-09 21:58:33 +02:00
.setRhs( Catch::toString( rhs ) )
.setOp( Internal::OperatorTraits<Op>::getName() );
}
2012-05-11 09:03:05 +02:00
private:
ExpressionResultBuilder m_result;
2012-05-11 09:03:05 +02:00
T m_lhs;
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_EXPRESSION_LHS_HPP_INCLUDED