-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split deprecated object tests into their own toplevel test
- Loading branch information
Gabriel Schulhof
committed
Oct 1, 2018
1 parent
b2a76ab
commit bdf90ad
Showing
6 changed files
with
127 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |