mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
commit
8f4bc77260
@ -341,3 +341,78 @@ test "std.meta.trait.isContainer" {
|
||||
testing.expect(isContainer(TestEnum));
|
||||
testing.expect(!isContainer(u8));
|
||||
}
|
||||
|
||||
pub fn hasDecls(comptime T: type, comptime names: var) bool {
|
||||
inline for (names) |name| {
|
||||
if (!@hasDecl(T, name))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
test "std.meta.trait.hasDecls" {
|
||||
const TestStruct1 = struct {};
|
||||
const TestStruct2 = struct {
|
||||
pub var a: u32;
|
||||
pub var b: u32;
|
||||
c: bool,
|
||||
pub fn useless() void {}
|
||||
};
|
||||
|
||||
const tuple = .{ "a", "b", "c" };
|
||||
|
||||
testing.expect(!hasDecls(TestStruct1, .{"a"}));
|
||||
testing.expect(hasDecls(TestStruct2, .{ "a", "b" }));
|
||||
testing.expect(hasDecls(TestStruct2, .{ "a", "b", "useless" }));
|
||||
testing.expect(!hasDecls(TestStruct2, .{ "a", "b", "c" }));
|
||||
testing.expect(!hasDecls(TestStruct2, tuple));
|
||||
}
|
||||
|
||||
pub fn hasFields(comptime T: type, comptime names: var) bool {
|
||||
inline for (names) |name| {
|
||||
if (!@hasField(T, name))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
test "std.meta.trait.hasFields" {
|
||||
const TestStruct1 = struct {};
|
||||
const TestStruct2 = struct {
|
||||
a: u32,
|
||||
b: u32,
|
||||
c: bool,
|
||||
pub fn useless() void {}
|
||||
};
|
||||
|
||||
const tuple = .{ "a", "b", "c" };
|
||||
|
||||
testing.expect(!hasFields(TestStruct1, .{"a"}));
|
||||
testing.expect(hasFields(TestStruct2, .{ "a", "b" }));
|
||||
testing.expect(hasFields(TestStruct2, .{ "a", "b", "c" }));
|
||||
testing.expect(hasFields(TestStruct2, tuple));
|
||||
testing.expect(!hasFields(TestStruct2, .{ "a", "b", "useless" }));
|
||||
}
|
||||
|
||||
pub fn hasFunctions(comptime T: type, comptime names: var) bool {
|
||||
inline for (names) |name| {
|
||||
if (!hasFn(name)(T))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
test "std.meta.trait.hasFunctions" {
|
||||
const TestStruct1 = struct {};
|
||||
const TestStruct2 = struct {
|
||||
pub fn a() void {}
|
||||
fn b() void {}
|
||||
};
|
||||
|
||||
const tuple = .{ "a", "b", "c" };
|
||||
|
||||
testing.expect(!hasFunctions(TestStruct1, .{"a"}));
|
||||
testing.expect(hasFunctions(TestStruct2, .{ "a", "b" }));
|
||||
testing.expect(!hasFunctions(TestStruct2, .{ "a", "b", "c" }));
|
||||
testing.expect(!hasFunctions(TestStruct2, tuple));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user