-
Notifications
You must be signed in to change notification settings - Fork 469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added test coverage for Symbol class #972
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -998,6 +998,27 @@ inline Symbol Symbol::WellKnown(napi_env env, const std::string& name) { | |||||
return Napi::Env(env).Global().Get("Symbol").As<Object>().Get(name).As<Symbol>(); | ||||||
} | ||||||
|
||||||
inline Symbol Symbol::For(napi_env env, const std::string& description) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
... and a similar change for all APIs below. |
||||||
napi_value descriptionValue = String::New(env, description); | ||||||
return Symbol::For(env, descriptionValue); | ||||||
} | ||||||
|
||||||
inline Symbol Symbol::For(napi_env env, const char* description) { | ||||||
napi_value descriptionValue = String::New(env, description); | ||||||
return Symbol::For(env, descriptionValue); | ||||||
} | ||||||
|
||||||
inline Symbol Symbol::For(napi_env env, String description) { | ||||||
return Symbol::For(env, static_cast<napi_value>(description)); | ||||||
} | ||||||
|
||||||
inline Symbol Symbol::For(napi_env env, napi_value description) { | ||||||
Object symbObject = Napi::Env(env).Global().Get("Symbol").As<Object>(); | ||||||
auto forSymb = | ||||||
symbObject.Get("for").As<Function>().Call(symbObject, {description}); | ||||||
return forSymb.As<Symbol>(); | ||||||
} | ||||||
|
||||||
inline Symbol::Symbol() : Name() { | ||||||
} | ||||||
|
||||||
|
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,77 @@ | ||
#include <napi.h> | ||
using namespace Napi; | ||
|
||
Symbol CreateNewSymbolWithNoArgs(const Napi::CallbackInfo&) { | ||
return Napi::Symbol(); | ||
} | ||
|
||
Symbol CreateNewSymbolWithCppStrDesc(const Napi::CallbackInfo& info) { | ||
String cppStrKey = info[0].As<String>(); | ||
return Napi::Symbol::New(info.Env(), cppStrKey.Utf8Value()); | ||
} | ||
|
||
Symbol CreateNewSymbolWithCStrDesc(const Napi::CallbackInfo& info) { | ||
String cStrKey = info[0].As<String>(); | ||
return Napi::Symbol::New(info.Env(), cStrKey.Utf8Value().c_str()); | ||
} | ||
|
||
Symbol CreateNewSymbolWithNapiString(const Napi::CallbackInfo& info) { | ||
String strKey = info[0].As<String>(); | ||
return Napi::Symbol::New(info.Env(), strKey); | ||
} | ||
|
||
Symbol GetWellknownSymbol(const Napi::CallbackInfo& info) { | ||
String registrySymbol = info[0].As<String>(); | ||
return Napi::Symbol::WellKnown(info.Env(), | ||
registrySymbol.Utf8Value().c_str()); | ||
} | ||
|
||
Symbol FetchSymbolFromGlobalRegistry(const Napi::CallbackInfo& info) { | ||
String registrySymbol = info[0].As<String>(); | ||
return Napi::Symbol::For(info.Env(), registrySymbol); | ||
} | ||
|
||
Symbol FetchSymbolFromGlobalRegistryWithCppKey(const Napi::CallbackInfo& info) { | ||
String cppStringKey = info[0].As<String>(); | ||
return Napi::Symbol::For(info.Env(), cppStringKey.Utf8Value()); | ||
} | ||
|
||
Symbol FetchSymbolFromGlobalRegistryWithCKey(const Napi::CallbackInfo& info) { | ||
String cppStringKey = info[0].As<String>(); | ||
return Napi::Symbol::For(info.Env(), cppStringKey.Utf8Value().c_str()); | ||
} | ||
|
||
Symbol TestUndefinedSymbolsCanBeCreated(const Napi::CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
return Napi::Symbol::For(env, env.Undefined()); | ||
} | ||
|
||
Symbol TestNullSymbolsCanBeCreated(const Napi::CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
return Napi::Symbol::For(env, env.Null()); | ||
} | ||
|
||
Object InitSymbol(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["createNewSymbolWithNoArgs"] = | ||
Function::New(env, CreateNewSymbolWithNoArgs); | ||
exports["createNewSymbolWithCppStr"] = | ||
Function::New(env, CreateNewSymbolWithCppStrDesc); | ||
exports["createNewSymbolWithCStr"] = | ||
Function::New(env, CreateNewSymbolWithCStrDesc); | ||
exports["createNewSymbolWithNapi"] = | ||
Function::New(env, CreateNewSymbolWithNapiString); | ||
exports["getWellKnownSymbol"] = Function::New(env, GetWellknownSymbol); | ||
exports["getSymbolFromGlobalRegistry"] = | ||
Function::New(env, FetchSymbolFromGlobalRegistry); | ||
exports["getSymbolFromGlobalRegistryWithCKey"] = | ||
Function::New(env, FetchSymbolFromGlobalRegistryWithCKey); | ||
exports["getSymbolFromGlobalRegistryWithCppKey"] = | ||
Function::New(env, FetchSymbolFromGlobalRegistryWithCppKey); | ||
exports["testUndefinedSymbolCanBeCreated"] = | ||
Function::New(env, TestUndefinedSymbolsCanBeCreated); | ||
exports["testNullSymbolCanBeCreated"] = | ||
Function::New(env, TestNullSymbolsCanBeCreated); | ||
return exports; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PLease add one empty new line. |
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,70 @@ | ||
'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`)); | ||
|
||
|
||
async function test(binding) | ||
{ | ||
|
||
const wellKnownSymbolFunctions = ['asyncIterator','hasInstance','isConcatSpreadable', 'iterator','match','matchAll','replace','search','split','species','toPrimitive','toStringTag','unscopables']; | ||
|
||
function assertCanCreateSymbol(symbol) | ||
{ | ||
assert(binding.symbol.createNewSymbolWithCppStr(symbol) !== null); | ||
assert(binding.symbol.createNewSymbolWithCStr(symbol) !== null); | ||
assert(binding.symbol.createNewSymbolWithNapi(symbol) !== null); | ||
} | ||
|
||
function assertSymbolAreUnique(symbol) | ||
{ | ||
const symbolOne = binding.symbol.createNewSymbolWithCppStr(symbol); | ||
const symbolTwo = binding.symbol.createNewSymbolWithCppStr(symbol); | ||
|
||
assert(symbolOne !== symbolTwo); | ||
} | ||
|
||
function assertSymbolIsWellknown(symbol) | ||
{ | ||
const symbOne = binding.symbol.getWellKnownSymbol(symbol); | ||
const symbTwo = binding.symbol.getWellKnownSymbol(symbol); | ||
assert(symbOne && symbTwo); | ||
assert(symbOne === symbTwo); | ||
} | ||
|
||
function assertSymbolIsNotWellknown(symbol) | ||
{ | ||
const symbolTest = binding.symbol.getWellKnownSymbol(symbol); | ||
assert(symbolTest === undefined); | ||
} | ||
|
||
function assertCanCreateOrFetchGlobalSymbols(symbol, fetchFunction) | ||
{ | ||
const symbOne = fetchFunction(symbol); | ||
const symbTwo = fetchFunction(symbol); | ||
assert(symbOne && symbTwo); | ||
assert(symbOne === symbTwo); | ||
} | ||
|
||
assertCanCreateSymbol("testing"); | ||
assertSymbolAreUnique("symbol"); | ||
assertSymbolIsNotWellknown("testing"); | ||
|
||
for(const wellknownProperty of wellKnownSymbolFunctions) | ||
{ | ||
assertSymbolIsWellknown(wellknownProperty); | ||
} | ||
|
||
assertCanCreateOrFetchGlobalSymbols("data", binding.symbol.getSymbolFromGlobalRegistry); | ||
assertCanCreateOrFetchGlobalSymbols("CppKey", binding.symbol.getSymbolFromGlobalRegistryWithCppKey); | ||
assertCanCreateOrFetchGlobalSymbols("CKey", binding.symbol.getSymbolFromGlobalRegistryWithCKey); | ||
|
||
assert(binding.symbol.createNewSymbolWithNoArgs() === undefined); | ||
|
||
assert(binding.symbol.testNullSymbolCanBeCreated() === binding.symbol.testNullSymbolCanBeCreated()); | ||
assert(binding.symbol.testUndefinedSymbolCanBeCreated() === binding.symbol.testUndefinedSymbolCanBeCreated()); | ||
assert(binding.symbol.testUndefinedSymbolCanBeCreated() !== binding.symbol.testNullSymbolCanBeCreated()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the documentation for all the overloads of
Napi::Symbol Napi::Symbol::WellKnown()
methos.