authorgravatar for k4@noreply.codeberg.orgK4 <k4@noreply.codeberg.org> 2026-07-28 17:31:32+03:00
committergravatar for k4@noreply.codeberg.orgK4 <k4@noreply.codeberg.org> 2026-08-05 11:16:10+03:00
log7aef9e5fb2cd17859510d80b25a8e9f5374eeadc
tree62ef189b2b04a6433cb6ed91a851c5dd5e19a232
parent03ab50821418fac99578fd762ba1ef135671b545

std.testing: rename `print*` to `failPrint*`


1 files changed, 67 insertions(+), 67 deletions(-)

lib/std/testing.zig+67-67
......@@ -41,7 +41,7 @@ pub const backend_can_print = switch (builtin.zig_backend) {
4141 else => true,
4242};
4343
44fn print(comptime fmt: []const u8, args: anytype) void {
44fn failPrint(comptime fmt: []const u8, args: anytype) void {
4545 if (@inComptime()) {
4646 @compileError(std.fmt.comptimePrint(fmt, args));
4747 } else if (backend_can_print) {
......@@ -53,11 +53,11 @@ fn print(comptime fmt: []const u8, args: anytype) void {
5353/// and then returns a test failure error when actual_error_union is not expected_error.
5454pub fn expectError(expected_error: anyerror, actual_error_union: anytype) !void {
5555 if (actual_error_union) |actual_payload| {
56 print("expected error.{s}, found {any}\n", .{ @errorName(expected_error), actual_payload });
56 failPrint("expected error.{s}, found {any}\n", .{ @errorName(expected_error), actual_payload });
5757 return error.TestExpectedError;
5858 } else |actual_error| {
5959 if (expected_error != actual_error) {
60 print("expected error.{s}, found error.{s}\n", .{
60 failPrint("expected error.{s}, found error.{s}\n", .{
6161 @errorName(expected_error),
6262 @errorName(actual_error),
6363 });
......@@ -91,7 +91,7 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
9191
9292 .type => {
9393 if (actual != expected) {
94 print("expected type {s}, found type {s}\n", .{ @typeName(expected), @typeName(actual) });
94 failPrint("expected type {s}, found type {s}\n", .{ @typeName(expected), @typeName(actual) });
9595 return error.TestExpectedEqual;
9696 }
9797 },
......@@ -107,7 +107,7 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
107107 .error_set,
108108 => {
109109 if (actual != expected) {
110 print("expected {any}, found {any}\n", .{ expected, actual });
110 failPrint("expected {any}, found {any}\n", .{ expected, actual });
111111 return error.TestExpectedEqual;
112112 }
113113 },
......@@ -116,17 +116,17 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
116116 switch (pointer.size) {
117117 .one, .many, .c => {
118118 if (actual != expected) {
119 print("expected {*}, found {*}\n", .{ expected, actual });
119 failPrint("expected {*}, found {*}\n", .{ expected, actual });
120120 return error.TestExpectedEqual;
121121 }
122122 },
123123 .slice => {
124124 if (actual.ptr != expected.ptr) {
125 print("expected slice ptr {*}, found {*}\n", .{ expected.ptr, actual.ptr });
125 failPrint("expected slice ptr {*}, found {*}\n", .{ expected.ptr, actual.ptr });
126126 return error.TestExpectedEqual;
127127 }
128128 if (actual.len != expected.len) {
129 print("expected slice len {}, found {}\n", .{ expected.len, actual.len });
129 failPrint("expected slice len {}, found {}\n", .{ expected.len, actual.len });
130130 return error.TestExpectedEqual;
131131 }
132132 },
......@@ -187,12 +187,12 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
187187 if (actual) |actual_payload| {
188188 try expectEqual(expected_payload, actual_payload);
189189 } else {
190 print("expected {any}, found null\n", .{expected_payload});
190 failPrint("expected {any}, found null\n", .{expected_payload});
191191 return error.TestExpectedEqual;
192192 }
193193 } else {
194194 if (actual) |actual_payload| {
195 print("expected null, found {any}\n", .{actual_payload});
195 failPrint("expected null, found {any}\n", .{actual_payload});
196196 return error.TestExpectedEqual;
197197 }
198198 }
......@@ -203,12 +203,12 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
203203 if (actual) |actual_payload| {
204204 try expectEqual(expected_payload, actual_payload);
205205 } else |actual_err| {
206 print("expected {any}, found {}\n", .{ expected_payload, actual_err });
206 failPrint("expected {any}, found {}\n", .{ expected_payload, actual_err });
207207 return error.TestExpectedEqual;
208208 }
209209 } else |expected_err| {
210210 if (actual) |actual_payload| {
211 print("expected {}, found {any}\n", .{ expected_err, actual_payload });
211 failPrint("expected {}, found {any}\n", .{ expected_err, actual_payload });
212212 return error.TestExpectedEqual;
213213 } else |actual_err| {
214214 try expectEqual(expected_err, actual_err);
......@@ -295,7 +295,7 @@ pub inline fn expectApproxEqAbs(expected: anytype, actual: anytype, tolerance: a
295295fn expectApproxEqAbsInner(comptime T: type, expected: T, actual: T, tolerance: T) !void {
296296 switch (@typeInfo(T)) {
297297 .float => if (!math.approxEqAbs(T, expected, actual, tolerance)) {
298 print("actual {}, not within absolute tolerance {} of expected {}\n", .{ actual, tolerance, expected });
298 failPrint("actual {}, not within absolute tolerance {} of expected {}\n", .{ actual, tolerance, expected });
299299 return error.TestExpectedApproxEqAbs;
300300 },
301301
......@@ -331,7 +331,7 @@ pub inline fn expectApproxEqRel(expected: anytype, actual: anytype, tolerance: a
331331fn expectApproxEqRelInner(comptime T: type, expected: T, actual: T, tolerance: T) !void {
332332 switch (@typeInfo(T)) {
333333 .float => if (!math.approxEqRel(T, expected, actual, tolerance)) {
334 print("actual {}, not within relative tolerance {} of expected {}\n", .{ actual, tolerance, expected });
334 failPrint("actual {}, not within relative tolerance {} of expected {}\n", .{ actual, tolerance, expected });
335335 return error.TestExpectedApproxEqRel;
336336 },
337337
......@@ -598,12 +598,12 @@ pub fn expectEqualSentinel(comptime T: type, comptime sentinel: T, expected: [:s
598598 };
599599
600600 if (!std.meta.eql(sentinel, expected_value_sentinel)) {
601 print("expectEqualSentinel: 'expected' sentinel in memory is different from its type sentinel. type sentinel {}, in memory sentinel {}\n", .{ sentinel, expected_value_sentinel });
601 failPrint("expectEqualSentinel: 'expected' sentinel in memory is different from its type sentinel. type sentinel {}, in memory sentinel {}\n", .{ sentinel, expected_value_sentinel });
602602 return error.TestExpectedEqual;
603603 }
604604
605605 if (!std.meta.eql(sentinel, actual_value_sentinel)) {
606 print("expectEqualSentinel: 'actual' sentinel in memory is different from its type sentinel. type sentinel {}, in memory sentinel {}\n", .{ sentinel, actual_value_sentinel });
606 failPrint("expectEqualSentinel: 'actual' sentinel in memory is different from its type sentinel. type sentinel {}, in memory sentinel {}\n", .{ sentinel, actual_value_sentinel });
607607 return error.TestExpectedEqual;
608608 }
609609}
......@@ -660,23 +660,23 @@ pub fn expectEqualStrings(expected: []const u8, actual: []const u8) !void {
660660 expected, actual, diff_index,
661661 }));
662662 }
663 print("\n====== expected this output: =========\n", .{});
664 printWithVisibleNewlines(expected);
665 print("\n======== instead found this: =========\n", .{});
666 printWithVisibleNewlines(actual);
667 print("\n======================================\n", .{});
663 failPrint("\n====== expected this output: =========\n", .{});
664 failPrintWithVisibleNewlines(expected);
665 failPrint("\n======== instead found this: =========\n", .{});
666 failPrintWithVisibleNewlines(actual);
667 failPrint("\n======================================\n", .{});
668668
669669 var diff_line_number: usize = 1;
670670 for (expected[0..diff_index]) |value| {
671671 if (value == '\n') diff_line_number += 1;
672672 }
673 print("First difference occurs on line {d}:\n", .{diff_line_number});
673 failPrint("First difference occurs on line {d}:\n", .{diff_line_number});
674674
675 print("expected:\n", .{});
676 printIndicatorLine(expected, diff_index);
675 failPrint("expected:\n", .{});
676 failPrintIndicatorLine(expected, diff_index);
677677
678 print("found:\n", .{});
679 printIndicatorLine(actual, diff_index);
678 failPrint("found:\n", .{});
679 failPrintIndicatorLine(actual, diff_index);
680680
681681 return error.TestExpectedEqual;
682682 }
......@@ -691,13 +691,13 @@ pub fn expectStringStartsWith(actual: []const u8, expected_starts_with: []const
691691 else
692692 actual;
693693
694 print("\n====== expected to start with: =========\n", .{});
695 printWithVisibleNewlines(expected_starts_with);
696 print("\n====== instead started with: ===========\n", .{});
697 printWithVisibleNewlines(shortened_actual);
698 print("\n========= full output: ==============\n", .{});
699 printWithVisibleNewlines(actual);
700 print("\n======================================\n", .{});
694 failPrint("\n====== expected to start with: =========\n", .{});
695 failPrintWithVisibleNewlines(expected_starts_with);
696 failPrint("\n====== instead started with: ===========\n", .{});
697 failPrintWithVisibleNewlines(shortened_actual);
698 failPrint("\n========= full output: ==============\n", .{});
699 failPrintWithVisibleNewlines(actual);
700 failPrint("\n======================================\n", .{});
701701
702702 return error.TestExpectedStartsWith;
703703}
......@@ -711,13 +711,13 @@ pub fn expectStringEndsWith(actual: []const u8, expected_ends_with: []const u8)
711711 else
712712 actual;
713713
714 print("\n====== expected to end with: =========\n", .{});
715 printWithVisibleNewlines(expected_ends_with);
716 print("\n====== instead ended with: ===========\n", .{});
717 printWithVisibleNewlines(shortened_actual);
718 print("\n========= full output: ==============\n", .{});
719 printWithVisibleNewlines(actual);
720 print("\n======================================\n", .{});
714 failPrint("\n====== expected to end with: =========\n", .{});
715 failPrintWithVisibleNewlines(expected_ends_with);
716 failPrint("\n====== instead ended with: ===========\n", .{});
717 failPrintWithVisibleNewlines(shortened_actual);
718 failPrint("\n========= full output: ==============\n", .{});
719 failPrintWithVisibleNewlines(actual);
720 failPrint("\n======================================\n", .{});
721721
722722 return error.TestExpectedEndsWith;
723723}
......@@ -756,7 +756,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
756756
757757 .type => {
758758 if (actual != expected) {
759 print("expected type {s}, found type {s}\n", .{ @typeName(expected), @typeName(actual) });
759 failPrint("expected type {s}, found type {s}\n", .{ @typeName(expected), @typeName(actual) });
760760 return error.TestExpectedEqual;
761761 }
762762 },
......@@ -772,7 +772,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
772772 .error_set,
773773 => {
774774 if (actual != expected) {
775 print("expected {any}, found {any}\n", .{ expected, actual });
775 failPrint("expected {any}, found {any}\n", .{ expected, actual });
776776 return error.TestExpectedEqual;
777777 }
778778 },
......@@ -782,7 +782,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
782782 // We have no idea what is behind those pointers, so the best we can do is `==` check.
783783 .c, .many => {
784784 if (actual != expected) {
785 print("expected {*}, found {*}\n", .{ expected, actual });
785 failPrint("expected {*}, found {*}\n", .{ expected, actual });
786786 return error.TestExpectedEqual;
787787 }
788788 },
......@@ -791,7 +791,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
791791 switch (@typeInfo(pointer.child)) {
792792 .@"fn", .@"opaque" => {
793793 if (actual != expected) {
794 print("expected {*}, found {*}\n", .{ expected, actual });
794 failPrint("expected {*}, found {*}\n", .{ expected, actual });
795795 return error.TestExpectedEqual;
796796 }
797797 },
......@@ -800,13 +800,13 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
800800 },
801801 .slice => {
802802 if (expected.len != actual.len) {
803 print("Slice len not the same, expected {d}, found {d}\n", .{ expected.len, actual.len });
803 failPrint("Slice len not the same, expected {d}, found {d}\n", .{ expected.len, actual.len });
804804 return error.TestExpectedEqual;
805805 }
806806 var i: usize = 0;
807807 while (i < expected.len) : (i += 1) {
808808 expectEqualDeep(expected[i], actual[i]) catch |e| {
809 print("index {d} incorrect. expected {any}, found {any}\n", .{
809 failPrint("index {d} incorrect. expected {any}, found {any}\n", .{
810810 i, expected[i], actual[i],
811811 });
812812 return e;
......@@ -818,13 +818,13 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
818818
819819 .array => {
820820 if (expected.len != actual.len) {
821 print("Array len not the same, expected {d}, found {d}\n", .{ expected.len, actual.len });
821 failPrint("Array len not the same, expected {d}, found {d}\n", .{ expected.len, actual.len });
822822 return error.TestExpectedEqual;
823823 }
824824 var i: usize = 0;
825825 while (i < expected.len) : (i += 1) {
826826 expectEqualDeep(expected[i], actual[i]) catch |e| {
827 print("index {d} incorrect. expected {any}, found {any}\n", .{
827 failPrint("index {d} incorrect. expected {any}, found {any}\n", .{
828828 i, expected[i], actual[i],
829829 });
830830 return e;
......@@ -834,12 +834,12 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
834834
835835 .vector => |info| {
836836 if (info.len != @typeInfo(@TypeOf(actual)).vector.len) {
837 print("Vector len not the same, expected {d}, found {d}\n", .{ info.len, @typeInfo(@TypeOf(actual)).vector.len });
837 failPrint("Vector len not the same, expected {d}, found {d}\n", .{ info.len, @typeInfo(@TypeOf(actual)).vector.len });
838838 return error.TestExpectedEqual;
839839 }
840840 inline for (0..info.len) |i| {
841841 expectEqualDeep(expected[i], actual[i]) catch |e| {
842 print("index {d} incorrect. expected {any}, found {any}\n", .{
842 failPrint("index {d} incorrect. expected {any}, found {any}\n", .{
843843 i, expected[i], actual[i],
844844 });
845845 return e;
......@@ -850,7 +850,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
850850 .@"struct" => |structType| {
851851 inline for (structType.field_names) |field_name| {
852852 expectEqualDeep(@field(expected, field_name), @field(actual, field_name)) catch |e| {
853 print("Field {s} incorrect. expected {any}, found {any}\n", .{ field_name, @field(expected, field_name), @field(actual, field_name) });
853 failPrint("Field {s} incorrect. expected {any}, found {any}\n", .{ field_name, @field(expected, field_name), @field(actual, field_name) });
854854 return e;
855855 };
856856 }
......@@ -881,12 +881,12 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
881881 if (actual) |actual_payload| {
882882 try expectEqualDeep(expected_payload, actual_payload);
883883 } else {
884 print("expected {any}, found null\n", .{expected_payload});
884 failPrint("expected {any}, found null\n", .{expected_payload});
885885 return error.TestExpectedEqual;
886886 }
887887 } else {
888888 if (actual) |actual_payload| {
889 print("expected null, found {any}\n", .{actual_payload});
889 failPrint("expected null, found {any}\n", .{actual_payload});
890890 return error.TestExpectedEqual;
891891 }
892892 }
......@@ -897,12 +897,12 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
897897 if (actual) |actual_payload| {
898898 try expectEqualDeep(expected_payload, actual_payload);
899899 } else |actual_err| {
900 print("expected {any}, found {any}\n", .{ expected_payload, actual_err });
900 failPrint("expected {any}, found {any}\n", .{ expected_payload, actual_err });
901901 return error.TestExpectedEqual;
902902 }
903903 } else |expected_err| {
904904 if (actual) |actual_payload| {
905 print("expected {any}, found {any}\n", .{ expected_err, actual_payload });
905 failPrint("expected {any}, found {any}\n", .{ expected_err, actual_payload });
906906 return error.TestExpectedEqual;
907907 } else |actual_err| {
908908 try expectEqualDeep(expected_err, actual_err);
......@@ -998,7 +998,7 @@ test "expectEqualDeep composite type" {
998998 );
999999}
10001000
1001fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
1001fn failPrintIndicatorLine(source: []const u8, indicator_index: usize) void {
10021002 const line_begin_index = if (std.mem.findScalarLast(u8, source[0..indicator_index], '\n')) |line_begin|
10031003 line_begin + 1
10041004 else
......@@ -1008,29 +1008,29 @@ fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
10081008 else
10091009 source.len;
10101010
1011 printLine(source[line_begin_index..line_end_index]);
1011 failPrintLine(source[line_begin_index..line_end_index]);
10121012 for (line_begin_index..indicator_index) |_|
1013 print(" ", .{});
1013 failPrint(" ", .{});
10141014 if (indicator_index >= source.len)
1015 print("^ (end of string)\n", .{})
1015 failPrint("^ (end of string)\n", .{})
10161016 else
1017 print("^ ('\\x{x:0>2}')\n", .{source[indicator_index]});
1017 failPrint("^ ('\\x{x:0>2}')\n", .{source[indicator_index]});
10181018}
10191019
1020fn printWithVisibleNewlines(source: []const u8) void {
1020fn failPrintWithVisibleNewlines(source: []const u8) void {
10211021 var i: usize = 0;
10221022 while (std.mem.findScalar(u8, source[i..], '\n')) |nl| : (i += nl + 1) {
1023 printLine(source[i..][0..nl]);
1023 failPrintLine(source[i..][0..nl]);
10241024 }
1025 print("{s}␃\n", .{source[i..]}); // End of Text symbol (ETX)
1025 failPrint("{s}␃\n", .{source[i..]}); // End of Text symbol (ETX)
10261026}
10271027
1028fn printLine(line: []const u8) void {
1028fn failPrintLine(line: []const u8) void {
10291029 if (line.len != 0) switch (line[line.len - 1]) {
1030 ' ', '\t' => return print("{s}⏎\n", .{line}), // Return symbol
1030 ' ', '\t' => return failPrint("{s}⏎\n", .{line}), // Return symbol
10311031 else => {},
10321032 };
1033 print("{s}\n", .{line});
1033 failPrint("{s}\n", .{line});
10341034}
10351035
10361036test {
......@@ -1139,7 +1139,7 @@ pub fn checkAllAllocationFailures(
11391139 } else |err| switch (err) {
11401140 error.OutOfMemory => {
11411141 if (failing_allocator_inst.allocated_bytes != failing_allocator_inst.freed_bytes) {
1142 print(
1142 failPrint(
11431143 "\nfail_index: {d}/{d}\nallocated bytes: {d}\nfreed bytes: {d}\nallocations: {d}\ndeallocations: {d}\nallocation that was made to fail: {f}",
11441144 .{
11451145 fail_index,