Skip to content

test: Add test covg for obj wrap #1269

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

Merged
merged 6 commits into from
Feb 17, 2023
Merged
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
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