1
1
Fork 0
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:
Sergei Zimmerman 2025-07-18 21:20:24 +03:00
parent ffc9bfb66d
commit d9053390ce
No known key found for this signature in database
2 changed files with 57 additions and 0 deletions

View file

@ -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

View file

@ -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',