Playing with tests in build.zig

This commit is contained in:
jaina heartles 2022-09-08 01:50:10 -07:00
parent ca123c1cc0
commit 6e2754225f
2 changed files with 37 additions and 80 deletions

View file

@ -1,15 +1,21 @@
const std = @import("std");
fn addRunStep(b: *std.build.Builder, exe: *std.build.LibExeObjStep) void {
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const util_pkg = std.build.Pkg{
.name = "util",
.source = std.build.FileSource.relative("src/util/lib.zig"),
};
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
const http_pkg = std.build.Pkg{
.name = "http",
.source = std.build.FileSource.relative("src/http/lib.zig"),
.dependencies = &.{util_pkg},
};
const sql_pkg = std.build.Pkg{
.name = "sql",
.source = std.build.FileSource.relative("src/sql/lib.zig"),
.dependencies = &.{util_pkg},
};
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
@ -22,31 +28,34 @@ pub fn build(b: *std.build.Builder) void {
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
// There are some weird problems relating to sentinel values and function pointers
// when using the stage1 compiler. Just disable it entirely for now.
//b.use_stage1 = false;
const exe = b.addExecutable("apub", "src/main/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
const util = std.build.Pkg{
.name = "util",
.source = std.build.FileSource.relative("src/util/lib.zig"),
};
const sql = std.build.Pkg{ .name = "sql", .source = std.build.FileSource.relative("src/sql/lib.zig"), .dependencies = &.{util} };
const http = std.build.Pkg{
.name = "http",
.source = std.build.FileSource.relative("src/http/lib.zig"),
.dependencies = &.{util},
};
exe.addPackage(sql);
exe.addPackage(util);
exe.addPackage(http);
exe.addPackage(sql_pkg);
exe.addPackage(util_pkg);
exe.addPackage(http_pkg);
exe.linkSystemLibrary("sqlite3");
exe.linkLibC();
exe.install();
addRunStep(b, exe);
const util_tests = b.addTest("src/util/lib.zig");
const http_tests = b.addTest("src/http/lib.zig");
const sql_tests = b.addTest("src/sql/lib.zig");
http_tests.addPackage(util_pkg);
sql_tests.addPackage(util_pkg);
const unit_tests = b.step("unit-tests", "Run tests");
unit_tests.dependOn(&util_tests.step);
unit_tests.dependOn(&http_tests.step);
unit_tests.dependOn(&sql_tests.step);
const run_cmd = exe.run();
//run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the server");
run_step.dependOn(&run_cmd.step);
}

View file

@ -11,58 +11,6 @@ pub fn cloneStr(str: []const u8, alloc: std.mem.Allocator) ![]const u8 {
return new;
}
pub const case = struct {
// returns the number of capital letters in a string.
// only works with ascii characters
fn countCaps(str: []const u8) usize {
var count: usize = 0;
for (str) |ch| {
if (std.ascii.isUpper(ch)) {
count += 1;
}
}
return count;
}
// converts a string from PascalCase to snake_case at comptime.
// only works with ascii characters
pub fn pascalToSnake(comptime str: []const u8) Return: {
break :Return if (str.len == 0)
*const [0:0]u8
else
*const [str.len + countCaps(str) - 1:0]u8;
} {
comptime {
if (str.len == 0) return "";
var buf = std.mem.zeroes([str.len + countCaps(str) - 1:0]u8);
var i = 0;
for (str) |ch| {
if (std.ascii.isUpper(ch)) {
if (i != 0) {
buf[i] = '_';
i += 1;
}
buf[i] = std.ascii.toLower(ch);
} else {
buf[i] = ch;
}
i += 1;
}
return &buf;
}
}
};
test "pascalToSnake" {
try std.testing.expectEqual("", case.pascalToSnake(""));
try std.testing.expectEqual("abc", case.pascalToSnake("Abc"));
try std.testing.expectEqual("a_bc", case.pascalToSnake("ABc"));
try std.testing.expectEqual("a_b_c", case.pascalToSnake("ABC"));
try std.testing.expectEqual("ab_c", case.pascalToSnake("AbC"));
}
test {
_ = ciutf8;
_ = Uuid;