authorgravatar for matthew.mcallister.0@gmail.comMatthew McAllister <matthew.mcallister.0@gmail.com> 2019-02-17 12:47:53-08:00
committergravatar for matthew.mcallister.0@gmail.comMatthew McAllister <matthew.mcallister.0@gmail.com> 2019-02-17 15:33:28-08:00
logc70ee9177e5b0095c152b66fe8f22cd870e778b9
treea3f538f14b0c597422398d595d02053bf3698315
parent51783510b9b972fab52d429ff3311b0fe8402e42

Check for duped error messages in compile tests


2 files changed, 81 insertions(+), 24 deletions(-)

test/compile_errors.zig+17-13
......@@ -137,19 +137,23 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
137137 ".tmp_source.zig:3:15: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
138138 );
139139
140 cases.addTest(
141 "compile log statement warning deduplication in generic fn",
142 \\export fn entry() void {
143 \\ inner(1);
144 \\ inner(2);
145 \\}
146 \\fn inner(comptime n: usize) void {
147 \\ comptime var i = 0;
148 \\ inline while (i < n) : (i += 1) { @compileLog("!@#$"); }
149 \\}
150 ,
151 ".tmp_source.zig:7:39: error: found compile log statement",
152 );
140 cases.addCase(x: {
141 var tc = cases.create(
142 "compile log statement warning deduplication in generic fn",
143 \\export fn entry() void {
144 \\ inner(1);
145 \\ inner(2);
146 \\}
147 \\fn inner(comptime n: usize) void {
148 \\ comptime var i = 0;
149 \\ inline while (i < n) : (i += 1) { @compileLog("!@#$"); }
150 \\}
151 ,
152 ".tmp_source.zig:7:39: error: found compile log statement",
153 );
154 tc.expect_exact = true;
155 break :x tc;
156 });
153157
154158 cases.addTest(
155159 "@truncate undefined value",
test/tests.zig+64-11
......@@ -536,6 +536,7 @@ pub const CompileErrorContext = struct {
536536 name: []const u8,
537537 sources: ArrayList(SourceFile),
538538 expected_errors: ArrayList([]const u8),
539 expect_exact: bool,
539540 link_libc: bool,
540541 is_exe: bool,
541542 is_test: bool,
......@@ -565,6 +566,26 @@ pub const CompileErrorContext = struct {
565566 case: *const TestCase,
566567 build_mode: Mode,
567568
569 const ErrLineIter = struct {
570 lines: mem.SplitIterator,
571
572 const source_file = ".tmp_source.zig";
573
574 fn init(input: []const u8) ErrLineIter {
575 return ErrLineIter {
576 .lines = mem.separate(input, "\n"),
577 };
578 }
579
580 fn next(self: *ErrLineIter) ?[]const u8 {
581 while (self.lines.next()) |line| {
582 if (mem.indexOf(u8, line, source_file) != null)
583 return line;
584 }
585 return null;
586 }
587 };
588
568589 pub fn create(context: *CompileErrorContext, name: []const u8, case: *const TestCase, build_mode: Mode) *CompileCmpOutputStep {
569590 const allocator = context.b.allocator;
570591 const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
......@@ -674,19 +695,50 @@ pub const CompileErrorContext = struct {
674695 return error.TestFailed;
675696 }
676697
677 for (self.case.expected_errors.toSliceConst()) |expected_error| {
678 if (mem.indexOf(u8, stderr, expected_error) == null) {
679 warn(
680 \\
681 \\========= Expected this compile error: =========
682 \\{}
683 \\================================================
684 \\{}
685 \\
686 , expected_error, stderr);
687 return error.TestFailed;
698 var ok = true;
699 if (self.case.expect_exact) {
700 var err_iter = ErrLineIter.init(stderr);
701 var i: usize = 0;
702 ok = while (err_iter.next()) |line| : (i += 1) {
703 if (i >= self.case.expected_errors.len) break false;
704 const expected = self.case.expected_errors.at(i);
705 if (mem.indexOf(u8, line, expected) == null) break false;
706 continue;
707 } else true;
708
709 ok = ok and i == self.case.expected_errors.len;
710
711 if (!ok) {
712 warn("\n======== Expected these compile errors: ========\n");
713 for (self.case.expected_errors.toSliceConst()) |expected| {
714 warn("{}\n", expected);
715 }
716 }
717 } else {
718 for (self.case.expected_errors.toSliceConst()) |expected| {
719 if (mem.indexOf(u8, stderr, expected) == null) {
720 warn(
721 \\=========== Expected compile error: ============
722 \\{}
723 \\
724 , expected
725 );
726 ok = false;
727 break;
728 }
688729 }
689730 }
731
732 if (!ok) {
733 warn(
734 \\================= Full output: =================
735 \\{}
736 \\
737 , stderr
738 );
739 return error.TestFailed;
740 }
741
690742 warn("OK\n");
691743 }
692744 };
......@@ -704,6 +756,7 @@ pub const CompileErrorContext = struct {
704756 .name = name,
705757 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
706758 .expected_errors = ArrayList([]const u8).init(self.b.allocator),
759 .expect_exact = false,
707760 .link_libc = false,
708761 .is_exe = false,
709762 .is_test = false,