Skip to content

Commit

Permalink
split deprecated object tests into their own toplevel test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Oct 1, 2018
1 parent b2a76ab commit bdf90ad
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 31 deletions.
6 changes: 6 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Object InitHandleScope(Env env);
Object InitMemoryManagement(Env env);
Object InitName(Env env);
Object InitObject(Env env);
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
Object InitObjectDeprecated(Env env);
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
Object InitPromise(Env env);
Object InitTypedArray(Env env);
Object InitObjectWrap(Env env);
Expand Down Expand Up @@ -52,6 +55,9 @@ Object Init(Env env, Object exports) {
exports.Set("handlescope", InitHandleScope(env));
exports.Set("memory_management", InitMemoryManagement(env));
exports.Set("object", InitObject(env));
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
exports.Set("object_deprecated", InitObjectDeprecated(env));
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
exports.Set("promise", InitPromise(env));
exports.Set("typedarray", InitTypedArray(env));
exports.Set("objectwrap", InitObjectWrap(env));
Expand Down
7 changes: 6 additions & 1 deletion test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
],
'conditions': [
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ],
['disable_deprecated=="true"', { 'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED'] }]
['disable_deprecated=="true"', {
'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED'],
'sources!': ['object/object_deprecated.cc']
}, {
'sources': ['object/object_deprecated.cc']
}]
],
'include_dirs': ["<!@(node -p \"require('../').include\")"],
'dependencies': ["<!(node -p \"require('../').gyp\")"],
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let testModules = [
'object/has_own_property',
'object/has_property',
'object/object',
'object/object_deprecated',
'object/set_property',
'promise',
'typedarray',
Expand Down
30 changes: 0 additions & 30 deletions test/object/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,13 @@ void DefineProperties(const CallbackInfo& info) {

if (nameType.Utf8Value() == "literal") {
obj.DefineProperties({
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor("readonlyAccessor", TestGetter),
PropertyDescriptor::Accessor("readwriteAccessor", TestGetter, TestSetter),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(env, obj, "readonlyAccessor", TestGetter),
PropertyDescriptor::Accessor(env, obj, "readwriteAccessor", TestGetter, TestSetter),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Value("readonlyValue", trueValue),
PropertyDescriptor::Value("readwriteValue", trueValue, napi_writable),
PropertyDescriptor::Value("enumerableValue", trueValue, napi_enumerable),
PropertyDescriptor::Value("configurableValue", trueValue, napi_configurable),
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function("function", TestFunction),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(env, obj, "function", TestFunction),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
});
} else if (nameType.Utf8Value() == "string") {
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
Expand All @@ -92,36 +83,20 @@ void DefineProperties(const CallbackInfo& info) {
std::string str7("function");

obj.DefineProperties({
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(str1, TestGetter),
PropertyDescriptor::Accessor(str2, TestGetter, TestSetter),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(env, obj, str1, TestGetter),
PropertyDescriptor::Accessor(env, obj, str2, TestGetter, TestSetter),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Value(str3, trueValue),
PropertyDescriptor::Value(str4, trueValue, napi_writable),
PropertyDescriptor::Value(str5, trueValue, napi_enumerable),
PropertyDescriptor::Value(str6, trueValue, napi_configurable),
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(str7, TestFunction),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(env, obj, str7, TestFunction),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
});
} else if (nameType.Utf8Value() == "value") {
obj.DefineProperties({
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(
Napi::String::New(env, "readonlyAccessor"), TestGetter),
PropertyDescriptor::Accessor(
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(env, obj,
Napi::String::New(env, "readonlyAccessor"), TestGetter),
PropertyDescriptor::Accessor(env, obj,
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Value(
Napi::String::New(env, "readonlyValue"), trueValue),
PropertyDescriptor::Value(
Expand All @@ -130,13 +105,8 @@ void DefineProperties(const CallbackInfo& info) {
Napi::String::New(env, "enumerableValue"), trueValue, napi_enumerable),
PropertyDescriptor::Value(
Napi::String::New(env, "configurableValue"), trueValue, napi_configurable),
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(
Napi::String::New(env, "function"), TestFunction),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(env, obj,
Napi::String::New(env, "function"), TestFunction),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
});
}
}
Expand Down
66 changes: 66 additions & 0 deletions test/object/object_deprecated.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "napi.h"

using namespace Napi;

static bool testValue = true;

namespace {

Value TestGetter(const CallbackInfo& info) {
return Boolean::New(info.Env(), testValue);
}

void TestSetter(const CallbackInfo& info) {
testValue = info[0].As<Boolean>();
}

Value TestFunction(const CallbackInfo& info) {
return Boolean::New(info.Env(), true);
}

void DefineProperties(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String nameType = info[1].As<String>();
Env env = info.Env();

if (nameType.Utf8Value() == "literal") {
obj.DefineProperties({
PropertyDescriptor::Accessor("readonlyAccessor", TestGetter),
PropertyDescriptor::Accessor("readwriteAccessor", TestGetter, TestSetter),
PropertyDescriptor::Function("function", TestFunction),
});
} else if (nameType.Utf8Value() == "string") {
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
// object. It generates code to destruct the object as soon as the constructor call returns.
// Since this isn't a common case for using std::string objects, I'm refactoring the test to
// work around the issue.
std::string str1("readonlyAccessor");
std::string str2("readwriteAccessor");
std::string str7("function");

obj.DefineProperties({
PropertyDescriptor::Accessor(str1, TestGetter),
PropertyDescriptor::Accessor(str2, TestGetter, TestSetter),
PropertyDescriptor::Function(str7, TestFunction),
});
} else if (nameType.Utf8Value() == "value") {
obj.DefineProperties({
PropertyDescriptor::Accessor(
Napi::String::New(env, "readonlyAccessor"), TestGetter),
PropertyDescriptor::Accessor(
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
PropertyDescriptor::Function(
Napi::String::New(env, "function"), TestFunction),
});
}
}

} // end of anonymous namespace

Object InitObjectDeprecated(Env env) {
Object exports = Object::New(env);

exports["defineProperties"] = Function::New(env, DefineProperties);

return exports;
}
48 changes: 48 additions & 0 deletions test/object/object_deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));

function test(binding) {
if (!('object_deprecated' in binding)) {
return;
}
function assertPropertyIs(obj, key, attribute) {
const propDesc = Object.getOwnPropertyDescriptor(obj, key);
assert.ok(propDesc);
assert.ok(propDesc[attribute]);
}

function assertPropertyIsNot(obj, key, attribute) {
const propDesc = Object.getOwnPropertyDescriptor(obj, key);
assert.ok(propDesc);
assert.ok(!propDesc[attribute]);
}

function testDefineProperties(nameType) {
const obj = {};
binding.object.defineProperties(obj, nameType);

assertPropertyIsNot(obj, 'readonlyAccessor', 'enumerable');
assertPropertyIsNot(obj, 'readonlyAccessor', 'configurable');
assert.strictEqual(obj.readonlyAccessor, true);

assertPropertyIsNot(obj, 'readwriteAccessor', 'enumerable');
assertPropertyIsNot(obj, 'readwriteAccessor', 'configurable');
obj.readwriteAccessor = false;
assert.strictEqual(obj.readwriteAccessor, false);
obj.readwriteAccessor = true;
assert.strictEqual(obj.readwriteAccessor, true);

assertPropertyIsNot(obj, 'function', 'writable');
assertPropertyIsNot(obj, 'function', 'enumerable');
assertPropertyIsNot(obj, 'function', 'configurable');
assert.strictEqual(obj.function(), true);
}

testDefineProperties('literal');
testDefineProperties('string');
testDefineProperties('value');
}

0 comments on commit bdf90ad

Please sign in to comment.