Implements matcher SizeIs().
This commit is contained in:
@@ -1972,6 +1972,58 @@ class ResultOfMatcher {
|
||||
GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
|
||||
};
|
||||
|
||||
// Implements a matcher that checks the size of an STL-style container.
|
||||
template <typename SizeMatcher>
|
||||
class SizeIsMatcher {
|
||||
public:
|
||||
explicit SizeIsMatcher(const SizeMatcher& size_matcher)
|
||||
: size_matcher_(size_matcher) {
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
operator Matcher<Container>() const {
|
||||
return MakeMatcher(new Impl<Container>(size_matcher_));
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
class Impl : public MatcherInterface<Container> {
|
||||
public:
|
||||
typedef internal::StlContainerView<
|
||||
GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
|
||||
typedef typename ContainerView::type::size_type SizeType;
|
||||
explicit Impl(const SizeMatcher& size_matcher)
|
||||
: size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
|
||||
|
||||
virtual void DescribeTo(::std::ostream* os) const {
|
||||
*os << "size ";
|
||||
size_matcher_.DescribeTo(os);
|
||||
}
|
||||
virtual void DescribeNegationTo(::std::ostream* os) const {
|
||||
*os << "size ";
|
||||
size_matcher_.DescribeNegationTo(os);
|
||||
}
|
||||
|
||||
virtual bool MatchAndExplain(Container container,
|
||||
MatchResultListener* listener) const {
|
||||
SizeType size = container.size();
|
||||
StringMatchResultListener size_listener;
|
||||
const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
|
||||
*listener
|
||||
<< "whose size " << size << (result ? " matches" : " doesn't match");
|
||||
PrintIfNotEmpty(size_listener.str(), listener->stream());
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
const Matcher<SizeType> size_matcher_;
|
||||
GTEST_DISALLOW_ASSIGN_(Impl);
|
||||
};
|
||||
|
||||
private:
|
||||
const SizeMatcher size_matcher_;
|
||||
GTEST_DISALLOW_ASSIGN_(SizeIsMatcher);
|
||||
};
|
||||
|
||||
// Implements an equality matcher for any STL-style container whose elements
|
||||
// support ==. This matcher is like Eq(), but its failure explanations provide
|
||||
// more detailed information that is useful when the container is used as a set.
|
||||
@@ -3079,6 +3131,18 @@ Truly(Predicate pred) {
|
||||
return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
|
||||
}
|
||||
|
||||
// Returns a matcher that matches the container size. The container must
|
||||
// support both size() and size_type which all STL-like containers provide.
|
||||
// Note that the parameter 'size' can be a value of type size_type as well as
|
||||
// matcher. For instance:
|
||||
// EXPECT_THAT(container, SizeIs(2)); // Checks container has 2 elements.
|
||||
// EXPECT_THAT(container, SizeIs(Le(2)); // Checks container has at most 2.
|
||||
template <typename SizeMatcher>
|
||||
inline internal::SizeIsMatcher<SizeMatcher>
|
||||
SizeIs(const SizeMatcher& size_matcher) {
|
||||
return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
|
||||
}
|
||||
|
||||
// Returns a matcher that matches an equal container.
|
||||
// This matcher behaves like Eq(), but in the event of mismatch lists the
|
||||
// values that are included in one container but not the other. (Duplicate
|
||||
|
||||
Reference in New Issue
Block a user