-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathglobal_object_get_property.js
56 lines (46 loc) · 1.98 KB
/
global_object_get_property.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
'use strict';
const assert = require('assert');
module.exports = require('../common').runTest(test);
function test (binding) {
const KEY_TYPE = {
C_STR: 'KEY_AS_C_STRING',
CPP_STR: 'KEY_AS_CPP_STRING',
NAPI: 'KEY_AS_NAPI_VALUES',
INT_32: 'KEY_AS_INT_32_NUM'
};
binding.globalObject.createMockTestObject();
function assertGlobalObjectPropertyIs (key, attribute, keyType) {
let napiObjectAttr;
switch (keyType) {
case KEY_TYPE.NAPI:
napiObjectAttr = binding.globalObject.getPropertyWithNapiValue(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;
case KEY_TYPE.C_STR:
napiObjectAttr = binding.globalObject.getPropertyWithCString(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;
case KEY_TYPE.CPP_STR:
napiObjectAttr = binding.globalObject.getPropertyWithCppString(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;
case KEY_TYPE.INT_32:
napiObjectAttr = binding.globalObject.getPropertyWithInt32(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;
}
}
function assertErrMessageIsThrown (propertyFetchFunction, errMsg) {
assert.throws(() => {
propertyFetchFunction(undefined);
}, errMsg);
}
assertGlobalObjectPropertyIs('2', global['2'], KEY_TYPE.NAPI);
assertGlobalObjectPropertyIs('c_str_key', global.c_str_key, KEY_TYPE.C_STR);
assertGlobalObjectPropertyIs('cpp_string_key', global.cpp_string_key, KEY_TYPE.CPP_STR);
assertGlobalObjectPropertyIs('circular', global.circular, KEY_TYPE.CPP_STR);
assertGlobalObjectPropertyIs(15, global['15'], KEY_TYPE.INT_32);
assertErrMessageIsThrown(binding.globalObject.getPropertyWithCString, 'Error: A string was expected');
assertErrMessageIsThrown(binding.globalObject.getPropertyWithCppString, 'Error: A string was expected');
assertErrMessageIsThrown(binding.globalObject.getPropertyWithInt32, 'Error: A number was expected');
}