|
| 1 | +/* Copyright 2017 The Abseil Authors & TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#ifndef GOOGLETEST_WRAPPER_GMOCK_GMOCK_H_ |
| 17 | +#define GOOGLETEST_WRAPPER_GMOCK_GMOCK_H_ |
| 18 | + |
| 19 | +// gmock/gmock.h wrapper that also provides assert macros. |
| 20 | +// |
| 21 | +// These already exist in internal version of gmock, but upstream version |
| 22 | +// doesn't have them. We use this wrapper to make dependency translation when |
| 23 | +// exporting to OSS easier. |
| 24 | +// |
| 25 | +// - We want to use standard internal header and ASSERT_OK, EXPECT_OK macros |
| 26 | +// when developing internally. |
| 27 | +// - We want the same macros to work externally, rather than having to add or |
| 28 | +// strip TF_ prefix. |
| 29 | +// - We want the OSS export to still work after the export and header |
| 30 | +// translation. |
| 31 | +// - We want to minimize the amount of patching third party projects to reduce |
| 32 | +// maintenance overhead. |
| 33 | +// - To ensure the OSS patches cleanly apply onto internal repo, we need the |
| 34 | +// header translation to be reversible, which requires 1:1 header mapping. |
| 35 | +// |
| 36 | +// To achieve this, we swap out gmock.h for this wrapper in all XLA code, which |
| 37 | +// should (TM) make ASSERT_OK/EXPECT_OK "just work" in all XLA tests. |
| 38 | +// |
| 39 | +// The only way to make this work without patching googletest and/or absl is to |
| 40 | +// make XLA *always* use this wrapper, and *never* directly depend on upstream |
| 41 | +// googletest. |
| 42 | +// |
| 43 | +// absl/status/status_matchers.h depends on gmock.h, so we can't simply add it |
| 44 | +// here. This causes either: |
| 45 | +// |
| 46 | +// - A circular dependency between this and absl - which bazel doesn't allow, |
| 47 | +// - absl dependency on the upstream gmock - which depending on the dependency |
| 48 | +// graph structure may introduce upstream gmock include path *before* one |
| 49 | +// defined in here, so we end up with *sometimes* including the wrong one and |
| 50 | +// the entire idea of drop-in replacing gmock.h goes out of the window. |
| 51 | + |
| 52 | +#include_next "gmock/gmock.h" |
| 53 | + |
| 54 | +#include "absl/status/status.h" |
| 55 | +#include "absl/status/statusor.h" |
| 56 | + |
| 57 | +namespace xla_testing { |
| 58 | + |
| 59 | +// Macros for testing the results of functions that return absl::Status or |
| 60 | +// absl::StatusOr<T> (for any type T). |
| 61 | +#define EXPECT_OK(expression) EXPECT_THAT(expression, ::xla_testing::IsOk()) |
| 62 | +#define ASSERT_OK(expression) ASSERT_THAT(expression, ::xla_testing::IsOk()) |
| 63 | + |
| 64 | +#define ASSERT_OK_AND_ASSIGN(lhs, rexpr) \ |
| 65 | + TF_ASSERT_OK_AND_ASSIGN_IMPL( \ |
| 66 | + XLA_STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), \ |
| 67 | + lhs, rexpr); |
| 68 | + |
| 69 | +#define ASSERT_OK_AND_ASSIGN_IMPL(statusor, lhs, rexpr) \ |
| 70 | + auto statusor = (rexpr); \ |
| 71 | + ASSERT_OK(statusor.status()); \ |
| 72 | + lhs = std::move(statusor).value() |
| 73 | + |
| 74 | +#define XLA_STATUS_MACROS_CONCAT_NAME(x, y) XLA_STATUS_MACROS_CONCAT_IMPL(x, y) |
| 75 | +#define XLA_STATUS_MACROS_CONCAT_IMPL(x, y) x##y |
| 76 | + |
| 77 | + |
| 78 | +namespace internal { |
| 79 | + |
| 80 | +inline const absl::Status& GetStatus(const absl::Status& status) { |
| 81 | + return status; |
| 82 | +} |
| 83 | + |
| 84 | +template <typename T> |
| 85 | +inline const absl::Status& GetStatus(const absl::StatusOr<T>& status) { |
| 86 | + return status.status(); |
| 87 | +} |
| 88 | + |
| 89 | +// Monomorphic implementation of matcher IsOk() for a given type T. |
| 90 | +// T can be Status, StatusOr<>, or a reference to either of them. |
| 91 | +template <typename T> |
| 92 | +class MonoIsOkMatcherImpl : public ::testing::MatcherInterface<T> { |
| 93 | + public: |
| 94 | + void DescribeTo(std::ostream* os) const override { *os << "is OK"; } |
| 95 | + void DescribeNegationTo(std::ostream* os) const override { |
| 96 | + *os << "is not OK"; |
| 97 | + } |
| 98 | + bool MatchAndExplain(T actual_value, |
| 99 | + ::testing::MatchResultListener*) const override { |
| 100 | + return GetStatus(actual_value).ok(); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +// Implements IsOk() as a polymorphic matcher. |
| 105 | +class IsOkMatcher { |
| 106 | + public: |
| 107 | + template <typename T> |
| 108 | + /*implicit*/ operator ::testing::Matcher<T>() const { // NOLINT |
| 109 | + return ::testing::Matcher<T>(new MonoIsOkMatcherImpl<const T&>()); |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +} // namespace internal |
| 114 | + |
| 115 | +// Returns a gMock matcher that matches a Status or StatusOr<> which is OK. |
| 116 | +inline ::xla_testing::internal::IsOkMatcher IsOk() { |
| 117 | + return ::xla_testing::internal::IsOkMatcher(); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace xla_testing |
| 121 | + |
| 122 | +#endif // GOOGLETEST_WRAPPER_GMOCK_GMOCK_H_ |
0 commit comments