From 081f21d7b99764305f3025bb007c7caf2c2422ef Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 28 Jan 2024 22:02:23 -0800 Subject: [PATCH] add expectSimilarType --- src/expectSimilarType.zig | 39 +++++++++++++++++++++++++++++++++++++++ src/lib.zig | 1 + 2 files changed, 40 insertions(+) create mode 100644 src/expectSimilarType.zig diff --git a/src/expectSimilarType.zig b/src/expectSimilarType.zig new file mode 100644 index 0000000000000000000000000000000000000000..2eaa6f8434c92a2859cfbde996bb6da6b0be44a4 --- /dev/null +++ b/src/expectSimilarType.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const string = []const u8; +const extras = @import("./lib.zig"); + +pub fn expectSimilarType(comptime A: type, comptime B: type) !void { + const info_a = @typeInfo(A); + const info_b = @typeInfo(B); + try std.testing.expect(std.meta.activeTag(info_a) == std.meta.activeTag(info_b)); + + if (info_a == .Struct) { + const info_a_s = info_a.Struct; + const info_b_s = info_b.Struct; + + try std.testing.expect(info_a_s.layout == info_b_s.layout); + try std.testing.expect(info_a_s.is_tuple == info_b_s.is_tuple); + try std.testing.expect(info_a_s.backing_integer == info_b_s.backing_integer); + + for (info_a_s.decls, info_b_s.decls) |da, db| { + try std.testing.expect(std.mem.eql(u8, da.name, db.name)); + } + + inline for (info_a_s.fields, info_b_s.fields) |fa, fb| { + try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); + try std.testing.expect(fa.type == fb.type); + try std.testing.expect(fa.alignment == fb.alignment); + try std.testing.expect(fa.is_comptime == fb.is_comptime); + } + + return; + } + @compileError("not implemented"); +} + +test { + try expectSimilarType( + struct { a: u32, b: u8, c: u16 }, + struct { a: u32, b: u8, c: u16 }, + ); +} diff --git a/src/lib.zig b/src/lib.zig index a5de6d29cadf24c7e2b890d528509eae6a419279..a268bc277ba4242f35008b74382ab647097c5ed9 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -88,3 +88,4 @@ pub fn fd_realpath(fd: std.os.fd_t) ![std.fs.MAX_PATH_BYTES:0]u8 { } pub usingnamespace @import("./rawInt.zig"); +pub usingnamespace @import("./expectSimilarType.zig"); -- 2.54.0