-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Trompeloeil C++ mocking framework | ||
* | ||
* Copyright (C) Björn Fahller | ||
* | ||
* Use, modification and distribution is subject to the | ||
* Boost Software License, Version 1.0. (See accompanying | ||
* file LICENSE_1_0.txt or copy atl | ||
* http://www.boost.org/LICENSE_1_0.txt) | ||
* | ||
* Project home: https://github.com/rollbear/trompeloeil | ||
*/ | ||
|
||
#ifndef TROMPELOEIL_SET_PREDICATE_HPP | ||
#define TROMPELOEIL_SET_PREDICATE_HPP | ||
|
||
#ifndef TROMPELOEIL_MATCHER_HPP | ||
#include "../matcher.hpp" | ||
#endif | ||
|
||
namespace trompeloeil { | ||
namespace impl { | ||
struct any_of_checker | ||
{ | ||
template <typename T, typename ... Cs> | ||
bool operator()(const T& t, const Cs& ... compare) const | ||
{ | ||
bool any_true = false; | ||
trompeloeil::ignore(std::initializer_list<bool>{ | ||
(any_true = any_true || trompeloeil::param_matches(compare, std::ref(t)))... | ||
}); | ||
return any_true; | ||
} | ||
}; | ||
struct any_of_printer | ||
{ | ||
template <typename ... Cs> | ||
void operator()(std::ostream& os, const Cs& ... compare) const | ||
{ | ||
os << " to be any of {"; | ||
const char* sep = " "; | ||
const auto print = [&](const auto& v) { | ||
os << std::exchange(sep, ", ") << v; | ||
return 0; | ||
}; | ||
trompeloeil::ignore(std::initializer_list<int>{ | ||
(print(compare))... | ||
}); | ||
os << " }"; | ||
} | ||
}; | ||
} | ||
|
||
template < | ||
typename Type = trompeloeil::wildcard, | ||
typename ... Cs, | ||
typename R = make_matcher_return< | ||
Type, | ||
impl::any_of_checker, | ||
impl::any_of_printer, | ||
Cs... | ||
> | ||
> | ||
R any_of(Cs&& ... cs) | ||
{ | ||
return trompeloeil::make_matcher<Type>( | ||
impl::any_of_checker{}, | ||
impl::any_of_printer{}, | ||
std::forward<Cs>(cs)...); | ||
} | ||
|
||
namespace impl { | ||
struct none_of_checker | ||
{ | ||
template <typename T, typename ... Cs> | ||
bool operator()(const T& t, const Cs& ... compare) const | ||
{ | ||
bool any_true = false; | ||
trompeloeil::ignore(std::initializer_list<bool>{ | ||
(any_true = any_true || trompeloeil::param_matches(compare, std::ref(t)))... | ||
}); | ||
return !any_true; | ||
} | ||
}; | ||
struct none_of_printer | ||
{ | ||
template <typename ... Cs> | ||
void operator()(std::ostream& os, const Cs& ... compare) const | ||
{ | ||
os << " to be none of {"; | ||
const char* sep = " "; | ||
const auto print = [&](const auto& v) { | ||
os << std::exchange(sep, ", ") << v; | ||
return 0; | ||
}; | ||
trompeloeil::ignore(std::initializer_list<int>{ | ||
(print(compare))... | ||
}); | ||
os << " }"; | ||
} | ||
}; | ||
} | ||
|
||
template < | ||
typename Type = trompeloeil::wildcard, | ||
typename ... Cs, | ||
typename R = make_matcher_return< | ||
Type, | ||
impl::none_of_checker, | ||
impl::none_of_printer, | ||
Cs... | ||
> | ||
> | ||
R none_of(Cs&& ... cs) | ||
{ | ||
return trompeloeil::make_matcher<Type>( | ||
impl::none_of_checker{}, | ||
impl::none_of_printer{}, | ||
std::forward<Cs>(cs)...); | ||
} | ||
|
||
namespace impl { | ||
struct all_of_checker | ||
{ | ||
template <typename T, typename ... Cs> | ||
bool operator()(const T& t, const Cs& ... compare) const | ||
{ | ||
bool all_true = true; | ||
trompeloeil::ignore(std::initializer_list<bool>{ | ||
(all_true = all_true && trompeloeil::param_matches(compare, std::ref(t)))... | ||
}); | ||
return all_true; | ||
} | ||
}; | ||
struct all_of_printer | ||
{ | ||
template <typename ... Cs> | ||
void operator()(std::ostream& os, const Cs& ... compare) const | ||
{ | ||
os << " to be all of {"; | ||
const char* sep = " "; | ||
const auto print = [&](const auto& v) { | ||
os << std::exchange(sep, ", ") << v; | ||
return 0; | ||
}; | ||
trompeloeil::ignore(std::initializer_list<int>{ | ||
(print(compare))... | ||
}); | ||
os << " }"; | ||
} | ||
}; | ||
} | ||
|
||
template < | ||
typename Type = trompeloeil::wildcard, | ||
typename ... Cs, | ||
typename R = make_matcher_return< | ||
Type, | ||
impl::all_of_checker, | ||
impl::all_of_printer, | ||
Cs... | ||
> | ||
> | ||
R all_of(Cs&& ... cs) | ||
{ | ||
return trompeloeil::make_matcher<Type>( | ||
impl::all_of_checker{}, | ||
impl::all_of_printer{}, | ||
std::forward<Cs>(cs)...); | ||
} | ||
|
||
} | ||
#endif // TROMPELOEIL_SET_PREDICATE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters