Skip to content

Commit e8935bd

Browse files
gms1Gabriel Schulhof
authored andcommitted
test: add test for own properties on ObjectWrap
PR-URL: #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>
1 parent 23ff7f0 commit e8935bd

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

test/objectwrap.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,22 @@ class Test : public Napi::ObjectWrap<Test> {
2828
if(info.Length() > 0) {
2929
finalizeCb_ = Napi::Persistent(info[0].As<Napi::Function>());
3030
}
31+
// Create an own instance property.
32+
info.This().As<Napi::Object>().DefineProperty(
33+
Napi::PropertyDescriptor::Accessor(info.Env(),
34+
info.This().As<Napi::Object>(),
35+
"ownProperty",
36+
OwnPropertyGetter,
37+
napi_enumerable, this));
38+
39+
// Create an own instance property with a templated function.
40+
info.This().As<Napi::Object>().DefineProperty(
41+
Napi::PropertyDescriptor::Accessor<OwnPropertyGetter>("ownPropertyT",
42+
napi_enumerable, this));
43+
}
3144

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

3449
void Setter(const Napi::CallbackInfo& /*info*/, const Napi::Value& value) {

test/objectwrap.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ const test = (binding) => {
7272
obj[clazz.kTestAccessorTInternal] = 'instance internal getset 4';
7373
assert.strictEqual(obj[clazz.kTestAccessorTInternal], 'instance internal getset 4');
7474
}
75+
76+
// own property
77+
{
78+
obj.testSetter = 'own property value';
79+
// Make sure the properties are enumerable.
80+
assert(Object.getOwnPropertyNames(obj).indexOf('ownProperty') >= 0);
81+
assert(Object.getOwnPropertyNames(obj).indexOf('ownPropertyT') >= 0);
82+
83+
// Make sure the properties return the right value.
84+
assert.strictEqual(obj.ownProperty, 'own property value');
85+
assert.strictEqual(obj.ownPropertyT, 'own property value');
86+
}
7587
};
7688

7789
const testMethod = (obj, clazz) => {
@@ -84,10 +96,12 @@ const test = (binding) => {
8496
};
8597

8698
const testEnumerables = (obj, clazz) => {
87-
// Object.keys: only object
88-
assert.deepEqual(Object.keys(obj), []);
99+
// Object.keys: only object without prototype
100+
assert(Object.keys(obj).length === 2);
101+
assert(Object.keys(obj).includes('ownProperty'));
102+
assert(Object.keys(obj).indexOf('ownPropertyT') >= 0);
89103

90-
// for..in: object + prototype
104+
// for..in: object and prototype
91105
{
92106
const keys = [];
93107
for (let key in obj) {

0 commit comments

Comments
 (0)