authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-19 15:38:12-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-19 15:38:12-04:00
logd12f1f5b4954d893111c05420060354537a1485c
tree1026150a776607a5aae06273b3c3fa37ec47831f
parent9b7f438882b4f283d252d8ca364607fae385cc1a

test framework supports name prefix and filter argument

rename self hosted tests to behavior tests

10 files changed, 137 insertions(+), 84 deletions(-)

build.zig+12-8
......@@ -5,18 +5,20 @@ pub fn build(b: &Builder) {
55 const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
66 const test_step = b.step("test", "Run all the tests");
77
8 const self_hosted_tests = b.step("test-self-hosted", "Run the self-hosted tests");
9 test_step.dependOn(self_hosted_tests);
8 const behavior_tests = b.step("test-behavior", "Run the behavior tests");
9 test_step.dependOn(behavior_tests);
1010 for ([]bool{false, true}) |release| {
1111 for ([]bool{false, true}) |link_libc| {
12 const these_tests = b.addTest("test/self_hosted.zig");
13 // TODO add prefix to test names
14 // TODO pass test_filter to these_tests
12 const these_tests = b.addTest("test/behavior.zig");
13 these_tests.setNamePrefix(b.fmt("behavior-{}-{} ",
14 if (release) "release" else "debug",
15 if (link_libc) "c" else "bare"));
16 these_tests.setFilter(test_filter);
1517 these_tests.setRelease(release);
1618 if (link_libc) {
1719 these_tests.linkLibrary("c");
1820 }
19 self_hosted_tests.dependOn(&these_tests.step);
21 behavior_tests.dependOn(&these_tests.step);
2022 }
2123 }
2224
......@@ -25,8 +27,10 @@ pub fn build(b: &Builder) {
2527 for ([]bool{false, true}) |release| {
2628 for ([]bool{false, true}) |link_libc| {
2729 const these_tests = b.addTest("std/index.zig");
28 // TODO add prefix to test names
29 // TODO pass test_filter to these_tests
30 these_tests.setNamePrefix(b.fmt("std-{}-{} ",
31 if (release) "release" else "debug",
32 if (link_libc) "c" else "bare"));
33 these_tests.setFilter(test_filter);
3034 these_tests.setRelease(release);
3135 if (link_libc) {
3236 these_tests.linkLibrary("c");
src/all_types.hpp+3
......@@ -1458,6 +1458,9 @@ struct CodeGen {
14581458 ZigList<Buf *> link_objects;
14591459
14601460 ZigList<TypeTableEntry *> name_table_enums;
1461
1462 Buf *test_filter;
1463 Buf *test_name_prefix;
14611464};
14621465
14631466enum VarLinkage {
src/analyze.cpp+8-1
......@@ -1959,7 +1959,14 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope
19591959 if (import->package != g->root_package)
19601960 return;
19611961
1962 Buf *test_name = node->data.test_decl.name;
1962 Buf *decl_name_buf = node->data.test_decl.name;
1963
1964 Buf *test_name = g->test_name_prefix ?
1965 buf_sprintf("%s%s", buf_ptr(g->test_name_prefix), buf_ptr(decl_name_buf)) : decl_name_buf;
1966
1967 if (g->test_filter != nullptr && strstr(buf_ptr(test_name), buf_ptr(g->test_filter)) == nullptr) {
1968 return;
1969 }
19631970
19641971 TldFn *tld_fn = allocate<TldFn>(1);
19651972 init_tld(&tld_fn->base, TldIdFn, test_name, VisibModPrivate, node, &decls_scope->base);
src/codegen.cpp+8
......@@ -147,6 +147,14 @@ void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt) {
147147 g->omit_zigrt = omit_zigrt;
148148}
149149
150void codegen_set_test_filter(CodeGen *g, Buf *filter) {
151 g->test_filter = filter;
152}
153
154void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix) {
155 g->test_name_prefix = prefix;
156}
157
150158void codegen_set_is_test(CodeGen *g, bool is_test_build) {
151159 g->is_test_build = is_test_build;
152160}
src/codegen.hpp+2
......@@ -44,6 +44,8 @@ void codegen_set_mmacosx_version_min(CodeGen *g, Buf *mmacosx_version_min);
4444void codegen_set_mios_version_min(CodeGen *g, Buf *mios_version_min);
4545void codegen_set_linker_script(CodeGen *g, const char *linker_script);
4646void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt);
47void codegen_set_test_filter(CodeGen *g, Buf *filter);
48void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix);
4749
4850PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path);
4951void codegen_add_root_code(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code);
src/main.cpp+17
......@@ -64,6 +64,9 @@ static int usage(const char *arg0) {
6464 " -mwindows (windows only) --subsystem windows to the linker\n"
6565 " -rdynamic add all symbols to the dynamic symbol table\n"
6666 " -rpath [path] add directory to the runtime library search path\n"
67 "Test Options:\n"
68 " --test-filter [text] skip tests that do not match filter\n"
69 " --test-name-prefix [text] add prefix to all tests\n"
6770 , arg0);
6871 return EXIT_FAILURE;
6972}
......@@ -151,6 +154,8 @@ int main(int argc, char **argv) {
151154 ZigList<const char *> rpath_list = {0};
152155 bool each_lib_rpath = false;
153156 ZigList<const char *> objects = {0};
157 const char *test_filter = nullptr;
158 const char *test_name_prefix = nullptr;
154159
155160 if (argc >= 2 && strcmp(argv[1], "build") == 0) {
156161 const char *zig_exe_path = arg0;
......@@ -341,6 +346,10 @@ int main(int argc, char **argv) {
341346 linker_script = argv[i];
342347 } else if (strcmp(arg, "-rpath") == 0) {
343348 rpath_list.append(argv[i]);
349 } else if (strcmp(arg, "--test-filter") == 0) {
350 test_filter = argv[i];
351 } else if (strcmp(arg, "--test-name-prefix") == 0) {
352 test_name_prefix = argv[i];
344353 } else {
345354 fprintf(stderr, "Invalid argument: %s\n", arg);
346355 return usage(arg0);
......@@ -562,6 +571,14 @@ int main(int argc, char **argv) {
562571 codegen_set_mios_version_min(g, buf_create_from_str(mios_version_min));
563572 }
564573
574 if (test_filter) {
575 codegen_set_test_filter(g, buf_create_from_str(test_filter));
576 }
577
578 if (test_name_prefix) {
579 codegen_set_test_name_prefix(g, buf_create_from_str(test_name_prefix));
580 }
581
565582 if (cmd == CmdBuild) {
566583 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);
567584 codegen_link(g, out_file);
std/build.zig+46-34
......@@ -10,7 +10,7 @@ const StdIo = os.ChildProcess.StdIo;
1010const Term = os.ChildProcess.Term;
1111const BufSet = @import("buf_set.zig").BufSet;
1212const BufMap = @import("buf_map.zig").BufMap;
13const fmt = @import("fmt.zig");
13const fmt_lib = @import("fmt.zig");
1414
1515error ExtraArg;
1616error UncleanExit;
......@@ -189,7 +189,7 @@ pub const Builder = struct {
189189 }
190190
191191 pub fn addLog(self: &Builder, comptime format: []const u8, args: ...) -> &LogStep {
192 const data = %%fmt.allocPrint(self.allocator, format, args);
192 const data = self.fmt(format, args);
193193 const log_step = %%self.allocator.create(LogStep);
194194 *log_step = LogStep.init(self, data);
195195 return log_step;
......@@ -543,6 +543,10 @@ pub const Builder = struct {
543543 fn pathFromRoot(self: &Builder, rel_path: []const u8) -> []u8 {
544544 return %%os.path.join(self.allocator, self.build_root, rel_path);
545545 }
546
547 pub fn fmt(self: &Builder, comptime format: []const u8, args: ...) -> []u8 {
548 return %%fmt_lib.allocPrint(self.allocator, format, args);
549 }
546550};
547551
548552const Version = struct {
......@@ -891,19 +895,16 @@ pub const LinkStep = struct {
891895 fn computeOutFileName(self: &LinkStep) {
892896 switch (self.out_type) {
893897 OutType.Exe => {
894 self.out_filename = %%fmt.allocPrint(self.builder.allocator, "{}{}",
895 self.name, self.target.exeFileExt());
898 self.out_filename = self.builder.fmt("{}{}", self.name, self.target.exeFileExt());
896899 },
897900 OutType.Lib => {
898901 if (self.static) {
899 self.out_filename = %%fmt.allocPrint(self.builder.allocator, "lib{}.a", self.name);
902 self.out_filename = self.builder.fmt("lib{}.a", self.name);
900903 } else {
901 self.out_filename = %%fmt.allocPrint(self.builder.allocator, "lib{}.so.{d}.{d}.{d}",
904 self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}",
902905 self.name, self.version.major, self.version.minor, self.version.patch);
903 self.major_only_filename = %%fmt.allocPrint(self.builder.allocator,
904 "lib{}.so.{d}", self.name, self.version.major);
905 self.name_only_filename = %%fmt.allocPrint(self.builder.allocator,
906 "lib{}.so", self.name);
906 self.major_only_filename = self.builder.fmt("lib{}.so.{d}", self.name, self.version.major);
907 self.name_only_filename = self.builder.fmt("lib{}.so", self.name);
907908 }
908909 },
909910 }
......@@ -1051,15 +1052,19 @@ pub const TestStep = struct {
10511052 release: bool,
10521053 verbose: bool,
10531054 link_libs: BufSet,
1055 name_prefix: []const u8,
1056 filter: ?[]const u8,
10541057
10551058 pub fn init(builder: &Builder, root_src: []const u8) -> TestStep {
1056 const step_name = %%fmt.allocPrint(builder.allocator, "test {}", root_src);
1059 const step_name = builder.fmt("test {}", root_src);
10571060 TestStep {
10581061 .step = Step.init(step_name, builder.allocator, make),
10591062 .builder = builder,
10601063 .root_src = root_src,
10611064 .release = false,
10621065 .verbose = false,
1066 .name_prefix = "",
1067 .filter = null,
10631068 .link_libs = BufSet.init(builder.allocator),
10641069 }
10651070 }
......@@ -1076,6 +1081,14 @@ pub const TestStep = struct {
10761081 %%self.link_libs.put(name);
10771082 }
10781083
1084 pub fn setNamePrefix(self: &TestStep, text: []const u8) {
1085 self.name_prefix = text;
1086 }
1087
1088 pub fn setFilter(self: &TestStep, text: ?[]const u8) {
1089 self.filter = text;
1090 }
1091
10791092 fn make(step: &Step) -> %void {
10801093 const self = @fieldParentPtr(TestStep, "step", step);
10811094 const builder = self.builder;
......@@ -1094,6 +1107,16 @@ pub const TestStep = struct {
10941107 %%zig_args.append("--release");
10951108 }
10961109
1110 if (const filter ?= self.filter) {
1111 %%zig_args.append("--test-filter");
1112 %%zig_args.append(filter);
1113 }
1114
1115 if (self.name_prefix.len != 0) {
1116 %%zig_args.append("--test-name-prefix");
1117 %%zig_args.append(self.name_prefix);
1118 }
1119
10971120 {
10981121 var it = self.link_libs.iterator();
10991122 while (true) {
......@@ -1169,14 +1192,12 @@ pub const CLibrary = struct {
11691192
11701193 fn computeOutFileName(self: &CLibrary) {
11711194 if (self.static) {
1172 self.out_filename = %%fmt.allocPrint(self.builder.allocator, "lib{}.a", self.name);
1195 self.out_filename = self.builder.fmt("lib{}.a", self.name);
11731196 } else {
1174 self.out_filename = %%fmt.allocPrint(self.builder.allocator, "lib{}.so.{d}.{d}.{d}",
1197 self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}",
11751198 self.name, self.version.major, self.version.minor, self.version.patch);
1176 self.major_only_filename = %%fmt.allocPrint(self.builder.allocator,
1177 "lib{}.so.{d}", self.name, self.version.major);
1178 self.name_only_filename = %%fmt.allocPrint(self.builder.allocator,
1179 "lib{}.so", self.name);
1199 self.major_only_filename = self.builder.fmt("lib{}.so.{d}", self.name, self.version.major);
1200 self.name_only_filename = self.builder.fmt("lib{}.so", self.name);
11801201 }
11811202 }
11821203
......@@ -1235,7 +1256,7 @@ pub const CLibrary = struct {
12351256 %%cc_args.append(source_file);
12361257
12371258 // TODO don't dump the .o file in the same place as the source file
1238 const o_file = %%fmt.allocPrint(builder.allocator, "{}{}", source_file, self.target.oFileExt());
1259 const o_file = builder.fmt("{}{}", source_file, self.target.oFileExt());
12391260 defer builder.allocator.free(o_file);
12401261 %%cc_args.append("-o");
12411262 %%cc_args.append(o_file);
......@@ -1262,8 +1283,7 @@ pub const CLibrary = struct {
12621283 %%cc_args.append("-fPIC");
12631284 %%cc_args.append("-shared");
12641285
1265 const soname_arg = %%fmt.allocPrint(builder.allocator, "-Wl,-soname,lib{}.so.{d}",
1266 self.name, self.version.major);
1286 const soname_arg = builder.fmt("-Wl,-soname,lib{}.so.{d}", self.name, self.version.major);
12671287 defer builder.allocator.free(soname_arg);
12681288 %%cc_args.append(soname_arg);
12691289
......@@ -1372,7 +1392,7 @@ pub const CExecutable = struct {
13721392 %%cc_args.append(source_file);
13731393
13741394 // TODO don't dump the .o file in the same place as the source file
1375 const o_file = %%fmt.allocPrint(builder.allocator, "{}{}", source_file, self.target.oFileExt());
1395 const o_file = builder.fmt("{}{}", source_file, self.target.oFileExt());
13761396 defer builder.allocator.free(o_file);
13771397 %%cc_args.append("-o");
13781398 %%cc_args.append(o_file);
......@@ -1400,7 +1420,7 @@ pub const CExecutable = struct {
14001420 %%cc_args.append("-o");
14011421 %%cc_args.append(self.name);
14021422
1403 const rpath_arg = %%fmt.allocPrint(builder.allocator, "-Wl,-rpath,{}", builder.out_dir);
1423 const rpath_arg = builder.fmt("-Wl,-rpath,{}", builder.out_dir);
14041424 defer builder.allocator.free(rpath_arg);
14051425 %%cc_args.append(rpath_arg);
14061426
......@@ -1462,9 +1482,7 @@ pub const InstallCLibraryStep = struct {
14621482 pub fn init(builder: &Builder, lib: &CLibrary) -> InstallCLibraryStep {
14631483 var self = InstallCLibraryStep {
14641484 .builder = builder,
1465 .step = Step.init(
1466 %%fmt.allocPrint(builder.allocator, "install {}", lib.step.name),
1467 builder.allocator, make),
1485 .step = Step.init(builder.fmt("install {}", lib.step.name), builder.allocator, make),
14681486 .lib = lib,
14691487 .dest_file = undefined,
14701488 };
......@@ -1497,9 +1515,7 @@ pub const InstallFileStep = struct {
14971515 pub fn init(builder: &Builder, src_path: []const u8, dest_path: []const u8) -> InstallFileStep {
14981516 return InstallFileStep {
14991517 .builder = builder,
1500 .step = Step.init(
1501 %%fmt.allocPrint(builder.allocator, "install {}", src_path),
1502 builder.allocator, make),
1518 .step = Step.init(builder.fmt("install {}", src_path), builder.allocator, make),
15031519 .src_path = src_path,
15041520 .dest_path = dest_path,
15051521 };
......@@ -1521,9 +1537,7 @@ pub const WriteFileStep = struct {
15211537 pub fn init(builder: &Builder, file_path: []const u8, data: []const u8) -> WriteFileStep {
15221538 return WriteFileStep {
15231539 .builder = builder,
1524 .step = Step.init(
1525 %%fmt.allocPrint(builder.allocator, "writefile {}", file_path),
1526 builder.allocator, make),
1540 .step = Step.init(builder.fmt("writefile {}", file_path), builder.allocator, make),
15271541 .file_path = file_path,
15281542 .data = data,
15291543 };
......@@ -1550,9 +1564,7 @@ pub const LogStep = struct {
15501564 pub fn init(builder: &Builder, data: []const u8) -> LogStep {
15511565 return LogStep {
15521566 .builder = builder,
1553 .step = Step.init(
1554 %%fmt.allocPrint(builder.allocator, "log {}", data),
1555 builder.allocator, make),
1567 .step = Step.init(builder.fmt("log {}", data), builder.allocator, make),
15561568 .data = data,
15571569 };
15581570 }
test/behavior.zig created+39
......@@ -0,0 +1,39 @@
1comptime {
2 _ = @import("cases/array.zig");
3 _ = @import("cases/asm.zig");
4 _ = @import("cases/atomics.zig");
5 _ = @import("cases/bool.zig");
6 _ = @import("cases/cast.zig");
7 _ = @import("cases/const_slice_child.zig");
8 _ = @import("cases/defer.zig");
9 _ = @import("cases/enum.zig");
10 _ = @import("cases/enum_with_members.zig");
11 _ = @import("cases/error.zig");
12 _ = @import("cases/eval.zig");
13 _ = @import("cases/field_parent_ptr.zig");
14 _ = @import("cases/fn.zig");
15 _ = @import("cases/for.zig");
16 _ = @import("cases/generics.zig");
17 _ = @import("cases/goto.zig");
18 _ = @import("cases/if.zig");
19 _ = @import("cases/import.zig");
20 _ = @import("cases/incomplete_struct_param_tld.zig");
21 _ = @import("cases/ir_block_deps.zig");
22 _ = @import("cases/math.zig");
23 _ = @import("cases/misc.zig");
24 _ = @import("cases/namespace_depends_on_compile_var/index.zig");
25 _ = @import("cases/null.zig");
26 _ = @import("cases/pub_enum/index.zig");
27 _ = @import("cases/sizeof_and_typeof.zig");
28 _ = @import("cases/struct.zig");
29 _ = @import("cases/struct_contains_slice_of_itself.zig");
30 _ = @import("cases/switch.zig");
31 _ = @import("cases/switch_prong_err_enum.zig");
32 _ = @import("cases/switch_prong_implicit_cast.zig");
33 _ = @import("cases/this.zig");
34 _ = @import("cases/try.zig");
35 _ = @import("cases/undefined.zig");
36 _ = @import("cases/var_args.zig");
37 _ = @import("cases/void.zig");
38 _ = @import("cases/while.zig");
39}
test/self_hosted.zig deleted-39
......@@ -1,39 +0,0 @@
1comptime {
2 _ = @import("cases/array.zig");
3 _ = @import("cases/asm.zig");
4 _ = @import("cases/atomics.zig");
5 _ = @import("cases/bool.zig");
6 _ = @import("cases/cast.zig");
7 _ = @import("cases/const_slice_child.zig");
8 _ = @import("cases/defer.zig");
9 _ = @import("cases/enum.zig");
10 _ = @import("cases/enum_with_members.zig");
11 _ = @import("cases/error.zig");
12 _ = @import("cases/eval.zig");
13 _ = @import("cases/field_parent_ptr.zig");
14 _ = @import("cases/fn.zig");
15 _ = @import("cases/for.zig");
16 _ = @import("cases/generics.zig");
17 _ = @import("cases/goto.zig");
18 _ = @import("cases/if.zig");
19 _ = @import("cases/import.zig");
20 _ = @import("cases/incomplete_struct_param_tld.zig");
21 _ = @import("cases/ir_block_deps.zig");
22 _ = @import("cases/math.zig");
23 _ = @import("cases/misc.zig");
24 _ = @import("cases/namespace_depends_on_compile_var/index.zig");
25 _ = @import("cases/null.zig");
26 _ = @import("cases/pub_enum/index.zig");
27 _ = @import("cases/sizeof_and_typeof.zig");
28 _ = @import("cases/struct.zig");
29 _ = @import("cases/struct_contains_slice_of_itself.zig");
30 _ = @import("cases/switch.zig");
31 _ = @import("cases/switch_prong_err_enum.zig");
32 _ = @import("cases/switch_prong_implicit_cast.zig");
33 _ = @import("cases/this.zig");
34 _ = @import("cases/try.zig");
35 _ = @import("cases/undefined.zig");
36 _ = @import("cases/var_args.zig");
37 _ = @import("cases/void.zig");
38 _ = @import("cases/while.zig");
39}
test/tests.zig+2-2
......@@ -341,8 +341,8 @@ pub const CompareOutputContext = struct {
341341 },
342342 Special.None => {
343343 for ([]bool{false, true}) |release| {
344 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "{} ({})",
345 case.name, if (release) "release" else "debug");
344 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "{} {} ({})",
345 "compare-output", case.name, if (release) "release" else "debug");
346346 if (const filter ?= self.test_filter) {
347347 if (mem.indexOf(u8, annotated_case_name, filter) == null)
348348 continue;