Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
gmock-matchers.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/error.hh"
6#include "nix/util/source-accessor.hh"
8#include <gmock/gmock.h>
9
10namespace nix::testing {
11
12namespace internal {
13
18class HasSubstrIgnoreANSIMatcher
19{
20public:
21 explicit HasSubstrIgnoreANSIMatcher(std::string substring)
22 : substring(std::move(substring))
23 {
24 }
25
26 bool MatchAndExplain(const char * s, ::testing::MatchResultListener * listener) const
27 {
28 return s != nullptr && MatchAndExplain(std::string(s), listener);
29 }
30
31 template<typename MatcheeStringType>
32 bool MatchAndExplain(const MatcheeStringType & s, [[maybe_unused]] ::testing::MatchResultListener * listener) const
33 {
34 return stripANSIEscapes(s).find(substring) != substring.npos;
35 }
36
37 void DescribeTo(::std::ostream * os) const
38 {
39 *os << "has substring " << substring;
40 }
41
42 void DescribeNegationTo(::std::ostream * os) const
43 {
44 *os << "has no substring " << substring;
45 }
46
47private:
48 std::string substring;
49};
50
51} // namespace internal
52
53inline ::testing::PolymorphicMatcher<internal::HasSubstrIgnoreANSIMatcher>
54HasSubstrIgnoreANSIMatcher(const std::string & substring)
55{
56 return ::testing::MakePolymorphicMatcher(internal::HasSubstrIgnoreANSIMatcher(substring));
57}
58
66inline auto ThrowsSysError(int expected)
67{
68 return ::testing::Throws<SysError>(::testing::Field(&SysError::errNo, expected));
69}
70
79inline auto ThrowsSystemError(std::errc expected)
80{
81 return ::testing::Throws<SystemError>(
82 ::testing::Property(&SystemError::ec, ::testing::Eq(std::make_error_condition(expected))));
83}
84
85MATCHER_P2(HasContents, path, expected, "")
86{
87 auto stat = arg->maybeLstat(path);
88 if (!stat) {
89 *result_listener << arg->showPath(path) << " does not exist";
90 return false;
91 }
92 if (stat->type != SourceAccessor::tRegular) {
93 *result_listener << arg->showPath(path) << " is not a regular file";
94 return false;
95 }
96 auto actual = arg->readFile(path);
97 if (actual != expected) {
98 *result_listener << arg->showPath(path) << " has contents " << ::testing::PrintToString(actual);
99 return false;
100 }
101 return true;
102}
103
104MATCHER_P2(HasSymlink, path, target, "")
105{
106 auto stat = arg->maybeLstat(path);
107 if (!stat) {
108 *result_listener << arg->showPath(path) << " does not exist";
109 return false;
110 }
111 if (stat->type != SourceAccessor::tSymlink) {
112 *result_listener << arg->showPath(path) << " is not a symlink";
113 return false;
114 }
115 auto actual = arg->readLink(path);
116 if (actual != target) {
117 *result_listener << arg->showPath(path) << " points to " << ::testing::PrintToString(actual);
118 return false;
119 }
120 return true;
121}
122
123MATCHER_P2(HasDirectory, path, dirents, "")
124{
125 auto stat = arg->maybeLstat(path);
126 if (!stat) {
127 *result_listener << arg->showPath(path) << " does not exist";
128 return false;
129 }
130 if (stat->type != SourceAccessor::tDirectory) {
131 *result_listener << arg->showPath(path) << " is not a directory";
132 return false;
133 }
134 auto actual = arg->readDirectory(path);
135 std::set<std::string> actualKeys, expectedKeys(dirents.begin(), dirents.end());
136 for (auto & [k, _] : actual)
137 actualKeys.insert(k);
138 if (actualKeys != expectedKeys) {
139 *result_listener << arg->showPath(path) << " has entries " << ::testing::PrintToString(actualKeys);
140 return false;
141 }
142 return true;
143}
144
145#ifdef _WIN32
153inline auto ThrowsWinError(DWORD expected)
154{
155 return ::testing::Throws<windows::WinError>(::testing::Field(&windows::WinError::lastError, expected));
156}
157
158#endif
159
160} // namespace nix::testing
This file defines two main structs/classes used in nix error handling.
auto ThrowsSysError(int expected)
Definition gmock-matchers.hh:66
auto ThrowsSystemError(std::errc expected)
Definition gmock-matchers.hh:79
Errors from the operating system: SystemError, and its subclasses for the native error codes,...
std::string stripANSIEscapes(std::string_view s)
Definition terminal.cc:185