Rename Tf{StatusIs,IsOkAndHolds} to {StatusIs,IsOkAndHolds}.
I used TfStatusIs following TF_EXPECT_OK. On a second thought, I think
the macro has the prefix because it's defined in the global namespace.
StatusIs is defined in the tensorflow namespace, so it doesn't need to
be called "Tf*".
Similarly, `tensorflow::Status` is called `Status` not `TfStatus`.
Also added unit tests to test failure messages.
PiperOrigin-RevId: 381145621
Change-Id: I22c71a1e7fc6bc708db318b9dd902da41b76ed78
diff --git a/tensorflow/core/data/service/data_service_test.cc b/tensorflow/core/data/service/data_service_test.cc
index 6c93526..107e3ba 100644
--- a/tensorflow/core/data/service/data_service_test.cc
+++ b/tensorflow/core/data/service/data_service_test.cc
@@ -49,7 +49,7 @@
TEST(DataService, ParseInvalidProcessingMode) {
ProcessingMode mode;
EXPECT_THAT(ParseProcessingMode("invalid", mode),
- testing::TfStatusIs(error::INVALID_ARGUMENT));
+ testing::StatusIs(error::INVALID_ARGUMENT));
}
TEST(DataService, ProcessingModeToString) {
@@ -61,24 +61,24 @@
TEST(DataService, ParseTargetWorkers) {
EXPECT_THAT(ParseTargetWorkers("AUTO"),
- testing::TfIsOkAndHolds(TargetWorkers::AUTO));
+ testing::IsOkAndHolds(TargetWorkers::AUTO));
EXPECT_THAT(ParseTargetWorkers("Auto"),
- testing::TfIsOkAndHolds(TargetWorkers::AUTO));
+ testing::IsOkAndHolds(TargetWorkers::AUTO));
EXPECT_THAT(ParseTargetWorkers("ANY"),
- testing::TfIsOkAndHolds(TargetWorkers::ANY));
+ testing::IsOkAndHolds(TargetWorkers::ANY));
EXPECT_THAT(ParseTargetWorkers("any"),
- testing::TfIsOkAndHolds(TargetWorkers::ANY));
+ testing::IsOkAndHolds(TargetWorkers::ANY));
EXPECT_THAT(ParseTargetWorkers("LOCAL"),
- testing::TfIsOkAndHolds(TargetWorkers::LOCAL));
+ testing::IsOkAndHolds(TargetWorkers::LOCAL));
EXPECT_THAT(ParseTargetWorkers("local"),
- testing::TfIsOkAndHolds(TargetWorkers::LOCAL));
+ testing::IsOkAndHolds(TargetWorkers::LOCAL));
EXPECT_THAT(ParseTargetWorkers(""),
- testing::TfIsOkAndHolds(TargetWorkers::AUTO));
+ testing::IsOkAndHolds(TargetWorkers::AUTO));
}
TEST(DataService, ParseInvalidTargetWorkers) {
EXPECT_THAT(ParseTargetWorkers("UNSET"),
- testing::TfStatusIs(error::INVALID_ARGUMENT));
+ testing::StatusIs(error::INVALID_ARGUMENT));
}
TEST(DataService, TargetWorkersToString) {
diff --git a/tensorflow/core/data/service/task_runner_test.cc b/tensorflow/core/data/service/task_runner_test.cc
index 6551def..8957893 100644
--- a/tensorflow/core/data/service/task_runner_test.cc
+++ b/tensorflow/core/data/service/task_runner_test.cc
@@ -141,7 +141,7 @@
for (int i = 0; i < elements.size(); ++i) {
GetElementResult result;
EXPECT_THAT(runner.GetNext(GetElementRequest(), result),
- testing::TfStatusIs(error::CANCELLED));
+ testing::StatusIs(error::CANCELLED));
}
}
@@ -163,7 +163,7 @@
for (; i < elements.size(); ++i) {
GetElementResult result;
EXPECT_THAT(runner.GetNext(GetElementRequest(), result),
- testing::TfStatusIs(error::CANCELLED));
+ testing::StatusIs(error::CANCELLED));
}
}
@@ -172,11 +172,11 @@
absl::make_unique<TestErrorIterator>(errors::Aborted("Aborted")));
GetElementResult result;
EXPECT_THAT(runner.GetNext(GetElementRequest(), result),
- testing::TfStatusIs(error::ABORTED));
+ testing::StatusIs(error::ABORTED));
EXPECT_THAT(runner.GetNext(GetElementRequest(), result),
- testing::TfStatusIs(error::ABORTED));
+ testing::StatusIs(error::ABORTED));
EXPECT_THAT(runner.GetNext(GetElementRequest(), result),
- testing::TfStatusIs(error::ABORTED));
+ testing::StatusIs(error::ABORTED));
}
class ConsumeParallelTest
diff --git a/tensorflow/core/data/service/thread_safe_buffer_test.cc b/tensorflow/core/data/service/thread_safe_buffer_test.cc
index d2a3d5f..9ef0591 100644
--- a/tensorflow/core/data/service/thread_safe_buffer_test.cc
+++ b/tensorflow/core/data/service/thread_safe_buffer_test.cc
@@ -34,7 +34,8 @@
namespace data {
namespace {
-using ::tensorflow::testing::TfIsOk;
+using ::tensorflow::testing::IsOk;
+using ::tensorflow::testing::StatusIs;
using ::testing::UnorderedElementsAreArray;
class ThreadSafeBufferTest
@@ -63,7 +64,7 @@
auto thread = absl::WrapUnique(Env::Default()->StartThread(
/*thread_options=*/{}, /*name=*/"writer_thread", [this, &buffer]() {
for (int i = 0; i < GetNumOfElements(); ++i) {
- ASSERT_THAT(buffer.Push(i), TfIsOk());
+ ASSERT_THAT(buffer.Push(i), IsOk());
}
}));
@@ -79,7 +80,7 @@
for (int i = 0; i < GetNumOfElements(); ++i) {
threads.push_back(absl::WrapUnique(Env::Default()->StartThread(
/*thread_options=*/{}, /*name=*/absl::StrCat("writer_thread_", i),
- [&buffer, i] { ASSERT_THAT(buffer.Push(i), TfIsOk()); })));
+ [&buffer, i] { ASSERT_THAT(buffer.Push(i), IsOk()); })));
}
std::vector<int> results;
@@ -107,7 +108,7 @@
}
for (int i = 0; i < GetNumOfElements(); ++i) {
- ASSERT_THAT(buffer.Push(i), TfIsOk());
+ ASSERT_THAT(buffer.Push(i), IsOk());
}
// Wait for all threads to complete.
@@ -134,7 +135,7 @@
for (int i = 0; i < GetNumOfElements(); ++i) {
threads.push_back(absl::WrapUnique(Env::Default()->StartThread(
/*thread_options=*/{}, /*name=*/absl::StrCat("writer_thread_", i),
- [&buffer, i]() { ASSERT_THAT(buffer.Push(i), TfIsOk()); })));
+ [&buffer, i]() { ASSERT_THAT(buffer.Push(i), IsOk()); })));
}
// Wait for all threads to complete.
@@ -154,27 +155,27 @@
// Pushing an element unblocks the `Pop` call.
Env::Default()->SleepForMicroseconds(10000);
- ASSERT_THAT(buffer.Push(Tensor("Test tensor")), TfIsOk());
+ ASSERT_THAT(buffer.Push(Tensor("Test tensor")), IsOk());
}
TEST_P(ThreadSafeBufferTest, BlockWriterWhenBufferIsFull) {
ThreadSafeBuffer<Tensor> buffer(GetBufferSize());
// Fills the buffer to block the next `Push` call.
for (int i = 0; i < GetBufferSize(); ++i) {
- ASSERT_THAT(buffer.Push(Tensor("Test tensor")), TfIsOk());
+ ASSERT_THAT(buffer.Push(Tensor("Test tensor")), IsOk());
}
uint64 push_time = 0;
auto thread = absl::WrapUnique(Env::Default()->StartThread(
/*thread_options=*/{}, /*name=*/"writer_thread", [&buffer, &push_time]() {
- ASSERT_THAT(buffer.Push(Tensor("Test tensor")), TfIsOk());
+ ASSERT_THAT(buffer.Push(Tensor("Test tensor")), IsOk());
push_time = Env::Default()->NowMicros();
}));
// Popping an element unblocks the `Push` call.
Env::Default()->SleepForMicroseconds(10000);
uint64 pop_time = Env::Default()->NowMicros();
- ASSERT_THAT(buffer.Pop(), TfIsOk());
+ ASSERT_THAT(buffer.Pop(), IsOk());
thread.reset();
EXPECT_LE(pop_time, push_time);
}
@@ -186,9 +187,7 @@
for (int i = 0; i < GetNumOfElements(); ++i) {
threads.push_back(absl::WrapUnique(Env::Default()->StartThread(
/*thread_options=*/{}, /*name=*/absl::StrCat("reader_thread_", i),
- [&buffer]() {
- EXPECT_THAT(buffer.Pop(), testing::TfStatusIs(error::ABORTED));
- })));
+ [&buffer]() { EXPECT_THAT(buffer.Pop(), StatusIs(error::ABORTED)); })));
}
buffer.Cancel(errors::Aborted("Aborted"));
}
@@ -197,7 +196,7 @@
ThreadSafeBuffer<Tensor> buffer(GetBufferSize());
// Fills the buffer so subsequent pushes are all cancelled.
for (int i = 0; i < GetBufferSize(); ++i) {
- ASSERT_THAT(buffer.Push(Tensor("Test tensor")), TfIsOk());
+ ASSERT_THAT(buffer.Push(Tensor("Test tensor")), IsOk());
}
std::vector<std::unique_ptr<Thread>> threads;
@@ -207,7 +206,7 @@
[&buffer]() {
for (int i = 0; i < 100; ++i) {
EXPECT_THAT(buffer.Push(Tensor("Test tensor")),
- testing::TfStatusIs(error::CANCELLED));
+ StatusIs(error::CANCELLED));
}
})));
}
@@ -217,13 +216,12 @@
TEST_P(ThreadSafeBufferTest, CancelMultipleTimes) {
ThreadSafeBuffer<Tensor> buffer(GetBufferSize());
buffer.Cancel(errors::Unknown("Unknown"));
- EXPECT_THAT(buffer.Push(Tensor("Test tensor")),
- testing::TfStatusIs(error::UNKNOWN));
+ EXPECT_THAT(buffer.Push(Tensor("Test tensor")), StatusIs(error::UNKNOWN));
buffer.Cancel(errors::DeadlineExceeded("Deadline exceeded"));
- EXPECT_THAT(buffer.Pop(), testing::TfStatusIs(error::DEADLINE_EXCEEDED));
+ EXPECT_THAT(buffer.Pop(), StatusIs(error::DEADLINE_EXCEEDED));
buffer.Cancel(errors::ResourceExhausted("Resource exhausted"));
EXPECT_THAT(buffer.Push(Tensor("Test tensor")),
- testing::TfStatusIs(error::RESOURCE_EXHAUSTED));
+ StatusIs(error::RESOURCE_EXHAUSTED));
}
} // namespace
diff --git a/tensorflow/core/data/service/worker_client_test.cc b/tensorflow/core/data/service/worker_client_test.cc
index dd4f86c..f64e9f1 100644
--- a/tensorflow/core/data/service/worker_client_test.cc
+++ b/tensorflow/core/data/service/worker_client_test.cc
@@ -137,8 +137,8 @@
LocalWorkers::Remove(GetWorkerAddress());
EXPECT_THAT(
GetElement(*client, task_id),
- testing::TfStatusIs(error::CANCELLED,
- MatchesRegex("Worker.*is no longer available.*")));
+ testing::StatusIs(error::CANCELLED,
+ MatchesRegex("Worker.*is no longer available.*")));
}
TEST_F(WorkerClientTest, LocalReadEmptyDataset) {
@@ -156,8 +156,8 @@
LocalWorkers::Remove(GetWorkerAddress());
EXPECT_THAT(
GetElement(*client, task_id),
- testing::TfStatusIs(error::CANCELLED,
- MatchesRegex("Worker.*is no longer available.*")));
+ testing::StatusIs(error::CANCELLED,
+ MatchesRegex("Worker.*is no longer available.*")));
}
TEST_F(WorkerClientTest, GrpcRead) {
@@ -193,8 +193,8 @@
test_cluster_->StopWorkers();
EXPECT_THAT(
GetElement(*client, task_id),
- testing::TfStatusIs(error::CANCELLED,
- MatchesRegex("Worker.*is no longer available.*")));
+ testing::StatusIs(error::CANCELLED,
+ MatchesRegex("Worker.*is no longer available.*")));
}
TEST_F(WorkerClientTest, CancelClient) {
@@ -206,7 +206,7 @@
client->TryCancel();
EXPECT_THAT(GetElement(*client, task_id),
- testing::TfStatusIs(
+ testing::StatusIs(
error::CANCELLED,
MatchesRegex("Client for worker.*has been cancelled.")));
}
diff --git a/tensorflow/core/lib/core/status_test_util.h b/tensorflow/core/lib/core/status_test_util.h
index 1295f1b..c50574e 100644
--- a/tensorflow/core/lib/core/status_test_util.h
+++ b/tensorflow/core/lib/core/status_test_util.h
@@ -30,6 +30,6 @@
// which conveys no more information than EXPECT_FALSE(status.ok());
// If you want to check for particular errors, a better alternative is with
// status matchers:
-// EXPECT_THAT(s, tensorflow::testing::TfStatusIs(status.code(), "message"));
+// EXPECT_THAT(s, tensorflow::testing::StatusIs(status.code(), "message"));
#endif // TENSORFLOW_CORE_LIB_CORE_STATUS_TEST_UTIL_H_
diff --git a/tensorflow/core/platform/status_matchers.cc b/tensorflow/core/platform/status_matchers.cc
index d5efc5c..a463105 100644
--- a/tensorflow/core/platform/status_matchers.cc
+++ b/tensorflow/core/platform/status_matchers.cc
@@ -25,21 +25,21 @@
namespace testing {
namespace internal_status {
-void TfStatusIsMatcherCommonImpl::DescribeTo(std::ostream* os) const {
+void StatusIsMatcherCommonImpl::DescribeTo(std::ostream* os) const {
*os << "has a status code that ";
code_matcher_.DescribeTo(os);
*os << ", and has an error message that ";
message_matcher_.DescribeTo(os);
}
-void TfStatusIsMatcherCommonImpl::DescribeNegationTo(std::ostream* os) const {
+void StatusIsMatcherCommonImpl::DescribeNegationTo(std::ostream* os) const {
*os << "has a status code that ";
code_matcher_.DescribeNegationTo(os);
*os << ", or has an error message that ";
message_matcher_.DescribeNegationTo(os);
}
-bool TfStatusIsMatcherCommonImpl::MatchAndExplain(
+bool StatusIsMatcherCommonImpl::MatchAndExplain(
const Status& status,
::testing::MatchResultListener* result_listener) const {
::testing::StringMatchResultListener inner_listener;
diff --git a/tensorflow/core/platform/status_matchers.h b/tensorflow/core/platform/status_matchers.h
index d8b7735..811b1e2 100644
--- a/tensorflow/core/platform/status_matchers.h
+++ b/tensorflow/core/platform/status_matchers.h
@@ -26,33 +26,33 @@
// Defines the following utilities:
//
-// =================
-// TfIsOkAndHolds(m)
-// =================
+// ===============
+// IsOkAndHolds(m)
+// ===============
//
// This matcher matches a StatusOr<T> value whose status is OK and whose inner
// value matches matcher m. Example:
//
-// using ::tensorflow::testing::TfIsOkAndHolds;
+// using ::tensorflow::testing::IsOkAndHolds;
// using ::testing::HasSubstr;
// ...
-// StatusOr<std::string> status_or_message = std::string("Hello, world");
-// EXPECT_THAT(status_or_message, TfIsOkAndHolds("Hello, world")));
-// EXPECT_THAT(status_or_message, TfIsOkAndHolds(HasSubstr("Hello,")));
+// StatusOr<std::string> status_or_message("Hello, world");
+// EXPECT_THAT(status_or_message, IsOkAndHolds("Hello, world")));
+// EXPECT_THAT(status_or_message, IsOkAndHolds(HasSubstr("Hello,")));
//
-// =================================
-// TfStatusIs(status_code_matcher,
-// error_message_matcher)
-// =================================
+// ===============================
+// StatusIs(status_code_matcher,
+// error_message_matcher)
+// ===============================
//
// This matcher matches a Status or StatusOr<T> if the following are true:
//
-// - the status's error_code() matches status_code_matcher, and
+// - the status's code() matches status_code_matcher, and
// - the status's error_message() matches error_message_matcher.
//
// Example:
//
-// using ::tensorflow::testing::TfStatusIs;
+// using ::tensorflow::testing::StatusIs;
// using ::testing::HasSubstr;
// using ::testing::MatchesRegex;
// using ::testing::Ne;
@@ -62,47 +62,47 @@
//
// // The status code must be CANCELLED; the error message can be anything.
// EXPECT_THAT(GetName(42),
-// TfStatusIs(tensorflow::error::CANCELLED, _));
+// StatusIs(tensorflow::error::CANCELLED, _));
//
// // The status code can be anything; the error message must match the regex.
// EXPECT_THAT(GetName(43),
-// TfStatusIs(_, MatchesRegex("server.*time-out")));
+// StatusIs(_, MatchesRegex("server.*time-out")));
//
// // The status code should not be CANCELLED; the error message can be
// // anything with "Cancelled" in it.
// EXPECT_THAT(GetName(44),
-// TfStatusIs(Ne(tensorflow::error::CANCELLED),
-// HasSubstr("Cancelled"))));
+// StatusIs(Ne(tensorflow::error::CANCELLED),
+// HasSubstr("Cancelled"))));
//
-// ===============================
-// TfStatusIs(status_code_matcher)
-// ===============================
+// =============================
+// StatusIs(status_code_matcher)
+// =============================
//
// This is a shorthand for
-// TfStatusIs(status_code_matcher, ::testing::_)
+// StatusIs(status_code_matcher, ::testing::_)
//
-// In other words, it's like the two-argument TfStatusIs(), except that it
-// ignores error message.
+// In other words, it's like the two-argument StatusIs(), except that it ignores
+// error messages.
//
-// ========
-// TfIsOk()
-// ========
+// ======
+// IsOk()
+// ======
//
// Matches a Status or StatusOr<T> whose status value is OK.
-// Equivalent to 'TfStatusIs(error::OK)'.
+// Equivalent to 'StatusIs(error::OK)'.
//
// Example:
// ...
-// StatusOr<std::string> message = std::string("Hello, world");
-// EXPECT_THAT(message, TfIsOk());
+// StatusOr<std::string> message("Hello, world");
+// EXPECT_THAT(message, IsOk());
// Status status = Status::OK();
-// EXPECT_THAT(status, TfIsOk());
+// EXPECT_THAT(status, IsOk());
namespace tensorflow {
template <typename T>
void PrintTo(const StatusOr<T>& status_or, std::ostream* os) {
- *os << status_or.status();
+ *os << ::testing::PrintToString(status_or.status());
if (status_or.ok()) {
*os << ": " << ::testing::PrintToString(status_or.ValueOrDie());
}
@@ -125,19 +125,19 @@
}
////////////////////////////////////////////////////////////
-// Implementation of TfIsOkAndHolds().
+// Implementation of IsOkAndHolds().
//
-// Monomorphic implementation of matcher TfIsOkAndHolds(m). StatusOrType is a
+// Monomorphic implementation of matcher IsOkAndHolds(m). StatusOrType is a
// reference to StatusOr<T>.
template <typename StatusOrType>
-class TfIsOkAndHoldsMatcherImpl
+class IsOkAndHoldsMatcherImpl
: public ::testing::MatcherInterface<StatusOrType> {
public:
typedef
typename std::remove_reference<StatusOrType>::type::value_type value_type;
template <typename InnerMatcher>
- explicit TfIsOkAndHoldsMatcherImpl(InnerMatcher&& inner_matcher)
+ explicit IsOkAndHoldsMatcherImpl(InnerMatcher&& inner_matcher)
: inner_matcher_(::testing::SafeMatcherCast<const value_type&>(
std::forward<InnerMatcher>(inner_matcher))) {}
@@ -175,11 +175,11 @@
const ::testing::Matcher<const value_type&> inner_matcher_;
};
-// Implements TfIsOkAndHolds(m) as a polymorphic matcher.
+// Implements IsOkAndHolds(m) as a polymorphic matcher.
template <typename InnerMatcher>
-class TfIsOkAndHoldsMatcher {
+class IsOkAndHoldsMatcher {
public:
- explicit TfIsOkAndHoldsMatcher(InnerMatcher inner_matcher)
+ explicit IsOkAndHoldsMatcher(InnerMatcher inner_matcher)
: inner_matcher_(std::move(inner_matcher)) {}
// Converts this polymorphic matcher to a monomorphic matcher of the given
@@ -187,7 +187,7 @@
template <typename StatusOrType>
operator ::testing::Matcher<StatusOrType>() const { // NOLINT
return ::testing::Matcher<StatusOrType>(
- new TfIsOkAndHoldsMatcherImpl<const StatusOrType&>(inner_matcher_));
+ new IsOkAndHoldsMatcherImpl<const StatusOrType&>(inner_matcher_));
}
private:
@@ -195,15 +195,15 @@
};
////////////////////////////////////////////////////////////
-// Implementation of TfStatusIs().
+// Implementation of StatusIs().
//
-// TfStatusIs() is a polymorphic matcher. This class is the common
-// implementation of it shared by all types T where TfStatusIs() can be used as
+// StatusIs() is a polymorphic matcher. This class is the common
+// implementation of it shared by all types T where StatusIs() can be used as
// a Matcher<T>.
-class TfStatusIsMatcherCommonImpl {
+class StatusIsMatcherCommonImpl {
public:
- TfStatusIsMatcherCommonImpl(
+ StatusIsMatcherCommonImpl(
::testing::Matcher<const tensorflow::error::Code> code_matcher,
::testing::Matcher<const std::string&> message_matcher)
: code_matcher_(std::move(code_matcher)),
@@ -221,12 +221,12 @@
const ::testing::Matcher<const std::string&> message_matcher_;
};
-// Monomorphic implementation of matcher TfStatusIs() for a given type T. T can
+// Monomorphic implementation of matcher StatusIs() for a given type T. T can
// be Status, StatusOr<>, or a reference to either of them.
template <typename T>
-class TfMonoStatusIsMatcherImpl : public ::testing::MatcherInterface<T> {
+class MonoStatusIsMatcherImpl : public ::testing::MatcherInterface<T> {
public:
- explicit TfMonoStatusIsMatcherImpl(TfStatusIsMatcherCommonImpl common_impl)
+ explicit MonoStatusIsMatcherImpl(StatusIsMatcherCommonImpl common_impl)
: common_impl_(std::move(common_impl)) {}
void DescribeTo(std::ostream* os) const override {
@@ -245,13 +245,13 @@
}
private:
- TfStatusIsMatcherCommonImpl common_impl_;
+ StatusIsMatcherCommonImpl common_impl_;
};
-// Implements TfStatusIs() as a polymorphic matcher.
-class TfStatusIsMatcher {
+// Implements StatusIs() as a polymorphic matcher.
+class StatusIsMatcher {
public:
- TfStatusIsMatcher(
+ StatusIsMatcher(
::testing::Matcher<const tensorflow::error::Code> code_matcher,
::testing::Matcher<const std::string&> message_matcher)
: common_impl_(
@@ -262,18 +262,17 @@
// type. T can be StatusOr<>, Status, or a reference to either of them.
template <typename T>
operator ::testing::Matcher<T>() const { // NOLINT
- return ::testing::MakeMatcher(
- new TfMonoStatusIsMatcherImpl<T>(common_impl_));
+ return ::testing::MakeMatcher(new MonoStatusIsMatcherImpl<T>(common_impl_));
}
private:
- const TfStatusIsMatcherCommonImpl common_impl_;
+ const StatusIsMatcherCommonImpl common_impl_;
};
-// Monomorphic implementation of matcher TfIsOk() for a given type T.
+// Monomorphic implementation of matcher IsOk() for a given type T.
// T can be Status, StatusOr<>, or a reference to either of them.
template <typename T>
-class TfMonoIsOkMatcherImpl : public ::testing::MatcherInterface<T> {
+class MonoIsOkMatcherImpl : public ::testing::MatcherInterface<T> {
public:
void DescribeTo(std::ostream* os) const override { *os << "is OK"; }
void DescribeNegationTo(std::ostream* os) const override {
@@ -285,12 +284,12 @@
}
};
-// Implements TfIsOk() as a polymorphic matcher.
-class TfIsOkMatcher {
+// Implements IsOk() as a polymorphic matcher.
+class IsOkMatcher {
public:
template <typename T>
operator ::testing::Matcher<T>() const { // NOLINT
- return ::testing::Matcher<T>(new TfMonoIsOkMatcherImpl<const T&>());
+ return ::testing::Matcher<T>(new MonoIsOkMatcherImpl<const T&>());
}
};
} // namespace internal_status
@@ -298,9 +297,9 @@
// Returns a matcher that matches a StatusOr<> whose status is OK and whose
// value matches the inner matcher.
template <typename InnerMatcher>
-internal_status::TfIsOkAndHoldsMatcher<typename std::decay<InnerMatcher>::type>
-TfIsOkAndHolds(InnerMatcher&& inner_matcher) {
- return internal_status::TfIsOkAndHoldsMatcher<
+internal_status::IsOkAndHoldsMatcher<typename std::decay<InnerMatcher>::type>
+IsOkAndHolds(InnerMatcher&& inner_matcher) {
+ return internal_status::IsOkAndHoldsMatcher<
typename std::decay<InnerMatcher>::type>(
std::forward<InnerMatcher>(inner_matcher));
}
@@ -308,22 +307,22 @@
// Returns a matcher that matches a Status or StatusOr<> whose status code
// matches code_matcher, and whose error message matches message_matcher.
template <typename CodeMatcher, typename MessageMatcher>
-internal_status::TfStatusIsMatcher TfStatusIs(CodeMatcher code_matcher,
- MessageMatcher message_matcher) {
- return internal_status::TfStatusIsMatcher(std::move(code_matcher),
- std::move(message_matcher));
+internal_status::StatusIsMatcher StatusIs(CodeMatcher code_matcher,
+ MessageMatcher message_matcher) {
+ return internal_status::StatusIsMatcher(std::move(code_matcher),
+ std::move(message_matcher));
}
// Returns a matcher that matches a Status or StatusOr<> whose status code
// matches code_matcher.
template <typename CodeMatcher>
-internal_status::TfStatusIsMatcher TfStatusIs(CodeMatcher code_matcher) {
- return TfStatusIs(std::move(code_matcher), ::testing::_);
+internal_status::StatusIsMatcher StatusIs(CodeMatcher code_matcher) {
+ return StatusIs(std::move(code_matcher), ::testing::_);
}
// Returns a matcher that matches a Status or StatusOr<> which is OK.
-inline internal_status::TfIsOkMatcher TfIsOk() {
- return internal_status::TfIsOkMatcher();
+inline internal_status::IsOkMatcher IsOk() {
+ return internal_status::IsOkMatcher();
}
} // namespace testing
diff --git a/tensorflow/core/platform/status_matchers_test.cc b/tensorflow/core/platform/status_matchers_test.cc
index 1847cd5..ed70868 100644
--- a/tensorflow/core/platform/status_matchers_test.cc
+++ b/tensorflow/core/platform/status_matchers_test.cc
@@ -14,6 +14,7 @@
==============================================================================*/
#include "tensorflow/core/platform/status_matchers.h"
+#include <sstream>
#include <string>
#include <vector>
@@ -30,68 +31,234 @@
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::HasSubstr;
+using ::testing::Matcher;
using ::testing::MatchesRegex;
using ::testing::Ne;
using ::testing::Not;
+using ::testing::PrintToString;
-TEST(StatusMatchersTest, TfIsOkAndHolds) {
- StatusOr<std::string> status_or_message = std::string("Hello, world");
- EXPECT_THAT(status_or_message, TfIsOkAndHolds("Hello, world"));
- EXPECT_THAT(status_or_message, TfIsOkAndHolds(HasSubstr("Hello,")));
+// Matches a value less than the given upper bound. This matcher is chatty (it
+// always explains the match result with some detail), and thus is useful for
+// testing that an outer matcher correctly incorporates an inner matcher's
+// explanation.
+MATCHER_P(LessThan, upper, "") {
+ if (arg < upper) {
+ *result_listener << "which is " << (upper - arg) << " less than " << upper;
+ return true;
+ }
+ *result_listener << "which is " << (arg - upper) << " more than " << upper;
+ return false;
}
-TEST(StatusMatchersTest, TfIsOkAndHoldsContainer) {
+// Returns the description of the given matcher.
+template <typename T>
+std::string Describe(const Matcher<T>& matcher) {
+ std::stringstream ss;
+ matcher.DescribeTo(&ss);
+ return ss.str();
+}
+
+// Returns the description of the negation of the given matcher.
+template <typename T>
+std::string DescribeNegation(const Matcher<T>& matcher) {
+ std::stringstream ss;
+ matcher.DescribeNegationTo(&ss);
+ return ss.str();
+}
+
+// Returns the explanation on the result of using the given matcher to
+// match the given value.
+template <typename T, typename V>
+std::string ExplainMatch(const Matcher<T>& matcher, const V& value) {
+ ::testing::StringMatchResultListener listener;
+ matcher.MatchAndExplain(value, &listener);
+ return listener.str();
+}
+
+TEST(IsOkAndHoldsTest, MatchesValue) {
+ StatusOr<std::string> status_or_message("Hello, world");
+ EXPECT_THAT(status_or_message, IsOkAndHolds("Hello, world"));
+ EXPECT_THAT(status_or_message, IsOkAndHolds(HasSubstr("Hello,")));
+}
+
+TEST(IsOkAndHoldsTest, MatchesContainer) {
StatusOr<std::vector<std::string>> status_or_messages =
std::vector<std::string>{"Hello, world", "Hello, tf"};
EXPECT_THAT(status_or_messages,
- TfIsOkAndHolds(ElementsAre("Hello, world", "Hello, tf")));
- EXPECT_THAT(
- status_or_messages,
- TfIsOkAndHolds(ElementsAre(HasSubstr("Hello,"), HasSubstr("Hello,"))));
+ IsOkAndHolds(ElementsAre("Hello, world", "Hello, tf")));
+ EXPECT_THAT(status_or_messages,
+ IsOkAndHolds(ElementsAre(HasSubstr("world"), HasSubstr("tf"))));
}
-TEST(StatusMatchersTest, TfStatusIsOk) {
- EXPECT_THAT(Status::OK(), TfStatusIs(error::OK));
+TEST(IsOkAndHoldsTest, DoesNotMatchStatus) {
+ StatusOr<std::string> status_or_message =
+ errors::InvalidArgument("Invalid argument");
+ EXPECT_THAT(status_or_message, Not(IsOkAndHolds("Hello, world")));
+}
+
+TEST(IsOkAndHoldsTest, DoesNotMatchValue) {
+ StatusOr<std::string> status_or_message("Hello, tf");
+ EXPECT_THAT(status_or_message, Not(IsOkAndHolds("Hello, world")));
+}
+
+TEST(IsOkAndHoldsTest, DoesNotMatchContainer) {
+ StatusOr<std::vector<int>> status_or_container({1, 2, 3});
+ EXPECT_THAT(status_or_container, Not(IsOkAndHolds(ElementsAre(4, 5, 6))));
+}
+
+TEST(IsOkAndHoldsTest, DescribeExpectedValue) {
+ Matcher<StatusOr<std::string>> is_ok_and_has_substr =
+ IsOkAndHolds(HasSubstr("Hello"));
+ EXPECT_EQ(Describe(is_ok_and_has_substr),
+ "is OK and has a value that has substring \"Hello\"");
+ EXPECT_EQ(DescribeNegation(is_ok_and_has_substr),
+ "isn't OK or has a value that has no substring \"Hello\"");
+}
+
+TEST(IsOkAndHoldsTest, ExplainNotMatchingStatus) {
+ Matcher<StatusOr<int>> is_ok_and_less_than = IsOkAndHolds(LessThan(100));
+ StatusOr<int> status = errors::Unknown("Unknown");
+ EXPECT_EQ(ExplainMatch(is_ok_and_less_than, status),
+ "which has status " + PrintToString(status));
+}
+
+TEST(IsOkAndHoldsTest, ExplainNotMatchingValue) {
+ Matcher<StatusOr<int>> is_ok_and_less_than = IsOkAndHolds(LessThan(100));
+ EXPECT_EQ(ExplainMatch(is_ok_and_less_than, 120),
+ "which contains value 120, which is 20 more than 100");
+}
+
+TEST(IsOkAndHoldsTest, ExplainNotMatchingContainer) {
+ Matcher<StatusOr<std::vector<int>>> is_ok_and_less_than =
+ IsOkAndHolds(ElementsAre(1, 2, 3));
+ std::vector<int> actual{4, 5, 6};
+ EXPECT_THAT(ExplainMatch(is_ok_and_less_than, actual),
+ HasSubstr("which contains value " + PrintToString(actual)));
+}
+
+TEST(StatusIsTest, MatchesOK) {
+ EXPECT_THAT(Status::OK(), StatusIs(error::OK));
+ StatusOr<std::string> message("Hello, world");
+ EXPECT_THAT(message, StatusIs(error::OK));
+}
+
+TEST(StatusIsTest, DoesNotMatchOk) {
EXPECT_THAT(errors::DeadlineExceeded("Deadline exceeded"),
- Not(TfStatusIs(error::OK)));
-
- StatusOr<std::string> message = std::string("Hello, world");
- EXPECT_THAT(message, TfStatusIs(error::OK));
+ Not(StatusIs(error::OK)));
StatusOr<std::string> status = errors::NotFound("Not found");
- EXPECT_THAT(status, Not(TfStatusIs(error::OK)));
+ EXPECT_THAT(status, Not(StatusIs(error::OK)));
}
-TEST(StatusMatchersTest, TfStatusIsCancelled) {
+TEST(StatusIsTest, MatchesStatus) {
Status s = errors::Cancelled("Cancelled");
- EXPECT_THAT(s, TfStatusIs(error::CANCELLED));
- EXPECT_THAT(s, TfStatusIs(error::CANCELLED, "Cancelled"));
- EXPECT_THAT(s, TfStatusIs(_, "Cancelled"));
- EXPECT_THAT(s, TfStatusIs(error::CANCELLED, _));
- EXPECT_THAT(s, TfStatusIs(Ne(error::INVALID_ARGUMENT), _));
- EXPECT_THAT(s, TfStatusIs(error::CANCELLED, HasSubstr("Can")));
- EXPECT_THAT(s, TfStatusIs(error::CANCELLED, MatchesRegex("Can.*")));
+ EXPECT_THAT(s, StatusIs(error::CANCELLED));
+ EXPECT_THAT(s, StatusIs(error::CANCELLED, "Cancelled"));
+ EXPECT_THAT(s, StatusIs(_, "Cancelled"));
+ EXPECT_THAT(s, StatusIs(error::CANCELLED, _));
+ EXPECT_THAT(s, StatusIs(Ne(error::INVALID_ARGUMENT), _));
+ EXPECT_THAT(s, StatusIs(error::CANCELLED, HasSubstr("Can")));
+ EXPECT_THAT(s, StatusIs(error::CANCELLED, MatchesRegex("Can.*")));
}
-TEST(StatusMatchersTest, TfStatusOrStatusIs) {
+TEST(StatusIsTest, StatusOrMatchesStatus) {
StatusOr<int> s = errors::InvalidArgument("Invalid Argument");
- EXPECT_THAT(s, TfStatusIs(error::INVALID_ARGUMENT));
- EXPECT_THAT(s, TfStatusIs(error::INVALID_ARGUMENT, "Invalid Argument"));
- EXPECT_THAT(s, TfStatusIs(_, "Invalid Argument"));
- EXPECT_THAT(s, TfStatusIs(error::INVALID_ARGUMENT, _));
- EXPECT_THAT(s, TfStatusIs(Ne(error::CANCELLED), _));
- EXPECT_THAT(s, TfStatusIs(error::INVALID_ARGUMENT, HasSubstr("Argument")));
- EXPECT_THAT(s,
- TfStatusIs(error::INVALID_ARGUMENT, MatchesRegex(".*Argument")));
+ EXPECT_THAT(s, StatusIs(error::INVALID_ARGUMENT));
+ EXPECT_THAT(s, StatusIs(error::INVALID_ARGUMENT, "Invalid Argument"));
+ EXPECT_THAT(s, StatusIs(_, "Invalid Argument"));
+ EXPECT_THAT(s, StatusIs(error::INVALID_ARGUMENT, _));
+ EXPECT_THAT(s, StatusIs(Ne(error::CANCELLED), _));
+ EXPECT_THAT(s, StatusIs(error::INVALID_ARGUMENT, HasSubstr("Argument")));
+ EXPECT_THAT(s, StatusIs(error::INVALID_ARGUMENT, MatchesRegex(".*Argument")));
}
-TEST(StatusMatchersTest, TfIsOk) {
- EXPECT_THAT(Status::OK(), TfIsOk());
- EXPECT_THAT(errors::DeadlineExceeded("Deadline exceeded"), Not(TfIsOk()));
+TEST(StatusIsTest, DoesNotMatchStatus) {
+ Status s = errors::Internal("Internal");
+ EXPECT_THAT(s, Not(StatusIs(error::FAILED_PRECONDITION)));
+ EXPECT_THAT(s, Not(StatusIs(error::INTERNAL, "Failed Precondition")));
+ EXPECT_THAT(s, Not(StatusIs(_, "Failed Precondition")));
+ EXPECT_THAT(s, Not(StatusIs(error::FAILED_PRECONDITION, _)));
+}
+TEST(StatusIsTest, StatusOrDoesNotMatchStatus) {
+ StatusOr<int> s = errors::FailedPrecondition("Failed Precondition");
+ EXPECT_THAT(s, Not(StatusIs(error::INTERNAL)));
+ EXPECT_THAT(s, Not(StatusIs(error::FAILED_PRECONDITION, "Internal")));
+ EXPECT_THAT(s, Not(StatusIs(_, "Internal")));
+ EXPECT_THAT(s, Not(StatusIs(error::INTERNAL, _)));
+}
+
+TEST(StatusIsTest, DescribeExpectedValue) {
+ Matcher<Status> status_is =
+ StatusIs(error::UNAVAILABLE, std::string("Unavailable"));
+ EXPECT_EQ(Describe(status_is),
+ "has a status code that is equal to UNAVAILABLE, "
+ "and has an error message that is equal to \"Unavailable\"");
+}
+
+TEST(StatusIsTest, DescribeNegatedExpectedValue) {
+ Matcher<StatusOr<std::string>> status_is =
+ StatusIs(error::ABORTED, std::string("Aborted"));
+ EXPECT_EQ(DescribeNegation(status_is),
+ "has a status code that isn't equal to ABORTED, "
+ "or has an error message that isn't equal to \"Aborted\"");
+}
+
+TEST(StatusIsTest, ExplainNotMatchingErrorCode) {
+ Matcher<Status> status_is = StatusIs(error::NOT_FOUND, _);
+ const Status status = errors::AlreadyExists("Already exists");
+ EXPECT_EQ(ExplainMatch(status_is, status), "whose status code is wrong");
+}
+
+TEST(StatusIsTest, ExplainNotMatchingErrorMessage) {
+ Matcher<Status> status_is = StatusIs(error::NOT_FOUND, "Not found");
+ const Status status = errors::NotFound("Already exists");
+ EXPECT_EQ(ExplainMatch(status_is, status), "whose error message is wrong");
+}
+
+TEST(StatusIsTest, ExplainStatusOrNotMatchingErrorCode) {
+ Matcher<StatusOr<int>> status_is = StatusIs(error::ALREADY_EXISTS, _);
+ const StatusOr<int> status_or = errors::NotFound("Not found");
+ EXPECT_EQ(ExplainMatch(status_is, status_or), "whose status code is wrong");
+}
+
+TEST(StatusIsTest, ExplainStatusOrNotMatchingErrorMessage) {
+ Matcher<StatusOr<int>> status_is =
+ StatusIs(error::ALREADY_EXISTS, "Already exists");
+ const StatusOr<int> status_or = errors::AlreadyExists("Not found");
+ EXPECT_EQ(ExplainMatch(status_is, status_or), "whose error message is wrong");
+}
+
+TEST(StatusIsTest, ExplainStatusOrHasValue) {
+ Matcher<StatusOr<int>> status_is =
+ StatusIs(error::RESOURCE_EXHAUSTED, "Resource exhausted");
+ const StatusOr<int> value = -1;
+ EXPECT_EQ(ExplainMatch(status_is, value), "whose status code is wrong");
+}
+
+TEST(IsOkTest, MatchesOK) {
+ EXPECT_THAT(Status::OK(), IsOk());
StatusOr<std::string> message = std::string("Hello, world");
- EXPECT_THAT(message, TfIsOk());
- StatusOr<std::string> status = errors::NotFound("Not found");
- EXPECT_THAT(status, Not(TfIsOk()));
+ EXPECT_THAT(message, IsOk());
+}
+
+TEST(IsOkTest, DoesNotMatchOK) {
+ EXPECT_THAT(errors::PermissionDenied("Permission denied"), Not(IsOk()));
+ StatusOr<std::string> status = errors::Unauthenticated("Unauthenticated");
+ EXPECT_THAT(status, Not(IsOk()));
+}
+
+TEST(IsOkTest, DescribeExpectedValue) {
+ Matcher<Status> status_is_ok = IsOk();
+ EXPECT_EQ(Describe(status_is_ok), "is OK");
+ Matcher<StatusOr<std::string>> status_or_is_ok = IsOk();
+ EXPECT_EQ(Describe(status_or_is_ok), "is OK");
+}
+
+TEST(IsOkTest, DescribeNegatedExpectedValue) {
+ Matcher<Status> status_is_ok = IsOk();
+ EXPECT_EQ(DescribeNegation(status_is_ok), "is not OK");
+ Matcher<StatusOr<std::string>> status_or_is_ok = IsOk();
+ EXPECT_EQ(DescribeNegation(status_or_is_ok), "is not OK");
}
} // namespace