Files
cpp17-threaded-app-non-thre…/tests/core/result_test.cpp
T

117 lines
2.3 KiB
C++

#include <cassert>
#include <string>
#include <utility>
#include "result.hpp"
namespace {
using namespace core;
void testSuccessCreation() {
auto result = Result<int>::Success(42);
assert(result.isSuccess());
assert(result.getValue() == 42);
}
void testFailureCreation() {
auto result = Result<int>::Failure("Invalid value");
assert(result.isFailure());
assert(result.getError() == "Invalid value");
}
void testStatusSuccess() {
auto status = Status::Success(true);
assert(status.isSuccess());
assert(status.getValue());
}
void testStatusFailure() {
auto status = Status::Failure("File not found");
assert(status.isFailure());
assert(status.getError() == "File not found");
}
void testMoveConstruction() {
auto source = Result<std::string>::Success("hello");
auto moved = std::move(source);
assert(moved.isSuccess());
assert(moved.getValue() == "hello");
}
void testMoveAssignment() {
auto first = Result<int>::Success(10);
auto second = Result<int>::Failure("error");
second = std::move(first);
assert(second.isSuccess());
assert(second.getValue() == 10);
}
void testDifferentTypes() {
struct User {
int id;
std::string name;
};
auto result = Result<User>::Success(User{1, "Alex"});
assert(result.isSuccess());
assert(result.getValue().id == 1);
assert(result.getValue().name == "Alex");
}
void testValueModification() {
auto result = Result<int>::Success(10);
result.getValue() = 20;
assert(result.getValue() == 20);
}
void testErrorModification() {
auto result = Result<int>::Failure("old error");
result.getError() = "new error";
assert(result.getError() == "new error");
}
void testBasicResultWithCustomError() {
enum class ErrorCode { NotFound, PermissionDenied };
using CustomResult = BasicResult<int, ErrorCode>;
auto result = CustomResult::Failure(ErrorCode::PermissionDenied);
assert(result.isFailure());
assert(result.getError() == ErrorCode::PermissionDenied);
}
void runAllTests() {
testSuccessCreation();
testFailureCreation();
testStatusSuccess();
testStatusFailure();
testMoveConstruction();
testMoveAssignment();
testDifferentTypes();
testValueModification();
testErrorModification();
testBasicResultWithCustomError();
}
} // namespace
int main() {
runAllTests();
return 0;
}