forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.js
66 lines (51 loc) · 2.54 KB
/
symbol.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
const assert = require('assert');
module.exports = require('./common').runTest(test);
function test (binding) {
const majorNodeVersion = process.versions.node.split('.')[0];
const wellKnownSymbolFunctions = ['asyncIterator', 'hasInstance', 'isConcatSpreadable', 'iterator', 'match', 'replace', 'search', 'split', 'species', 'toPrimitive', 'toStringTag', 'unscopables'];
if (majorNodeVersion >= 12) {
wellKnownSymbolFunctions.push('matchAll');
}
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);
// eslint-disable-next-line no-self-compare
assert(binding.symbol.testNullSymbolCanBeCreated() === binding.symbol.testNullSymbolCanBeCreated());
// eslint-disable-next-line no-self-compare
assert(binding.symbol.testUndefinedSymbolCanBeCreated() === binding.symbol.testUndefinedSymbolCanBeCreated());
assert(binding.symbol.testUndefinedSymbolCanBeCreated() !== binding.symbol.testNullSymbolCanBeCreated());
}