mirror of
https://github.com/catchorg/Catch2.git
synced 2025-02-16 19:43:28 +01:00
Extracted string matchers impl into cpp that is only compiled into main TU
This commit is contained in:
parent
7fed25ad1f
commit
1400127d6f
@ -154,6 +154,7 @@ set(INTERNAL_HEADERS
|
|||||||
${HEADER_DIR}/internal/catch_legacy_reporter_adapter.hpp
|
${HEADER_DIR}/internal/catch_legacy_reporter_adapter.hpp
|
||||||
${HEADER_DIR}/internal/catch_list.hpp
|
${HEADER_DIR}/internal/catch_list.hpp
|
||||||
${HEADER_DIR}/internal/catch_matchers.hpp
|
${HEADER_DIR}/internal/catch_matchers.hpp
|
||||||
|
${HEADER_DIR}/internal/catch_matchers_string.h
|
||||||
${HEADER_DIR}/internal/catch_matchers_string.hpp
|
${HEADER_DIR}/internal/catch_matchers_string.hpp
|
||||||
${HEADER_DIR}/internal/catch_message.h
|
${HEADER_DIR}/internal/catch_message.h
|
||||||
${HEADER_DIR}/internal/catch_message.hpp
|
${HEADER_DIR}/internal/catch_message.hpp
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
#include "internal/catch_generators.hpp"
|
#include "internal/catch_generators.hpp"
|
||||||
#include "internal/catch_interfaces_exception.h"
|
#include "internal/catch_interfaces_exception.h"
|
||||||
#include "internal/catch_approx.hpp"
|
#include "internal/catch_approx.hpp"
|
||||||
#include "internal/catch_matchers_string.hpp"
|
#include "internal/catch_matchers_string.h"
|
||||||
#include "internal/catch_compiler_capabilities.h"
|
#include "internal/catch_compiler_capabilities.h"
|
||||||
#include "internal/catch_interfaces_tag_alias_registry.h"
|
#include "internal/catch_interfaces_tag_alias_registry.h"
|
||||||
|
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
#include "catch_result_builder.hpp"
|
#include "catch_result_builder.hpp"
|
||||||
#include "catch_tag_alias_registry.hpp"
|
#include "catch_tag_alias_registry.hpp"
|
||||||
#include "catch_test_case_tracker.hpp"
|
#include "catch_test_case_tracker.hpp"
|
||||||
|
#include "catch_matchers_string.hpp"
|
||||||
|
|
||||||
#include "../reporters/catch_reporter_multi.hpp"
|
#include "../reporters/catch_reporter_multi.hpp"
|
||||||
#include "../reporters/catch_reporter_xml.hpp"
|
#include "../reporters/catch_reporter_xml.hpp"
|
||||||
|
67
include/internal/catch_matchers_string.h
Normal file
67
include/internal/catch_matchers_string.h
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Created by Phil Nash on 08/02/2017.
|
||||||
|
* Copyright (c) 2017 Two Blue Cubes Ltd. All rights reserved.
|
||||||
|
*
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
#ifndef TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED
|
||||||
|
#define TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include "catch_matchers.hpp"
|
||||||
|
|
||||||
|
namespace Catch {
|
||||||
|
namespace Matchers {
|
||||||
|
|
||||||
|
namespace StdString {
|
||||||
|
|
||||||
|
struct CasedString
|
||||||
|
{
|
||||||
|
CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity );
|
||||||
|
std::string adjustString( std::string const& str ) const;
|
||||||
|
std::string caseSensitivitySuffix() const;
|
||||||
|
|
||||||
|
CaseSensitive::Choice m_caseSensitivity;
|
||||||
|
std::string m_str;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct StringMatcherBase : Impl::MatcherBase<std::string> {
|
||||||
|
StringMatcherBase( std::string operation, CasedString const& comparator );
|
||||||
|
virtual std::string toStringUncached() const CATCH_OVERRIDE;
|
||||||
|
|
||||||
|
CasedString m_comparator;
|
||||||
|
std::string m_operation;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct EqualsMatcher : StringMatcherBase {
|
||||||
|
EqualsMatcher( CasedString const& comparator );
|
||||||
|
virtual bool match( std::string const& source ) const CATCH_OVERRIDE;
|
||||||
|
};
|
||||||
|
struct ContainsMatcher : StringMatcherBase {
|
||||||
|
ContainsMatcher( CasedString const& comparator );
|
||||||
|
virtual bool match( std::string const& source ) const CATCH_OVERRIDE;
|
||||||
|
};
|
||||||
|
struct StartsWithMatcher : StringMatcherBase {
|
||||||
|
StartsWithMatcher( CasedString const& comparator );
|
||||||
|
virtual bool match( std::string const& source ) const CATCH_OVERRIDE;
|
||||||
|
};
|
||||||
|
struct EndsWithMatcher : StringMatcherBase {
|
||||||
|
EndsWithMatcher( CasedString const& comparator );
|
||||||
|
virtual bool match( std::string const& source ) const CATCH_OVERRIDE;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace StdString
|
||||||
|
|
||||||
|
|
||||||
|
// The following functions create the actual matcher objects.
|
||||||
|
// This allows the types to be inferred
|
||||||
|
|
||||||
|
StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
|
StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
|
StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
|
StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
|
|
||||||
|
} // namespace Matchers
|
||||||
|
} // namespace Catch
|
||||||
|
|
||||||
|
#endif // TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED
|
@ -5,8 +5,6 @@
|
|||||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
*/
|
*/
|
||||||
#ifndef TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED
|
|
||||||
#define TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED
|
|
||||||
|
|
||||||
#include "catch_matchers.hpp"
|
#include "catch_matchers.hpp"
|
||||||
|
|
||||||
@ -15,100 +13,81 @@ namespace Matchers {
|
|||||||
|
|
||||||
namespace StdString {
|
namespace StdString {
|
||||||
|
|
||||||
struct CasedString
|
CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
|
||||||
{
|
: m_caseSensitivity( caseSensitivity ),
|
||||||
CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
|
m_str( adjustString( str ) )
|
||||||
: m_caseSensitivity( caseSensitivity ),
|
{}
|
||||||
m_str( adjustString( str ) )
|
std::string CasedString::adjustString( std::string const& str ) const {
|
||||||
{}
|
return m_caseSensitivity == CaseSensitive::No
|
||||||
std::string adjustString( std::string const& str ) const {
|
? toLower( str )
|
||||||
return m_caseSensitivity == CaseSensitive::No
|
: str;
|
||||||
? toLower( str )
|
}
|
||||||
: str;
|
std::string CasedString::caseSensitivitySuffix() const {
|
||||||
}
|
return m_caseSensitivity == CaseSensitive::No
|
||||||
std::string caseSensitivitySuffix() const {
|
? " (case insensitive)"
|
||||||
return m_caseSensitivity == CaseSensitive::No
|
: std::string();
|
||||||
? " (case insensitive)"
|
}
|
||||||
: std::string();
|
|
||||||
}
|
|
||||||
CaseSensitive::Choice m_caseSensitivity;
|
|
||||||
std::string m_str;
|
|
||||||
};
|
|
||||||
|
|
||||||
// !TBD Move impl
|
|
||||||
struct StringMatcherBase : Impl::MatcherBase<std::string> {
|
|
||||||
StringMatcherBase( std::string operation, CasedString const& comparator )
|
|
||||||
: m_comparator( comparator ),
|
|
||||||
m_operation( operation ) {
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual std::string toStringUncached() const CATCH_OVERRIDE {
|
StringMatcherBase::StringMatcherBase( std::string operation, CasedString const& comparator )
|
||||||
std::string description;
|
: m_comparator( comparator ),
|
||||||
description.reserve(5 + m_operation.size() + m_comparator.m_str.size() +
|
m_operation( operation ) {
|
||||||
m_comparator.caseSensitivitySuffix().size());
|
}
|
||||||
description += m_operation;
|
|
||||||
description += ": \"";
|
|
||||||
description += m_comparator.m_str;
|
|
||||||
description += "\"";
|
|
||||||
description += m_comparator.caseSensitivitySuffix();
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
CasedString m_comparator;
|
std::string StringMatcherBase::toStringUncached() const {
|
||||||
std::string m_operation;
|
std::string description;
|
||||||
};
|
description.reserve(5 + m_operation.size() + m_comparator.m_str.size() +
|
||||||
|
m_comparator.caseSensitivitySuffix().size());
|
||||||
|
description += m_operation;
|
||||||
|
description += ": \"";
|
||||||
|
description += m_comparator.m_str;
|
||||||
|
description += "\"";
|
||||||
|
description += m_comparator.caseSensitivitySuffix();
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
// !TBD Move impl
|
EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {}
|
||||||
struct EqualsMatcher : StringMatcherBase {
|
|
||||||
EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {}
|
|
||||||
|
|
||||||
virtual bool match( std::string const& source ) const CATCH_OVERRIDE {
|
bool EqualsMatcher::match( std::string const& source ) const {
|
||||||
return m_comparator.adjustString( source ) == m_comparator.m_str;
|
return m_comparator.adjustString( source ) == m_comparator.m_str;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
struct ContainsMatcher : StringMatcherBase {
|
|
||||||
ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {}
|
|
||||||
|
|
||||||
virtual bool match( std::string const& source ) const CATCH_OVERRIDE {
|
|
||||||
return contains( m_comparator.adjustString( source ), m_comparator.m_str );
|
|
||||||
}
|
|
||||||
};
|
|
||||||
struct StartsWithMatcher : StringMatcherBase {
|
|
||||||
StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {}
|
|
||||||
|
|
||||||
virtual bool match( std::string const& source ) const CATCH_OVERRIDE {
|
ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {}
|
||||||
return startsWith( m_comparator.adjustString( source ), m_comparator.m_str );
|
|
||||||
}
|
|
||||||
};
|
|
||||||
struct EndsWithMatcher : StringMatcherBase {
|
|
||||||
EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {}
|
|
||||||
|
|
||||||
virtual bool match( std::string const& source ) const CATCH_OVERRIDE {
|
bool ContainsMatcher::match( std::string const& source ) const {
|
||||||
return endsWith( m_comparator.adjustString( source ), m_comparator.m_str );
|
return contains( m_comparator.adjustString( source ), m_comparator.m_str );
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {}
|
||||||
|
|
||||||
|
bool StartsWithMatcher::match( std::string const& source ) const {
|
||||||
|
return startsWith( m_comparator.adjustString( source ), m_comparator.m_str );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {}
|
||||||
|
|
||||||
|
bool EndsWithMatcher::match( std::string const& source ) const {
|
||||||
|
return endsWith( m_comparator.adjustString( source ), m_comparator.m_str );
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace StdString
|
} // namespace StdString
|
||||||
|
|
||||||
|
|
||||||
|
StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
// The following functions create the actual matcher objects.
|
|
||||||
// This allows the types to be inferred
|
|
||||||
|
|
||||||
inline StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
|
||||||
return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) );
|
return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) );
|
||||||
}
|
}
|
||||||
inline StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) );
|
return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) );
|
||||||
}
|
}
|
||||||
inline StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) );
|
return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) );
|
||||||
}
|
}
|
||||||
inline StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) );
|
return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) );
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Matchers
|
} // namespace Matchers
|
||||||
} // namespace Catch
|
} // namespace Catch
|
||||||
|
|
||||||
#endif // TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED
|
|
||||||
|
Loading…
Reference in New Issue
Block a user