2026-07-18 21:16:55 +04:00
|
|
|
#include <cassert>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include "result.hpp"
|
|
|
|
|
|
2026-07-20 23:01:50 +04:00
|
|
|
namespace core::tests {
|
2026-07-18 21:16:55 +04:00
|
|
|
|
2026-07-19 16:41:22 +04:00
|
|
|
using namespace core;
|
|
|
|
|
|
2026-07-18 21:16:55 +04:00
|
|
|
void testSuccessCreation() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto result = Result<int>::Success(42);
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
assert(result.isSuccess());
|
|
|
|
|
assert(result.getValue() == 42);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testFailureCreation() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto result = Result<int>::Failure("Invalid value");
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
assert(result.isFailure());
|
|
|
|
|
assert(result.getError() == "Invalid value");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testStatusSuccess() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto status = Status::Success(true);
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
assert(status.isSuccess());
|
|
|
|
|
assert(status.getValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testStatusFailure() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto status = Status::Failure("File not found");
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
assert(status.isFailure());
|
|
|
|
|
assert(status.getError() == "File not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testMoveConstruction() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto source = Result<std::string>::Success("hello");
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
auto moved = std::move(source);
|
|
|
|
|
|
|
|
|
|
assert(moved.isSuccess());
|
|
|
|
|
assert(moved.getValue() == "hello");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testMoveAssignment() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto first = Result<int>::Success(10);
|
|
|
|
|
auto second = Result<int>::Failure("error");
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
second = std::move(first);
|
|
|
|
|
|
|
|
|
|
assert(second.isSuccess());
|
|
|
|
|
assert(second.getValue() == 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testDifferentTypes() {
|
|
|
|
|
struct User {
|
|
|
|
|
int id;
|
|
|
|
|
std::string name;
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-19 16:41:22 +04:00
|
|
|
auto result = Result<User>::Success(User{1, "Alex"});
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
assert(result.isSuccess());
|
|
|
|
|
assert(result.getValue().id == 1);
|
|
|
|
|
assert(result.getValue().name == "Alex");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testValueModification() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto result = Result<int>::Success(10);
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
result.getValue() = 20;
|
|
|
|
|
|
|
|
|
|
assert(result.getValue() == 20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testErrorModification() {
|
2026-07-19 16:41:22 +04:00
|
|
|
auto result = Result<int>::Failure("old error");
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
result.getError() = "new error";
|
|
|
|
|
|
|
|
|
|
assert(result.getError() == "new error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testBasicResultWithCustomError() {
|
|
|
|
|
enum class ErrorCode { NotFound, PermissionDenied };
|
|
|
|
|
|
2026-07-19 16:41:22 +04:00
|
|
|
using CustomResult = BasicResult<int, ErrorCode>;
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 23:01:50 +04:00
|
|
|
} // namespace core::tests
|
2026-07-18 21:16:55 +04:00
|
|
|
|
|
|
|
|
int main() {
|
2026-07-20 23:01:50 +04:00
|
|
|
core::tests::runAllTests();
|
2026-07-18 21:16:55 +04:00
|
|
|
return 0;
|
|
|
|
|
}
|