Skip to content

Commit ac41fb4

Browse files
committed
Reformat code
1 parent 0f97f39 commit ac41fb4

File tree

5 files changed

+143
-70
lines changed

5 files changed

+143
-70
lines changed

check.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
IFS=$'\n\t'
5+
6+
# Default values
7+
TEST_FILTER=""
8+
TEST_LOG_CAPTURE="true"
9+
TEST_FAIL_FIRST="false"
10+
TEST_VERBOSE="true"
11+
CI_MODE=false
12+
13+
# Parse arguments
14+
usage() {
15+
echo "Usage: $0 [--test-filter \"test name\"] [--test-log-capture true|false] [--test-fail-first true|false] [--test-verbose true|false] [--ci]"
16+
}
17+
while [[ $# -gt 0 ]]; do
18+
case $1 in
19+
-h|--help)
20+
usage
21+
exit 0
22+
;;
23+
--test-filter)
24+
[[ $# -ge 2 ]] || { echo "--test-filter requires an argument"; usage; exit 1; }
25+
TEST_FILTER="$2"; shift 2
26+
;;
27+
--test-log-capture)
28+
[[ $# -ge 2 ]] || { echo "--test-log-capture requires true|false"; usage; exit 1; }
29+
TEST_LOG_CAPTURE="$2"; shift 2
30+
;;
31+
--test-fail-first)
32+
[[ $# -ge 2 ]] || { echo "--test-fail-first requires true|false"; usage; exit 1; }
33+
TEST_FAIL_FIRST="$2"; shift 2
34+
;;
35+
--test-verbose)
36+
[[ $# -ge 2 ]] || { echo "--test-verbose requires true|false"; usage; exit 1; }
37+
TEST_VERBOSE="$2"; shift 2
38+
;;
39+
--ci)
40+
CI_MODE=true
41+
shift
42+
;;
43+
*)
44+
echo "Unknown option: $1"
45+
usage
46+
exit 1
47+
;;
48+
esac
49+
done
50+
51+
echo "=== Formatting code ==="
52+
if [ "$CI_MODE" = true ]; then
53+
echo "Checking formatting (CI mode)..."
54+
zig fmt --check .
55+
else
56+
echo "Formatting code..."
57+
zig fmt .
58+
fi
59+
60+
# Set up environment variables for tests
61+
if [ -n "$TEST_FILTER" ]; then
62+
export TEST_FILTER
63+
fi
64+
export TEST_LOG_CAPTURE="$TEST_LOG_CAPTURE"
65+
export TEST_FAIL_FIRST="$TEST_FAIL_FIRST"
66+
export TEST_VERBOSE="$TEST_VERBOSE"
67+
68+
echo "=== Running unit tests ==="
69+
if [ -n "$TEST_FILTER" ]; then
70+
echo "Running unit tests with filter: $TEST_FILTER"
71+
else
72+
echo "Running all unit tests..."
73+
fi
74+
zig build test --summary all

src/enum.zig

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn getEnumSize(comptime T: type, value: T) usize {
3030
return 1; // size of null
3131
}
3232
}
33-
33+
3434
const tag_type = @typeInfo(T).@"enum".tag_type;
3535
const int_value = @intFromEnum(value);
3636
return getIntSize(tag_type, int_value);
@@ -39,23 +39,23 @@ pub fn getEnumSize(comptime T: type, value: T) usize {
3939
pub fn packEnum(writer: anytype, comptime T: type, value_or_maybe_null: T) !void {
4040
const Type = assertEnumType(T);
4141
const value: Type = try maybePackNull(writer, T, value_or_maybe_null) orelse return;
42-
42+
4343
const tag_type = @typeInfo(Type).@"enum".tag_type;
4444
const int_value = @intFromEnum(value);
45-
45+
4646
try packInt(writer, tag_type, int_value);
4747
}
4848

4949
pub fn unpackEnum(reader: anytype, comptime T: type) !T {
5050
const Type = assertEnumType(T);
5151
const tag_type = @typeInfo(Type).@"enum".tag_type;
52-
52+
5353
// Construct the optional tag type to match T's optionality
5454
const OptionalTagType = if (@typeInfo(T) == .optional) ?tag_type else tag_type;
55-
55+
5656
// Use unpackInt directly with the constructed optional tag type
5757
const int_value = try unpackInt(reader, OptionalTagType);
58-
58+
5959
// Handle the optional case
6060
if (@typeInfo(T) == .optional) {
6161
if (int_value) |value| {
@@ -72,15 +72,15 @@ test "getMaxEnumSize" {
7272
const PlainEnum = enum { foo, bar };
7373
const U8Enum = enum(u8) { foo = 1, bar = 2 };
7474
const U16Enum = enum(u16) { foo, bar };
75-
75+
7676
try std.testing.expectEqual(2, getMaxEnumSize(PlainEnum)); // u1 + header
7777
try std.testing.expectEqual(2, getMaxEnumSize(U8Enum)); // u8 + header
7878
try std.testing.expectEqual(3, getMaxEnumSize(U16Enum)); // u16 + header
7979
}
8080

8181
test "getEnumSize" {
8282
const U8Enum = enum(u8) { foo = 0, bar = 150 };
83-
83+
8484
try std.testing.expectEqual(1, getEnumSize(U8Enum, .foo)); // fits in positive fixint
8585
try std.testing.expectEqual(2, getEnumSize(U8Enum, .bar)); // requires u8 format
8686
}
@@ -89,59 +89,58 @@ test "pack/unpack enum" {
8989
const PlainEnum = enum { foo, bar };
9090
const U8Enum = enum(u8) { foo = 1, bar = 2 };
9191
const U16Enum = enum(u16) { alpha = 1000, beta = 2000 };
92-
92+
9393
// Test plain enum
9494
{
9595
var buffer = std.ArrayList(u8){};
9696
defer buffer.deinit(std.testing.allocator);
97-
97+
9898
try packEnum(buffer.writer(std.testing.allocator), PlainEnum, .bar);
99-
99+
100100
var stream = std.io.fixedBufferStream(buffer.items);
101101
const result = try unpackEnum(stream.reader(), PlainEnum);
102102
try std.testing.expectEqual(PlainEnum.bar, result);
103103
}
104-
104+
105105
// Test enum(u8)
106106
{
107107
var buffer = std.ArrayList(u8){};
108108
defer buffer.deinit(std.testing.allocator);
109-
109+
110110
try packEnum(buffer.writer(std.testing.allocator), U8Enum, .bar);
111-
111+
112112
var stream = std.io.fixedBufferStream(buffer.items);
113113
const result = try unpackEnum(stream.reader(), U8Enum);
114114
try std.testing.expectEqual(U8Enum.bar, result);
115115
}
116-
117-
// Test enum(u16)
116+
117+
// Test enum(u16)
118118
{
119119
var buffer = std.ArrayList(u8){};
120120
defer buffer.deinit(std.testing.allocator);
121-
121+
122122
try packEnum(buffer.writer(std.testing.allocator), U16Enum, .alpha);
123-
123+
124124
var stream = std.io.fixedBufferStream(buffer.items);
125125
const result = try unpackEnum(stream.reader(), U16Enum);
126126
try std.testing.expectEqual(U16Enum.alpha, result);
127127
}
128128
}
129129

130-
131130
test "enum edge cases" {
132131
// Test enum with explicit and auto values
133-
const MixedEnum = enum(u8) {
132+
const MixedEnum = enum(u8) {
134133
first = 10,
135134
second, // auto-assigned to 11
136135
third = 20,
137136
fourth, // auto-assigned to 21
138137
};
139-
138+
140139
var buffer = std.ArrayList(u8){};
141140
defer buffer.deinit(std.testing.allocator);
142-
141+
143142
try packEnum(buffer.writer(std.testing.allocator), MixedEnum, .second);
144-
143+
145144
var stream = std.io.fixedBufferStream(buffer.items);
146145
const result = try unpackEnum(stream.reader(), MixedEnum);
147146
try std.testing.expectEqual(MixedEnum.second, result);
@@ -151,28 +150,28 @@ test "enum edge cases" {
151150
test "optional enum" {
152151
const TestEnum = enum(u8) { foo = 1, bar = 2 };
153152
const OptionalEnum = ?TestEnum;
154-
153+
155154
// Test non-null optional enum
156155
{
157156
var buffer = std.ArrayList(u8){};
158157
defer buffer.deinit(std.testing.allocator);
159-
158+
160159
const value: OptionalEnum = .bar;
161160
try packEnum(buffer.writer(std.testing.allocator), OptionalEnum, value);
162-
161+
163162
var stream = std.io.fixedBufferStream(buffer.items);
164163
const result = try unpackEnum(stream.reader(), OptionalEnum);
165164
try std.testing.expectEqual(@as(OptionalEnum, .bar), result);
166165
}
167-
166+
168167
// Test null optional enum
169168
{
170169
var buffer = std.ArrayList(u8){};
171170
defer buffer.deinit(std.testing.allocator);
172-
171+
173172
const value: OptionalEnum = null;
174173
try packEnum(buffer.writer(std.testing.allocator), OptionalEnum, value);
175-
174+
176175
var stream = std.io.fixedBufferStream(buffer.items);
177176
const result = try unpackEnum(stream.reader(), OptionalEnum);
178177
try std.testing.expectEqual(@as(OptionalEnum, null), result);
@@ -182,12 +181,12 @@ test "optional enum" {
182181
test "getEnumSize with optional" {
183182
const TestEnum = enum(u8) { foo = 0, bar = 150 };
184183
const OptionalEnum = ?TestEnum;
185-
184+
186185
// Test non-null optional enum size
187186
const value: OptionalEnum = .bar;
188187
try std.testing.expectEqual(2, getEnumSize(OptionalEnum, value)); // requires u8 format
189-
188+
190189
// Test null optional enum size
191190
const null_value: OptionalEnum = null;
192191
try std.testing.expectEqual(1, getEnumSize(OptionalEnum, null_value)); // size of null
193-
}
192+
}

src/msgpack.zig

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -321,56 +321,56 @@ test "encode/decode" {
321321
test "encode/decode enum" {
322322
const Status = enum(u8) { pending = 1, active = 2, inactive = 3 };
323323
const PlainEnum = enum { foo, bar, baz };
324-
324+
325325
// Test enum(u8)
326326
{
327327
var buffer = std.ArrayList(u8){};
328328
defer buffer.deinit(std.testing.allocator);
329-
329+
330330
try encode(Status.active, buffer.writer(std.testing.allocator));
331-
331+
332332
const decoded = try decodeFromSlice(Status, std.testing.allocator, buffer.items);
333333
defer decoded.deinit();
334-
334+
335335
try std.testing.expectEqual(Status.active, decoded.value);
336336
}
337-
338-
// Test plain enum
337+
338+
// Test plain enum
339339
{
340340
var buffer = std.ArrayList(u8){};
341341
defer buffer.deinit(std.testing.allocator);
342-
342+
343343
try encode(PlainEnum.bar, buffer.writer(std.testing.allocator));
344-
344+
345345
const decoded = try decodeFromSlice(PlainEnum, std.testing.allocator, buffer.items);
346346
defer decoded.deinit();
347-
347+
348348
try std.testing.expectEqual(PlainEnum.bar, decoded.value);
349349
}
350-
350+
351351
// Test optional enum with null
352352
{
353353
var buffer = std.ArrayList(u8){};
354354
defer buffer.deinit(std.testing.allocator);
355-
355+
356356
try encode(@as(?Status, null), buffer.writer(std.testing.allocator));
357-
357+
358358
const decoded = try decodeFromSlice(?Status, std.testing.allocator, buffer.items);
359359
defer decoded.deinit();
360-
360+
361361
try std.testing.expectEqual(@as(?Status, null), decoded.value);
362362
}
363-
363+
364364
// Test optional enum with value
365365
{
366366
var buffer = std.ArrayList(u8){};
367367
defer buffer.deinit(std.testing.allocator);
368-
368+
369369
try encode(@as(?Status, .pending), buffer.writer(std.testing.allocator));
370-
370+
371371
const decoded = try decodeFromSlice(?Status, std.testing.allocator, buffer.items);
372372
defer decoded.deinit();
373-
373+
374374
try std.testing.expectEqual(@as(?Status, .pending), decoded.value);
375375
}
376376
}

src/struct.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ test "writeStruct: optional struct field with null default" {
600600
const InnerStruct = struct {
601601
x: u32,
602602
};
603-
603+
604604
const OuterStruct = struct {
605605
a: u32,
606606
inner: ?InnerStruct = null,
@@ -609,7 +609,7 @@ test "writeStruct: optional struct field with null default" {
609609
return .{ .as_map = .{ .key = .field_name, .omit_defaults = true } };
610610
}
611611
};
612-
612+
613613
const msg = OuterStruct{ .a = 42 };
614614

615615
var buffer: [100]u8 = undefined;

0 commit comments

Comments
 (0)