catch_matchers_string.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "catch_matchers_string.h"
  9. #include "catch_string_manip.h"
  10. #include "catch_tostring.h"
  11. #include <regex>
  12. namespace Catch {
  13. namespace Matchers {
  14. namespace StdString {
  15. CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
  16. : m_caseSensitivity( caseSensitivity ),
  17. m_str( adjustString( str ) )
  18. {}
  19. std::string CasedString::adjustString( std::string const& str ) const {
  20. return m_caseSensitivity == CaseSensitive::No
  21. ? toLower( str )
  22. : str;
  23. }
  24. std::string CasedString::caseSensitivitySuffix() const {
  25. return m_caseSensitivity == CaseSensitive::No
  26. ? " (case insensitive)"
  27. : std::string();
  28. }
  29. StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator )
  30. : m_comparator( comparator ),
  31. m_operation( operation ) {
  32. }
  33. std::string StringMatcherBase::describe() const {
  34. std::string description;
  35. description.reserve(5 + m_operation.size() + m_comparator.m_str.size() +
  36. m_comparator.caseSensitivitySuffix().size());
  37. description += m_operation;
  38. description += ": \"";
  39. description += m_comparator.m_str;
  40. description += "\"";
  41. description += m_comparator.caseSensitivitySuffix();
  42. return description;
  43. }
  44. EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {}
  45. bool EqualsMatcher::match( std::string const& source ) const {
  46. return m_comparator.adjustString( source ) == m_comparator.m_str;
  47. }
  48. ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {}
  49. bool ContainsMatcher::match( std::string const& source ) const {
  50. return contains( m_comparator.adjustString( source ), m_comparator.m_str );
  51. }
  52. StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {}
  53. bool StartsWithMatcher::match( std::string const& source ) const {
  54. return startsWith( m_comparator.adjustString( source ), m_comparator.m_str );
  55. }
  56. EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {}
  57. bool EndsWithMatcher::match( std::string const& source ) const {
  58. return endsWith( m_comparator.adjustString( source ), m_comparator.m_str );
  59. }
  60. RegexMatcher::RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {}
  61. bool RegexMatcher::match(std::string const& matchee) const {
  62. auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway
  63. if (m_caseSensitivity == CaseSensitive::Choice::No) {
  64. flags |= std::regex::icase;
  65. }
  66. auto reg = std::regex(m_regex, flags);
  67. return std::regex_match(matchee, reg);
  68. }
  69. std::string RegexMatcher::describe() const {
  70. return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively");
  71. }
  72. } // namespace StdString
  73. StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
  74. return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) );
  75. }
  76. StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
  77. return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) );
  78. }
  79. StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
  80. return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) );
  81. }
  82. StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
  83. return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) );
  84. }
  85. StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) {
  86. return StdString::RegexMatcher(regex, caseSensitivity);
  87. }
  88. } // namespace Matchers
  89. } // namespace Catch