• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "catch_wildcard_pattern.h"
9 #include "catch_enforce.h"
10 #include "catch_string_manip.h"
11 
12 namespace Catch {
13 
WildcardPattern(std::string const & pattern,CaseSensitive::Choice caseSensitivity)14     WildcardPattern::WildcardPattern( std::string const& pattern,
15                                       CaseSensitive::Choice caseSensitivity )
16     :   m_caseSensitivity( caseSensitivity ),
17         m_pattern( normaliseString( pattern ) )
18     {
19         if( startsWith( m_pattern, '*' ) ) {
20             m_pattern = m_pattern.substr( 1 );
21             m_wildcard = WildcardAtStart;
22         }
23         if( endsWith( m_pattern, '*' ) ) {
24             m_pattern = m_pattern.substr( 0, m_pattern.size()-1 );
25             m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd );
26         }
27     }
28 
matches(std::string const & str) const29     bool WildcardPattern::matches( std::string const& str ) const {
30         switch( m_wildcard ) {
31             case NoWildcard:
32                 return m_pattern == normaliseString( str );
33             case WildcardAtStart:
34                 return endsWith( normaliseString( str ), m_pattern );
35             case WildcardAtEnd:
36                 return startsWith( normaliseString( str ), m_pattern );
37             case WildcardAtBothEnds:
38                 return contains( normaliseString( str ), m_pattern );
39             default:
40                 CATCH_INTERNAL_ERROR( "Unknown enum" );
41         }
42     }
43 
normaliseString(std::string const & str) const44     std::string WildcardPattern::normaliseString( std::string const& str ) const {
45         return trim( m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str );
46     }
47 }
48