Skip to content

Commit

Permalink
test: add test for own properties on ObjectWrap
Browse files Browse the repository at this point in the history
PR-URL: nodejs#645
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
gms1 authored and Gabriel Schulhof committed Jan 10, 2020
1 parent 23ff7f0 commit e8935bd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
15 changes: 15 additions & 0 deletions test/objectwrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ class Test : public Napi::ObjectWrap<Test> {
if(info.Length() > 0) {
finalizeCb_ = Napi::Persistent(info[0].As<Napi::Function>());
}
// Create an own instance property.
info.This().As<Napi::Object>().DefineProperty(
Napi::PropertyDescriptor::Accessor(info.Env(),
info.This().As<Napi::Object>(),
"ownProperty",
OwnPropertyGetter,
napi_enumerable, this));

// Create an own instance property with a templated function.
info.This().As<Napi::Object>().DefineProperty(
Napi::PropertyDescriptor::Accessor<OwnPropertyGetter>("ownPropertyT",
napi_enumerable, this));
}

static Napi::Value OwnPropertyGetter(const Napi::CallbackInfo& info) {
return static_cast<Test*>(info.Data())->Getter(info);
}

void Setter(const Napi::CallbackInfo& /*info*/, const Napi::Value& value) {
Expand Down
20 changes: 17 additions & 3 deletions test/objectwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) {
Expand Down

0 comments on commit e8935bd

Please sign in to comment.