catch_wildcard_pattern.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Created by Martin on 19/07/2017.
  3. *
  4. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include "catch_wildcard_pattern.h"
  8. #include "catch_enforce.h"
  9. #include "catch_string_manip.h"
  10. namespace Catch {
  11. WildcardPattern::WildcardPattern( std::string const& pattern,
  12. CaseSensitive::Choice caseSensitivity )
  13. : m_caseSensitivity( caseSensitivity ),
  14. m_pattern( normaliseString( pattern ) )
  15. {
  16. if( startsWith( m_pattern, '*' ) ) {
  17. m_pattern = m_pattern.substr( 1 );
  18. m_wildcard = WildcardAtStart;
  19. }
  20. if( endsWith( m_pattern, '*' ) ) {
  21. m_pattern = m_pattern.substr( 0, m_pattern.size()-1 );
  22. m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd );
  23. }
  24. }
  25. bool WildcardPattern::matches( std::string const& str ) const {
  26. switch( m_wildcard ) {
  27. case NoWildcard:
  28. return m_pattern == normaliseString( str );
  29. case WildcardAtStart:
  30. return endsWith( normaliseString( str ), m_pattern );
  31. case WildcardAtEnd:
  32. return startsWith( normaliseString( str ), m_pattern );
  33. case WildcardAtBothEnds:
  34. return contains( normaliseString( str ), m_pattern );
  35. default:
  36. CATCH_INTERNAL_ERROR( "Unknown enum" );
  37. }
  38. }
  39. std::string WildcardPattern::normaliseString( std::string const& str ) const {
  40. return trim( m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str );
  41. }
  42. }