1 /*
2 * Copyright (C) 2023 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 #ifndef SRC_BASE_TEST_STATUS_MATCHERS_H_
18 #define SRC_BASE_TEST_STATUS_MATCHERS_H_
19
20 #include <ostream>
21
22 #include "perfetto/base/status.h"
23 #include "perfetto/ext/base/status_or.h"
24 #include "test/gtest_and_gmock.h"
25
26 namespace perfetto::base {
27 namespace gtest_matchers {
28
29 // Returns a gMock matcher that matches a Status or StatusOr<> which is OK.
30 MATCHER(IsOk, negation ? "is not OK" : "is OK") {
31 return arg.ok();
32 }
33
34 // Returns a gMock matcher that matches a Status or StatusOr<> which is an
35 // error.
36 MATCHER(IsError, negation ? "is not error" : "is error") {
37 return !arg.ok();
38 }
39
40 // Macros for testing the results of function returning base::Status*.
41 #define PERFETTO_TEST_STATUS_MATCHER_CONCAT(x, y) x##y
42 #define PERFETTO_TEST_STATUS_MATCHER_CONCAT2(x, y) \
43 PERFETTO_TEST_STATUS_MATCHER_CONCAT(x, y)
44
45 // Macros for testing the results of functions that return base::Status or
46 // base::StatusOr<T> (for any type T).
47 #define EXPECT_OK(expression) \
48 EXPECT_THAT(expression, ::perfetto::base::gtest_matchers::IsOk())
49 #define ASSERT_OK(expression) \
50 ASSERT_THAT(expression, ::perfetto::base::gtest_matchers::IsOk())
51
52 #define ASSERT_OK_AND_ASSIGN(lhs, rhs) \
53 PERFETTO_TEST_STATUS_MATCHER_CONCAT2(auto status_or, __LINE__) = rhs; \
54 lhs = PERFETTO_TEST_STATUS_MATCHER_CONCAT2(status_or, __LINE__).ok() \
55 ? std::move( \
56 PERFETTO_TEST_STATUS_MATCHER_CONCAT2(status_or, __LINE__) \
57 .value()) \
58 : decltype(rhs)::value_type{}; \
59 ASSERT_OK(PERFETTO_TEST_STATUS_MATCHER_CONCAT2(status_or, __LINE__).status())
60
61 } // namespace gtest_matchers
62
63 // Add a |PrintTo| function to allow easily determining what the cause of the
64 // failure is.
PrintTo(const Status & status,std::ostream * os)65 inline void PrintTo(const Status& status, std::ostream* os) {
66 if (status.ok()) {
67 *os << "OK";
68 } else {
69 *os << "Error(message=" << status.message() << ")";
70 }
71 }
72 template <typename T>
PrintTo(const StatusOr<T> & status,std::ostream * os)73 inline void PrintTo(const StatusOr<T>& status, std::ostream* os) {
74 if (status.ok()) {
75 *os << testing::PrintToString(status.value());
76 } else {
77 *os << "Error(message=" << status.status().message() << ")";
78 }
79 }
80
81 } // namespace perfetto::base
82
83 #endif // SRC_BASE_TEST_STATUS_MATCHERS_H_
84