aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/ZigGlobalObject.cpp
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-06-09 19:26:36 -0700
committerGravatar GitHub <noreply@github.com> 2023-06-09 19:26:36 -0700
commit76cf465cc2e87c400b6bea56cad1f17f94b91da2 (patch)
treed72518cf461407fb3c0b88d8e108057f57c8b5b6 /src/bun.js/bindings/ZigGlobalObject.cpp
parent0f018ea2159f7bad499d8a2f50837a4d888b2344 (diff)
downloadbun-76cf465cc2e87c400b6bea56cad1f17f94b91da2.tar.gz
bun-76cf465cc2e87c400b6bea56cad1f17f94b91da2.tar.zst
bun-76cf465cc2e87c400b6bea56cad1f17f94b91da2.zip
`toMatchObject` and some asymmetric matchers (#3260)
* `toMatchObject` progress * add `expect.stringContaining()` * add `expect.stringMatching()` * print asymmetric matchers * cleanup * return before printing if constructor value isn't there * move matcher logic to cpp * pretty format and tests * fix formatting for snapshots * format `stringContaining` and `stringMatching` like jest * better test * remove commented tests * remove old property matcher code * add types * make sure all props are matched in arrays * add `Bun.deepMatch`
Diffstat (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp')
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index f2d0e248e..f31a3c1cc 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -2020,6 +2020,35 @@ JSC_DEFINE_HOST_FUNCTION(functionBunDeepEquals, (JSGlobalObject * globalObject,
}
}
+JSC_DECLARE_HOST_FUNCTION(functionBunDeepMatch);
+
+JSC_DEFINE_HOST_FUNCTION(functionBunDeepMatch, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
+{
+ auto* global = reinterpret_cast<GlobalObject*>(globalObject);
+ JSC::VM& vm = global->vm();
+
+ auto scope = DECLARE_THROW_SCOPE(vm);
+
+ if (callFrame->argumentCount() < 2) {
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+ throwTypeError(globalObject, throwScope, "Expected 2 values to compare"_s);
+ return JSValue::encode(jsUndefined());
+ }
+
+ JSC::JSValue subset = callFrame->uncheckedArgument(0);
+ JSC::JSValue object = callFrame->uncheckedArgument(1);
+
+ if (!subset.isObject() || !object.isObject()) {
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+ throwTypeError(globalObject, throwScope, "Expected 2 object to match"_s);
+ return JSValue::encode(jsUndefined());
+ }
+
+ bool isEqual = Bun__deepMatch(object, subset, globalObject, &scope, false);
+ RETURN_IF_EXCEPTION(scope, {});
+ return JSValue::encode(jsBoolean(isEqual));
+}
+
JSC_DECLARE_HOST_FUNCTION(functionBunNanoseconds);
JSC_DEFINE_HOST_FUNCTION(functionBunNanoseconds, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
@@ -3668,6 +3697,12 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm
}
{
+ JSC::Identifier identifier = JSC::Identifier::fromString(vm, "deepMatch"_s);
+ object->putDirectNativeFunction(vm, this, identifier, 2, functionBunDeepMatch, ImplementationVisibility::Public, NoIntrinsic,
+ JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0);
+ }
+
+ {
JSC::Identifier identifier = JSC::Identifier::fromString(vm, "version"_s);
object->putDirect(vm, PropertyName(identifier), JSC::jsOwnedString(vm, makeString(Bun__version + 1)),