Skip to content

Commit 69ba24b

Browse files
test: add tests for creating objects with properties in js-native-api
1 parent b96fca7 commit 69ba24b

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

test/js-native-api/test_object/test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const assert = require('assert');
55
// Testing api calls for objects
66
const test_object = require(`./build/${common.buildType}/test_object`);
77

8-
98
const object = {
109
hello: 'world',
1110
array: [
@@ -391,3 +390,20 @@ assert.deepStrictEqual(test_object.TestGetProperty(), {
391390
delete obj.x;
392391
}, /Cannot delete property 'x' of #<Object>/);
393392
}
393+
394+
{
395+
const objectWithProperties = test_object.TestCreateObjectWithProperties();
396+
assert.strictEqual(typeof objectWithProperties, 'object');
397+
assert.strictEqual(objectWithProperties.name, 'Foo');
398+
assert.strictEqual(objectWithProperties.age, 42);
399+
assert.strictEqual(objectWithProperties.active, true);
400+
401+
const emptyObject = test_object.TestCreateObjectWithPropertiesEmpty();
402+
assert.strictEqual(typeof emptyObject, 'object');
403+
assert.strictEqual(Object.keys(emptyObject).length, 0);
404+
405+
const objectWithCustomPrototype = test_object.TestCreateObjectWithCustomPrototype();
406+
assert.strictEqual(typeof objectWithCustomPrototype, 'object');
407+
assert.strictEqual(objectWithCustomPrototype.value, 42);
408+
assert.strictEqual(typeof objectWithCustomPrototype.test, 'function');
409+
}

test/js-native-api/test_object/test_object.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,62 @@ CheckTypeTag(napi_env env, napi_callback_info info) {
710710
return js_result;
711711
}
712712

713+
static napi_value TestCreateObjectWithProperties(napi_env env, napi_callback_info info) {
714+
napi_value names[3];
715+
napi_value values[3];
716+
napi_value result;
717+
718+
NODE_API_CALL(env, napi_create_string_utf8(env, "name", NAPI_AUTO_LENGTH, &names[0]));
719+
NODE_API_CALL(env, napi_create_string_utf8(env, "Foo", NAPI_AUTO_LENGTH, &values[0]));
720+
721+
NODE_API_CALL(env, napi_create_string_utf8(env, "age", NAPI_AUTO_LENGTH, &names[1]));
722+
NODE_API_CALL(env, napi_create_int32(env, 42, &values[1]));
723+
724+
NODE_API_CALL(env, napi_create_string_utf8(env, "active", NAPI_AUTO_LENGTH, &names[2]));
725+
NODE_API_CALL(env, napi_get_boolean(env, true, &values[2]));
726+
727+
napi_value null_prototype;
728+
NODE_API_CALL(env, napi_get_null(env, &null_prototype));
729+
NODE_API_CALL(env, napi_create_object_with_properties(
730+
env, null_prototype, names, values, 3, &result));
731+
732+
return result;
733+
}
734+
735+
static napi_value TestCreateObjectWithPropertiesEmpty(napi_env env, napi_callback_info info) {
736+
napi_value result;
737+
738+
napi_value null_prototype;
739+
NODE_API_CALL(env, napi_get_null(env, &null_prototype));
740+
NODE_API_CALL(env, napi_create_object_with_properties(
741+
env, null_prototype, NULL, NULL, 0, &result));
742+
743+
return result;
744+
}
745+
746+
static napi_value TestCreateObjectWithCustomPrototype(napi_env env, napi_callback_info info) {
747+
napi_value prototype;
748+
napi_value method_name;
749+
napi_value method_func;
750+
napi_value names[1];
751+
napi_value values[1];
752+
napi_value result;
753+
754+
NODE_API_CALL(env, napi_create_object(env, &prototype));
755+
NODE_API_CALL(env, napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &method_name));
756+
NODE_API_CALL(env, napi_create_function(env, "test", NAPI_AUTO_LENGTH,
757+
TestCreateObjectWithProperties, NULL, &method_func));
758+
NODE_API_CALL(env, napi_set_property(env, prototype, method_name, method_func));
759+
760+
NODE_API_CALL(env, napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &names[0]));
761+
NODE_API_CALL(env, napi_create_int32(env, 42, &values[0]));
762+
763+
NODE_API_CALL(env, napi_create_object_with_properties(
764+
env, prototype, names, values, 1, &result));
765+
766+
return result;
767+
}
768+
713769
EXTERN_C_START
714770
napi_value Init(napi_env env, napi_value exports) {
715771
napi_property_descriptor descriptors[] = {
@@ -743,6 +799,9 @@ napi_value Init(napi_env env, napi_value exports) {
743799
DECLARE_NODE_API_PROPERTY("TestGetProperty", TestGetProperty),
744800
DECLARE_NODE_API_PROPERTY("TestFreeze", TestFreeze),
745801
DECLARE_NODE_API_PROPERTY("TestSeal", TestSeal),
802+
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithProperties", TestCreateObjectWithProperties),
803+
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithPropertiesEmpty", TestCreateObjectWithPropertiesEmpty),
804+
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithCustomPrototype", TestCreateObjectWithCustomPrototype),
746805
};
747806

748807
init_test_null(env, exports);

0 commit comments

Comments
 (0)