Skip to content

Commit bdf90ad

Browse files
author
Gabriel Schulhof
committed
split deprecated object tests into their own toplevel test
1 parent b2a76ab commit bdf90ad

File tree

6 files changed

+127
-31
lines changed

6 files changed

+127
-31
lines changed

test/binding.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Object InitHandleScope(Env env);
2323
Object InitMemoryManagement(Env env);
2424
Object InitName(Env env);
2525
Object InitObject(Env env);
26+
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
27+
Object InitObjectDeprecated(Env env);
28+
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
2629
Object InitPromise(Env env);
2730
Object InitTypedArray(Env env);
2831
Object InitObjectWrap(Env env);
@@ -52,6 +55,9 @@ Object Init(Env env, Object exports) {
5255
exports.Set("handlescope", InitHandleScope(env));
5356
exports.Set("memory_management", InitMemoryManagement(env));
5457
exports.Set("object", InitObject(env));
58+
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
59+
exports.Set("object_deprecated", InitObjectDeprecated(env));
60+
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
5561
exports.Set("promise", InitPromise(env));
5662
exports.Set("typedarray", InitTypedArray(env));
5763
exports.Set("objectwrap", InitObjectWrap(env));

test/binding.gyp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
],
3737
'conditions': [
3838
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ],
39-
['disable_deprecated=="true"', { 'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED'] }]
39+
['disable_deprecated=="true"', {
40+
'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED'],
41+
'sources!': ['object/object_deprecated.cc']
42+
}, {
43+
'sources': ['object/object_deprecated.cc']
44+
}]
4045
],
4146
'include_dirs': ["<!@(node -p \"require('../').include\")"],
4247
'dependencies': ["<!(node -p \"require('../').gyp\")"],

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ let testModules = [
2828
'object/has_own_property',
2929
'object/has_property',
3030
'object/object',
31+
'object/object_deprecated',
3132
'object/set_property',
3233
'promise',
3334
'typedarray',

test/object/object.cc

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,13 @@ void DefineProperties(const CallbackInfo& info) {
6161

6262
if (nameType.Utf8Value() == "literal") {
6363
obj.DefineProperties({
64-
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
65-
PropertyDescriptor::Accessor("readonlyAccessor", TestGetter),
66-
PropertyDescriptor::Accessor("readwriteAccessor", TestGetter, TestSetter),
67-
#else // NODE_ADDON_API_DISABLE_DEPRECATED
6864
PropertyDescriptor::Accessor(env, obj, "readonlyAccessor", TestGetter),
6965
PropertyDescriptor::Accessor(env, obj, "readwriteAccessor", TestGetter, TestSetter),
70-
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
7166
PropertyDescriptor::Value("readonlyValue", trueValue),
7267
PropertyDescriptor::Value("readwriteValue", trueValue, napi_writable),
7368
PropertyDescriptor::Value("enumerableValue", trueValue, napi_enumerable),
7469
PropertyDescriptor::Value("configurableValue", trueValue, napi_configurable),
75-
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
76-
PropertyDescriptor::Function("function", TestFunction),
77-
#else // NODE_ADDON_API_DISABLE_DEPRECATED
7870
PropertyDescriptor::Function(env, obj, "function", TestFunction),
79-
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
8071
});
8172
} else if (nameType.Utf8Value() == "string") {
8273
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
@@ -92,36 +83,20 @@ void DefineProperties(const CallbackInfo& info) {
9283
std::string str7("function");
9384

9485
obj.DefineProperties({
95-
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
96-
PropertyDescriptor::Accessor(str1, TestGetter),
97-
PropertyDescriptor::Accessor(str2, TestGetter, TestSetter),
98-
#else // NODE_ADDON_API_DISABLE_DEPRECATED
9986
PropertyDescriptor::Accessor(env, obj, str1, TestGetter),
10087
PropertyDescriptor::Accessor(env, obj, str2, TestGetter, TestSetter),
101-
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
10288
PropertyDescriptor::Value(str3, trueValue),
10389
PropertyDescriptor::Value(str4, trueValue, napi_writable),
10490
PropertyDescriptor::Value(str5, trueValue, napi_enumerable),
10591
PropertyDescriptor::Value(str6, trueValue, napi_configurable),
106-
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
107-
PropertyDescriptor::Function(str7, TestFunction),
108-
#else // NODE_ADDON_API_DISABLE_DEPRECATED
10992
PropertyDescriptor::Function(env, obj, str7, TestFunction),
110-
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
11193
});
11294
} else if (nameType.Utf8Value() == "value") {
11395
obj.DefineProperties({
114-
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
115-
PropertyDescriptor::Accessor(
116-
Napi::String::New(env, "readonlyAccessor"), TestGetter),
117-
PropertyDescriptor::Accessor(
118-
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
119-
#else // NODE_ADDON_API_DISABLE_DEPRECATED
12096
PropertyDescriptor::Accessor(env, obj,
12197
Napi::String::New(env, "readonlyAccessor"), TestGetter),
12298
PropertyDescriptor::Accessor(env, obj,
12399
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
124-
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
125100
PropertyDescriptor::Value(
126101
Napi::String::New(env, "readonlyValue"), trueValue),
127102
PropertyDescriptor::Value(
@@ -130,13 +105,8 @@ void DefineProperties(const CallbackInfo& info) {
130105
Napi::String::New(env, "enumerableValue"), trueValue, napi_enumerable),
131106
PropertyDescriptor::Value(
132107
Napi::String::New(env, "configurableValue"), trueValue, napi_configurable),
133-
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
134-
PropertyDescriptor::Function(
135-
Napi::String::New(env, "function"), TestFunction),
136-
#else // NODE_ADDON_API_DISABLE_DEPRECATED
137108
PropertyDescriptor::Function(env, obj,
138109
Napi::String::New(env, "function"), TestFunction),
139-
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
140110
});
141111
}
142112
}

test/object/object_deprecated.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
static bool testValue = true;
6+
7+
namespace {
8+
9+
Value TestGetter(const CallbackInfo& info) {
10+
return Boolean::New(info.Env(), testValue);
11+
}
12+
13+
void TestSetter(const CallbackInfo& info) {
14+
testValue = info[0].As<Boolean>();
15+
}
16+
17+
Value TestFunction(const CallbackInfo& info) {
18+
return Boolean::New(info.Env(), true);
19+
}
20+
21+
void DefineProperties(const CallbackInfo& info) {
22+
Object obj = info[0].As<Object>();
23+
String nameType = info[1].As<String>();
24+
Env env = info.Env();
25+
26+
if (nameType.Utf8Value() == "literal") {
27+
obj.DefineProperties({
28+
PropertyDescriptor::Accessor("readonlyAccessor", TestGetter),
29+
PropertyDescriptor::Accessor("readwriteAccessor", TestGetter, TestSetter),
30+
PropertyDescriptor::Function("function", TestFunction),
31+
});
32+
} else if (nameType.Utf8Value() == "string") {
33+
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
34+
// object. It generates code to destruct the object as soon as the constructor call returns.
35+
// Since this isn't a common case for using std::string objects, I'm refactoring the test to
36+
// work around the issue.
37+
std::string str1("readonlyAccessor");
38+
std::string str2("readwriteAccessor");
39+
std::string str7("function");
40+
41+
obj.DefineProperties({
42+
PropertyDescriptor::Accessor(str1, TestGetter),
43+
PropertyDescriptor::Accessor(str2, TestGetter, TestSetter),
44+
PropertyDescriptor::Function(str7, TestFunction),
45+
});
46+
} else if (nameType.Utf8Value() == "value") {
47+
obj.DefineProperties({
48+
PropertyDescriptor::Accessor(
49+
Napi::String::New(env, "readonlyAccessor"), TestGetter),
50+
PropertyDescriptor::Accessor(
51+
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
52+
PropertyDescriptor::Function(
53+
Napi::String::New(env, "function"), TestFunction),
54+
});
55+
}
56+
}
57+
58+
} // end of anonymous namespace
59+
60+
Object InitObjectDeprecated(Env env) {
61+
Object exports = Object::New(env);
62+
63+
exports["defineProperties"] = Function::New(env, DefineProperties);
64+
65+
return exports;
66+
}

test/object/object_deprecated.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
const buildType = process.config.target_defaults.default_configuration;
3+
const assert = require('assert');
4+
5+
test(require(`../build/${buildType}/binding.node`));
6+
test(require(`../build/${buildType}/binding_noexcept.node`));
7+
8+
function test(binding) {
9+
if (!('object_deprecated' in binding)) {
10+
return;
11+
}
12+
function assertPropertyIs(obj, key, attribute) {
13+
const propDesc = Object.getOwnPropertyDescriptor(obj, key);
14+
assert.ok(propDesc);
15+
assert.ok(propDesc[attribute]);
16+
}
17+
18+
function assertPropertyIsNot(obj, key, attribute) {
19+
const propDesc = Object.getOwnPropertyDescriptor(obj, key);
20+
assert.ok(propDesc);
21+
assert.ok(!propDesc[attribute]);
22+
}
23+
24+
function testDefineProperties(nameType) {
25+
const obj = {};
26+
binding.object.defineProperties(obj, nameType);
27+
28+
assertPropertyIsNot(obj, 'readonlyAccessor', 'enumerable');
29+
assertPropertyIsNot(obj, 'readonlyAccessor', 'configurable');
30+
assert.strictEqual(obj.readonlyAccessor, true);
31+
32+
assertPropertyIsNot(obj, 'readwriteAccessor', 'enumerable');
33+
assertPropertyIsNot(obj, 'readwriteAccessor', 'configurable');
34+
obj.readwriteAccessor = false;
35+
assert.strictEqual(obj.readwriteAccessor, false);
36+
obj.readwriteAccessor = true;
37+
assert.strictEqual(obj.readwriteAccessor, true);
38+
39+
assertPropertyIsNot(obj, 'function', 'writable');
40+
assertPropertyIsNot(obj, 'function', 'enumerable');
41+
assertPropertyIsNot(obj, 'function', 'configurable');
42+
assert.strictEqual(obj.function(), true);
43+
}
44+
45+
testDefineProperties('literal');
46+
testDefineProperties('string');
47+
testDefineProperties('value');
48+
}

0 commit comments

Comments
 (0)