Skip to content

Commit

Permalink
test: increase n-api constructor coverage
Browse files Browse the repository at this point in the history
Add tests to validate that properties marked as static
are available through the class as opposed to instances

Backport-PR-URL: #19447
PR-URL: #13124
Reviewed-By: Jason Ginchereau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Hitesh Kanwathirtha <[email protected]>
  • Loading branch information
mhdawson authored and MylesBorins committed Apr 16, 2018
1 parent 2e29052 commit d89afe8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/addons-napi/test_constructor/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ test_object.readwriteAccessor2 = 2;
assert.strictEqual(test_object.readwriteAccessor2, 2);
assert.strictEqual(test_object.readonlyAccessor2, 2);
assert.throws(() => { test_object.readonlyAccessor2 = 3; }, TypeError);

// validate that static properties are on the class as opposed
// to the instance
assert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10);
assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
16 changes: 16 additions & 0 deletions test/addons-napi/test_constructor/test_constructor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../common.h"

static double value_ = 1;
static double static_value_ = 10;
napi_ref constructor_;

napi_value GetValue(napi_env env, napi_callback_info info) {
Expand Down Expand Up @@ -45,6 +46,19 @@ napi_value New(napi_env env, napi_callback_info info) {
return _this;
}

napi_value GetStaticValue(napi_env env, napi_callback_info info) {
size_t argc = 0;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));

NAPI_ASSERT(env, argc == 0, "Wrong number of arguments");

napi_value number;
NAPI_CALL(env, napi_create_number(env, static_value_, &number));

return number;
}


void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
napi_value number;
NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number));
Expand All @@ -58,6 +72,8 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
{ "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0},
{ "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0},
{ "readonlyAccessor2", 0, 0, GetValue, NULL, 0, napi_writable, 0},
{ "staticReadonlyAccessor1", 0, 0, GetStaticValue, NULL, 0,
napi_default | napi_static, 0},
};

napi_value cons;
Expand Down

0 comments on commit d89afe8

Please sign in to comment.