From e8935bd8d97ba61cd4c005fab571d9fb39ab9249 Mon Sep 17 00:00:00 2001 From: Guenter Sandner Date: Mon, 6 Jan 2020 09:27:55 +0100 Subject: [PATCH] test: add test for own properties on ObjectWrap PR-URL: https://github.com/nodejs/node-addon-api/pull/645 Reviewed-By: Gabriel Schulhof Reviewed-By: Chengzhong Wu Reviewed-By: Michael Dawson --- test/objectwrap.cc | 15 +++++++++++++++ test/objectwrap.js | 20 +++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/test/objectwrap.cc b/test/objectwrap.cc index 0d61171ca..92a29ce74 100644 --- a/test/objectwrap.cc +++ b/test/objectwrap.cc @@ -28,7 +28,22 @@ class Test : public Napi::ObjectWrap { if(info.Length() > 0) { finalizeCb_ = Napi::Persistent(info[0].As()); } + // Create an own instance property. + info.This().As().DefineProperty( + Napi::PropertyDescriptor::Accessor(info.Env(), + info.This().As(), + "ownProperty", + OwnPropertyGetter, + napi_enumerable, this)); + + // Create an own instance property with a templated function. + info.This().As().DefineProperty( + Napi::PropertyDescriptor::Accessor("ownPropertyT", + napi_enumerable, this)); + } + static Napi::Value OwnPropertyGetter(const Napi::CallbackInfo& info) { + return static_cast(info.Data())->Getter(info); } void Setter(const Napi::CallbackInfo& /*info*/, const Napi::Value& value) { diff --git a/test/objectwrap.js b/test/objectwrap.js index de16a6067..e1281d3d2 100644 --- a/test/objectwrap.js +++ b/test/objectwrap.js @@ -72,6 +72,18 @@ const test = (binding) => { obj[clazz.kTestAccessorTInternal] = 'instance internal getset 4'; assert.strictEqual(obj[clazz.kTestAccessorTInternal], 'instance internal getset 4'); } + + // own property + { + obj.testSetter = 'own property value'; + // Make sure the properties are enumerable. + assert(Object.getOwnPropertyNames(obj).indexOf('ownProperty') >= 0); + assert(Object.getOwnPropertyNames(obj).indexOf('ownPropertyT') >= 0); + + // Make sure the properties return the right value. + assert.strictEqual(obj.ownProperty, 'own property value'); + assert.strictEqual(obj.ownPropertyT, 'own property value'); + } }; const testMethod = (obj, clazz) => { @@ -84,10 +96,12 @@ const test = (binding) => { }; const testEnumerables = (obj, clazz) => { - // Object.keys: only object - assert.deepEqual(Object.keys(obj), []); + // Object.keys: only object without prototype + assert(Object.keys(obj).length === 2); + assert(Object.keys(obj).includes('ownProperty')); + assert(Object.keys(obj).indexOf('ownPropertyT') >= 0); - // for..in: object + prototype + // for..in: object and prototype { const keys = []; for (let key in obj) {