parser: add infix, fn trailing comma, enum, struct literal tests

Port tests from upstream parser_test.zig:
- "respect line breaks after infix operators"
- "fn decl with trailing comma"
- "enum decl with no trailing comma"
- "struct literal no trailing comma"
- "2nd arg multiline string"
- "final arg multiline string"
- "function call with multiline argument"

No new parser.c changes needed — all features already implemented.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-02-10 13:09:54 +00:00
parent c67c54c3fb
commit 9f77f5a234

View File

@@ -1496,3 +1496,101 @@ test "zig fmt: spaces around slice operator" {
\\
);
}
test "zig fmt: respect line breaks after infix operators" {
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)];
\\}
\\
);
}
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: 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, };
,
\\const a = foo{ .x = 1, .y = 2 };
\\const a = foo{ .x = 1, .y = 2 };
\\const a = foo{
\\ .x = 1,
\\ .y = 2,
\\};
\\
);
}
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,
\\ });
\\}
\\
);
}