129 lines
4.6 KiB
Zig
129 lines
4.6 KiB
Zig
//! Tests for error handling module
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
|
|
const Error = @import("../../src/core/error.zig").Error;
|
|
const ErrorInfo = @import("../../src/core/error.zig").ErrorInfo;
|
|
const ErrorContext = @import("../../src/core/error.zig").ErrorContext;
|
|
const ErrorResult = @import("../../src/core/error.zig").ErrorResult;
|
|
const ErrorStats = @import("../../src/core/error.zig").ErrorStats;
|
|
|
|
test "error basic operations" {
|
|
// Test error propagation
|
|
const test_fn = fn() Error!void {
|
|
return Error.ConnectionRefused;
|
|
};
|
|
|
|
const result = test_fn();
|
|
try testing.expectError(Error.ConnectionRefused, result);
|
|
}
|
|
|
|
test "error context operations" {
|
|
var context = ErrorContext.init("connect", "network");
|
|
try testing.expectEqualSlices(u8, "connect", context.operation);
|
|
try testing.expectEqualSlices(u8, "network", context.component);
|
|
try testing.expect(context.peer_id == null);
|
|
try testing.expect(context.connection_id == null);
|
|
|
|
context = context.withPeerId("peer123");
|
|
try testing.expectEqualSlices(u8, "peer123", context.peer_id.?);
|
|
|
|
context = context.withConnectionId("conn456");
|
|
try testing.expectEqualSlices(u8, "conn456", context.connection_id.?);
|
|
}
|
|
|
|
test "error result creation" {
|
|
const context = ErrorContext.init("send", "transport");
|
|
const error_result = ErrorResult.create(
|
|
Error.ConnectionRefused,
|
|
"Connection failed",
|
|
context,
|
|
"test.zig",
|
|
42,
|
|
"test_function",
|
|
);
|
|
|
|
try testing.expect(Error.ConnectionRefused == error_result.error);
|
|
try testing.expect(error_result.info.message.len > 0);
|
|
}
|
|
|
|
test "error string conversion" {
|
|
try testing.expectEqualSlices(u8, "ConnectionRefused", Error.toString(Error.ConnectionRefused));
|
|
try testing.expectEqualSlices(u8, "NetworkError", Error.toString(Error.NetworkError));
|
|
try testing.expectEqualSlices(u8, "AddressInUse", Error.toString(Error.AddressInUse));
|
|
}
|
|
|
|
test "error conversion helpers" {
|
|
try testing.expect(Error.ConnectionRefused == Error.fromInt(-2));
|
|
try testing.expect(Error.AddressInUse == Error.fromInt(-3));
|
|
try testing.expect(Error.NetworkError == Error.fromInt(-1));
|
|
}
|
|
|
|
test "assert operations" {
|
|
// Test successful assertion
|
|
const result = Error.assertWithError(true, Error.ConnectionRefused, "should not error");
|
|
try testing.expect(result == void);
|
|
|
|
// Test failed assertion
|
|
const error_result = Error.assertWithError(false, Error.ConnectionRefused, "should error");
|
|
try testing.expectError(Error.ConnectionRefused, error_result);
|
|
}
|
|
|
|
test "error stats operations" {
|
|
var stats = ErrorStats.init();
|
|
|
|
// Test initial state
|
|
try testing.expectEqual(@as(u64, 0), stats.getTotal());
|
|
try testing.expectEqual(@as(u64, 0), stats.getCount(Error.ConnectionRefused));
|
|
|
|
// Record some errors
|
|
stats.record(Error.ConnectionRefused);
|
|
stats.record(Error.ConnectionRefused);
|
|
stats.record(Error.AddressInUse);
|
|
|
|
// Verify statistics
|
|
try testing.expectEqual(@as(u64, 3), stats.getTotal());
|
|
try testing.expectEqual(@as(u64, 2), stats.getCount(Error.ConnectionRefused));
|
|
try testing.expectEqual(@as(u64, 1), stats.getCount(Error.AddressInUse));
|
|
try testing.expectEqual(@as(u64, 0), stats.getCount(Error.NetworkError));
|
|
|
|
// Test utilization calculation
|
|
const utilization = stats.utilization();
|
|
try testing.expect(utilization >= 0.0 and utilization <= 1.0);
|
|
}
|
|
|
|
test "global error statistics" {
|
|
var stats = ErrorStats.init();
|
|
Error.setGlobalErrorStats(&stats);
|
|
|
|
try testing.expect(Error.getGlobalErrorStats() == &stats);
|
|
|
|
// Record global errors
|
|
Error.recordGlobalError(Error.ConnectionRefused);
|
|
Error.recordGlobalError(Error.AddressInUse);
|
|
Error.recordGlobalError(Error.ConnectionRefused);
|
|
|
|
// Verify global stats
|
|
try testing.expectEqual(@as(u64, 3), stats.getTotal());
|
|
try testing.expectEqual(@as(u64, 2), stats.getCount(Error.ConnectionRefused));
|
|
try testing.expectEqual(@as(u64, 1), stats.getCount(Error.AddressInUse));
|
|
}
|
|
|
|
test "error wrapping" {
|
|
const wrapped = Error.wrapError(Error.ConnectionRefused, Error.NetworkError);
|
|
try testing.expectError(Error.ConnectionRefused, wrapped);
|
|
}
|
|
|
|
test "macros and utilities" {
|
|
// Test error creation macro
|
|
const err = Error.err(Error.ConnectionRefused, "test error", .{});
|
|
try testing.expect(Error.ConnectionRefused == err);
|
|
|
|
// Test unexpected macro
|
|
_ = Error.assert(true, "should not panic");
|
|
|
|
// Test unreachable macro
|
|
// This would panic if called, so we just verify it exists
|
|
_ = Error.unreachable;
|
|
} |