1 //===- Testing/Support/SupportHelpers.h -----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
10 #define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/raw_os_ostream.h"
16 #include "gmock/gmock-matchers.h"
17 #include "gtest/gtest-printers.h"
24 std::vector
<std::shared_ptr
<ErrorInfoBase
>> Infos
;
26 bool Success() const { return Infos
.empty(); }
29 template <typename T
> struct ExpectedHolder
: public ErrorHolder
{
30 ExpectedHolder(ErrorHolder Err
, Expected
<T
> &Exp
)
31 : ErrorHolder(std::move(Err
)), Exp(Exp
) {}
36 inline void PrintTo(const ErrorHolder
&Err
, std::ostream
*Out
) {
37 raw_os_ostream
OS(*Out
);
38 OS
<< (Err
.Success() ? "succeeded" : "failed");
40 const char *Delim
= " (";
41 for (const auto &Info
: Err
.Infos
) {
51 void PrintTo(const ExpectedHolder
<T
> &Item
, std::ostream
*Out
) {
53 *Out
<< "succeeded with value " << ::testing::PrintToString(*Item
.Exp
);
55 PrintTo(static_cast<const ErrorHolder
&>(Item
), Out
);
59 template <class InnerMatcher
> class ValueIsMatcher
{
61 explicit ValueIsMatcher(InnerMatcher ValueMatcher
)
62 : ValueMatcher(ValueMatcher
) {}
65 operator ::testing::Matcher
<const llvm::Optional
<T
> &>() const {
66 return ::testing::MakeMatcher(
67 new Impl
<T
>(::testing::SafeMatcherCast
<T
>(ValueMatcher
)));
71 class Impl
: public ::testing::MatcherInterface
<const llvm::Optional
<T
> &> {
73 explicit Impl(const ::testing::Matcher
<T
> &ValueMatcher
)
74 : ValueMatcher(ValueMatcher
) {}
76 bool MatchAndExplain(const llvm::Optional
<T
> &Input
,
77 testing::MatchResultListener
*L
) const override
{
78 return Input
&& ValueMatcher
.MatchAndExplain(Input
.getValue(), L
);
81 void DescribeTo(std::ostream
*OS
) const override
{
82 *OS
<< "has a value that ";
83 ValueMatcher
.DescribeTo(OS
);
85 void DescribeNegationTo(std::ostream
*OS
) const override
{
86 *OS
<< "does not have a value that ";
87 ValueMatcher
.DescribeTo(OS
);
91 testing::Matcher
<T
> ValueMatcher
;
95 InnerMatcher ValueMatcher
;
99 /// Matches an llvm::Optional<T> with a value that conforms to an inner matcher.
100 /// To match llvm::None you could use Eq(llvm::None).
101 template <class InnerMatcher
>
102 detail::ValueIsMatcher
<InnerMatcher
> ValueIs(const InnerMatcher
&ValueMatcher
) {
103 return detail::ValueIsMatcher
<InnerMatcher
>(ValueMatcher
);
106 SmallString
<128> getInputFileDirectory(const char *Argv0
);
107 } // namespace unittest