Skip to content

Commit 0a1fabc

Browse files
author
John French
committed
test: Add test covg for obj wrap (#1269)
* test: Add test covg for ObjectWrap<T>
1 parent 7f29f14 commit 0a1fabc

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

test/objectwrap.cc

+23-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ void StaticSetter(const Napi::CallbackInfo& /*info*/,
1212
testStaticContextRef.Value().Set("value", value);
1313
}
1414

15+
void StaticMethodVoidCb(const Napi::CallbackInfo& info) {
16+
StaticSetter(info, info[0].As<Napi::Number>());
17+
}
18+
1519
Napi::Value TestStaticMethod(const Napi::CallbackInfo& info) {
1620
std::string str = MaybeUnwrap(info[0].ToString());
1721
return Napi::String::New(info.Env(), str + " static");
@@ -53,6 +57,15 @@ class Test : public Napi::ObjectWrap<Test> {
5357
return static_cast<Test*>(info.Data())->Getter(info);
5458
}
5559

60+
static Napi::Value CanUnWrap(const Napi::CallbackInfo& info) {
61+
Napi::Object wrappedObject = info[0].As<Napi::Object>();
62+
std::string expectedString = info[1].As<Napi::String>();
63+
Test* nativeObject = Test::Unwrap(wrappedObject);
64+
std::string strVal = MaybeUnwrap(nativeObject->Getter(info).ToString());
65+
66+
return Napi::Boolean::New(info.Env(), strVal == expectedString);
67+
}
68+
5669
void Setter(const Napi::CallbackInfo& /*info*/, const Napi::Value& value) {
5770
value_ = MaybeUnwrap(value.ToString());
5871
}
@@ -115,7 +128,8 @@ class Test : public Napi::ObjectWrap<Test> {
115128
Napi::Symbol::New(env, "kTestStaticMethodTInternal");
116129
Napi::Symbol kTestStaticVoidMethodTInternal =
117130
Napi::Symbol::New(env, "kTestStaticVoidMethodTInternal");
118-
131+
Napi::Symbol kTestStaticVoidMethodInternal =
132+
Napi::Symbol::New(env, "kTestStaticVoidMethodInternal");
119133
Napi::Symbol kTestValueInternal =
120134
Napi::Symbol::New(env, "kTestValueInternal");
121135
Napi::Symbol kTestAccessorInternal =
@@ -147,6 +161,8 @@ class Test : public Napi::ObjectWrap<Test> {
147161
kTestStaticMethodInternal),
148162
StaticValue("kTestStaticMethodTInternal",
149163
kTestStaticMethodTInternal),
164+
StaticValue("kTestStaticVoidMethodInternal",
165+
kTestStaticVoidMethodInternal),
150166
StaticValue("kTestStaticVoidMethodTInternal",
151167
kTestStaticVoidMethodTInternal),
152168
StaticValue("kTestValueInternal", kTestValueInternal),
@@ -184,7 +200,11 @@ class Test : public Napi::ObjectWrap<Test> {
184200
"testStaticGetSetT"),
185201
StaticAccessor<&StaticGetter, &StaticSetter>(
186202
kTestStaticAccessorTInternal),
187-
203+
StaticMethod(
204+
"testStaticVoidMethod", &StaticMethodVoidCb, napi_default),
205+
StaticMethod(kTestStaticVoidMethodInternal,
206+
&StaticMethodVoidCb,
207+
napi_default),
188208
StaticMethod(
189209
"testStaticMethod", &TestStaticMethod, napi_enumerable),
190210
StaticMethod(kTestStaticMethodInternal,
@@ -195,7 +215,7 @@ class Test : public Napi::ObjectWrap<Test> {
195215
StaticMethod<&TestStaticVoidMethodT>(
196216
kTestStaticVoidMethodTInternal),
197217
StaticMethod<&TestStaticMethodT>(kTestStaticMethodTInternal),
198-
218+
StaticMethod("canUnWrap", &CanUnWrap, napi_enumerable),
199219
InstanceValue("testValue",
200220
Napi::Boolean::New(env, true),
201221
napi_enumerable),

test/objectwrap.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ async function test (binding) {
210210
};
211211

212212
const testStaticMethod = (clazz) => {
213+
clazz.testStaticVoidMethod(52);
214+
assert.strictEqual(clazz.testStaticGetter, 52);
215+
clazz[clazz.kTestStaticVoidMethodInternal](94);
216+
assert.strictEqual(clazz.testStaticGetter, 94);
213217
assert.strictEqual(clazz.testStaticMethod('method'), 'method static');
214218
assert.strictEqual(clazz[clazz.kTestStaticMethodInternal]('method'), 'method static internal');
215219
clazz.testStaticVoidMethodT('static method<>(const char*)');
@@ -224,7 +228,8 @@ async function test (binding) {
224228
'testStaticValue',
225229
'testStaticGetter',
226230
'testStaticGetSet',
227-
'testStaticMethod'
231+
'testStaticMethod',
232+
'canUnWrap'
228233
]);
229234

230235
// for..in
@@ -238,7 +243,8 @@ async function test (binding) {
238243
'testStaticValue',
239244
'testStaticGetter',
240245
'testStaticGetSet',
241-
'testStaticMethod'
246+
'testStaticMethod',
247+
'canUnWrap'
242248
]);
243249
}
244250
};
@@ -260,6 +266,11 @@ async function test (binding) {
260266
]);
261267
}
262268

269+
const testUnwrap = (obj, clazz) => {
270+
obj.testSetter = 'unwrapTest';
271+
assert(clazz.canUnWrap(obj, 'unwrapTest'));
272+
};
273+
263274
const testObj = (obj, clazz) => {
264275
testValue(obj, clazz);
265276
testAccessor(obj, clazz);
@@ -268,6 +279,7 @@ async function test (binding) {
268279
testEnumerables(obj, clazz);
269280

270281
testConventions(obj, clazz);
282+
testUnwrap(obj, clazz);
271283
};
272284

273285
async function testClass (clazz) {

0 commit comments

Comments
 (0)