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

add test for own properties on ObjectWrap #645

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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,
Copy link
Contributor Author

@gms1 gms1 Jan 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth considering:

-                                           OwnPropertyGetter,
-                                           napi_enumerable, this));
+                                           std::bind(&Test::Getter, this, std::placeholders::_1),
+                                           napi_enumerable));

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