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 <android-base/expected.h> 21 #include <gmock/gmock.h> 22 23 #include "common/libs/utils/result.h" 24 25 namespace cuttlefish { 26 27 MATCHER(IsOk, "an ok result") { 28 auto& result = arg; 29 if (!result.ok()) { 30 *result_listener << "which is an error result with trace: " 31 << result.error().Trace(); 32 return false; 33 } 34 return true; 35 } 36 37 MATCHER(IsError, "an error result") { 38 auto& result = arg; 39 if (result.ok()) { 40 *result_listener << "which is an ok result"; 41 return false; 42 } 43 return true; 44 } 45 46 MATCHER_P(IsOkAndValue, result_value_matcher, "") { 47 auto& result = arg; 48 using ResultType = std::decay_t<decltype(result)>; 49 return ExplainMatchResult( 50 ::testing::AllOf(IsOk(), ::testing::Property("value", &ResultType::value, 51 result_value_matcher)), 52 result, result_listener); 53 } 54 55 MATCHER_P(IsErrorAndMessage, message_matcher, "") { 56 auto& result = arg; 57 using ResultType = std::decay_t<decltype(result)>; 58 return ExplainMatchResult( 59 ::testing::AllOf( 60 IsError(), 61 ::testing::Property( 62 "error", &ResultType::error, 63 ::testing::Property(&StackTraceError::Message, message_matcher))), 64 result, result_listener); 65 } 66 67 } // namespace cuttlefish 68