Remove okay Status, rename Status->Error
Phil pointed out that we weren't actually using the okay state of the
Status type, and that it was effectively just an Error type. Rename it
to reflect that and remove the ok() methods.
Change-Id: I73644a55ec8b276b07376d4a4687fc0820837614
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/util/status_test.cc b/aos/util/status_test.cc
index 46c9e40..b838636 100644
--- a/aos/util/status_test.cc
+++ b/aos/util/status_test.cc
@@ -11,38 +11,20 @@
DECLARE_bool(die_on_malloc);
namespace aos::testing {
-class StatusTest : public ::testing::Test {
+class ErrorTest : public ::testing::Test {
protected:
- StatusTest() {}
+ ErrorTest() {}
};
-// Tests that we can construct an "Ok" status and that it presents the correct
-// interface.
-TEST_F(StatusTest, Okay) {
- std::optional<Status> ok;
- {
- aos::ScopedRealtime realtime;
- ok = Status::Ok();
- }
- ASSERT_TRUE(ok.has_value());
- EXPECT_TRUE(ok->ok());
- EXPECT_EQ(0, ok->code());
- EXPECT_EQ("", ok->message());
- EXPECT_FALSE(ok->source_location().has_value());
- EXPECT_EQ(std::string("Status is okay with code of 0 and message: "),
- ok->ToString());
-}
-
// Tests that we can construct an errored status in realtime code.
-TEST_F(StatusTest, RealtimeError) {
- std::optional<Status> error;
+TEST_F(ErrorTest, RealtimeError) {
+ std::optional<Error> error;
{
aos::ScopedRealtime realtime;
- error = Status::Error("Hello, World!");
+ error = Error::MakeError("Hello, World!");
}
const int line = __LINE__ - 2;
ASSERT_TRUE(error.has_value());
- EXPECT_FALSE(error->ok());
EXPECT_NE(0, error->code());
EXPECT_EQ(std::string("Hello, World!"), error->message());
ASSERT_TRUE(error->source_location().has_value());
@@ -51,7 +33,7 @@
std::filesystem::path(error->source_location()->file_name()).filename());
EXPECT_EQ(
std::string("virtual void "
- "aos::testing::StatusTest_RealtimeError_Test::TestBody()"),
+ "aos::testing::ErrorTest_RealtimeError_Test::TestBody()"),
error->source_location()->function_name());
EXPECT_EQ(line, error->source_location()->line());
EXPECT_LT(1, error->source_location()->column());
@@ -59,22 +41,32 @@
error->ToString(),
::testing::HasSubstr(absl::StrFormat(
"status_test.cc:%d in virtual void "
- "aos::testing::StatusTest_RealtimeError_Test::TestBody(): Status is "
- "errored with code of 1 and message: Hello, World!",
+ "aos::testing::ErrorTest_RealtimeError_Test::TestBody(): Errored "
+ "with code of 1 and message: Hello, World!",
line)));
}
+// Tests that the ResultExitCode() function will correctly transform a Result<>
+// object into an exit code suitable for exiting a program.
+TEST_F(ErrorTest, ExitCode) {
+ static_assert(0 == static_cast<int>(Error::StatusCode::kOk));
+ EXPECT_EQ(static_cast<int>(Error::StatusCode::kOk),
+ ResultExitCode(Result<void>{}));
+ EXPECT_EQ(static_cast<int>(Error::StatusCode::kError),
+ ResultExitCode(Error::MakeUnexpectedError("")));
+}
+
// Malloc hooks don't work with asan/msan.
#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
// Tests that we do indeed malloc (and catch it) on an extra-long error message
// (this is mostly intended to ensure that the test setup is working correctly).
-TEST(StatusDeathTest, BlowsUpOnRealtimeAllocation) {
- std::string message(" ", Status::kStaticMessageLength + 1);
+TEST(ErrorDeathTest, BlowsUpOnRealtimeAllocation) {
+ std::string message(" ", Error::kStaticMessageLength + 1);
EXPECT_DEATH(
{
aos::ScopedRealtime realtime;
aos::CheckRealtime();
- Status foo = Status::Error(message);
+ Error foo = Error::MakeError(message);
},
"Malloced");
}
@@ -82,20 +74,19 @@
#endif
// Tests that we can use arbitrarily-sized string literals for error messages.
-TEST_F(StatusTest, StringLiteralError) {
- std::optional<Status> error;
+TEST_F(ErrorTest, StringLiteralError) {
+ std::optional<Error> error;
const char *message =
"Hellllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"
"llllllllllllllloooooooooooooooooooooooooooooooooooooooooooo, "
"World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
"!!!!!!!!!!!!!!";
- ASSERT_LT(Status::kStaticMessageLength, strlen(message));
+ ASSERT_LT(Error::kStaticMessageLength, strlen(message));
{
aos::ScopedRealtime realtime;
- error = Status::StringLiteralError(message);
+ error = Error::MakeStringLiteralError(message);
}
ASSERT_TRUE(error.has_value());
- EXPECT_FALSE(error->ok());
EXPECT_EQ(message, error->message());
ASSERT_TRUE(error->source_location().has_value());
EXPECT_EQ(
@@ -104,16 +95,16 @@
}
// Tests that the CheckExpected() call works as intended.
-TEST(StatusDeathTest, CheckExpected) {
- tl::expected<int, Status> expected;
+TEST(ErrorDeathTest, CheckExpected) {
+ tl::expected<int, Error> expected;
expected.emplace(971);
EXPECT_EQ(971, CheckExpected(expected))
<< "Should have gotten out the emplaced value on no error.";
- expected = Status::UnexpectedError("Hello, World!");
+ expected = Error::MakeUnexpectedError("Hello, World!");
EXPECT_DEATH(CheckExpected(expected), "Hello, World!")
<< "An error message including the error string should have been printed "
"on death.";
- EXPECT_DEATH(CheckExpected<void>(Status::UnexpectedError("void expected")),
+ EXPECT_DEATH(CheckExpected<void>(Error::MakeUnexpectedError("void expected")),
"void expected")
<< "A void expected should work with CheckExpected().";
}