| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var mockProperty = require('mock-property');
|
|---|
| 4 |
|
|---|
| 5 | module.exports = function (getDescriptors, t) {
|
|---|
| 6 | var enumDescriptor = {
|
|---|
| 7 | configurable: true,
|
|---|
| 8 | enumerable: false,
|
|---|
| 9 | value: true,
|
|---|
| 10 | writable: false
|
|---|
| 11 | };
|
|---|
| 12 | var writableDescriptor = {
|
|---|
| 13 | configurable: true,
|
|---|
| 14 | enumerable: true,
|
|---|
| 15 | value: 42,
|
|---|
| 16 | writable: true
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 | t.test('works with Object.prototype poisoned setter', { skip: !Object.defineProperty }, function (st) {
|
|---|
| 20 | var key = 'foo';
|
|---|
| 21 |
|
|---|
| 22 | var obj = {};
|
|---|
| 23 | obj[key] = 42;
|
|---|
| 24 |
|
|---|
| 25 | var expected = {};
|
|---|
| 26 | expected[key] = {
|
|---|
| 27 | configurable: true,
|
|---|
| 28 | enumerable: true,
|
|---|
| 29 | value: 42,
|
|---|
| 30 | writable: true
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | st.teardown(mockProperty(Object.prototype, key, { set: function (v) { throw new Error(v); } }));
|
|---|
| 34 |
|
|---|
| 35 | var hasOwnNamesBug = false;
|
|---|
| 36 | try {
|
|---|
| 37 | Object.getOwnPropertyNames(obj);
|
|---|
| 38 | } catch (e) {
|
|---|
| 39 | // v8 in node 0.6 - 0.12 has a bug :-(
|
|---|
| 40 | hasOwnNamesBug = true;
|
|---|
| 41 | st.comment('SKIP: this engine has a bug with Object.getOwnPropertyNames: it can not handle a throwing setter on Object.prototype.');
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if (!hasOwnNamesBug) {
|
|---|
| 45 | st.doesNotThrow(function () {
|
|---|
| 46 | var result = getDescriptors(obj);
|
|---|
| 47 | st.deepEqual(result, expected, 'got expected descriptors');
|
|---|
| 48 | });
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | delete Object.prototype[key];
|
|---|
| 52 | st.end();
|
|---|
| 53 | });
|
|---|
| 54 |
|
|---|
| 55 | t.test('gets all expected non-Symbol descriptors', function (st) {
|
|---|
| 56 | var obj = { normal: Infinity };
|
|---|
| 57 | Object.defineProperty(obj, 'enumerable', enumDescriptor);
|
|---|
| 58 | Object.defineProperty(obj, 'writable', writableDescriptor);
|
|---|
| 59 |
|
|---|
| 60 | var descriptors = getDescriptors(obj);
|
|---|
| 61 |
|
|---|
| 62 | st.deepEqual(descriptors, {
|
|---|
| 63 | enumerable: enumDescriptor,
|
|---|
| 64 | normal: {
|
|---|
| 65 | configurable: true,
|
|---|
| 66 | enumerable: true,
|
|---|
| 67 | value: Infinity,
|
|---|
| 68 | writable: true
|
|---|
| 69 | },
|
|---|
| 70 | writable: writableDescriptor
|
|---|
| 71 | });
|
|---|
| 72 | st.end();
|
|---|
| 73 | });
|
|---|
| 74 |
|
|---|
| 75 | var supportsSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
|
|---|
| 76 | t.test('gets Symbol descriptors too', { skip: !supportsSymbols }, function (st) {
|
|---|
| 77 | var symbol = Symbol('sym');
|
|---|
| 78 | var symDescriptor = {
|
|---|
| 79 | configurable: false,
|
|---|
| 80 | enumerable: true,
|
|---|
| 81 | value: [symbol],
|
|---|
| 82 | writable: true
|
|---|
| 83 | };
|
|---|
| 84 | var obj = { normal: Infinity };
|
|---|
| 85 | Object.defineProperty(obj, 'enumerable', enumDescriptor);
|
|---|
| 86 | Object.defineProperty(obj, 'writable', writableDescriptor);
|
|---|
| 87 | Object.defineProperty(obj, 'symbol', symDescriptor);
|
|---|
| 88 |
|
|---|
| 89 | var descriptors = getDescriptors(obj);
|
|---|
| 90 |
|
|---|
| 91 | st.deepEqual(descriptors, {
|
|---|
| 92 | enumerable: enumDescriptor,
|
|---|
| 93 | normal: {
|
|---|
| 94 | configurable: true,
|
|---|
| 95 | enumerable: true,
|
|---|
| 96 | value: Infinity,
|
|---|
| 97 | writable: true
|
|---|
| 98 | },
|
|---|
| 99 | symbol: symDescriptor,
|
|---|
| 100 | writable: writableDescriptor
|
|---|
| 101 | });
|
|---|
| 102 | st.end();
|
|---|
| 103 | });
|
|---|
| 104 |
|
|---|
| 105 | var supportsProxy = typeof Proxy === 'function';
|
|---|
| 106 | t.test('Proxies that return an undefined descriptor', { skip: !supportsProxy }, function (st) {
|
|---|
| 107 | var obj = { foo: true };
|
|---|
| 108 | var fooDescriptor = Object.getOwnPropertyDescriptor(obj, 'foo');
|
|---|
| 109 |
|
|---|
| 110 | var proxy = new Proxy(obj, {
|
|---|
| 111 | getOwnPropertyDescriptor: function (target, key) {
|
|---|
| 112 | return Object.getOwnPropertyDescriptor(target, key);
|
|---|
| 113 | },
|
|---|
| 114 | ownKeys: function () {
|
|---|
| 115 | return ['foo', 'bar'];
|
|---|
| 116 | }
|
|---|
| 117 | });
|
|---|
| 118 | st.deepEqual(getDescriptors(proxy), { foo: fooDescriptor }, 'object has no descriptors');
|
|---|
| 119 | st.end();
|
|---|
| 120 | });
|
|---|
| 121 | };
|
|---|