2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_UTIL_STATUS_MATCHERS_H_
|
|
|
|
#define SANDBOXED_API_UTIL_STATUS_MATCHERS_H_
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
2019-04-24 01:29:37 +08:00
|
|
|
#include <type_traits>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <utility>
|
2019-04-24 01:29:37 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "gmock/gmock.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "gtest/gtest.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-03 22:40:09 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/types/optional.h"
|
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
2022-03-17 15:13:21 +08:00
|
|
|
#define SAPI_ASSERT_OK(expr) ASSERT_THAT(expr, ::sapi::IsOk())
|
2022-03-16 21:28:30 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#define SAPI_ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN_IMPL( \
|
|
|
|
SAPI_MACROS_IMPL_CONCAT(_sapi_statusor, __LINE__), lhs, rexpr)
|
|
|
|
|
|
|
|
#define SAPI_ASSERT_OK_AND_ASSIGN_IMPL(statusor, lhs, rexpr) \
|
|
|
|
auto statusor = (rexpr); \
|
2020-04-02 22:42:17 +08:00
|
|
|
ASSERT_THAT(statusor.status(), ::sapi::IsOk()); \
|
2022-04-04 15:59:25 +08:00
|
|
|
lhs = std::move(statusor).value()
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class IsOkMatcher {
|
|
|
|
public:
|
|
|
|
template <typename StatusT>
|
|
|
|
bool MatchAndExplain(const StatusT& status_container,
|
|
|
|
::testing::MatchResultListener* listener) const {
|
|
|
|
if (!status_container.ok()) {
|
|
|
|
*listener << "which is not OK";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeTo(std::ostream* os) const { *os << "is OK"; }
|
|
|
|
|
|
|
|
void DescribeNegationTo(std::ostream* os) const { *os << "is not OK"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class StatusIsMatcher {
|
|
|
|
public:
|
|
|
|
StatusIsMatcher(const StatusIsMatcher&) = default;
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIsMatcher(absl::StatusCode code,
|
|
|
|
absl::optional<absl::string_view> message)
|
|
|
|
: code_(code), message_(message) {}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2019-04-24 01:29:37 +08:00
|
|
|
template <typename T>
|
|
|
|
bool MatchAndExplain(const T& value,
|
2019-03-19 00:21:48 +08:00
|
|
|
::testing::MatchResultListener* listener) const {
|
2019-04-24 01:29:37 +08:00
|
|
|
auto status = GetStatus(value);
|
2019-03-19 00:21:48 +08:00
|
|
|
if (code_ != status.code()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
*listener << "whose error code is "
|
|
|
|
<< absl::StatusCodeToString(status.code());
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
if (message_.has_value() && status.message() != message_.value()) {
|
2019-03-19 00:21:48 +08:00
|
|
|
*listener << "whose error message is '" << message_.value() << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeTo(std::ostream* os) const {
|
2020-02-28 01:23:44 +08:00
|
|
|
*os << "has a status code that is " << absl::StatusCodeToString(code_);
|
2019-03-19 00:21:48 +08:00
|
|
|
if (message_.has_value()) {
|
|
|
|
*os << ", and has an error message that is '" << message_.value() << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeNegationTo(std::ostream* os) const {
|
2020-02-28 01:23:44 +08:00
|
|
|
*os << "has a status code that is not " << absl::StatusCodeToString(code_);
|
2019-03-19 00:21:48 +08:00
|
|
|
if (message_.has_value()) {
|
|
|
|
*os << ", and has an error message that is not '" << message_.value()
|
|
|
|
<< "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-04-24 01:29:37 +08:00
|
|
|
template <typename StatusT,
|
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_void<decltype(std::declval<StatusT>().code())>::value,
|
|
|
|
int>::type = 0>
|
|
|
|
static const StatusT& GetStatus(const StatusT& status) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename StatusOrT,
|
|
|
|
typename StatusT = decltype(std::declval<StatusOrT>().status())>
|
|
|
|
static StatusT GetStatus(const StatusOrT& status_or) {
|
|
|
|
return status_or.status();
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
const absl::StatusCode code_;
|
2019-03-19 00:21:48 +08:00
|
|
|
const absl::optional<std::string> message_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
inline ::testing::PolymorphicMatcher<internal::IsOkMatcher> IsOk() {
|
|
|
|
return ::testing::MakePolymorphicMatcher(internal::IsOkMatcher{});
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
inline ::testing::PolymorphicMatcher<internal::StatusIsMatcher> StatusIs(
|
|
|
|
absl::StatusCode code,
|
|
|
|
absl::optional<absl::string_view> message = absl::nullopt) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return ::testing::MakePolymorphicMatcher(
|
2020-02-28 01:23:44 +08:00
|
|
|
internal::StatusIsMatcher(code, message));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sapi
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_UTIL_STATUS_MATCHERS_H_
|