101 lines
3.2 KiB
Zig
101 lines
3.2 KiB
Zig
const std = @import("std");
|
|
const errors = @import("errors");
|
|
|
|
test "Error: all error types defined" {
|
|
// Verify all error types exist
|
|
const err_types = [_]errors.Error{
|
|
error.IncompatibleArgumentType,
|
|
error.UnknownArgument,
|
|
error.InvalidValue,
|
|
error.InvalidIntegerValue,
|
|
error.InvalidBooleanValue,
|
|
error.InvalidEnumValue,
|
|
error.MissingArgumentValue,
|
|
error.OutOfMemory,
|
|
};
|
|
|
|
// If we can create all these, they're defined
|
|
try std.testing.expect(err_types.len == 8);
|
|
}
|
|
|
|
test "ErrorContext: default initialization" {
|
|
const ctx = errors.ErrorContext{};
|
|
try std.testing.expectEqual(@as(?[]const u8, null), ctx.argument_name);
|
|
try std.testing.expectEqual(@as(?[]const u8, null), ctx.invalid_value);
|
|
try std.testing.expectEqual(@as(?[]const u8, null), ctx.expected_type);
|
|
try std.testing.expectEqual(@as(?[]const u8, null), ctx.message);
|
|
}
|
|
|
|
test "ErrorContext: with values" {
|
|
const ctx = errors.ErrorContext{
|
|
.argument_name = "--verbose",
|
|
.invalid_value = "maybe",
|
|
.expected_type = "bool",
|
|
.message = "Invalid boolean value",
|
|
};
|
|
|
|
try std.testing.expectEqualStrings("--verbose", ctx.argument_name.?);
|
|
try std.testing.expectEqualStrings("maybe", ctx.invalid_value.?);
|
|
try std.testing.expectEqualStrings("bool", ctx.expected_type.?);
|
|
try std.testing.expectEqualStrings("Invalid boolean value", ctx.message.?);
|
|
}
|
|
|
|
test "Result: ok value" {
|
|
const IntResult = errors.Result(u32);
|
|
const result = IntResult{ .ok = 42 };
|
|
|
|
try std.testing.expect(result.isOk());
|
|
try std.testing.expect(!result.isErr());
|
|
try std.testing.expectEqual(@as(u32, 42), result.unwrap());
|
|
}
|
|
|
|
test "Result: error value" {
|
|
const IntResult = errors.Result(u32);
|
|
const result = IntResult{
|
|
.err = .{
|
|
.error_type = error.InvalidIntegerValue,
|
|
.context = .{
|
|
.argument_name = "--count",
|
|
.invalid_value = "abc",
|
|
},
|
|
},
|
|
};
|
|
|
|
try std.testing.expect(!result.isOk());
|
|
try std.testing.expect(result.isErr());
|
|
try std.testing.expectEqual(error.InvalidIntegerValue, result.err.error_type);
|
|
try std.testing.expectEqualStrings("--count", result.err.context.argument_name.?);
|
|
}
|
|
|
|
test "Result: unwrapOr with ok" {
|
|
const IntResult = errors.Result(u32);
|
|
const result = IntResult{ .ok = 42 };
|
|
const value = result.unwrapOr(100);
|
|
try std.testing.expectEqual(@as(u32, 42), value);
|
|
}
|
|
|
|
test "Result: unwrapOr with error" {
|
|
const IntResult = errors.Result(u32);
|
|
const result = IntResult{
|
|
.err = .{
|
|
.error_type = error.InvalidValue,
|
|
.context = .{},
|
|
},
|
|
};
|
|
const value = result.unwrapOr(100);
|
|
try std.testing.expectEqual(@as(u32, 100), value);
|
|
}
|
|
|
|
test "Result: works with different types" {
|
|
{
|
|
const BoolResult = errors.Result(bool);
|
|
const result = BoolResult{ .ok = true };
|
|
try std.testing.expect(result.unwrap());
|
|
}
|
|
{
|
|
const StringResult = errors.Result([]const u8);
|
|
const result = StringResult{ .ok = "hello" };
|
|
try std.testing.expectEqualStrings("hello", result.unwrap());
|
|
}
|
|
}
|