• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #include <android-base/result.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 /*
23  * Matchers for android::base::Result<T> that produces human-readable test results.
24  *
25  * Example:
26  *
27  * Result<int> result = ...
28  *
29  * using namespace ::android::base::testing;
30  * using namespace ::testing;
31  *
32  * EXPECT_THAT(result, Ok());
33  * EXPECT_THAT(result, Not(Ok()));
34  * EXPECT_THAT(result, HasValue(5));
35  * EXPECT_THAT(result, HasError(WithCode(EBADF)));
36  * EXPECT_THAT(result, HasError(WithMessageMessage("expected error message")));
37  *
38  * // Advance usage
39  * EXPECT_THAT(result, AnyOf(Ok(), HasError(WithCode(EACCES)));
40  * EXPECT_THAT(result, HasError(WithCode(AnyOf(EBADF, EACCES))) << "Unexpected code from library";
41  */
42 
43 namespace android::base {
44 
45 template <typename T>
PrintTo(const Result<T> & result,std::ostream * os)46 inline void PrintTo(const Result<T>& result, std::ostream* os) {
47   if (result.ok()) {
48     *os << "OK: " << ::testing::PrintToString(result.value());
49   } else {
50     *os << "Error: " << result.error();
51   }
52 }
53 
54 template <>
PrintTo(const Result<void> & result,std::ostream * os)55 inline void PrintTo(const Result<void>& result, std::ostream* os) {
56   if (result.ok()) {
57     *os << "OK";
58   } else {
59     *os << "Error: " << result.error();
60   }
61 }
62 
63 namespace testing {
64 
65 MATCHER(Ok, "") {
66   if (arg.ok()) {
67     *result_listener << "result is OK";
68     return true;
69   }
70   *result_listener << "error is " << arg.error();
71   return false;
72 }
73 
74 MATCHER_P(HasValue, value_matcher, "") {
75   if (arg.ok()) {
76     return ::testing::ExplainMatchResult(value_matcher, arg.value(), result_listener);
77   }
78   *result_listener << "error is " << arg.error();
79   return false;
80 }
81 
82 MATCHER_P(HasError, error_matcher, "") {
83   if (!arg.ok()) {
84     return ::testing::ExplainMatchResult(error_matcher, arg.error(), result_listener);
85   }
86   *result_listener << "result is OK";
87   return false;
88 }
89 
90 MATCHER_P(WithCode, code_matcher, "") {
91   *result_listener << "actual error is " << arg;
92   return ::testing::ExplainMatchResult(code_matcher, arg.code(), result_listener);
93 }
94 
95 MATCHER_P(WithMessage, message_matcher, "") {
96   *result_listener << "actual error is " << arg;
97   return ::testing::ExplainMatchResult(message_matcher, arg.message(), result_listener);
98 }
99 
100 }  // namespace testing
101 }  // namespace android::base
102