From ebda1c03bd55cef52a2629b9245246fa61353c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 10 Feb 2026 18:20:11 +0000 Subject: [PATCH] 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) --- parser_test.zig | 1244 +++++++++++++++++++++++------------------------ 1 file changed, 622 insertions(+), 622 deletions(-) diff --git a/parser_test.zig b/parser_test.zig index a97458520c..b121167a06 100644 --- 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,6 +1253,17 @@ test "zig fmt: struct literal 1 element" { ); } +test "zig fmt: Unicode code point literal larger than u8" { + try testCanonical( + \\test { + \\ const x = X{ + \\ .a = b, + \\ }; + \\} + \\ + ); +} + test "zig fmt: struct literal 2 element" { try testCanonical( \\test { @@ -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,6 +1797,59 @@ test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" { ); } +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: 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]; @@ -1499,6 +1864,108 @@ test "zig fmt: spaces around slice operator" { ); } +test "zig fmt: 2nd arg multiline string" { + try testCanonical( + \\comptime { + \\ cases.addAsm("hello world linux x86_64", + \\ \\.text + \\ , "Hello, world!\n"); + \\} + \\ + ); + try testCanonical( + \\comptime { + \\ cases.addAsm("hello world linux x86_64", + \\ \\.text + \\ , "Hello, world!\n", "Hello, world!\n"); + \\} + \\ + ); +} + +test "zig fmt: final arg multiline string" { + try testCanonical( + \\comptime { + \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n", + \\ \\.text + \\ ); + \\} + \\ + ); +} + +test "zig fmt: function call with multiline argument" { + try testCanonical( + \\comptime { + \\ self.user_input_options.put(name, UserInputOption{ + \\ .name = name, + \\ .used = false, + \\ }); + \\} + \\ + ); +} + +test "zig fmt: if-else with comment before else" { + 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 "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 + \\} + \\ + ); +} + +test "zig fmt: respect line breaks in if-else" { + try testCanonical( + \\comptime { + \\ return if (cond) a else b; + \\ return if (cond) + \\ a + \\ else + \\ b; + \\ return if (cond) + \\ a + \\ else if (cond) + \\ b + \\ else + \\ c; + \\} + \\ + ); +} + test "zig fmt: respect line breaks after infix operators" { try testCanonical( \\comptime { @@ -1555,120 +2022,6 @@ test "zig fmt: struct literal no trailing comma" { ); } -test "zig fmt: 2nd arg multiline string" { - try testCanonical( - \\comptime { - \\ cases.addAsm("hello world linux x86_64", - \\ \\.text - \\ , "Hello, world!\n"); - \\} - \\ - ); - try testCanonical( - \\comptime { - \\ cases.addAsm("hello world linux x86_64", - \\ \\.text - \\ , "Hello, world!\n", "Hello, world!\n"); - \\} - \\ - ); -} - -test "zig fmt: final arg multiline string" { - try testCanonical( - \\comptime { - \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n", - \\ \\.text - \\ ); - \\} - \\ - ); -} - -test "zig fmt: function call with multiline argument" { - try testCanonical( - \\comptime { - \\ self.user_input_options.put(name, UserInputOption{ - \\ .name = name, - \\ .used = false, - \\ }); - \\} - \\ - ); -} - -test "zig fmt: if statement" { - try testCanonical( - \\test "" { - \\ if (optional()) |some| - \\ bar = some.foo(); - \\} - \\ - ); -} - -test "zig fmt: respect line breaks in if-else" { - try testCanonical( - \\comptime { - \\ return if (cond) a else b; - \\ return if (cond) - \\ a - \\ else - \\ b; - \\ return if (cond) - \\ a - \\ else if (cond) - \\ b - \\ else - \\ c; - \\} - \\ - ); -} - -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 - \\} - \\ - ); -} - -test "zig fmt: remove empty lines at start/end of block" { - try testTransform( - \\test { - \\ - \\ if (foo) { - \\ foo(); - \\ } - \\ - \\} - \\ - , - \\test { - \\ if (foo) { - \\ foo(); - \\ } - \\} - \\ - ); -} - test "zig fmt: multiline string with backslash at end of line" { try testCanonical( \\comptime { @@ -1781,169 +2134,39 @@ test "zig fmt: nested struct literal with one item" { ); } -test "zig fmt: if-else with comment before else" { - 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 "zig fmt: nosuspend block" { - try testCanonical( - \\pub fn main() anyerror!void { - \\ nosuspend { - \\ var foo: Foo = .{ .bar = 42 }; - \\ } - \\} - \\ - ); -} - -test "zig fmt: c pointer type" { - try testCanonical( - \\pub extern fn repro() [*c]const u8; - \\ - ); -} - -test "zig fmt: sentinel array literal 1 element" { +test "zig fmt: block with same line comment after end brace" { try testCanonical( \\test { - \\ const x = [_:9000]u32{a}; + \\ { + \\ const a = b; + \\ } // end of block \\} \\ ); } -test "zig fmt: anon literal in array" { +test "zig fmt: comments before var decl in struct" { try testCanonical( - \\var arr: [2]Foo = .{ - \\ .{ .a = 2 }, - \\ .{ .b = 3 }, + \\const Foo = struct { + \\ /// comment + \\ bar: bool = true, \\}; \\ ); } -test "zig fmt: Unicode code point literal larger than u8" { +test "zig fmt: comments before global variables" { try testCanonical( - \\test { - \\ const x = X{ - \\ .a = b, - \\ }; - \\} + \\/// comment + \\var foo: i32 = undefined; \\ ); } -test "zig fmt: slices with spaces in bounds" { +test "zig fmt: comments before test decl" { 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: C pointers" { - try testCanonical( - \\const Ptr = [*c]i32; - \\ - ); -} - -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: 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; + \\/// top level doc comment + \\test "hi" {} \\ ); } @@ -1977,6 +2200,25 @@ test "zig fmt: return" { ); } +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: arrays" { try testCanonical( \\test "arrays" { @@ -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" { +test "zig fmt: defer" { 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" { - 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" { +test "zig fmt: container doc comments" { 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, - \\}; + \\//! tld 1 + \\//! tld 2 + \\//! tld 3 + \\const a = b; \\ ); } -test "zig fmt: pointer of unknown length" { +test "zig fmt: decimal float literals with underscore separators" { try testCanonical( - \\fn foo(ptr: [*]u8) void {} + \\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: call expression" { +test "Ast header smoke test" { + try std.testing.expectEqual(zigNode(c.AST_NODE_IF), Ast.Node.Tag.@"if"); +} + +test "my function" { 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" { - 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; - \\ } + \\pub fn main() void { + \\ @panic("hello"); \\} \\ );