authorgravatar for 178735591+87flowers@users.noreply.github.com87flowers <178735591+87flowers@users.noreply.github.com> 2024-10-18 09:32:19+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-22 17:09:20-08:00
logfdf68c2f2e4f6f7ca77752b8610e05778b07a22b
treeb22124839fd19bd4674253963fba7262d8c48d73
parentc724e157d6da0236dd8e84e3456ccb13c812adaf

std/zig/parser: Add indentation tests


1 files changed, 145 insertions(+), 0 deletions(-)

lib/std/zig/parser_test.zig+145
...@@ -5962,6 +5962,151 @@ test "zig fmt: pointer type syntax to index" {...@@ -5962,6 +5962,151 @@ test "zig fmt: pointer type syntax to index" {
5962 );5962 );
5963}5963}
59645964
5965test "zig fmt: binop indentation in if statement" {
5966 try testCanonical(
5967 \\test {
5968 \\ if (first_param_type.isGenericPoison() or
5969 \\ (first_param_type.zigTypeTag(zcu) == .pointer and
5970 \\ (first_param_type.ptrSize(zcu) == .One or
5971 \\ first_param_type.ptrSize(zcu) == .C) and
5972 \\ first_param_type.childType(zcu).eql(concrete_ty, zcu)))
5973 \\ {
5974 \\ f(x);
5975 \\ }
5976 \\}
5977 \\
5978 );
5979}
5980
5981test "zig fmt: test indentation after equals sign" {
5982 try testCanonical(
5983 \\test {
5984 \\ const foo =
5985 \\ if (1 == 2)
5986 \\ 1
5987 \\ else if (3 > 4)
5988 \\ 2
5989 \\ else
5990 \\ 0;
5991 \\
5992 \\ const foo, const bar =
5993 \\ if (1 == 2)
5994 \\ .{ 0, 0 }
5995 \\ else if (3 > 4)
5996 \\ .{ 1, 1 }
5997 \\ else
5998 \\ .{ 2, 2 };
5999 \\
6000 \\ while (foo) if (bar)
6001 \\ f(x);
6002 \\
6003 \\ foobar =
6004 \\ if (true)
6005 \\ 1
6006 \\ else
6007 \\ 0;
6008 \\
6009 \\ const foo = if (1 == 2)
6010 \\ 1
6011 \\ else if (3 > 4)
6012 \\ 2
6013 \\ else
6014 \\ 0;
6015 \\
6016 \\ const foo, const bar = if (1 == 2)
6017 \\ .{ 0, 0 }
6018 \\ else if (3 > 4)
6019 \\ .{ 1, 1 }
6020 \\ else
6021 \\ .{ 2, 2 };
6022 \\
6023 \\ foobar = if (true)
6024 \\ 1
6025 \\ else
6026 \\ 0;
6027 \\
6028 \\ const is_alphanum =
6029 \\ (ch >= 'a' and ch <= 'z') or
6030 \\ (ch >= 'A' and ch <= 'Z') or
6031 \\ (ch >= '0' and ch <= '9');
6032 \\
6033 \\ const bar = 100 + calculate(
6034 \\ 200,
6035 \\ 300,
6036 \\ );
6037 \\
6038 \\ const gcc_pragma = std.meta.stringToEnum(Directive, pp.expandedSlice(directive_tok)) orelse
6039 \\ return pp.comp.addDiagnostic(.{
6040 \\ .tag = .unknown_gcc_pragma,
6041 \\ .loc = directive_tok.loc,
6042 \\ }, pp.expansionSlice(start_idx + 1));
6043 \\
6044 \\ const vec4s =
6045 \\ [_][4]i32{
6046 \\ [_]i32{ 0, 1, 0, 0 },
6047 \\ [_]i32{ 0, -1, 0, 0 },
6048 \\ [_]i32{ 2, 1, 2, 0 },
6049 \\ };
6050 \\}
6051 \\
6052 );
6053}
6054
6055test "zig fmt: test indentation of if expressions" {
6056 try testCanonical(
6057 \\test {
6058 \\ const foo = 1 +
6059 \\ if (1 == 2)
6060 \\ 2
6061 \\ else
6062 \\ 0;
6063 \\
6064 \\ const foo = 1 + if (1 == 2)
6065 \\ 2
6066 \\ else
6067 \\ 0;
6068 \\
6069 \\ errval catch |e|
6070 \\ if (e == error.Meow)
6071 \\ return 0x1F408
6072 \\ else
6073 \\ unreachable;
6074 \\
6075 \\ errval catch |e| if (e == error.Meow)
6076 \\ return 0x1F408
6077 \\ else
6078 \\ unreachable;
6079 \\
6080 \\ return if (1 == 2)
6081 \\ 1
6082 \\ else if (3 > 4)
6083 \\ 2
6084 \\ else
6085 \\ 0;
6086 \\}
6087 \\
6088 );
6089}
6090
6091test "zig fmt: indentation of comments within catch, else, orelse" {
6092 try testCanonical(
6093 \\comptime {
6094 \\ _ = foo() catch
6095 \\ //
6096 \\ bar();
6097 \\
6098 \\ _ = if (foo) bar() else
6099 \\ //
6100 \\ qux();
6101 \\
6102 \\ _ = foo() orelse
6103 \\ //
6104 \\ qux();
6105 \\}
6106 \\
6107 );
6108}
6109
5965test "recovery: top level" {6110test "recovery: top level" {
5966 try testError(6111 try testError(
5967 \\test "" {inline}6112 \\test "" {inline}