| 1 | const std = @import("std"); |
| 2 | const expectEqual = std.testing.expectEqual; |
| 3 | |
| 4 | const Point = struct { |
| 5 | x: u32, |
| 6 | y: u32, |
| 7 | |
| 8 | pub var z: u32 = 1; |
| 9 | }; |
| 10 | |
| 11 | test "field access by string" { |
| 12 | var p = Point{ .x = 0, .y = 0 }; |
| 13 | |
| 14 | @field(p, "x") = 4; |
| 15 | @field(p, "y") = @field(p, "x") + 1; |
| 16 | |
| 17 | try expectEqual(4, @field(p, "x")); |
| 18 | try expectEqual(5, @field(p, "y")); |
| 19 | } |
| 20 | |
| 21 | test "decl access by string" { |
| 22 | try expectEqual(1, @field(Point, "z")); |
| 23 | |
| 24 | @field(Point, "z") = 2; |
| 25 | try expectEqual(2, @field(Point, "z")); |
| 26 | } |
| 27 | |
| 28 | // test |