Skip to content

Commit 71aa251

Browse files
mhdawsonMylesBorins
authored andcommittedApr 16, 2018
test: Improve N-API test coverage
- add coverage for napi_get_prototype - add coverage for napi_strict_equals Backport-PR-URL: #19447 PR-URL: #13044 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
1 parent 8d3162d commit 71aa251

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "test_general",
5+
"sources": [ "test_general.c" ]
6+
}
7+
]
8+
}

‎test/addons-napi/test_general/test.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const test_general = require(`./build/${common.buildType}/test_general`);
5+
const assert = require('assert');
6+
7+
const val1 = '1';
8+
const val2 = 1;
9+
const val3 = 1;
10+
11+
class BaseClass {
12+
}
13+
14+
class ExtendedClass extends BaseClass {
15+
}
16+
17+
const baseObject = new BaseClass();
18+
const extendedObject = new ExtendedClass();
19+
20+
// test napi_strict_equals
21+
assert.ok(test_general.testStrictEquals(val1, val1));
22+
assert.strictEqual(test_general.testStrictEquals(val1, val2), false);
23+
assert.ok(test_general.testStrictEquals(val2, val3));
24+
25+
// test napi_get_prototype
26+
assert.strictEqual(test_general.testGetPrototype(baseObject),
27+
Object.getPrototypeOf(baseObject));
28+
assert.strictEqual(test_general.testGetPrototype(extendedObject),
29+
Object.getPrototypeOf(extendedObject));
30+
assert.ok(test_general.testGetPrototype(baseObject) !==
31+
test_general.testGetPrototype(extendedObject),
32+
'Prototypes for base and extended should be different');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <node_api.h>
2+
#include "../common.h"
3+
4+
napi_value testStrictEquals(napi_env env, napi_callback_info info) {
5+
size_t argc = 2;
6+
napi_value args[2];
7+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
8+
9+
bool bool_result;
10+
napi_value result;
11+
NAPI_CALL(env, napi_strict_equals(env, args[0], args[1], &bool_result));
12+
NAPI_CALL(env, napi_get_boolean(env, bool_result, &result));
13+
14+
return result;
15+
}
16+
17+
napi_value testGetPrototype(napi_env env, napi_callback_info info) {
18+
size_t argc = 1;
19+
napi_value args[1];
20+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
21+
22+
napi_value result;
23+
NAPI_CALL(env, napi_get_prototype(env, args[0], &result));
24+
25+
return result;
26+
}
27+
28+
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
29+
napi_property_descriptor descriptors[] = {
30+
DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals),
31+
DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype),
32+
};
33+
34+
NAPI_CALL_RETURN_VOID(env, napi_define_properties(
35+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
36+
}
37+
38+
NAPI_MODULE(addon, Init)

0 commit comments

Comments
 (0)
Please sign in to comment.