aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/hasdecl.zig
blob: 8f371defdbd33d6ad02b4830b82a499c4d3de975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;

const Foo = @import("hasdecl/foo.zig");

const Bar = struct {
    nope: i32,

    const hi = 1;
    pub var blah = "xxx";
};

test "@hasDecl" {
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    try expect(@hasDecl(Foo, "public_thing"));
    try expect(!@hasDecl(Foo, "private_thing"));
    try expect(!@hasDecl(Foo, "no_thing"));

    try expect(@hasDecl(Bar, "hi"));
    try expect(@hasDecl(Bar, "blah"));
    try expect(!@hasDecl(Bar, "nope"));
}

test "@hasDecl using a sliced string literal" {
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    try expect(@hasDecl(@This(), "std") == true);
    try expect(@hasDecl(@This(), "std"[0..0]) == false);
    try expect(@hasDecl(@This(), "std"[0..1]) == false);
    try expect(@hasDecl(@This(), "std"[0..2]) == false);
    try expect(@hasDecl(@This(), "std"[0..3]) == true);
    try expect(@hasDecl(@This(), "std"[0..]) == true);
}