Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions clang-tools-extra/clangd/unittests/Matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,74 +127,6 @@ PolySubsequenceMatcher<Args...> HasSubsequence(Args &&... M) {
llvm::consumeError(ComputedValue.takeError()); \
} while (false)

// Implements the HasValue(m) matcher for matching an Optional whose
// value matches matcher m.
template <typename InnerMatcher> class OptionalMatcher {
public:
explicit OptionalMatcher(const InnerMatcher &matcher) : matcher_(matcher) {}
OptionalMatcher(const OptionalMatcher&) = default;
OptionalMatcher &operator=(const OptionalMatcher&) = delete;

// This type conversion operator template allows Optional(m) to be
// used as a matcher for any Optional type whose value type is
// compatible with the inner matcher.
//
// The reason we do this instead of relying on
// MakePolymorphicMatcher() is that the latter is not flexible
// enough for implementing the DescribeTo() method of Optional().
template <typename Optional> operator Matcher<Optional>() const {
return MakeMatcher(new Impl<Optional>(matcher_));
}

private:
// The monomorphic implementation that works for a particular optional type.
template <typename Optional>
class Impl : public ::testing::MatcherInterface<Optional> {
public:
using Value = typename std::remove_const<
typename std::remove_reference<Optional>::type>::type::value_type;

explicit Impl(const InnerMatcher &matcher)
: matcher_(::testing::MatcherCast<const Value &>(matcher)) {}

Impl(const Impl&) = default;
Impl &operator=(const Impl&) = delete;

virtual void DescribeTo(::std::ostream *os) const {
*os << "has a value that ";
matcher_.DescribeTo(os);
}

virtual void DescribeNegationTo(::std::ostream *os) const {
*os << "does not have a value that ";
matcher_.DescribeTo(os);
}

virtual bool
MatchAndExplain(Optional optional,
::testing::MatchResultListener *listener) const {
if (!optional)
return false;

*listener << "which has a value ";
return MatchPrintAndExplain(*optional, matcher_, listener);
}

private:
const Matcher<const Value &> matcher_;
};

const InnerMatcher matcher_;
};

// Creates a matcher that matches an Optional that has a value
// that matches inner_matcher.
template <typename InnerMatcher>
inline OptionalMatcher<InnerMatcher>
HasValue(const InnerMatcher &inner_matcher) {
return OptionalMatcher<InnerMatcher>(inner_matcher);
}

} // namespace clangd
} // namespace clang
#endif
11 changes: 6 additions & 5 deletions clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Matcher;
using ::testing::Optional;
using ::testing::SizeIs;
using ::testing::UnorderedElementsAre;

Expand All @@ -38,12 +39,12 @@ MATCHER_P(selectionRangeIs, R, "") { return arg.selectionRange == R; }
template <class... ParentMatchers>
::testing::Matcher<TypeHierarchyItem> parents(ParentMatchers... ParentsM) {
return Field(&TypeHierarchyItem::parents,
HasValue(UnorderedElementsAre(ParentsM...)));
Optional(UnorderedElementsAre(ParentsM...)));
}
template <class... ChildMatchers>
::testing::Matcher<TypeHierarchyItem> children(ChildMatchers... ChildrenM) {
return Field(&TypeHierarchyItem::children,
HasValue(UnorderedElementsAre(ChildrenM...)));
Optional(UnorderedElementsAre(ChildrenM...)));
}
// Note: "not resolved" is different from "resolved but empty"!
MATCHER(parentsNotResolved, "") { return !arg.parents; }
Expand Down Expand Up @@ -790,7 +791,7 @@ struct Child : Parent1, Parent2 {};
Children,
UnorderedElementsAre(
AllOf(withName("Child"),
withResolveParents(HasValue(UnorderedElementsAre(withResolveID(
withResolveParents(Optional(UnorderedElementsAre(withResolveID(
getSymbolID(&findDecl(AST, "Parent1")).str())))))));
}

Expand All @@ -810,9 +811,9 @@ struct Chil^d : Parent {};
ASSERT_THAT(Result, SizeIs(1));
auto Parents = superTypes(Result.front(), Index.get());

EXPECT_THAT(Parents, HasValue(UnorderedElementsAre(
EXPECT_THAT(Parents, Optional(UnorderedElementsAre(
AllOf(withName("Parent"),
withResolveParents(HasValue(IsEmpty()))))));
withResolveParents(Optional(IsEmpty()))))));
}
} // namespace
} // namespace clangd
Expand Down
Loading