Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update std.testing.expectEqual and friends to use peer type resolution #17431

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove some @as coercions from assertions
These are some spurious fixes to help illustrate the improved ergonomics of the `expectEqual` change. It is by no means complete.
  • Loading branch information
castholm committed Jan 3, 2024
commit d7b36503cad1de60cd0b6a115ef7ac336a03a09a
4 changes: 2 additions & 2 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,8 @@ test "machoSearchSymbols" {
.{ .addr = 300, .strx = undefined, .size = undefined, .ofile = undefined },
};

try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 0));
try testing.expectEqual(@as(?*const MachoSymbol, null), machoSearchSymbols(&symbols, 99));
try testing.expectEqual(null, machoSearchSymbols(&symbols, 0));
try testing.expectEqual(null, machoSearchSymbols(&symbols, 99));
try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 100).?);
try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 150).?);
try testing.expectEqual(&symbols[0], machoSearchSymbols(&symbols, 199).?);
Expand Down
4 changes: 2 additions & 2 deletions lib/std/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ pub const Status = enum(u10) {
}

test {
try std.testing.expectEqual(@as(?Status.Class, Status.Class.success), Status.ok.class());
try std.testing.expectEqual(@as(?Status.Class, Status.Class.client_error), Status.not_found.class());
try std.testing.expectEqual(Status.Class.success, Status.ok.class());
try std.testing.expectEqual(Status.Class.client_error, Status.not_found.class());
}
};

Expand Down
26 changes: 13 additions & 13 deletions lib/std/json/static_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,19 @@ test "test all types" {
test "parse" {
try testing.expectEqual(false, try parseFromSliceLeaky(bool, testing.allocator, "false", .{}));
try testing.expectEqual(true, try parseFromSliceLeaky(bool, testing.allocator, "true", .{}));
try testing.expectEqual(@as(u1, 1), try parseFromSliceLeaky(u1, testing.allocator, "1", .{}));
try testing.expectEqual(1, try parseFromSliceLeaky(u1, testing.allocator, "1", .{}));
try testing.expectError(error.Overflow, parseFromSliceLeaky(u1, testing.allocator, "50", .{}));
try testing.expectEqual(@as(u64, 42), try parseFromSliceLeaky(u64, testing.allocator, "42", .{}));
try testing.expectEqual(@as(f64, 42), try parseFromSliceLeaky(f64, testing.allocator, "42.0", .{}));
try testing.expectEqual(@as(?bool, null), try parseFromSliceLeaky(?bool, testing.allocator, "null", .{}));
try testing.expectEqual(@as(?bool, true), try parseFromSliceLeaky(?bool, testing.allocator, "true", .{}));
try testing.expectEqual(42, try parseFromSliceLeaky(u64, testing.allocator, "42", .{}));
try testing.expectEqual(42, try parseFromSliceLeaky(f64, testing.allocator, "42.0", .{}));
try testing.expectEqual(null, try parseFromSliceLeaky(?bool, testing.allocator, "null", .{}));
try testing.expectEqual(true, try parseFromSliceLeaky(?bool, testing.allocator, "true", .{}));

try testing.expectEqual(@as([3]u8, "foo".*), try parseFromSliceLeaky([3]u8, testing.allocator, "\"foo\"", .{}));
try testing.expectEqual(@as([3]u8, "foo".*), try parseFromSliceLeaky([3]u8, testing.allocator, "[102, 111, 111]", .{}));
try testing.expectEqual(@as([0]u8, undefined), try parseFromSliceLeaky([0]u8, testing.allocator, "[]", .{}));
try testing.expectEqual("foo".*, try parseFromSliceLeaky([3]u8, testing.allocator, "\"foo\"", .{}));
try testing.expectEqual("foo".*, try parseFromSliceLeaky([3]u8, testing.allocator, "[102, 111, 111]", .{}));
try testing.expectEqual(undefined, try parseFromSliceLeaky([0]u8, testing.allocator, "[]", .{}));

try testing.expectEqual(@as(u64, 12345678901234567890), try parseFromSliceLeaky(u64, testing.allocator, "\"12345678901234567890\"", .{}));
try testing.expectEqual(@as(f64, 123.456), try parseFromSliceLeaky(f64, testing.allocator, "\"123.456\"", .{}));
try testing.expectEqual(12345678901234567890, try parseFromSliceLeaky(u64, testing.allocator, "\"12345678901234567890\"", .{}));
try testing.expectEqual(123.456, try parseFromSliceLeaky(f64, testing.allocator, "\"123.456\"", .{}));
}

test "parse into enum" {
Expand All @@ -394,9 +394,9 @@ test "parse into enum" {
Bar,
@"with\\escape",
};
try testing.expectEqual(@as(T, .Foo), try parseFromSliceLeaky(T, testing.allocator, "\"Foo\"", .{}));
try testing.expectEqual(@as(T, .Foo), try parseFromSliceLeaky(T, testing.allocator, "42", .{}));
try testing.expectEqual(@as(T, .@"with\\escape"), try parseFromSliceLeaky(T, testing.allocator, "\"with\\\\escape\"", .{}));
try testing.expectEqual(.Foo, try parseFromSliceLeaky(T, testing.allocator, "\"Foo\"", .{}));
try testing.expectEqual(.Foo, try parseFromSliceLeaky(T, testing.allocator, "42", .{}));
try testing.expectEqual(.@"with\\escape", try parseFromSliceLeaky(T, testing.allocator, "\"with\\\\escape\"", .{}));
try testing.expectError(error.InvalidEnumTag, parseFromSliceLeaky(T, testing.allocator, "5", .{}));
try testing.expectError(error.InvalidEnumTag, parseFromSliceLeaky(T, testing.allocator, "\"Qux\"", .{}));
}
Expand Down
50 changes: 25 additions & 25 deletions test/behavior/packed-struct.zig
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ test "correct sizeOf and offsets in packed structs" {
try expectEqual(true, s1.bool_d);
try expectEqual(true, s1.bool_e);
try expectEqual(true, s1.bool_f);
try expectEqual(@as(u1, 1), s1.u1_a);
try expectEqual(1, s1.u1_a);
try expectEqual(false, s1.bool_g);
try expectEqual(@as(u1, 0), s1.u1_b);
try expectEqual(@as(u3, 3), s1.u3_a);
try expectEqual(@as(u10, 0b1101000101), s1.u10_a);
try expectEqual(@as(u10, 0b0001001000), s1.u10_b);
try expectEqual(0, s1.u1_b);
try expectEqual(3, s1.u3_a);
try expectEqual(0b1101000101, s1.u10_a);
try expectEqual(0b0001001000, s1.u10_b);

const s2 = @as(packed struct { x: u1, y: u7, z: u24 }, @bitCast(@as(u32, 0xd5c71ff4)));
try expectEqual(@as(u1, 0), s2.x);
try expectEqual(@as(u7, 0b1111010), s2.y);
try expectEqual(@as(u24, 0xd5c71f), s2.z);
try expectEqual(0, s2.x);
try expectEqual(0b1111010, s2.y);
try expectEqual(0xd5c71f, s2.z);
}
}

Expand All @@ -208,12 +208,12 @@ test "nested packed structs" {

if (native_endian == .little) {
const s3 = @as(S3Padded, @bitCast(@as(u64, 0xe952d5c71ff4))).s3;
try expectEqual(@as(u8, 0xf4), s3.x.a);
try expectEqual(@as(u8, 0x1f), s3.x.b);
try expectEqual(@as(u8, 0xc7), s3.x.c);
try expectEqual(@as(u8, 0xd5), s3.y.d);
try expectEqual(@as(u8, 0x52), s3.y.e);
try expectEqual(@as(u8, 0xe9), s3.y.f);
try expectEqual(0xf4, s3.x.a);
try expectEqual(0x1f, s3.x.b);
try expectEqual(0xc7, s3.x.c);
try expectEqual(0xd5, s3.y.d);
try expectEqual(0x52, s3.y.e);
try expectEqual(0xe9, s3.y.f);
}

const S4 = packed struct { a: i32, b: i8 };
Expand Down Expand Up @@ -249,8 +249,8 @@ test "regular in irregular packed struct" {
foo.bar.a = 235;
foo.bar.b = 42;

try expectEqual(@as(u16, 235), foo.bar.a);
try expectEqual(@as(u8, 42), foo.bar.b);
try expectEqual(235, foo.bar.a);
try expectEqual(42, foo.bar.b);
}

test "nested packed struct unaligned" {
Expand Down Expand Up @@ -456,12 +456,12 @@ test "nested packed struct field pointers" {
const ptr_p0_c = &S2.s.p0.c;
const ptr_p1_a = &S2.s.p1.a;
const ptr_p1_b = &S2.s.p1.b;
try expectEqual(@as(u8, 1), ptr_base.*);
try expectEqual(@as(u4, 2), ptr_p0_a.*);
try expectEqual(@as(u4, 3), ptr_p0_b.*);
try expectEqual(@as(u8, 4), ptr_p0_c.*);
try expectEqual(@as(u7, 5), ptr_p1_a.*);
try expectEqual(@as(u8, 6), ptr_p1_b.*);
try expectEqual(1, ptr_base.*);
try expectEqual(2, ptr_p0_a.*);
try expectEqual(3, ptr_p0_b.*);
try expectEqual(4, ptr_p0_c.*);
try expectEqual(5, ptr_p1_a.*);
try expectEqual(6, ptr_p1_b.*);
}

test "load pointer from packed struct" {
Expand Down Expand Up @@ -1033,12 +1033,12 @@ test "modify nested packed struct aligned field" {

var opts = Options{};
opts.pretty_print.indent += 1;
try std.testing.expectEqual(@as(u17, 0b00000000100100000), @as(u17, @bitCast(opts)));
try std.testing.expectEqual(0b00000000100100000, @as(u17, @bitCast(opts)));
try std.testing.expect(!opts.foo);
try std.testing.expect(!opts.bar);
try std.testing.expect(!opts.pretty_print.enabled);
try std.testing.expectEqual(@as(u4, 4), opts.pretty_print.num_spaces);
try std.testing.expectEqual(@as(u8, 1), opts.pretty_print.indent);
try std.testing.expectEqual(4, opts.pretty_print.num_spaces);
try std.testing.expectEqual(1, opts.pretty_print.indent);
try std.testing.expect(!opts.baz);
}

Expand Down