| ... | ... | @@ -52,8 +52,13 @@ pub fn expectError(expected_error: anyerror, actual_error_union: anytype) !void |
| 52 | 52 | /// This function is intended to be used only in tests. When the two values are not |
| 53 | 53 | /// equal, prints diagnostics to stderr to show exactly how they are not equal, |
| 54 | 54 | /// then returns a test failure error. |
| 55 | | /// `actual` is casted to the type of `expected`. |
| 56 | | pub fn expectEqual(expected: anytype, actual: @TypeOf(expected)) !void { |
| 55 | /// `actual` and `expected` are coerced to a common type using peer type resolution. |
| 56 | pub inline fn expectEqual(expected: anytype, actual: anytype) !void { |
| 57 | const T = @TypeOf(expected, actual); |
| 58 | return expectEqualInner(T, expected, actual); |
| 59 | } |
| 60 | |
| 61 | fn expectEqualInner(comptime T: type, expected: T, actual: T) !void { |
| 57 | 62 | switch (@typeInfo(@TypeOf(actual))) { |
| 58 | 63 | .NoReturn, |
| 59 | 64 | .Opaque, |
| ... | ... | @@ -224,9 +229,13 @@ pub fn expectFmt(expected: []const u8, comptime template: []const u8, args: anyt |
| 224 | 229 | /// to show exactly how they are not equal, then returns a test failure error. |
| 225 | 230 | /// See `math.approxEqAbs` for more information on the tolerance parameter. |
| 226 | 231 | /// The types must be floating-point. |
| 227 | | pub fn expectApproxEqAbs(expected: anytype, actual: @TypeOf(expected), tolerance: @TypeOf(expected)) !void { |
| 228 | | const T = @TypeOf(expected); |
| 232 | /// `actual` and `expected` are coerced to a common type using peer type resolution. |
| 233 | pub inline fn expectApproxEqAbs(expected: anytype, actual: anytype, tolerance: anytype) !void { |
| 234 | const T = @TypeOf(expected, actual, tolerance); |
| 235 | return expectApproxEqAbsInner(T, expected, actual, tolerance); |
| 236 | } |
| 229 | 237 | |
| 238 | fn expectApproxEqAbsInner(comptime T: type, expected: T, actual: T, tolerance: T) !void { |
| 230 | 239 | switch (@typeInfo(T)) { |
| 231 | 240 | .Float => if (!math.approxEqAbs(T, expected, actual, tolerance)) { |
| 232 | 241 | print("actual {}, not within absolute tolerance {} of expected {}\n", .{ actual, tolerance, expected }); |
| ... | ... | @@ -256,9 +265,13 @@ test "expectApproxEqAbs" { |
| 256 | 265 | /// to show exactly how they are not equal, then returns a test failure error. |
| 257 | 266 | /// See `math.approxEqRel` for more information on the tolerance parameter. |
| 258 | 267 | /// The types must be floating-point. |
| 259 | | pub fn expectApproxEqRel(expected: anytype, actual: @TypeOf(expected), tolerance: @TypeOf(expected)) !void { |
| 260 | | const T = @TypeOf(expected); |
| 268 | /// `actual` and `expected` are coerced to a common type using peer type resolution. |
| 269 | pub inline fn expectApproxEqRel(expected: anytype, actual: anytype, tolerance: anytype) !void { |
| 270 | const T = @TypeOf(expected, actual, tolerance); |
| 271 | return expectApproxEqRelInner(T, expected, actual, tolerance); |
| 272 | } |
| 261 | 273 | |
| 274 | fn expectApproxEqRelInner(comptime T: type, expected: T, actual: T, tolerance: T) !void { |
| 262 | 275 | switch (@typeInfo(T)) { |
| 263 | 276 | .Float => if (!math.approxEqRel(T, expected, actual, tolerance)) { |
| 264 | 277 | print("actual {}, not within relative tolerance {} of expected {}\n", .{ actual, tolerance, expected }); |
| ... | ... | @@ -653,17 +666,22 @@ pub fn expectStringEndsWith(actual: []const u8, expected_ends_with: []const u8) |
| 653 | 666 | /// This function is intended to be used only in tests. When the two values are not |
| 654 | 667 | /// deeply equal, prints diagnostics to stderr to show exactly how they are not equal, |
| 655 | 668 | /// then returns a test failure error. |
| 656 | | /// `actual` is casted to the type of `expected`. |
| 669 | /// `actual` and `expected` are coerced to a common type using peer type resolution. |
| 657 | 670 | /// |
| 658 | 671 | /// Deeply equal is defined as follows: |
| 659 | | /// Primitive types are deeply equal if they are equal using `==` operator. |
| 672 | /// Primitive types are deeply equal if they are equal using `==` operator. |
| 660 | 673 | /// Struct values are deeply equal if their corresponding fields are deeply equal. |
| 661 | 674 | /// Container types(like Array/Slice/Vector) deeply equal when their corresponding elements are deeply equal. |
| 662 | 675 | /// Pointer values are deeply equal if values they point to are deeply equal. |
| 663 | 676 | /// |
| 664 | 677 | /// Note: Self-referential structs are supported (e.g. things like std.SinglyLinkedList) |
| 665 | 678 | /// but may cause infinite recursion or stack overflow when a container has a pointer to itself. |
| 666 | | pub fn expectEqualDeep(expected: anytype, actual: @TypeOf(expected)) error{TestExpectedEqual}!void { |
| 679 | pub inline fn expectEqualDeep(expected: anytype, actual: anytype) error{TestExpectedEqual}!void { |
| 680 | const T = @TypeOf(expected, actual); |
| 681 | return expectEqualDeepInner(T, expected, actual); |
| 682 | } |
| 683 | |
| 684 | fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpectedEqual}!void { |
| 667 | 685 | switch (@typeInfo(@TypeOf(actual))) { |
| 668 | 686 | .NoReturn, |
| 669 | 687 | .Opaque, |