tests.js
3.12 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
module.exports = function (getDescriptors, t) {
var enumDescriptor = {
configurable: true,
enumerable: false,
value: true,
writable: false
};
var writableDescriptor = {
configurable: true,
enumerable: true,
value: 42,
writable: true
};
t.test('works with Object.prototype poisoned setter', { skip: !Object.defineProperty }, function (st) {
var key = 'foo';
var obj = {};
obj[key] = 42;
var expected = {};
expected[key] = {
configurable: true,
enumerable: true,
value: 42,
writable: true
};
/* eslint-disable no-extend-native, accessor-pairs */
Object.defineProperty(Object.prototype, key, { configurable: true, set: function (v) { throw new Error(v); } });
/* eslint-enable no-extend-native, accessor-pairs */
var hasOwnNamesBug = false;
try {
Object.getOwnPropertyNames(obj);
} catch (e) {
// v8 in node 0.6 - 0.12 has a bug :-(
hasOwnNamesBug = true;
st.comment('SKIP: this engine has a bug with Object.getOwnPropertyNames: it can not handle a throwing setter on Object.prototype.');
}
if (!hasOwnNamesBug) {
st.doesNotThrow(function () {
var result = getDescriptors(obj);
st.deepEqual(result, expected, 'got expected descriptors');
});
}
delete Object.prototype[key];
st.end();
});
t.test('gets all expected non-Symbol descriptors', function (st) {
var obj = { normal: Infinity };
Object.defineProperty(obj, 'enumerable', enumDescriptor);
Object.defineProperty(obj, 'writable', writableDescriptor);
var descriptors = getDescriptors(obj);
st.deepEqual(descriptors, {
enumerable: enumDescriptor,
normal: {
configurable: true,
enumerable: true,
value: Infinity,
writable: true
},
writable: writableDescriptor
});
st.end();
});
var supportsSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
t.test('gets Symbol descriptors too', { skip: !supportsSymbols }, function (st) {
var symbol = Symbol('sym');
var symDescriptor = {
configurable: false,
enumerable: true,
value: [symbol],
writable: true
};
var obj = { normal: Infinity };
Object.defineProperty(obj, 'enumerable', enumDescriptor);
Object.defineProperty(obj, 'writable', writableDescriptor);
Object.defineProperty(obj, 'symbol', symDescriptor);
var descriptors = getDescriptors(obj);
st.deepEqual(descriptors, {
enumerable: enumDescriptor,
normal: {
configurable: true,
enumerable: true,
value: Infinity,
writable: true
},
symbol: symDescriptor,
writable: writableDescriptor
});
st.end();
});
/* global Proxy */
var supportsProxy = typeof Proxy === 'function';
t.test('Proxies that return an undefined descriptor', { skip: !supportsProxy }, function (st) {
var obj = { foo: true };
var fooDescriptor = Object.getOwnPropertyDescriptor(obj, 'foo');
var proxy = new Proxy(obj, {
getOwnPropertyDescriptor: function (target, key) {
return Object.getOwnPropertyDescriptor(target, key);
},
ownKeys: function () {
return ['foo', 'bar'];
}
});
st.deepEqual(getDescriptors(proxy), { foo: fooDescriptor }, 'object has no descriptors');
st.end();
});
};