master
 1const std = @import("std");
 2const builtin = @import("builtin");
 3const expect = std.testing.expect;
 4
 5const Foo = @import("hasdecl/foo.zig");
 6
 7const Bar = struct {
 8    nope: i32,
 9
10    const hi = 1;
11    pub var blah = "xxx";
12};
13
14test "@hasDecl" {
15    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
16    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
17
18    try expect(@hasDecl(Foo, "public_thing"));
19    try expect(!@hasDecl(Foo, "private_thing"));
20    try expect(!@hasDecl(Foo, "no_thing"));
21
22    try expect(@hasDecl(Bar, "hi"));
23    try expect(@hasDecl(Bar, "blah"));
24    try expect(!@hasDecl(Bar, "nope"));
25}
26
27test "@hasDecl using a sliced string literal" {
28    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
29    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
30
31    try expect(@hasDecl(@This(), "std") == true);
32    try expect(@hasDecl(@This(), "std"[0..0]) == false);
33    try expect(@hasDecl(@This(), "std"[0..1]) == false);
34    try expect(@hasDecl(@This(), "std"[0..2]) == false);
35    try expect(@hasDecl(@This(), "std"[0..3]) == true);
36    try expect(@hasDecl(@This(), "std"[0..]) == true);
37}