catch_matchers_string.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "catch_matchers.h"
  11. #include <string>
  12. namespace Catch {
  13. namespace Matchers {
  14. namespace StdString {
  15. struct CasedString
  16. {
  17. CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity );
  18. std::string adjustString( std::string const& str ) const;
  19. std::string caseSensitivitySuffix() const;
  20. CaseSensitive::Choice m_caseSensitivity;
  21. std::string m_str;
  22. };
  23. struct StringMatcherBase : MatcherBase<std::string> {
  24. StringMatcherBase( std::string const& operation, CasedString const& comparator );
  25. std::string describe() const override;
  26. CasedString m_comparator;
  27. std::string m_operation;
  28. };
  29. struct EqualsMatcher : StringMatcherBase {
  30. EqualsMatcher( CasedString const& comparator );
  31. bool match( std::string const& source ) const override;
  32. };
  33. struct ContainsMatcher : StringMatcherBase {
  34. ContainsMatcher( CasedString const& comparator );
  35. bool match( std::string const& source ) const override;
  36. };
  37. struct StartsWithMatcher : StringMatcherBase {
  38. StartsWithMatcher( CasedString const& comparator );
  39. bool match( std::string const& source ) const override;
  40. };
  41. struct EndsWithMatcher : StringMatcherBase {
  42. EndsWithMatcher( CasedString const& comparator );
  43. bool match( std::string const& source ) const override;
  44. };
  45. struct RegexMatcher : MatcherBase<std::string> {
  46. RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity );
  47. bool match( std::string const& matchee ) const override;
  48. std::string describe() const override;
  49. private:
  50. std::string m_regex;
  51. CaseSensitive::Choice m_caseSensitivity;
  52. };
  53. } // namespace StdString
  54. // The following functions create the actual matcher objects.
  55. // This allows the types to be inferred
  56. StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
  57. StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
  58. StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
  59. StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
  60. StdString::RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
  61. } // namespace Matchers
  62. } // namespace Catch
  63. #endif // TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED