1 //          Copyright Joakim Karlsson & Kim Gräsman 2010-2012.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef SNOWHOUSE_ENDSWITHCONSTRAINT_H
7 #define SNOWHOUSE_ENDSWITHCONSTRAINT_H
8 
9 #include "expressions/expression.h"
10 
11 namespace snowhouse
12 {
13   template<typename ExpectedType>
14   struct EndsWithConstraint : Expression<EndsWithConstraint<ExpectedType> >
15   {
EndsWithConstraintEndsWithConstraint16     EndsWithConstraint(const ExpectedType& expected)
17         : m_expected(expected)
18     {
19     }
20 
operatorEndsWithConstraint21     bool operator()(const std::string& actual) const
22     {
23       size_t expectedPos = actual.length() - m_expected.length();
24       return actual.find(m_expected) == expectedPos;
25     }
26 
27     ExpectedType m_expected;
28   };
29 
30   template<typename ExpectedType>
EndsWith(const ExpectedType & expected)31   inline EndsWithConstraint<ExpectedType> EndsWith(const ExpectedType& expected)
32   {
33     return EndsWithConstraint<ExpectedType>(expected);
34   }
35 
EndsWith(const char * expected)36   inline EndsWithConstraint<std::string> EndsWith(const char* expected)
37   {
38     return EndsWithConstraint<std::string>(expected);
39   }
40 
41   template<typename ExpectedType>
42   struct Stringizer<EndsWithConstraint<ExpectedType> >
43   {
44     static std::string ToString(const EndsWithConstraint<ExpectedType>& constraint)
45     {
46       std::ostringstream builder;
47       builder << "ends with " << snowhouse::Stringize(constraint.m_expected);
48 
49       return builder.str();
50     }
51   };
52 }
53 
54 #endif
55