Skip to content

Commit 0ec0272

Browse files
committed
test: improve test coverage for n-api
Add basic tests for handle scopes as code coverage reports that we are not covering these with the existing tests. PR-URL: #12327 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 75c471a commit 0ec0272

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "test_handle_scope",
5+
"sources": [ "test_handle_scope.c" ]
6+
}
7+
]
8+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
5+
// testing handle scope api calls
6+
const testHandleScope =
7+
require(`./build/${common.buildType}/test_handle_scope`);
8+
9+
testHandleScope.NewScope();
10+
assert.ok(testHandleScope.NewScopeEscape() instanceof Object);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <node_api.h>
2+
#include "../common.h"
3+
#include <string.h>
4+
5+
// these tests validate the handle scope functions in the normal
6+
// flow. Forcing gc behaviour to fully validate they are doing
7+
// the right right thing would be quite hard so we keep it
8+
// simple for now.
9+
10+
napi_value NewScope(napi_env env, napi_callback_info info) {
11+
napi_handle_scope scope;
12+
napi_value output = NULL;
13+
14+
NAPI_CALL(env, napi_open_handle_scope(env, &scope));
15+
NAPI_CALL(env, napi_create_object(env, &output));
16+
NAPI_CALL(env, napi_close_handle_scope(env, scope));
17+
return NULL;
18+
}
19+
20+
napi_value NewScopeEscape(napi_env env, napi_callback_info info) {
21+
napi_escapable_handle_scope scope;
22+
napi_value output = NULL;
23+
napi_value escapee = NULL;
24+
25+
NAPI_CALL(env, napi_open_escapable_handle_scope(env, &scope));
26+
NAPI_CALL(env, napi_create_object(env, &output));
27+
NAPI_CALL(env, napi_escape_handle(env, scope, output, &escapee));
28+
NAPI_CALL(env, napi_close_escapable_handle_scope(env, scope));
29+
return escapee;
30+
}
31+
32+
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
33+
napi_property_descriptor properties[] = {
34+
DECLARE_NAPI_PROPERTY("NewScope", NewScope),
35+
DECLARE_NAPI_PROPERTY("NewScopeEscape", NewScopeEscape),
36+
};
37+
38+
NAPI_CALL_RETURN_VOID(env, napi_define_properties(
39+
env, exports, sizeof(properties) / sizeof(*properties), properties));
40+
}
41+
42+
NAPI_MODULE(addon, Init)

0 commit comments

Comments
 (0)