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(expect): update toBeInstanceOf #2396

Merged
merged 2 commits into from
Mar 16, 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
23 changes: 23 additions & 0 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3795,6 +3795,29 @@ bool JSC__JSValue__isConstructor(JSC__JSValue JSValue0)
return value.isConstructor();
}

bool JSC__JSValue__isInstanceOf(JSC__JSValue JSValue0, JSC__JSGlobalObject* globalObject, JSC__JSValue JSValue1)
{
VM& vm = globalObject->vm();

auto scope = DECLARE_CATCH_SCOPE(vm);

JSValue jsValue = JSValue::decode(JSValue0);
JSValue jsValue1 = JSValue::decode(JSValue1);
if (UNLIKELY(!jsValue1.isObject())) {
return false;
}
JSObject* jsConstructor = JSC::asObject(jsValue1);
if (!jsConstructor->structure()->typeInfo().implementsHasInstance())
Jarred-Sumner marked this conversation as resolved.
Show resolved Hide resolved
return false;
bool result = jsConstructor->hasInstance(globalObject, jsValue);
if (scope.exception()) {
Jarred-Sumner marked this conversation as resolved.
Show resolved Hide resolved
scope.clearException();
return false;
}

return result;
}

extern "C" JSC__JSValue JSC__JSValue__createRopeString(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* globalObject)
{
return JSValue::encode(JSC::jsString(globalObject, JSC::JSValue::decode(JSValue0).toString(globalObject), JSC::JSValue::decode(JSValue1).toString(globalObject)));
Expand Down
3 changes: 2 additions & 1 deletion src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3042,7 +3042,7 @@ pub const JSValue = enum(JSValueReprInt) {
if (!this.isCell())
return false;

return JSC.C.JSValueIsInstanceOfConstructor(global, this.asObjectRef(), constructor.asObjectRef(), null);
return cppFn("isInstanceOf", .{ this, global, constructor });
}

pub fn call(this: JSValue, globalThis: *JSGlobalObject, args: []const JSC.JSValue) JSC.JSValue {
Expand Down Expand Up @@ -4122,6 +4122,7 @@ pub const JSValue = enum(JSValueReprInt) {
"toZigException",
"toZigString",
"isConstructor",
"isInstanceOf",
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/headers-cpp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//-- AUTOGENERATED FILE -- 1678254453
//-- AUTOGENERATED FILE -- 1678855956
// clang-format off
#pragma once

Expand Down
3 changes: 2 additions & 1 deletion src/bun.js/bindings/headers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// clang-format off
//-- AUTOGENERATED FILE -- 1678254453
//-- AUTOGENERATED FILE -- 1678855956
#pragma once

#include <stddef.h>
Expand Down Expand Up @@ -325,6 +325,7 @@ CPP_DECL bool JSC__JSValue__isError(JSC__JSValue JSValue0);
CPP_DECL bool JSC__JSValue__isException(JSC__JSValue JSValue0, JSC__VM* arg1);
CPP_DECL bool JSC__JSValue__isGetterSetter(JSC__JSValue JSValue0);
CPP_DECL bool JSC__JSValue__isHeapBigInt(JSC__JSValue JSValue0);
CPP_DECL bool JSC__JSValue__isInstanceOf(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2);
CPP_DECL bool JSC__JSValue__isInt32(JSC__JSValue JSValue0);
CPP_DECL bool JSC__JSValue__isInt32AsAnyInt(JSC__JSValue JSValue0);
CPP_DECL bool JSC__JSValue__isIterable(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/headers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ pub extern fn JSC__JSValue__isError(JSValue0: JSC__JSValue) bool;
pub extern fn JSC__JSValue__isException(JSValue0: JSC__JSValue, arg1: *bindings.VM) bool;
pub extern fn JSC__JSValue__isGetterSetter(JSValue0: JSC__JSValue) bool;
pub extern fn JSC__JSValue__isHeapBigInt(JSValue0: JSC__JSValue) bool;
pub extern fn JSC__JSValue__isInstanceOf(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, JSValue2: JSC__JSValue) bool;
pub extern fn JSC__JSValue__isInt32(JSValue0: JSC__JSValue) bool;
pub extern fn JSC__JSValue__isInt32AsAnyInt(JSValue0: JSC__JSValue) bool;
pub extern fn JSC__JSValue__isIterable(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject) bool;
Expand Down
27 changes: 15 additions & 12 deletions src/bun.js/test/jest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2733,10 +2733,11 @@ pub const Expect = struct {
}

active_test_expectation_counter.actual += 1;
var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };

const expected_value = arguments[0];
if (!expected_value.jsType().isFunction()) {
var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
globalObject.throw("Expected value must be a function: {any}", .{expected_value.toFmt(globalObject, &fmt)});
if (!expected_value.isConstructor()) {
globalObject.throw("Expected value must be a function: {any}", .{expected_value.toFmt(globalObject, &formatter)});
return .zero;
}
expected_value.ensureStillAlive();
Expand All @@ -2753,28 +2754,30 @@ pub const Expect = struct {
if (pass) return thisValue;

// handle failure
var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true };
const expected_fmt = expected_value.toFmt(globalObject, &formatter);
const value_fmt = value.toFmt(globalObject, &formatter);
if (not) {
const received_line = "Received: <red>{any}<r>\n";
const fmt = comptime getSignature("toBeInstanceOf", "", true) ++ "\n\n" ++ received_line;
const expected_line = "Expected constructor: not <green>{any}<r>\n";
const received_line = "Received value: <red>{any}<r>\n";
const fmt = comptime getSignature("toBeInstanceOf", "", true) ++ "\n\n" ++ expected_line ++ received_line;
if (Output.enable_ansi_colors) {
globalObject.throw(Output.prettyFmt(fmt, true), .{value_fmt});
globalObject.throw(Output.prettyFmt(fmt, true), .{ expected_fmt, value_fmt });
return .zero;
}

globalObject.throw(Output.prettyFmt(fmt, false), .{value_fmt});
globalObject.throw(Output.prettyFmt(fmt, false), .{ expected_fmt, value_fmt });
return .zero;
}

const received_line = "Received: <red>{any}<r>\n";
const fmt = comptime getSignature("toBeInstanceOf", "", false) ++ "\n\n" ++ received_line;
const expected_line = "Expected constructor: <green>{any}<r>\n";
const received_line = "Received value: <red>{any}<r>\n";
const fmt = comptime getSignature("toBeInstanceOf", "", false) ++ "\n\n" ++ expected_line ++ received_line;
if (Output.enable_ansi_colors) {
globalObject.throw(Output.prettyFmt(fmt, true), .{value_fmt});
globalObject.throw(Output.prettyFmt(fmt, true), .{ expected_fmt, value_fmt });
return .zero;
}

globalObject.throw(Output.prettyFmt(fmt, false), .{value_fmt});
globalObject.throw(Output.prettyFmt(fmt, false), .{ expected_fmt, value_fmt });
return .zero;
}

Expand Down
21 changes: 8 additions & 13 deletions test/js/bun/test/expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ describe("expect()", () => {
const tests = [
{
label: "string",
value: "",
instanceOf: String,
value: new String(""),
instanceOf: Number,
},
{
label: "number",
value: 1,
instanceof: Number,
},
{
label: "bigint",
value: 1n,
instanceof: BigInt,
value: new Number(1),
instanceOf: Number,
},
{
label: "object",
Expand All @@ -28,22 +23,22 @@ describe("expect()", () => {
{
label: "function",
value: () => {},
instanceof: Function,
instanceOf: Function,
},
{
label: "Class",
value: new Animal(),
instanceof: Animal,
instanceOf: Animal,
},
{
label: "extends Class",
value: new Dog(),
instanceof: Dog,
instanceOf: Dog,
},
{
label: "super Class",
value: new Dog(),
instanceof: Animal,
instanceOf: Animal,
},
];
for (const { label, value, instanceOf } of tests) {
Expand Down