diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-05-18 20:20:46 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-18 20:20:46 -0400 |
| commit | 6435750c99e705eb40bbdf75e51a3493d683e951 (patch) | |
| tree | 2f1ab1dc537ba8804ae6d1e0bdd094d646625e53 /test/behavior/reflection.zig | |
| parent | d228d86059cf16f4b37b2853cc1323bf98d242cf (diff) | |
| parent | 667236668f865de4c854a047d65017140317e7e9 (diff) | |
| download | zig-6435750c99e705eb40bbdf75e51a3493d683e951.tar.gz zig-6435750c99e705eb40bbdf75e51a3493d683e951.zip | |
Merge pull request #8554 from ziglang/stage2-whole-file-astgen
Stage2 whole file astgen
Diffstat (limited to 'test/behavior/reflection.zig')
| -rw-r--r-- | test/behavior/reflection.zig | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/behavior/reflection.zig b/test/behavior/reflection.zig new file mode 100644 index 0000000000..3e5e4087c4 --- /dev/null +++ b/test/behavior/reflection.zig @@ -0,0 +1,55 @@ +const expect = @import("std").testing.expect; +const mem = @import("std").mem; +const reflection = @This(); + +test "reflection: function return type, var args, and param types" { + comptime { + const info = @typeInfo(@TypeOf(dummy)).Fn; + try expect(info.return_type.? == i32); + try expect(!info.is_var_args); + try expect(info.args.len == 3); + try expect(info.args[0].arg_type.? == bool); + try expect(info.args[1].arg_type.? == i32); + try expect(info.args[2].arg_type.? == f32); + } +} + +fn dummy(a: bool, b: i32, c: f32) i32 { + return 1234; +} + +test "reflection: @field" { + var f = Foo{ + .one = 42, + .two = true, + .three = void{}, + }; + + try expect(f.one == f.one); + try expect(@field(f, "o" ++ "ne") == f.one); + try expect(@field(f, "t" ++ "wo") == f.two); + try expect(@field(f, "th" ++ "ree") == f.three); + try expect(@field(Foo, "const" ++ "ant") == Foo.constant); + try expect(@field(Bar, "O" ++ "ne") == Bar.One); + try expect(@field(Bar, "T" ++ "wo") == Bar.Two); + try expect(@field(Bar, "Th" ++ "ree") == Bar.Three); + try expect(@field(Bar, "F" ++ "our") == Bar.Four); + try expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2)); + @field(f, "o" ++ "ne") = 4; + try expect(f.one == 4); +} + +const Foo = struct { + const constant = 52; + + one: i32, + two: bool, + three: void, +}; + +const Bar = union(enum) { + One: void, + Two: i32, + Three: bool, + Four: f64, +}; |
