-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Result class to support empty values and same value and erro…
…r types
- Loading branch information
1 parent
615019e
commit 6c62d55
Showing
2 changed files
with
89 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "../src/Result/Result.hpp" | ||
#include <gtest/gtest.h> | ||
#include <string> | ||
|
||
TEST(Types, SameInt) { | ||
const int value = 42; | ||
const int error = 43; | ||
auto ok_result = res::Result<int, int>::Ok(value); | ||
auto err_result = res::Result<int, int>::Err(error); | ||
|
||
EXPECT_TRUE(ok_result.is_ok()); | ||
EXPECT_FALSE(ok_result.is_err()); | ||
EXPECT_EQ(ok_result.unwrap(), value); | ||
EXPECT_THROW(auto ___ = ok_result.unwrap_err(), std::runtime_error); | ||
|
||
EXPECT_TRUE(err_result.is_err()); | ||
EXPECT_FALSE(err_result.is_ok()); | ||
EXPECT_EQ(err_result.unwrap_err(), error); | ||
EXPECT_THROW(auto ___ = err_result.unwrap(), std::runtime_error); | ||
} | ||
|
||
TEST(Types, SameString) { | ||
const std::string value = "Hello"; | ||
const std::string error = "World"; | ||
auto ok_result = res::Result<std::string, std::string>::Ok(value); | ||
auto err_result = res::Result<std::string, std::string>::Err(error); | ||
|
||
EXPECT_TRUE(ok_result.is_ok()); | ||
EXPECT_FALSE(ok_result.is_err()); | ||
EXPECT_EQ(ok_result.unwrap(), value); | ||
EXPECT_THROW(auto ___ = ok_result.unwrap_err(), std::runtime_error); | ||
|
||
EXPECT_TRUE(err_result.is_err()); | ||
EXPECT_FALSE(err_result.is_ok()); | ||
EXPECT_EQ(err_result.unwrap_err(), error); | ||
EXPECT_THROW(auto ___ = err_result.unwrap(), std::runtime_error); | ||
} | ||
|
||
TEST(Types, EmptyValue) { | ||
auto empty_ok_result = res::Result<std::monostate, std::string>::Ok(); | ||
auto empty_err_result = res::Result<std::monostate, std::string>::Err("Hello"); | ||
|
||
EXPECT_TRUE(empty_ok_result.is_ok()); | ||
EXPECT_FALSE(empty_ok_result.is_err()); | ||
EXPECT_THROW(auto ___ = empty_ok_result.unwrap(), std::runtime_error); | ||
EXPECT_THROW(auto ___ = empty_ok_result.unwrap_err(), std::runtime_error); | ||
|
||
EXPECT_TRUE(empty_err_result.is_err()); | ||
EXPECT_FALSE(empty_err_result.is_ok()); | ||
EXPECT_EQ(empty_err_result.unwrap_err(), "Hello"); | ||
EXPECT_THROW(auto ___ = empty_err_result.unwrap(), std::runtime_error); | ||
} |