From dcb33cdfd3c0b497f66b8ab2fc66a283277d0c23 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Mon, 1 Oct 2018 14:08:58 -0400 Subject: [PATCH] split deprecated object tests into their own toplevel test --- test/binding.cc | 6 +++ test/binding.gyp | 6 ++- test/index.js | 1 + test/object/object.cc | 30 --------------- test/object/object_deprecated.cc | 66 ++++++++++++++++++++++++++++++++ test/object/object_deprecated.js | 48 +++++++++++++++++++++++ 6 files changed, 126 insertions(+), 31 deletions(-) create mode 100644 test/object/object_deprecated.cc create mode 100644 test/object/object_deprecated.js diff --git a/test/binding.cc b/test/binding.cc index e374ac124..95033646c 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -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); @@ -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)); diff --git a/test/binding.gyp b/test/binding.gyp index 06c91c306..3fc6686ce 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -36,7 +36,11 @@ ], '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'] + }] ], 'include_dirs': ["(); +} + +Value TestFunction(const CallbackInfo& info) { + return Boolean::New(info.Env(), true); +} + +void DefineProperties(const CallbackInfo& info) { + Object obj = info[0].As(); + String nameType = info[1].As(); + 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; +} diff --git a/test/object/object_deprecated.js b/test/object/object_deprecated.js new file mode 100644 index 000000000..153fb11e1 --- /dev/null +++ b/test/object/object_deprecated.js @@ -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'); +}