mirror of
https://github.com/NixOS/nix.git
synced 2025-11-17 16:02:43 +01:00
libutil-test-support: Add HasSubstrIgnoreANSIMatcher
This matcher is useful for checking error messages, which always contain ANSI escapes.
This commit is contained in:
parent
ffc9bfb66d
commit
d9053390ce
2 changed files with 57 additions and 0 deletions
|
|
@ -0,0 +1,56 @@
|
||||||
|
#pragma once
|
||||||
|
///@file
|
||||||
|
|
||||||
|
#include "nix/util/terminal.hh"
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
|
||||||
|
namespace nix::testing {
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GMock matcher that matches substring while stripping off all ANSI escapes.
|
||||||
|
* Useful for checking exceptions messages in unit tests.
|
||||||
|
*/
|
||||||
|
class HasSubstrIgnoreANSIMatcher
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit HasSubstrIgnoreANSIMatcher(std::string substring)
|
||||||
|
: substring(std::move(substring))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MatchAndExplain(const char * s, ::testing::MatchResultListener * listener) const
|
||||||
|
{
|
||||||
|
return s != nullptr && MatchAndExplain(std::string(s), listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename MatcheeStringType>
|
||||||
|
bool MatchAndExplain(const MatcheeStringType & s, [[maybe_unused]] ::testing::MatchResultListener * listener) const
|
||||||
|
{
|
||||||
|
return filterANSIEscapes(s, /*filterAll=*/true).find(substring) != substring.npos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DescribeTo(::std::ostream * os) const
|
||||||
|
{
|
||||||
|
*os << "has substring " << substring;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DescribeNegationTo(::std::ostream * os) const
|
||||||
|
{
|
||||||
|
*os << "has no substring " << substring;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string substring;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
inline ::testing::PolymorphicMatcher<internal::HasSubstrIgnoreANSIMatcher>
|
||||||
|
HasSubstrIgnoreANSIMatcher(const std::string & substring)
|
||||||
|
{
|
||||||
|
return ::testing::MakePolymorphicMatcher(internal::HasSubstrIgnoreANSIMatcher(substring));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nix::testing
|
||||||
|
|
@ -4,6 +4,7 @@ include_dirs = [include_directories('../../..')]
|
||||||
|
|
||||||
headers = files(
|
headers = files(
|
||||||
'characterization.hh',
|
'characterization.hh',
|
||||||
|
'gmock-matchers.hh',
|
||||||
'gtest-with-params.hh',
|
'gtest-with-params.hh',
|
||||||
'hash.hh',
|
'hash.hh',
|
||||||
'nix_api_util.hh',
|
'nix_api_util.hh',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue