Skip to content

Commit

Permalink
test: Add test covg for obj wrap (#1269)
Browse files Browse the repository at this point in the history
* test: Add test covg for ObjectWrap<T>
  • Loading branch information
JckXia committed Feb 17, 2023
1 parent c51d6b3 commit 61b8e28
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
26 changes: 23 additions & 3 deletions test/objectwrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ void StaticSetter(const Napi::CallbackInfo& /*info*/,
testStaticContextRef.Value().Set("value", value);
}

void StaticMethodVoidCb(const Napi::CallbackInfo& info) {
StaticSetter(info, info[0].As<Napi::Number>());
}

Napi::Value TestStaticMethod(const Napi::CallbackInfo& info) {
std::string str = MaybeUnwrap(info[0].ToString());
return Napi::String::New(info.Env(), str + " static");
Expand Down Expand Up @@ -53,6 +57,15 @@ class Test : public Napi::ObjectWrap<Test> {
return static_cast<Test*>(info.Data())->Getter(info);
}

static Napi::Value CanUnWrap(const Napi::CallbackInfo& info) {
Napi::Object wrappedObject = info[0].As<Napi::Object>();
std::string expectedString = info[1].As<Napi::String>();
Test* nativeObject = Test::Unwrap(wrappedObject);
std::string strVal = MaybeUnwrap(nativeObject->Getter(info).ToString());

return Napi::Boolean::New(info.Env(), strVal == expectedString);
}

void Setter(const Napi::CallbackInfo& /*info*/, const Napi::Value& value) {
value_ = MaybeUnwrap(value.ToString());
}
Expand Down Expand Up @@ -115,7 +128,8 @@ class Test : public Napi::ObjectWrap<Test> {
Napi::Symbol::New(env, "kTestStaticMethodTInternal");
Napi::Symbol kTestStaticVoidMethodTInternal =
Napi::Symbol::New(env, "kTestStaticVoidMethodTInternal");

Napi::Symbol kTestStaticVoidMethodInternal =
Napi::Symbol::New(env, "kTestStaticVoidMethodInternal");
Napi::Symbol kTestValueInternal =
Napi::Symbol::New(env, "kTestValueInternal");
Napi::Symbol kTestAccessorInternal =
Expand Down Expand Up @@ -147,6 +161,8 @@ class Test : public Napi::ObjectWrap<Test> {
kTestStaticMethodInternal),
StaticValue("kTestStaticMethodTInternal",
kTestStaticMethodTInternal),
StaticValue("kTestStaticVoidMethodInternal",
kTestStaticVoidMethodInternal),
StaticValue("kTestStaticVoidMethodTInternal",
kTestStaticVoidMethodTInternal),
StaticValue("kTestValueInternal", kTestValueInternal),
Expand Down Expand Up @@ -184,7 +200,11 @@ class Test : public Napi::ObjectWrap<Test> {
"testStaticGetSetT"),
StaticAccessor<&StaticGetter, &StaticSetter>(
kTestStaticAccessorTInternal),

StaticMethod(
"testStaticVoidMethod", &StaticMethodVoidCb, napi_default),
StaticMethod(kTestStaticVoidMethodInternal,
&StaticMethodVoidCb,
napi_default),
StaticMethod(
"testStaticMethod", &TestStaticMethod, napi_enumerable),
StaticMethod(kTestStaticMethodInternal,
Expand All @@ -195,7 +215,7 @@ class Test : public Napi::ObjectWrap<Test> {
StaticMethod<&TestStaticVoidMethodT>(
kTestStaticVoidMethodTInternal),
StaticMethod<&TestStaticMethodT>(kTestStaticMethodTInternal),

StaticMethod("canUnWrap", &CanUnWrap, napi_enumerable),
InstanceValue("testValue",
Napi::Boolean::New(env, true),
napi_enumerable),
Expand Down
16 changes: 14 additions & 2 deletions test/objectwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ async function test (binding) {
};

const testStaticMethod = (clazz) => {
clazz.testStaticVoidMethod(52);
assert.strictEqual(clazz.testStaticGetter, 52);
clazz[clazz.kTestStaticVoidMethodInternal](94);
assert.strictEqual(clazz.testStaticGetter, 94);
assert.strictEqual(clazz.testStaticMethod('method'), 'method static');
assert.strictEqual(clazz[clazz.kTestStaticMethodInternal]('method'), 'method static internal');
clazz.testStaticVoidMethodT('static method<>(const char*)');
Expand All @@ -224,7 +228,8 @@ async function test (binding) {
'testStaticValue',
'testStaticGetter',
'testStaticGetSet',
'testStaticMethod'
'testStaticMethod',
'canUnWrap'
]);

// for..in
Expand All @@ -238,7 +243,8 @@ async function test (binding) {
'testStaticValue',
'testStaticGetter',
'testStaticGetSet',
'testStaticMethod'
'testStaticMethod',
'canUnWrap'
]);
}
};
Expand All @@ -260,6 +266,11 @@ async function test (binding) {
]);
}

const testUnwrap = (obj, clazz) => {
obj.testSetter = 'unwrapTest';
assert(clazz.canUnWrap(obj, 'unwrapTest'));
};

const testObj = (obj, clazz) => {
testValue(obj, clazz);
testAccessor(obj, clazz);
Expand All @@ -268,6 +279,7 @@ async function test (binding) {
testEnumerables(obj, clazz);

testConventions(obj, clazz);
testUnwrap(obj, clazz);
};

async function testClass (clazz) {
Expand Down

0 comments on commit 61b8e28

Please sign in to comment.