Skip to content

Commit d438c15

Browse files
committed
chore: use named errors for tests
Signed-off-by: markkovari <kovarimarkofficial@gmail.com>
1 parent 9046a9c commit d438c15

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

2025/zig/src/day01/day01.zig

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Direction = enum { Left, Right };
66
const RotationParseError = error{
77
InvalidDirection,
88
InvalidAmount,
9+
InvalidLength,
910
};
1011

1112
fn Rotation(comptime T: type) type {
@@ -16,6 +17,9 @@ fn Rotation(comptime T: type) type {
1617
}
1718

1819
fn parseFromString(comptime T: type, input: []const u8) RotationParseError!Rotation(T) {
20+
if (input.len < 2) {
21+
return RotationParseError.InvalidLength;
22+
}
1923
const direction: Direction = switch (input[0]) {
2024
'L' => .Left,
2125
'R' => .Right,
@@ -66,7 +70,7 @@ test "parse to struct" {
6670

6771
test "parse to struct with error" {
6872
const asText = "G23";
69-
try std.testing.expectError(error.InvalidDirection, parseFromString(u32, asText));
73+
try std.testing.expectError(RotationParseError.InvalidDirection, parseFromString(u32, asText));
7074
}
7175

7276
test "parse multiple lines" {
@@ -93,5 +97,10 @@ test "parse multiple lines with error fails early" {
9397
\\R25
9498
;
9599
const allocator = std.testing.allocator;
96-
try std.testing.expectError(error.InvalidDirection, parseAllLines(u32, allocator, input));
100+
try std.testing.expectError(RotationParseError.InvalidDirection, parseAllLines(u32, allocator, input));
101+
}
102+
103+
test "parse with invalid length" {
104+
const asText = "G";
105+
try std.testing.expectError(RotationParseError.InvalidLength, parseFromString(u32, asText));
97106
}

0 commit comments

Comments
 (0)