Skip to content

Commit

Permalink
Restructure tests and cli
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Jul 23, 2024
1 parent 65074e4 commit 3f06d1f
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 388 deletions.
20 changes: 9 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/topi.zig"),
});

const topi_export = b.addModule("export", .{
.root_source_file = b.path("src/export/index.zig"),
});
topi_export.addImport("topi", topi);

const topilib = b.addSharedLibrary(.{
.name = "topi",
.root_source_file = b.path("src/export/main.zig"),
Expand All @@ -28,11 +33,13 @@ pub fn build(b: *std.Build) void {

const exe = b.addExecutable(.{
.name = "topi",
.root_source_file = b.path("src/cli.zig"),
.root_source_file = b.path("src/cli/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addOptions("build", build_options);
exe.root_module.addImport("topi", topi);

b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
Expand All @@ -54,20 +61,11 @@ pub fn build(b: *std.Build) void {
});

tests.root_module.addImport("topi", topi);
tests.root_module.addImport("export", topi_export);
const run_tests = b.addRunArtifact(tests);

const export_tests = b.addTest(.{
.root_source_file = b.path("src/export/main.test.zig"),
.target = target,
.optimize = optimize,
.filter = filter,
});
export_tests.root_module.addImport("topi", topi);
const run_export_tests = b.addRunArtifact(export_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
test_step.dependOn(&run_export_tests.step);
}

fn getVersion(b: *std.Build) ![]const u8 {
Expand Down
131 changes: 15 additions & 116 deletions src/cli.zig → src/cli/main.zig
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
const std = @import("std");

const runtime = @import("runtime/index.zig");
const Vm = runtime.Vm;
const Runner = runtime.Runner;
const Line = runtime.Line;
const Choice = runtime.Choice;
const topi = @import("topi");
const Vm = topi.runtime.Vm;
const Runner = topi.runtime.Runner;
const State = topi.runtime.State;

const types = @import("types/index.zig");
const Value = types.Value;
const Value = topi.types.Value;

const backend = @import("backend/index.zig");
const Scope = backend.Scope;
const Compiler = backend.Compiler;
const Errors = backend.CompilerErrors;
const Scope = topi.backend.Scope;
const Compiler = topi.backend.Compiler;
const Errors = topi.backend.CompilerErrors;

const locale = @import("locale.zig");
const Locale = locale.Locale;
const Locale = topi.locale.Locale;

const version = @import("build").version;

const module = @import("module.zig");
const File = module.File;
const Module = module.Module;
const File = topi.module.File;
const Module = topi.module.Module;

const runner = @import("runner.zig");
const CliRunner = runner.CliRunner;
const AutoTestRunner = runner.AutoTestRunner;

fn usage(comptime msg: []const u8) !void {
var out = std.io.getStdErr().writer();
Expand Down Expand Up @@ -277,103 +276,3 @@ fn getFilePath(allocator: std.mem.Allocator) !?[]const u8 {
_ = args.skip();
return args.next();
}

const CliRunner = struct {
runner: Runner,
is_auto: bool,

pub fn init(is_auto: bool) CliRunner {
return .{
.is_auto = is_auto,
.runner = .{
.on_line = onLine,
.on_choices = onChoices,
.on_value_changed = onValueChanged,
},
};
}

pub fn print(_: *CliRunner, comptime msg: []const u8, args: anytype) void {
const stdout = std.io.getStdOut().writer();
stdout.print(msg, args) catch {
std.debug.print("Could not print message", .{});
};
}

pub fn onLine(runner: *Runner, vm: *Vm, dialogue: Line) void {
const stdin = std.io.getStdIn().reader();
const self: *CliRunner = @fieldParentPtr("runner", runner);
self.print(":", .{});
if (dialogue.speaker) |speaker| {
self.print("{s}", .{speaker});
}
self.print(": ", .{});
self.print("{s}", .{dialogue.content});
if (self.is_auto) {
self.print("\n", .{});
vm.selectContinue();
} else {
var buf: [2]u8 = undefined;
if (stdin.readUntilDelimiterOrEof(&buf, '\n') catch &buf) |_| {
vm.selectContinue();
}
}
}

pub fn onChoices(runner: *Runner, vm: *Vm, choices: []Choice) void {
const stdin = std.io.getStdIn().reader();
const stderr = std.io.getStdErr().writer();
const self: *CliRunner = @fieldParentPtr("runner", runner);
var index: ?usize = null;
while (index == null) {
for (choices, 0..) |choice, i| {
self.print("[{d}] {s}\n", .{ i, choice.content });
}
var buf: [10]u8 = undefined;
if (stdin.readUntilDelimiterOrEof(&buf, '\n') catch &buf) |user_input| {
const input = std.mem.trim(u8, user_input, "\r\n");
index = std.fmt.parseInt(usize, input, 10) catch |err| blk: {
stderr.print("Invalid value: {}.\n", .{err}) catch {};
break :blk null;
};
if (index != null and index.? >= choices.len) {
index = null;
stderr.print("Invalid value.\n", .{}) catch {};
}
}
}
vm.selectChoice(index.?) catch |err| self.print("Error: {}", .{err});
}

pub fn onValueChanged(_: *Runner, _: *Vm, _: []const u8, _: Value) void {}
};

const AutoTestRunner = struct {
runner: Runner,
rnd: std.rand.Xoshiro256,

pub fn init() AutoTestRunner {
return .{
.rnd = std.rand.DefaultPrng.init(std.crypto.random.int(u64)),
.runner = .{
.on_line = onLine,
.on_choices = onChoices,
.on_value_changed = onValueChanged,
},
};
}

pub fn onLine(_: *Runner, vm: *Vm, _: Line) void {
vm.selectContinue();
}

pub fn onChoices(runner: *Runner, vm: *Vm, choices: []Choice) void {
var auto: *AutoTestRunner = @fieldParentPtr("runner", runner);
const index = auto.rnd.random().intRangeAtMost(usize, 0, choices.len - 1);
vm.selectChoice(index) catch |err| {
std.debug.print("Error: {}", .{err});
};
}

pub fn onValueChanged(_: *Runner, _: *Vm, _: []const u8, _: Value) void {}
};
109 changes: 109 additions & 0 deletions src/cli/runner.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const std = @import("std");

const topi = @import("topi");
const Vm = topi.runtime.Vm;
const Runner = topi.runtime.Runner;
const Line = topi.runtime.Line;
const Choice = topi.runtime.Choice;

const Value = topi.types.Value;

pub const CliRunner = struct {
runner: Runner,
is_auto: bool,

pub fn init(is_auto: bool) CliRunner {
return .{
.is_auto = is_auto,
.runner = .{
.on_line = onLine,
.on_choices = onChoices,
.on_value_changed = onValueChanged,
},
};
}

pub fn print(_: *CliRunner, comptime msg: []const u8, args: anytype) void {
const stdout = std.io.getStdOut().writer();
stdout.print(msg, args) catch {
std.debug.print("Could not print message", .{});
};
}

pub fn onLine(runner: *Runner, vm: *Vm, dialogue: Line) void {
const stdin = std.io.getStdIn().reader();
const self: *CliRunner = @fieldParentPtr("runner", runner);
self.print(":", .{});
if (dialogue.speaker) |speaker| {
self.print("{s}", .{speaker});
}
self.print(": ", .{});
self.print("{s}", .{dialogue.content});
if (self.is_auto) {
self.print("\n", .{});
vm.selectContinue();
} else {
var buf: [2]u8 = undefined;
if (stdin.readUntilDelimiterOrEof(&buf, '\n') catch &buf) |_| {
vm.selectContinue();
}
}
}

pub fn onChoices(runner: *Runner, vm: *Vm, choices: []Choice) void {
const stdin = std.io.getStdIn().reader();
const stderr = std.io.getStdErr().writer();
const self: *CliRunner = @fieldParentPtr("runner", runner);
var index: ?usize = null;
while (index == null) {
for (choices, 0..) |choice, i| {
self.print("[{d}] {s}\n", .{ i, choice.content });
}
var buf: [10]u8 = undefined;
if (stdin.readUntilDelimiterOrEof(&buf, '\n') catch &buf) |user_input| {
const input = std.mem.trim(u8, user_input, "\r\n");
index = std.fmt.parseInt(usize, input, 10) catch |err| blk: {
stderr.print("Invalid value: {}.\n", .{err}) catch {};
break :blk null;
};
if (index != null and index.? >= choices.len) {
index = null;
stderr.print("Invalid value.\n", .{}) catch {};
}
}
}
vm.selectChoice(index.?) catch |err| self.print("Error: {}", .{err});
}

pub fn onValueChanged(_: *Runner, _: *Vm, _: []const u8, _: Value) void {}
};

pub const AutoTestRunner = struct {
runner: Runner,
rnd: std.rand.Xoshiro256,

pub fn init() AutoTestRunner {
return .{
.rnd = std.rand.DefaultPrng.init(std.crypto.random.int(u64)),
.runner = .{
.on_line = onLine,
.on_choices = onChoices,
.on_value_changed = onValueChanged,
},
};
}

pub fn onLine(_: *Runner, vm: *Vm, _: Line) void {
vm.selectContinue();
}

pub fn onChoices(runner: *Runner, vm: *Vm, choices: []Choice) void {
var auto: *AutoTestRunner = @fieldParentPtr("runner", runner);
const index = auto.rnd.random().intRangeAtMost(usize, 0, choices.len - 1);
vm.selectChoice(index) catch |err| {
std.debug.print("Error: {}", .{err});
};
}

pub fn onValueChanged(_: *Runner, _: *Vm, _: []const u8, _: Value) void {}
};
12 changes: 12 additions & 0 deletions src/export/index.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const runner = @import("runner.zig");
pub const ExportLogger = runner.ExportLogger;
pub const ExportRunner = runner.ExportRunner;
pub const ExportFunction = runner.ExportFunction;
pub const ExportString = runner.ExportString;
pub const ExportLine = runner.ExportLine;
pub const ExportChoice = runner.ExportChoice;

pub const main = @import("main.zig");

const value = @import("value.zig");
pub const ExportValue = value.ExportValue;
Loading

0 comments on commit 3f06d1f

Please sign in to comment.