diff options
author | 2023-10-14 12:58:30 -0700 | |
---|---|---|
committer | 2023-10-14 12:58:30 -0700 | |
commit | f9add8b6bea4df3cdbd56a21f17e4cab1a854e4e (patch) | |
tree | 8e5306104d81c67b771181337bba02cd9ec39453 /src/bun.js/test/expect.zig | |
parent | 81a1a58d66c598ea35c42453d0ba4c6341a940fc (diff) | |
parent | 9b5e66453b0879ed77b71dcdbe50e4efa184261e (diff) | |
download | bun-f9add8b6bea4df3cdbd56a21f17e4cab1a854e4e.tar.gz bun-f9add8b6bea4df3cdbd56a21f17e4cab1a854e4e.tar.zst bun-f9add8b6bea4df3cdbd56a21f17e4cab1a854e4e.zip |
Merge branch 'main' into sdlsdl
Diffstat (limited to 'src/bun.js/test/expect.zig')
-rw-r--r-- | src/bun.js/test/expect.zig | 79 |
1 files changed, 78 insertions, 1 deletions
diff --git a/src/bun.js/test/expect.zig b/src/bun.js/test/expect.zig index 04b8670d2..6a7671f84 100644 --- a/src/bun.js/test/expect.zig +++ b/src/bun.js/test/expect.zig @@ -1518,7 +1518,7 @@ pub const Expect = struct { } const expected_diff = std.math.pow(f64, 10, -precision) / 2; - const actual_diff = std.math.fabs(received - expected); + const actual_diff = @abs(received - expected); var pass = actual_diff < expected_diff; const not = this.flags.not; @@ -2649,6 +2649,83 @@ pub const Expect = struct { return .zero; } + pub fn toEqualIgnoringWhitespace(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); + + const thisValue = callFrame.this(); + const _arguments = callFrame.arguments(1); + const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; + + if (arguments.len < 1) { + globalThis.throwInvalidArguments("toEqualIgnoringWhitespace() requires 1 argument", .{}); + return .zero; + } + + active_test_expectation_counter.actual += 1; + + const expected = arguments[0]; + const value: JSValue = this.getValue(globalThis, thisValue, "toEqualIgnoringWhitespace", "<green>expected<r>") orelse return .zero; + + if (!expected.isString()) { + globalThis.throw("toEqualIgnoringWhitespace() requires argument to be a string", .{}); + return .zero; + } + + const not = this.flags.not; + var pass = value.isString() and expected.isString(); + + if (pass) { + var valueStr = value.toString(globalThis).toSlice(globalThis, default_allocator).slice(); + var expectedStr = expected.toString(globalThis).toSlice(globalThis, default_allocator).slice(); + + var left: usize = 0; + var right: usize = 0; + + // Skip leading whitespaces + while (left < valueStr.len and std.ascii.isWhitespace(valueStr[left])) left += 1; + while (right < expectedStr.len and std.ascii.isWhitespace(expectedStr[right])) right += 1; + + while (left < valueStr.len and right < expectedStr.len) { + const left_char = valueStr[left]; + const right_char = expectedStr[right]; + + if (left_char != right_char) { + pass = false; + break; + } + + left += 1; + right += 1; + + // Skip trailing whitespaces + while (left < valueStr.len and std.ascii.isWhitespace(valueStr[left])) left += 1; + while (right < expectedStr.len and std.ascii.isWhitespace(expectedStr[right])) right += 1; + } + + if (left < valueStr.len or right < expectedStr.len) { + pass = false; + } + } + + if (not) pass = !pass; + if (pass) return thisValue; + + // handle failure + var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const expected_fmt = expected.toFmt(globalThis, &formatter); + const value_fmt = value.toFmt(globalThis, &formatter); + + if (not) { + const fmt = comptime getSignature("toEqualIgnoringWhitespace", "<green>expected<r>", true) ++ "\n\n" ++ "Expected: not <green>{any}<r>\n" ++ "Received: <red>{any}<r>\n"; + globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + return .zero; + } + + const fmt = comptime getSignature("toEqualIgnoringWhitespace", "<green>expected<r>", false) ++ "\n\n" ++ "Expected: <green>{any}<r>\n" ++ "Received: <red>{any}<r>\n"; + globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + return .zero; + } + pub fn toBeSymbol(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFrame) callconv(.C) JSValue { defer this.postMatch(globalThis); |