Skip to content
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

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/bun-types/bun-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,16 @@ declare module "bun:test" {
* @param end the end number (exclusive)
*/
toBeWithin(start: number, end: number): void;
/**
* Asserts that a value is equal to the expected string, ignoring any whitespace.
*
* @example
* expect(" foo ").toEqualIgnoringWhitespace("foo");
* expect("bar").toEqualIgnoringWhitespace(" bar ");
*
* @param expected the expected string
*/
toEqualIgnoringWhitespace(expected: string): void;
/**
* Asserts that a value is a `symbol`.
*
Expand Down
32 changes: 32 additions & 0 deletions src/bun.js/bindings/ZigGeneratedClasses.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/bun.js/bindings/generated_classes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,8 @@ pub const JSExpect = struct {
@compileLog("Expected Expect.toEndWith to be a callback but received " ++ @typeName(@TypeOf(Expect.toEndWith)));
if (@TypeOf(Expect.toEqual) != CallbackType)
@compileLog("Expected Expect.toEqual to be a callback but received " ++ @typeName(@TypeOf(Expect.toEqual)));
if (@TypeOf(Expect.toEqualIgnoringWhitespace) != CallbackType)
@compileLog("Expected Expect.toEqualIgnoringWhitespace to be a callback but received " ++ @typeName(@TypeOf(Expect.toEqualIgnoringWhitespace)));
if (@TypeOf(Expect.toHaveBeenCalled) != CallbackType)
@compileLog("Expected Expect.toHaveBeenCalled to be a callback but received " ++ @typeName(@TypeOf(Expect.toHaveBeenCalled)));
if (@TypeOf(Expect.toHaveBeenCalledTimes) != CallbackType)
Expand Down Expand Up @@ -2347,6 +2349,7 @@ pub const JSExpect = struct {
@export(Expect.toContainEqual, .{ .name = "ExpectPrototype__toContainEqual" });
@export(Expect.toEndWith, .{ .name = "ExpectPrototype__toEndWith" });
@export(Expect.toEqual, .{ .name = "ExpectPrototype__toEqual" });
@export(Expect.toEqualIgnoringWhitespace, .{ .name = "ExpectPrototype__toEqualIgnoringWhitespace" });
@export(Expect.toHaveBeenCalled, .{ .name = "ExpectPrototype__toHaveBeenCalled" });
@export(Expect.toHaveBeenCalledTimes, .{ .name = "ExpectPrototype__toHaveBeenCalledTimes" });
@export(Expect.toHaveBeenCalledWith, .{ .name = "ExpectPrototype__toHaveBeenCalledWith" });
Expand Down
51 changes: 51 additions & 0 deletions src/bun.js/test/expect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

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


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);
Copy link
Member

Choose a reason for hiding this comment

The 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);

Expand Down
4 changes: 4 additions & 0 deletions src/bun.js/test/jest.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ export default [
fn: "toBeWithin",
length: 2,
},
toEqualIgnoringWhitespace: {
fn: "toEqualIgnoringWhitespace",
length: 1,
},
toBeSymbol: {
fn: "toBeSymbol",
length: 0,
Expand Down
12 changes: 12 additions & 0 deletions src/string_immutable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4847,3 +4847,15 @@ pub fn concatIfNeeded(
}
std.debug.assert(remain.len == 0);
}

pub fn removeWhitespace(allocator: std.mem.Allocator, str: []const u8) []u8 {
var result = allocator.alloc(u8, str.len) catch unreachable;
var j: usize = 0;
for (str) |char| {
if (!std.ascii.isWhitespace(char)) {
result[j] = char;
j += 1;
}
}
return result[0..j];
}
12 changes: 12 additions & 0 deletions test/js/bun/test/expect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3080,6 +3080,18 @@ describe("expect()", () => {
expect(Infinity).not.toBeWithin(-Infinity, Infinity);
});

test("toEqualIgnoringWhitespace()", () => {
expect("hello world").toEqualIgnoringWhitespace("hello world");
expect(" hello world ").toEqualIgnoringWhitespace("hello world");
expect(" h e l l o w o r l d ").toEqualIgnoringWhitespace("hello world");
expect(" hello\nworld ").toEqualIgnoringWhitespace("hello\nworld");
expect(`h
e
l
l
o`).toEqualIgnoringWhitespace("hello");
});

test("toBeSymbol()", () => {
expect(Symbol()).toBeSymbol();
expect(Symbol("")).toBeSymbol();
Expand Down
12 changes: 11 additions & 1 deletion test/js/bun/test/jest-extended.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,17 @@ describe("jest-extended", () => {
});

// test("toIncludeMultiple()")
// test("toEqualIgnoringWhitespace()")
test("toEqualIgnoringWhitespace()", () => {
expect("hello world").toEqualIgnoringWhitespace("hello world");
expect(" hello world ").toEqualIgnoringWhitespace("hello world");
expect(" h e l l o w o r l d ").toEqualIgnoringWhitespace("hello world");
expect(" hello\nworld ").toEqualIgnoringWhitespace("hello\nworld");
expect(`h
e
l
l
o`).toEqualIgnoringWhitespace("hello");
});

// Symbol

Expand Down