zig/test/cases/this.zig

48 lines
764 B
Zig
Raw Normal View History

2017-01-05 16:57:48 +08:00
const assert = @import("std").debug.assert;
const module = this;
fn Point(comptime T: type) -> type {
2016-12-22 14:20:08 +08:00
struct {
const Self = this;
x: T,
y: T,
2016-12-22 14:20:08 +08:00
fn addOne(self: &Self) {
self.x += 1;
self.y += 1;
}
}
}
fn add(x: i32, y: i32) -> i32 {
x + y
}
fn factorial(x: i32) -> i32 {
const selfFn = this;
if (x == 0) {
1
} else {
x * selfFn(x - 1)
}
}
2017-05-24 09:38:31 +08:00
test "this refer to module call private fn" {
assert(module.add(1, 2) == 3);
}
2017-05-24 09:38:31 +08:00
test "this refer to container" {
var pt = Point(i32) {
.x = 12,
.y = 34,
};
pt.addOne();
assert(pt.x == 13);
assert(pt.y == 35);
}
2017-05-24 09:38:31 +08:00
test "this refer to fn" {
assert(factorial(5) == 120);
}