Android-cuttlefish cvd tool
result_matchers.h
Go to the documentation of this file.
1//
2// Copyright (C) 2022 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#pragma once
17
18#include <type_traits>
19
20#include "gmock/gmock.h"
21#include "tl/expected.hpp"
22
24
25namespace cuttlefish {
26
27MATCHER(IsOk, "an ok result") {
28 auto& result = arg;
29 if (!result.has_value()) {
30 *result_listener << "which is an error result with trace: "
31 << result.error();
32 return false;
33 }
34 return true;
35}
36
37MATCHER(IsError, "an error result") {
38 auto& result = arg;
39 if (result.has_value()) {
40 *result_listener << "which is an ok result";
41 return false;
42 }
43 return true;
44}
45
46MATCHER_P(IsOkAndValue, result_value_matcher, "") {
47 auto get_value = [](const auto& res) -> auto { return res.value(); };
48 return ExplainMatchResult(
49 ::testing::AllOf(IsOk(), ::testing::ResultOf("value", get_value,
50 result_value_matcher)),
51 arg, result_listener);
52}
53
54MATCHER_P(IsErrorAndMessage, message_matcher, "") {
55 auto get_error = [](const auto& res) -> auto { return res.error(); };
56 return ExplainMatchResult(
57 ::testing::AllOf(
58 IsError(),
59 ::testing::ResultOf(
60 "error", get_error,
61 ::testing::Property(&StackTraceError::Message, message_matcher))),
62 arg, result_listener);
63}
64
65} // namespace cuttlefish
std::string Message() const
Definition: error_type.cc:377
Definition: alloc_driver.h:20
MATCHER(IsOk, "an ok result")
Definition: result_matchers.h:27
MATCHER_P(IsOkAndValue, result_value_matcher, "")
Definition: result_matchers.h:46