1 /*
2  *  Created by Phil Nash on 08/02/2017.
3  *  Copyright (c) 2017 Two Blue Cubes Ltd. All rights reserved.
4  *
5  * Distributed under the Boost Software License, Version 1.0. (See accompanying
6  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 #ifndef TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED
9 #define TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED
10 
11 #include "catch_matchers.h"
12 
13 #include <string>
14 
15 namespace Catch {
16 namespace Matchers {
17 
18     namespace StdString {
19 
20         struct CasedString
21         {
22             CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity );
23             std::string adjustString( std::string const& str ) const;
24             std::string caseSensitivitySuffix() const;
25 
26             CaseSensitive::Choice m_caseSensitivity;
27             std::string m_str;
28         };
29 
30         struct StringMatcherBase : MatcherBase<std::string> {
31             StringMatcherBase( std::string const& operation, CasedString const& comparator );
32             std::string describe() const override;
33 
34             CasedString m_comparator;
35             std::string m_operation;
36         };
37 
38         struct EqualsMatcher : StringMatcherBase {
39             EqualsMatcher( CasedString const& comparator );
40             bool match( std::string const& source ) const override;
41         };
42         struct ContainsMatcher : StringMatcherBase {
43             ContainsMatcher( CasedString const& comparator );
44             bool match( std::string const& source ) const override;
45         };
46         struct StartsWithMatcher : StringMatcherBase {
47             StartsWithMatcher( CasedString const& comparator );
48             bool match( std::string const& source ) const override;
49         };
50         struct EndsWithMatcher : StringMatcherBase {
51             EndsWithMatcher( CasedString const& comparator );
52             bool match( std::string const& source ) const override;
53         };
54 
55         struct RegexMatcher : MatcherBase<std::string> {
56             RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity );
57             bool match( std::string const& matchee ) const override;
58             std::string describe() const override;
59 
60         private:
61             std::string m_regex;
62             CaseSensitive::Choice m_caseSensitivity;
63         };
64 
65     } // namespace StdString
66 
67 
68     // The following functions create the actual matcher objects.
69     // This allows the types to be inferred
70 
71     StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
72     StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
73     StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
74     StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
75     StdString::RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
76 
77 } // namespace Matchers
78 } // namespace Catch
79 
80 #endif // TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED
81