Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_matchers_string.cpp
Go to the documentation of this file.
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
10#include "catch_string_manip.h"
11#include "catch_tostring.h"
12
13#include <regex>
14
15namespace Catch {
16namespace Matchers {
17
18 namespace StdString {
19
20 CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
21 : m_caseSensitivity( caseSensitivity ),
22 m_str( adjustString( str ) )
23 {}
24 std::string CasedString::adjustString( std::string const& str ) const {
26 ? toLower( str )
27 : str;
28 }
31 ? " (case insensitive)"
32 : std::string();
33 }
34
35
36 StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator )
37 : m_comparator( comparator ),
38 m_operation( operation ) {
39 }
40
41 std::string StringMatcherBase::describe() const {
42 std::string description;
43 description.reserve(5 + m_operation.size() + m_comparator.m_str.size() +
45 description += m_operation;
46 description += ": \"";
47 description += m_comparator.m_str;
48 description += "\"";
49 description += m_comparator.caseSensitivitySuffix();
50 return description;
51 }
52
53 EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {}
54
55 bool EqualsMatcher::match( std::string const& source ) const {
57 }
58
59
60 ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {}
61
62 bool ContainsMatcher::match( std::string const& source ) const {
64 }
65
66
67 StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {}
68
69 bool StartsWithMatcher::match( std::string const& source ) const {
71 }
72
73
74 EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {}
75
76 bool EndsWithMatcher::match( std::string const& source ) const {
78 }
79
80
81
82 RegexMatcher::RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {}
83
84 bool RegexMatcher::match(std::string const& matchee) const {
85 auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway
86 if (m_caseSensitivity == CaseSensitive::Choice::No) {
87 flags |= std::regex::icase;
88 }
89 auto reg = std::regex(m_regex, flags);
90 return std::regex_match(matchee, reg);
91 }
92
93 std::string RegexMatcher::describe() const {
94 return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively");
95 }
96
97 } // namespace StdString
98
99
100 StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
101 return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) );
102 }
103 StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
104 return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) );
105 }
106 StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
107 return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) );
108 }
109 StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
110 return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) );
111 }
112
113 StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) {
114 return StdString::RegexMatcher(regex, caseSensitivity);
115 }
116
117} // namespace Matchers
118} // namespace Catch
std::string stringify(const T &e)
StdString::StartsWithMatcher StartsWith(std::string const &str, CaseSensitive::Choice caseSensitivity)
StdString::EqualsMatcher Equals(std::string const &str, CaseSensitive::Choice caseSensitivity)
StdString::EndsWithMatcher EndsWith(std::string const &str, CaseSensitive::Choice caseSensitivity)
StdString::ContainsMatcher Contains(std::string const &str, CaseSensitive::Choice caseSensitivity)
StdString::RegexMatcher Matches(std::string const &regex, CaseSensitive::Choice caseSensitivity)
bool startsWith(std::string const &s, std::string const &prefix)
bool contains(std::string const &s, std::string const &infix)
std::string toLower(std::string const &s)
bool endsWith(std::string const &s, std::string const &suffix)
Definition name.hpp:106
const CharType(& source)[N]
Definition pointer.h:1204
std::string adjustString(std::string const &str) const
CasedString(std::string const &str, CaseSensitive::Choice caseSensitivity)
bool match(std::string const &source) const override
bool match(std::string const &source) const override
bool match(std::string const &source) const override
bool match(std::string const &matchee) const override
RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity)
bool match(std::string const &source) const override
StringMatcherBase(std::string const &operation, CasedString const &comparator)
pInfo flags