| ... | ... | @@ -247,6 +247,7 @@ test "expectWithinEpsilon" { |
| 247 | 247 | /// This function is intended to be used only in tests. When the two slices are not |
| 248 | 248 | /// equal, prints diagnostics to stderr to show exactly how they are not equal, |
| 249 | 249 | /// then aborts. |
| 250 | /// If your inputs are UTF-8 encoded strings, consider calling `expectEqualStrings` instead. |
| 250 | 251 | pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) void { |
| 251 | 252 | // TODO better printing of the difference |
| 252 | 253 | // If the arrays are small enough we could print the whole thing |
| ... | ... | @@ -368,6 +369,26 @@ pub fn expectEqualStrings(expected: []const u8, actual: []const u8) void { |
| 368 | 369 | } |
| 369 | 370 | } |
| 370 | 371 | |
| 372 | pub fn expectStringEndsWith(actual: []const u8, expected_ends_with: []const u8) void { |
| 373 | if (std.mem.endsWith(u8, actual, expected_ends_with)) |
| 374 | return; |
| 375 | |
| 376 | const shortened_actual = if (actual.len >= expected_ends_with.len) |
| 377 | actual[0..expected_ends_with.len] |
| 378 | else |
| 379 | actual; |
| 380 | |
| 381 | print("\n====== expected to end with: =========\n", .{}); |
| 382 | printWithVisibleNewlines(expected_ends_with); |
| 383 | print("\n====== instead ended with: ===========\n", .{}); |
| 384 | printWithVisibleNewlines(shortened_actual); |
| 385 | print("\n========= full output: ==============\n", .{}); |
| 386 | printWithVisibleNewlines(actual); |
| 387 | print("\n======================================\n", .{}); |
| 388 | |
| 389 | @panic("test failure"); |
| 390 | } |
| 391 | |
| 371 | 392 | fn printIndicatorLine(source: []const u8, indicator_index: usize) void { |
| 372 | 393 | const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin| |
| 373 | 394 | line_begin + 1 |