-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(test): implement toEqualIgnoringWhitespace
#6293
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2649,6 +2649,57 @@ 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; | ||
|
||
const not = this.flags.not; | ||
var pass = value.isString() and expected.isString(); | ||
|
||
if (pass) { | ||
var expectedStr = expected.toString(globalThis).toSlice(globalThis, default_allocator).slice(); | ||
var valueStr = value.toString(globalThis).toSlice(globalThis, default_allocator).slice(); | ||
|
||
// Remove all whitespace from both strings | ||
expectedStr = strings.removeWhitespace(default_allocator, expectedStr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you change this to compare the original strings, skipping all whitespace characters in the comparison? This way we don't need to allocate more memory |
||
valueStr = strings.removeWhitespace(default_allocator, valueStr); | ||
|
||
// Compare the strings | ||
pass = strings.eql(expectedStr, valueStr); | ||
} | ||
|
||
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); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does
expect({}).not.toEqualIgnoringWhitespace({})
pass in jest extended? We might need to throw an error for unexpected arg types