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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
const std = @import("std");
const stub = @import("frontend/stub.zig");
const IO = @import("frontend/io_interface.zig").IOInterface;
const Action = @import("actions.zig").Action;
pub fn run(allocator: std.mem.Allocator, io: *IO) !void {
try io.displayMessage("Initialized");
var action = Action.illegal;
var tick_count: usize = 0;
while (action != Action.exit) {
const str = try std.fmt.allocPrint(allocator, "{}", .{tick_count});
defer allocator.free(str);
try io.displayMessage(str);
action = try io.waitForTick();
switch (action) {
Action.tick => {
tick_count += 1;
},
else => {},
}
}
}
test "can run game" {
const allocator = std.testing.allocator;
var as = stub.ActionStack.init(allocator);
defer as.deinit();
var io = try stub.IO.init(allocator, as);
defer io.deinit();
var res: ?anyerror = null;
run(allocator, &io) catch |err| {
res = err;
};
try std.testing.expect(res != null);
try std.testing.expectEqual(error.NoAvailableAction, res.?);
}
test "game exits on Action.exit" {
const allocator = std.testing.allocator;
var as = stub.ActionStack.init(allocator);
defer as.deinit();
try as.scheduleAction(Action.exit);
var io = try stub.IO.init(allocator, as);
defer io.deinit();
try run(allocator, &io);
const io_obj: *const stub.IO = @ptrCast(@alignCast(io.ptr));
var actions = io_obj.out_as;
const act = actions.next().?;
try std.testing.expectEqual(Action.exit, act);
}
test "game processes stream of actions" {
const allocator = std.testing.allocator;
var as = stub.ActionStack.init(allocator);
defer as.deinit();
try as.scheduleAction(Action.exit);
try as.scheduleAction(Action.move_right);
try as.scheduleAction(Action.move_left);
try as.scheduleAction(Action.move_down);
try as.scheduleAction(Action.move_up);
try as.scheduleAction(Action.tick);
var io = try stub.IO.init(allocator, as);
defer io.deinit();
try run(allocator, &io);
const io_obj: *const stub.IO = @ptrCast(@alignCast(io.ptr));
var actions = io_obj.out_as;
var act = actions.next().?;
try std.testing.expectEqual(Action.exit, act);
act = actions.next().?;
try std.testing.expectEqual(Action.move_right, act);
act = actions.next().?;
try std.testing.expectEqual(Action.move_left, act);
act = actions.next().?;
try std.testing.expectEqual(Action.move_down, act);
act = actions.next().?;
try std.testing.expectEqual(Action.move_up, act);
act = actions.next().?;
try std.testing.expectEqual(Action.tick, act);
}
|