// Copyright Joakim Karlsson & Kim Gräsman 2010-2012. // 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 IGLOO_EXPRESSIONBUILDER_H #define IGLOO_EXPRESSIONBUILDER_H namespace snowhouse { // ---- Evaluation of list of constraints template inline void EvaluateConstraintList(ConstraintListType& constraint_list, ResultStack& result, OperatorStack& operators, const ActualType& actual) { constraint_list.m_head.Evaluate(constraint_list, result, operators, actual); } template inline void EvaluateConstraintList(Nil&, ResultStack&, OperatorStack&, const ActualType&) {} template struct ExpressionBuilder { ExpressionBuilder(const ConstraintListType& list) : m_constraint_list(list) { } template ExpressionBuilder >, Nil> >::t> EqualTo(const ExpectedType& expected) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(expected); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } template ExpressionBuilder >, Nil> >::t> EqualToWithDelta(const ExpectedType& expected, const DeltaType& delta) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(EqualsWithDeltaConstraint(expected, delta)); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } template ExpressionBuilder >, Nil> >::t> Fulfilling(const MatcherType& matcher) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(matcher); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder >, Nil> >::t> False() { return EqualTo(false); } ExpressionBuilder >, Nil> >::t> True() { return EqualTo(true); } ExpressionBuilder >, Nil> >::t> EqualTo(const char* expected) { return EqualTo(std::string(expected)); } template ExpressionBuilder >, Nil> >::t> GreaterThan(const ExpectedType& expected) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(expected); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } template ExpressionBuilder >, Nil> >::t> LessThan(const ExpectedType& expected) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(expected); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } template ExpressionBuilder >, Nil> >::t> Containing(const ExpectedType& expected) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(expected); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder >, Nil> >::t> Containing(const char* expected) { return Containing(std::string(expected)); } template ExpressionBuilder >, Nil> >::t> EndingWith(const ExpectedType& expected) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(expected); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder >, Nil> >::t> EndingWith(const char* expected) { return EndingWith(std::string(expected)); } template ExpressionBuilder >, Nil> >::t> StartingWith(const ExpectedType& expected) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(expected); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder >, Nil> >::t> StartingWith(const char* expected) { return StartingWith(std::string(expected)); } template ExpressionBuilder >, Nil> >::t> OfLength(const ExpectedType& expected) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(expected); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder >, Nil> >::t> Empty() { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(0); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } template ExpressionBuilder >, Nil> >::t> EqualToContainer(const ExpectedType& expected) { typedef bool (*DefaultBinaryPredivateType)(const typename ExpectedType::value_type&, const typename ExpectedType::value_type&); typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(EqualsContainerConstraint(expected, constraint_internal::default_comparer)); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } template ExpressionBuilder >, Nil> >::t> EqualToContainer(const ExpectedType& expected, const BinaryPredicate predicate) { typedef ConstraintAdapter > ConstraintAdapterType; typedef ExpressionBuilder< typename type_concat >::t > BuilderType; ConstraintAdapterType constraint(EqualsContainerConstraint(expected, predicate)); ConstraintList node(constraint, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } typedef ConstraintList AndOperatorNode; typedef ConstraintList OrOperatorNode; typedef ConstraintList NotOperatorNode; typedef ConstraintList AllOperatorNode; typedef ConstraintList AtLeastOperatorNode; typedef ConstraintList ExactlyOperatorNode; typedef ConstraintList AtMostOperatorNode; typedef ConstraintList NoneOperatorNode; ExpressionBuilder::t> All() { typedef ExpressionBuilder::t> BuilderType; AllOperator op; AllOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder::t> AtLeast(unsigned int expected) { typedef ExpressionBuilder::t> BuilderType; AtLeastOperator op(expected); AtLeastOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder::t> Exactly(unsigned int expected) { typedef ExpressionBuilder::t> BuilderType; ExactlyOperator op(expected); ExactlyOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder::t> AtMost(unsigned int expected) { typedef ExpressionBuilder::t> BuilderType; AtMostOperator op(expected); AtMostOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder::t> None() { typedef ExpressionBuilder::t> BuilderType; NoneOperator op; NoneOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder::t> And() { typedef ExpressionBuilder::t> BuilderType; AndOperator op; AndOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder::t> Or() { typedef ExpressionBuilder::t> BuilderType; OrOperator op; OrOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } ExpressionBuilder::t> Not() { typedef ExpressionBuilder::t> BuilderType; NotOperator op; NotOperatorNode node(op, Nil()); return BuilderType(Concatenate(m_constraint_list, node)); } template void Evaluate(ResultStack& result, OperatorStack& operators, const ActualType& actual) { EvaluateConstraintList(m_constraint_list, result, operators, actual); } ConstraintListType m_constraint_list; }; template inline void StringizeConstraintList(const T& list, std::ostringstream& stm) { if (stm.tellp() > 0) stm << " "; stm << snowhouse::Stringize(list.m_head); StringizeConstraintList(list.m_tail, stm); } inline void StringizeConstraintList(const Nil&, std::ostringstream&) { } template struct Stringizer< ExpressionBuilder > { static std::string ToString(const ExpressionBuilder& builder) { std::ostringstream stm; StringizeConstraintList(builder.m_constraint_list, stm); return stm.str(); } }; } #endif