commit af3d9f29839d49b7d8a96f47a0d24f7a917e0bfa (tree)
parent 955df90c723859efb4a836d0b511f64dea294ab6
Author: Motiejus Jakštys <motiejus.jakstys@chronosphere.io>
Date: Tue, 10 Feb 2026 18:20:11 +0000
parser: reorder tests to match upstream file order
Reorder all test blocks in parser_test.zig to match the order
they appear in the upstream zig/lib/std/zig/parser_test.zig.
Tests not in upstream ("Ast header smoke test", "my function")
are placed at the end.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Diffstat:
| M | parser_test.zig | | | 1186 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 593 insertions(+), 593 deletions(-)
diff --git a/parser_test.zig b/parser_test.zig
@@ -476,9 +476,6 @@ fn zigAst(gpa: Allocator, c_ast: c.Ast) !Ast {
};
}
-test "Ast header smoke test" {
- try std.testing.expectEqual(zigNode(c.AST_NODE_IF), Ast.Node.Tag.@"if");
-}
// copy-past from parser_test.zig
const mem = std.mem;
@@ -557,16 +554,6 @@ test "zig fmt: remove extra whitespace at start and end of file with comment bet
);
}
-
-test "my function" {
- try testCanonical(
- \\pub fn main() void {
- \\ @panic("hello");
- \\}
- \\
- );
-}
-
test "zig fmt: tuple struct" {
try testCanonical(
\\const T = struct {
@@ -721,6 +708,16 @@ test "zig fmt: file ends in comment after var decl" {
);
}
+test "zig fmt: if statement" {
+ try testCanonical(
+ \\test "" {
+ \\ if (optional()) |some|
+ \\ bar = some.foo();
+ \\}
+ \\
+ );
+}
+
test "zig fmt: top-level fields" {
try testCanonical(
\\a: did_you_know,
@@ -730,6 +727,51 @@ test "zig fmt: top-level fields" {
);
}
+test "zig fmt: top-level tuple function call type" {
+ try testCanonical(
+ \\foo()
+ \\
+ );
+}
+
+test "zig fmt: top-level bare asterisk+identifier" {
+ try testCanonical(
+ \\*x
+ \\
+ );
+}
+
+test "zig fmt: top-level bare asterisk+asterisk+identifier" {
+ try testCanonical(
+ \\**x
+ \\
+ );
+}
+
+test "zig fmt: errdefer with payload" {
+ try testCanonical(
+ \\pub fn main() anyerror!void {
+ \\ errdefer |a| x += 1;
+ \\ errdefer |a| {}
+ \\ errdefer |a| {
+ \\ x += 1;
+ \\ }
+ \\}
+ \\
+ );
+}
+
+test "zig fmt: nosuspend block" {
+ try testCanonical(
+ \\pub fn main() anyerror!void {
+ \\ nosuspend {
+ \\ var foo: Foo = .{ .bar = 42 };
+ \\ }
+ \\}
+ \\
+ );
+}
+
test "zig fmt: container declaration, single line" {
try testCanonical(
\\const X = struct { foo: i32 };
@@ -880,6 +922,26 @@ test "zig fmt: remove empty lines at start/end of container decl" {
);
}
+test "zig fmt: remove empty lines at start/end of block" {
+ try testTransform(
+ \\test {
+ \\
+ \\ if (foo) {
+ \\ foo();
+ \\ }
+ \\
+ \\}
+ \\
+ ,
+ \\test {
+ \\ if (foo) {
+ \\ foo();
+ \\ }
+ \\}
+ \\
+ );
+}
+
test "zig fmt: allow empty line before comment at start of block" {
try testCanonical(
\\test {
@@ -891,6 +953,48 @@ test "zig fmt: allow empty line before comment at start of block" {
);
}
+test "zig fmt: trailing comma in fn parameter list" {
+ try testCanonical(
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) align(8) i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) addrspace(.generic) i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) linksection(".text") i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) callconv(.c) i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) align(8) linksection(".text") i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) align(8) callconv(.c) i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) align(8) linksection(".text") callconv(.c) i32 {}
+ \\pub fn f(
+ \\ a: i32,
+ \\ b: i32,
+ \\) linksection(".text") callconv(.c) i32 {}
+ \\
+ );
+}
+
test "zig fmt: comptime struct field" {
try testCanonical(
\\const Foo = struct {
@@ -926,6 +1030,28 @@ test "zig fmt: grouped expressions (parentheses)" {
);
}
+test "zig fmt: c pointer type" {
+ try testCanonical(
+ \\pub extern fn repro() [*c]const u8;
+ \\
+ );
+}
+
+test "zig fmt: builtin call with trailing comma" {
+ try testCanonical(
+ \\pub fn main() void {
+ \\ @breakpoint();
+ \\ _ = @intFromBool(a);
+ \\ _ = @call(
+ \\ a,
+ \\ b,
+ \\ c,
+ \\ );
+ \\}
+ \\
+ );
+}
+
test "zig fmt: array types last token" {
try testCanonical(
\\test {
@@ -939,6 +1065,104 @@ test "zig fmt: array types last token" {
);
}
+test "zig fmt: sentinel-terminated array type" {
+ try testCanonical(
+ \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {
+ \\ return sliceToPrefixedFileW(mem.toSliceConst(u8, s));
+ \\}
+ \\
+ );
+}
+
+test "zig fmt: sentinel-terminated slice type" {
+ try testCanonical(
+ \\pub fn toSlice(self: Buffer) [:0]u8 {
+ \\ return self.list.toSlice()[0..self.len()];
+ \\}
+ \\
+ );
+}
+
+test "zig fmt: pointer-to-one with modifiers" {
+ try testCanonical(
+ \\const x: *u32 = undefined;
+ \\const y: *allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+ \\const z: *allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
+ \\
+ );
+}
+
+test "zig fmt: pointer-to-many with modifiers" {
+ try testCanonical(
+ \\const x: [*]u32 = undefined;
+ \\const y: [*]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+ \\const z: [*]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
+ \\
+ );
+}
+
+test "zig fmt: sentinel pointer with modifiers" {
+ try testCanonical(
+ \\const x: [*:42]u32 = undefined;
+ \\const y: [*:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+ \\const y: [*:42]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
+ \\
+ );
+}
+
+test "zig fmt: c pointer with modifiers" {
+ try testCanonical(
+ \\const x: [*c]u32 = undefined;
+ \\const y: [*c]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+ \\const z: [*c]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
+ \\
+ );
+}
+
+test "zig fmt: slice with modifiers" {
+ try testCanonical(
+ \\const x: []u32 = undefined;
+ \\const y: []allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+ \\
+ );
+}
+
+test "zig fmt: sentinel slice with modifiers" {
+ try testCanonical(
+ \\const x: [:42]u32 = undefined;
+ \\const y: [:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+ \\
+ );
+}
+
+test "zig fmt: anon literal in array" {
+ try testCanonical(
+ \\var arr: [2]Foo = .{
+ \\ .{ .a = 2 },
+ \\ .{ .b = 3 },
+ \\};
+ \\
+ );
+}
+
+test "zig fmt: alignment in anonymous literal" {
+ try testTransform(
+ \\const a = .{
+ \\ "U", "L", "F",
+ \\ "U'",
+ \\ "L'",
+ \\ "F'",
+ \\};
+ \\
+ ,
+ \\const a = .{
+ \\ "U", "L", "F",
+ \\ "U'", "L'", "F'",
+ \\};
+ \\
+ );
+}
+
test "zig fmt: anon struct literal 0 element" {
try testCanonical(
\\test {
@@ -1029,10 +1253,21 @@ test "zig fmt: struct literal 1 element" {
);
}
-test "zig fmt: struct literal 2 element" {
+test "zig fmt: Unicode code point literal larger than u8" {
try testCanonical(
\\test {
- \\ const x = X{ .a = b, .c = d };
+ \\ const x = X{
+ \\ .a = b,
+ \\ };
+ \\}
+ \\
+ );
+}
+
+test "zig fmt: struct literal 2 element" {
+ try testCanonical(
+ \\test {
+ \\ const x = X{ .a = b, .c = d };
\\}
\\
);
@@ -1209,6 +1444,15 @@ test "zig fmt: array literal 3 element comma" {
);
}
+test "zig fmt: sentinel array literal 1 element" {
+ try testCanonical(
+ \\test {
+ \\ const x = [_:9000]u32{a};
+ \\}
+ \\
+ );
+}
+
test "zig fmt: slices" {
try testCanonical(
\\const a = b[0..];
@@ -1219,6 +1463,34 @@ test "zig fmt: slices" {
);
}
+test "zig fmt: slices with spaces in bounds" {
+ try testCanonical(
+ \\const a = b[0 + 0 ..];
+ \\const c = d[0 + 0 .. 1];
+ \\const c = d[0 + 0 .. :0];
+ \\const e = f[0 .. 1 + 1 :0];
+ \\
+ );
+}
+
+test "zig fmt: block in slice expression" {
+ try testCanonical(
+ \\const a = b[{
+ \\ _ = x;
+ \\}..];
+ \\const c = d[0..{
+ \\ _ = x;
+ \\ _ = y;
+ \\}];
+ \\const e = f[0..1 :{
+ \\ _ = x;
+ \\ _ = y;
+ \\ _ = z;
+ \\}];
+ \\
+ );
+}
+
test "zig fmt: tagged union with enum values" {
try testCanonical(
\\const MultipleChoice2 = union(enum(u32)) {
@@ -1255,6 +1527,13 @@ test "zig fmt: tagged union enum tag last token" {
);
}
+test "zig fmt: allowzero pointer" {
+ try testCanonical(
+ \\const T = [*]allowzero const u8;
+ \\
+ );
+}
+
test "zig fmt: empty enum decls" {
try testCanonical(
\\const A = enum {};
@@ -1283,6 +1562,21 @@ test "zig fmt: enum literal" {
);
}
+test "zig fmt: enum literal inside array literal" {
+ try testCanonical(
+ \\test "enums in arrays" {
+ \\ var colors = []Color{.Green};
+ \\ colors = []Colors{ .Green, .Cyan };
+ \\ colors = []Colors{
+ \\ .Grey,
+ \\ .Green,
+ \\ .Cyan,
+ \\ };
+ \\}
+ \\
+ );
+}
+
test "zig fmt: character literal larger than u8" {
try testCanonical(
\\const x = '\u{01f4a9}';
@@ -1310,6 +1604,38 @@ test "zig fmt: infix operator and then multiline string literal over multiple li
);
}
+test "zig fmt: C pointers" {
+ try testCanonical(
+ \\const Ptr = [*c]i32;
+ \\
+ );
+}
+
+test "zig fmt: threadlocal" {
+ try testCanonical(
+ \\threadlocal var x: i32 = 1234;
+ \\
+ );
+}
+
+test "zig fmt: linksection" {
+ try testCanonical(
+ \\export var aoeu: u64 linksection(".text.derp") = 1234;
+ \\export fn _start() linksection(".text.boot") callconv(.naked) noreturn {}
+ \\
+ );
+}
+
+test "zig fmt: addrspace" {
+ try testCanonical(
+ \\export var python_length: u64 align(1) addrspace(.generic);
+ \\export var python_color: Color addrspace(.generic) = .green;
+ \\export var python_legs: u0 align(8) addrspace(.generic) linksection(".python") = 0;
+ \\export fn python_hiss() align(8) addrspace(.generic) linksection(".python") void;
+ \\
+ );
+}
+
test "zig fmt: correctly space struct fields with doc comments" {
try testTransform(
\\pub const S = struct {
@@ -1339,6 +1665,27 @@ test "zig fmt: correctly space struct fields with doc comments" {
);
}
+test "zig fmt: doc comments on param decl" {
+ try testCanonical(
+ \\pub const Allocator = struct {
+ \\ shrinkFn: fn (
+ \\ self: Allocator,
+ \\ /// Guaranteed to be the same as what was returned from most recent call to
+ \\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
+ \\ old_mem: []u8,
+ \\ /// Guaranteed to be the same as what was returned from most recent call to
+ \\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
+ \\ old_alignment: u29,
+ \\ /// Guaranteed to be less than or equal to `old_mem.len`.
+ \\ new_byte_count: usize,
+ \\ /// Guaranteed to be less than or equal to `old_alignment`.
+ \\ new_alignment: u29,
+ \\ ) []u8,
+ \\};
+ \\
+ );
+}
+
test "zig fmt: aligned struct field" {
try testCanonical(
\\pub const S = struct {
@@ -1363,74 +1710,21 @@ test "zig fmt: comment to disable/enable zig fmt first" {
);
}
-test "zig fmt: trailing comma in fn parameter list" {
- try testCanonical(
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) align(8) i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) addrspace(.generic) i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) linksection(".text") i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) callconv(.c) i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) align(8) linksection(".text") i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) align(8) callconv(.c) i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) align(8) linksection(".text") callconv(.c) i32 {}
- \\pub fn f(
- \\ a: i32,
- \\ b: i32,
- \\) linksection(".text") callconv(.c) i32 {}
+test "zig fmt: 'zig fmt: (off|on)' can be surrounded by arbitrary whitespace" {
+ try testTransform(
+ \\// Test trailing comma syntax
+ \\// zig fmt: off
\\
- );
-}
-
-test "zig fmt: enum literal inside array literal" {
- try testCanonical(
- \\test "enums in arrays" {
- \\ var colors = []Color{.Green};
- \\ colors = []Colors{ .Green, .Cyan };
- \\ colors = []Colors{
- \\ .Grey,
- \\ .Green,
- \\ .Cyan,
- \\ };
- \\}
+ \\const struct_trailing_comma = struct { x: i32, y: i32, };
\\
- );
-}
-
-test "zig fmt: builtin call with trailing comma" {
- try testCanonical(
- \\pub fn main() void {
- \\ @breakpoint();
- \\ _ = @intFromBool(a);
- \\ _ = @call(
- \\ a,
- \\ b,
- \\ c,
- \\ );
- \\}
+ \\// zig fmt: on
+ ,
+ \\// Test trailing comma syntax
+ \\// zig fmt: off
+ \\
+ \\const struct_trailing_comma = struct { x: i32, y: i32, };
+ \\
+ \\// zig fmt: on
\\
);
}
@@ -1468,6 +1762,24 @@ test "zig fmt: doc comment following 'zig fmt: off'" {
);
}
+test "zig fmt: line and doc comment following 'zig fmt: off'" {
+ try testCanonical(
+ \\// zig fmt: off
+ \\// test 1
+ \\/// test 2
+ \\const e = f;
+ );
+}
+
+test "zig fmt: doc and line comment following 'zig fmt: off'" {
+ try testCanonical(
+ \\// zig fmt: off
+ \\/// test 1
+ \\// test 2
+ \\const e = f;
+ );
+}
+
test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" {
try testCanonical(
\\// zig fmt: off
@@ -1485,72 +1797,69 @@ test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" {
);
}
-test "zig fmt: spaces around slice operator" {
+test "zig fmt: line comment following 'zig fmt: on'" {
try testCanonical(
- \\var a = b[c..d];
- \\var a = b[c..d :0];
- \\var a = b[c + 1 .. d];
- \\var a = b[c + 1 ..];
- \\var a = b[c .. d + 1];
- \\var a = b[c .. d + 1 :0];
- \\var a = b[c.a..d.e];
- \\var a = b[c.a..d.e :0];
+ \\// zig fmt: off
+ \\const e = f;
+ \\// zig fmt: on
+ \\// test
+ \\const e = f;
\\
);
}
-test "zig fmt: respect line breaks after infix operators" {
+test "zig fmt: doc comment following 'zig fmt: on'" {
try testCanonical(
- \\comptime {
- \\ self.crc =
- \\ lookup_tables[0][p[7]] ^
- \\ lookup_tables[1][p[6]] ^
- \\ lookup_tables[2][p[5]] ^
- \\ lookup_tables[3][p[4]] ^
- \\ lookup_tables[4][@as(u8, self.crc >> 24)] ^
- \\ lookup_tables[5][@as(u8, self.crc >> 16)] ^
- \\ lookup_tables[6][@as(u8, self.crc >> 8)] ^
- \\ lookup_tables[7][@as(u8, self.crc >> 0)];
- \\}
+ \\// zig fmt: off
+ \\const e = f;
+ \\// zig fmt: on
+ \\/// test
+ \\const e = f;
\\
);
}
-test "zig fmt: fn decl with trailing comma" {
- try testTransform(
- \\fn foo(a: i32, b: i32,) void {}
- ,
- \\fn foo(
- \\ a: i32,
- \\ b: i32,
- \\) void {}
+test "zig fmt: line and doc comment following 'zig fmt: on'" {
+ try testCanonical(
+ \\// zig fmt: off
+ \\const e = f;
+ \\// zig fmt: on
+ \\// test1
+ \\/// test2
+ \\const e = f;
\\
);
}
-test "zig fmt: enum decl with no trailing comma" {
- try testTransform(
- \\const StrLitKind = enum {Normal, C};
- ,
- \\const StrLitKind = enum { Normal, C };
+test "zig fmt: doc and line comment following 'zig fmt: on'" {
+ try testCanonical(
+ \\// zig fmt: off
+ \\const e = f;
+ \\// zig fmt: on
+ \\/// test1
+ \\// test2
+ \\const e = f;
\\
);
}
-test "zig fmt: struct literal no trailing comma" {
- try testTransform(
- \\const a = foo{ .x = 1, .y = 2 };
- \\const a = foo{ .x = 1,
- \\ .y = 2 };
- \\const a = foo{ .x = 1,
- \\ .y = 2, };
- ,
- \\const a = foo{ .x = 1, .y = 2 };
- \\const a = foo{ .x = 1, .y = 2 };
- \\const a = foo{
- \\ .x = 1,
- \\ .y = 2,
- \\};
+test "zig fmt: pointer of unknown length" {
+ try testCanonical(
+ \\fn foo(ptr: [*]u8) void {}
+ \\
+ );
+}
+
+test "zig fmt: spaces around slice operator" {
+ try testCanonical(
+ \\var a = b[c..d];
+ \\var a = b[c..d :0];
+ \\var a = b[c + 1 .. d];
+ \\var a = b[c + 1 ..];
+ \\var a = b[c .. d + 1];
+ \\var a = b[c .. d + 1 :0];
+ \\var a = b[c.a..d.e];
+ \\var a = b[c.a..d.e :0];
\\
);
}
@@ -1597,11 +1906,42 @@ test "zig fmt: function call with multiline argument" {
);
}
-test "zig fmt: if statement" {
+test "zig fmt: if-else with comment before else" {
try testCanonical(
- \\test "" {
- \\ if (optional()) |some|
- \\ bar = some.foo();
+ \\comptime {
+ \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
+ \\ if ((hx & 0x7fffffff) != 0x7f800000) {
+ \\ return Complex(f32).init(y - y, y - y);
+ \\ } // cexp(-inf +- i inf|nan) = 0 + i0
+ \\ else if (hx & 0x80000000 != 0) {
+ \\ return Complex(f32).init(0, 0);
+ \\ } // cexp(+inf +- i inf|nan) = inf + i nan
+ \\ else {
+ \\ return Complex(f32).init(x, y - y);
+ \\ }
+ \\}
+ \\
+ );
+}
+
+test "zig fmt: if nested" {
+ try testCanonical(
+ \\pub fn foo() void {
+ \\ return if ((aInt & bInt) >= 0)
+ \\ if (aInt < bInt)
+ \\ GE_LESS
+ \\ else if (aInt == bInt)
+ \\ GE_EQUAL
+ \\ else
+ \\ GE_GREATER
+ \\ // comment
+ \\ else if (aInt > bInt)
+ \\ GE_LESS
+ \\ else if (aInt == bInt)
+ \\ GE_EQUAL
+ \\ else
+ \\ GE_GREATER;
+ \\ // comment
\\}
\\
);
@@ -1626,45 +1966,58 @@ test "zig fmt: respect line breaks in if-else" {
);
}
-test "zig fmt: if nested" {
+test "zig fmt: respect line breaks after infix operators" {
try testCanonical(
- \\pub fn foo() void {
- \\ return if ((aInt & bInt) >= 0)
- \\ if (aInt < bInt)
- \\ GE_LESS
- \\ else if (aInt == bInt)
- \\ GE_EQUAL
- \\ else
- \\ GE_GREATER
- \\ // comment
- \\ else if (aInt > bInt)
- \\ GE_LESS
- \\ else if (aInt == bInt)
- \\ GE_EQUAL
- \\ else
- \\ GE_GREATER;
- \\ // comment
+ \\comptime {
+ \\ self.crc =
+ \\ lookup_tables[0][p[7]] ^
+ \\ lookup_tables[1][p[6]] ^
+ \\ lookup_tables[2][p[5]] ^
+ \\ lookup_tables[3][p[4]] ^
+ \\ lookup_tables[4][@as(u8, self.crc >> 24)] ^
+ \\ lookup_tables[5][@as(u8, self.crc >> 16)] ^
+ \\ lookup_tables[6][@as(u8, self.crc >> 8)] ^
+ \\ lookup_tables[7][@as(u8, self.crc >> 0)];
\\}
\\
);
}
-test "zig fmt: remove empty lines at start/end of block" {
+test "zig fmt: fn decl with trailing comma" {
try testTransform(
- \\test {
- \\
- \\ if (foo) {
- \\ foo();
- \\ }
+ \\fn foo(a: i32, b: i32,) void {}
+ ,
+ \\fn foo(
+ \\ a: i32,
+ \\ b: i32,
+ \\) void {}
\\
- \\}
+ );
+}
+
+test "zig fmt: enum decl with no trailing comma" {
+ try testTransform(
+ \\const StrLitKind = enum {Normal, C};
+ ,
+ \\const StrLitKind = enum { Normal, C };
\\
+ );
+}
+
+test "zig fmt: struct literal no trailing comma" {
+ try testTransform(
+ \\const a = foo{ .x = 1, .y = 2 };
+ \\const a = foo{ .x = 1,
+ \\ .y = 2 };
+ \\const a = foo{ .x = 1,
+ \\ .y = 2, };
,
- \\test {
- \\ if (foo) {
- \\ foo();
- \\ }
- \\}
+ \\const a = foo{ .x = 1, .y = 2 };
+ \\const a = foo{ .x = 1, .y = 2 };
+ \\const a = foo{
+ \\ .x = 1,
+ \\ .y = 2,
+ \\};
\\
);
}
@@ -1781,198 +2134,87 @@ test "zig fmt: nested struct literal with one item" {
);
}
-test "zig fmt: if-else with comment before else" {
+test "zig fmt: block with same line comment after end brace" {
try testCanonical(
- \\comptime {
- \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
- \\ if ((hx & 0x7fffffff) != 0x7f800000) {
- \\ return Complex(f32).init(y - y, y - y);
- \\ } // cexp(-inf +- i inf|nan) = 0 + i0
- \\ else if (hx & 0x80000000 != 0) {
- \\ return Complex(f32).init(0, 0);
- \\ } // cexp(+inf +- i inf|nan) = inf + i nan
- \\ else {
- \\ return Complex(f32).init(x, y - y);
- \\ }
+ \\test {
+ \\ {
+ \\ const a = b;
+ \\ } // end of block
\\}
\\
);
}
-test "zig fmt: nosuspend block" {
+test "zig fmt: comments before var decl in struct" {
try testCanonical(
- \\pub fn main() anyerror!void {
- \\ nosuspend {
- \\ var foo: Foo = .{ .bar = 42 };
- \\ }
- \\}
+ \\const Foo = struct {
+ \\ /// comment
+ \\ bar: bool = true,
+ \\};
\\
);
}
-test "zig fmt: c pointer type" {
+test "zig fmt: comments before global variables" {
try testCanonical(
- \\pub extern fn repro() [*c]const u8;
+ \\/// comment
+ \\var foo: i32 = undefined;
\\
);
}
-test "zig fmt: sentinel array literal 1 element" {
+test "zig fmt: comments before test decl" {
try testCanonical(
- \\test {
- \\ const x = [_:9000]u32{a};
- \\}
+ \\/// top level doc comment
+ \\test "hi" {}
\\
);
}
-test "zig fmt: anon literal in array" {
+test "zig fmt: alignment" {
try testCanonical(
- \\var arr: [2]Foo = .{
- \\ .{ .a = 2 },
- \\ .{ .b = 3 },
- \\};
+ \\var foo: c_int align(1);
\\
);
}
-test "zig fmt: Unicode code point literal larger than u8" {
+test "zig fmt: C main" {
try testCanonical(
- \\test {
- \\ const x = X{
- \\ .a = b,
- \\ };
+ \\fn main(argc: c_int, argv: **u8) c_int {
+ \\ const a = b;
\\}
\\
);
}
-test "zig fmt: slices with spaces in bounds" {
+test "zig fmt: return" {
try testCanonical(
- \\const a = b[0 + 0 ..];
- \\const c = d[0 + 0 .. 1];
- \\const c = d[0 + 0 .. :0];
- \\const e = f[0 .. 1 + 1 :0];
+ \\fn foo(argc: c_int, argv: **u8) c_int {
+ \\ return 0;
+ \\}
\\
- );
-}
-
-test "zig fmt: C pointers" {
- try testCanonical(
- \\const Ptr = [*c]i32;
+ \\fn bar() void {
+ \\ return;
+ \\}
\\
);
}
-test "zig fmt: pointer-to-one with modifiers" {
+test "zig fmt: call expression" {
try testCanonical(
- \\const x: *u32 = undefined;
- \\const y: *allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
- \\const z: *allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
+ \\test "test calls" {
+ \\ a();
+ \\ a(1);
+ \\ a(1, 2);
+ \\ a(1, 2) + a(1, 2);
+ \\}
\\
);
}
-test "zig fmt: pointer-to-many with modifiers" {
+test "zig fmt: anytype type" {
try testCanonical(
- \\const x: [*]u32 = undefined;
- \\const y: [*]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
- \\const z: [*]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
- \\
- );
-}
-
-test "zig fmt: sentinel pointer with modifiers" {
- try testCanonical(
- \\const x: [*:42]u32 = undefined;
- \\const y: [*:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
- \\const y: [*:42]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
- \\
- );
-}
-
-test "zig fmt: c pointer with modifiers" {
- try testCanonical(
- \\const x: [*c]u32 = undefined;
- \\const y: [*c]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
- \\const z: [*c]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
- \\
- );
-}
-
-test "zig fmt: slice with modifiers" {
- try testCanonical(
- \\const x: []u32 = undefined;
- \\const y: []allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
- \\
- );
-}
-
-test "zig fmt: sentinel slice with modifiers" {
- try testCanonical(
- \\const x: [:42]u32 = undefined;
- \\const y: [:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
- \\
- );
-}
-
-test "zig fmt: allowzero pointer" {
- try testCanonical(
- \\const T = [*]allowzero const u8;
- \\
- );
-}
-
-test "zig fmt: threadlocal" {
- try testCanonical(
- \\threadlocal var x: i32 = 1234;
- \\
- );
-}
-
-test "zig fmt: linksection" {
- try testCanonical(
- \\export var aoeu: u64 linksection(".text.derp") = 1234;
- \\export fn _start() linksection(".text.boot") callconv(.naked) noreturn {}
- \\
- );
-}
-
-test "zig fmt: addrspace" {
- try testCanonical(
- \\export var python_length: u64 align(1) addrspace(.generic);
- \\export var python_color: Color addrspace(.generic) = .green;
- \\export var python_legs: u0 align(8) addrspace(.generic) linksection(".python") = 0;
- \\export fn python_hiss() align(8) addrspace(.generic) linksection(".python") void;
- \\
- );
-}
-
-test "zig fmt: alignment" {
- try testCanonical(
- \\var foo: c_int align(1);
- \\
- );
-}
-
-test "zig fmt: C main" {
- try testCanonical(
- \\fn main(argc: c_int, argv: **u8) c_int {
- \\ const a = b;
- \\}
- \\
- );
-}
-
-test "zig fmt: return" {
- try testCanonical(
- \\fn foo(argc: c_int, argv: **u8) c_int {
- \\ return 0;
- \\}
- \\
- \\fn bar() void {
- \\ return;
- \\}
+ \\fn print(args: anytype) @This() {}
\\
);
}
@@ -1989,6 +2231,26 @@ test "zig fmt: arrays" {
);
}
+test "zig fmt: container initializers" {
+ try testCanonical(
+ \\const a0 = []u8{};
+ \\const a1 = []u8{1};
+ \\const a2 = []u8{
+ \\ 1,
+ \\ 2,
+ \\ 3,
+ \\ 4,
+ \\};
+ \\const s0 = S{};
+ \\const s1 = S{ .a = 1 };
+ \\const s2 = S{
+ \\ .a = 1,
+ \\ .b = 2,
+ \\};
+ \\
+ );
+}
+
test "zig fmt: blocks" {
try testCanonical(
\\test {
@@ -2001,37 +2263,14 @@ test "zig fmt: blocks" {
);
}
-test "zig fmt: container doc comments" {
- try testCanonical(
- \\//! tld 1
- \\//! tld 2
- \\//! tld 3
- \\const a = b;
- \\
- );
-}
-
-test "zig fmt: comments before global variables" {
- try testCanonical(
- \\/// comment
- \\var foo: i32 = undefined;
- \\
- );
-}
-
-test "zig fmt: comments before test decl" {
- try testCanonical(
- \\/// top level doc comment
- \\test "hi" {}
- \\
- );
-}
-
-test "zig fmt: decimal float literals with underscore separators" {
+test "zig fmt: defer" {
try testCanonical(
- \\const x = 1_234_567.89_10_11;
- \\const y = 1_234_567.89_10_11e1_213_14;
- \\const z = 1_234_567;
+ \\test "defer" {
+ \\ defer foo();
+ \\ defer {
+ \\ bar();
+ \\ }
+ \\}
\\
);
}
@@ -2058,27 +2297,6 @@ test "zig fmt: comptime block in container" {
);
}
-test "zig fmt: comments before var decl in struct" {
- try testCanonical(
- \\const Foo = struct {
- \\ /// comment
- \\ bar: bool = true,
- \\};
- \\
- );
-}
-
-test "zig fmt: block with same line comment after end brace" {
- try testCanonical(
- \\test {
- \\ {
- \\ const a = b;
- \\ } // end of block
- \\}
- \\
- );
-}
-
test "zig fmt: comment after empty comment" {
try testCanonical(
\\//
@@ -2098,251 +2316,33 @@ test "zig fmt: comment after params" {
);
}
-test "zig fmt: doc comments on param decl" {
- try testCanonical(
- \\pub const Allocator = struct {
- \\ shrinkFn: fn (
- \\ self: Allocator,
- \\ /// Guaranteed to be the same as what was returned from most recent call to
- \\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
- \\ old_mem: []u8,
- \\ /// Guaranteed to be the same as what was returned from most recent call to
- \\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
- \\ old_alignment: u29,
- \\ /// Guaranteed to be less than or equal to `old_mem.len`.
- \\ new_byte_count: usize,
- \\ /// Guaranteed to be less than or equal to `old_alignment`.
- \\ new_alignment: u29,
- \\ ) []u8,
- \\};
- \\
- );
-}
-
-test "zig fmt: pointer of unknown length" {
- try testCanonical(
- \\fn foo(ptr: [*]u8) void {}
- \\
- );
-}
-
-test "zig fmt: call expression" {
- try testCanonical(
- \\test "test calls" {
- \\ a();
- \\ a(1);
- \\ a(1, 2);
- \\ a(1, 2) + a(1, 2);
- \\}
- \\
- );
-}
-
-test "zig fmt: anytype type" {
- try testCanonical(
- \\fn print(args: anytype) @This() {}
- \\
- );
-}
-
-test "zig fmt: alignment in anonymous literal" {
- try testTransform(
- \\const a = .{
- \\ "U", "L", "F",
- \\ "U'",
- \\ "L'",
- \\ "F'",
- \\};
- \\
- ,
- \\const a = .{
- \\ "U", "L", "F",
- \\ "U'", "L'", "F'",
- \\};
- \\
- );
-}
-
-test "zig fmt: 'zig fmt: (off|on)' can be surrounded by arbitrary whitespace" {
- try testTransform(
- \\// Test trailing comma syntax
- \\// zig fmt: off
- \\
- \\const struct_trailing_comma = struct { x: i32, y: i32, };
- \\
- \\// zig fmt: on
- ,
- \\// Test trailing comma syntax
- \\// zig fmt: off
- \\
- \\const struct_trailing_comma = struct { x: i32, y: i32, };
- \\
- \\// zig fmt: on
- \\
- );
-}
-
-test "zig fmt: line and doc comment following 'zig fmt: off'" {
- try testCanonical(
- \\// zig fmt: off
- \\// test 1
- \\/// test 2
- \\const e = f;
- );
-}
-
-test "zig fmt: doc and line comment following 'zig fmt: off'" {
- try testCanonical(
- \\// zig fmt: off
- \\/// test 1
- \\// test 2
- \\const e = f;
- );
-}
-
-test "zig fmt: line comment following 'zig fmt: on'" {
- try testCanonical(
- \\// zig fmt: off
- \\const e = f;
- \\// zig fmt: on
- \\// test
- \\const e = f;
- \\
- );
-}
-
-test "zig fmt: doc comment following 'zig fmt: on'" {
- try testCanonical(
- \\// zig fmt: off
- \\const e = f;
- \\// zig fmt: on
- \\/// test
- \\const e = f;
- \\
- );
-}
-
-test "zig fmt: line and doc comment following 'zig fmt: on'" {
- try testCanonical(
- \\// zig fmt: off
- \\const e = f;
- \\// zig fmt: on
- \\// test1
- \\/// test2
- \\const e = f;
- \\
- );
-}
-
-test "zig fmt: doc and line comment following 'zig fmt: on'" {
- try testCanonical(
- \\// zig fmt: off
- \\const e = f;
- \\// zig fmt: on
- \\/// test1
- \\// test2
- \\const e = f;
- \\
- );
-}
-
-test "zig fmt: block in slice expression" {
- try testCanonical(
- \\const a = b[{
- \\ _ = x;
- \\}..];
- \\const c = d[0..{
- \\ _ = x;
- \\ _ = y;
- \\}];
- \\const e = f[0..1 :{
- \\ _ = x;
- \\ _ = y;
- \\ _ = z;
- \\}];
- \\
- );
-}
-
-test "zig fmt: defer" {
- try testCanonical(
- \\test "defer" {
- \\ defer foo();
- \\ defer {
- \\ bar();
- \\ }
- \\}
- \\
- );
-}
-
-test "zig fmt: container initializers" {
- try testCanonical(
- \\const a0 = []u8{};
- \\const a1 = []u8{1};
- \\const a2 = []u8{
- \\ 1,
- \\ 2,
- \\ 3,
- \\ 4,
- \\};
- \\const s0 = S{};
- \\const s1 = S{ .a = 1 };
- \\const s2 = S{
- \\ .a = 1,
- \\ .b = 2,
- \\};
- \\
- );
-}
-
-test "zig fmt: sentinel-terminated array type" {
- try testCanonical(
- \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {
- \\ return sliceToPrefixedFileW(mem.toSliceConst(u8, s));
- \\}
- \\
- );
-}
-
-test "zig fmt: sentinel-terminated slice type" {
- try testCanonical(
- \\pub fn toSlice(self: Buffer) [:0]u8 {
- \\ return self.list.toSlice()[0..self.len()];
- \\}
- \\
- );
-}
-
-test "zig fmt: top-level tuple function call type" {
+test "zig fmt: container doc comments" {
try testCanonical(
- \\foo()
+ \\//! tld 1
+ \\//! tld 2
+ \\//! tld 3
+ \\const a = b;
\\
);
}
-test "zig fmt: top-level bare asterisk+identifier" {
+test "zig fmt: decimal float literals with underscore separators" {
try testCanonical(
- \\*x
+ \\const x = 1_234_567.89_10_11;
+ \\const y = 1_234_567.89_10_11e1_213_14;
+ \\const z = 1_234_567;
\\
);
}
-test "zig fmt: top-level bare asterisk+asterisk+identifier" {
- try testCanonical(
- \\**x
- \\
- );
+test "Ast header smoke test" {
+ try std.testing.expectEqual(zigNode(c.AST_NODE_IF), Ast.Node.Tag.@"if");
}
-test "zig fmt: errdefer with payload" {
+test "my function" {
try testCanonical(
- \\pub fn main() anyerror!void {
- \\ errdefer |a| x += 1;
- \\ errdefer |a| {}
- \\ errdefer |a| {
- \\ x += 1;
- \\ }
+ \\pub fn main() void {
+ \\ @panic("hello");
\\}
\\
);