Skip to content

Commit ac696b0

Browse files
committed
[Refactor] CopyDataProperties tests are the same in ES2020 as in ES2018
1 parent 57c4043 commit ac696b0

File tree

4 files changed

+13
-114
lines changed

4 files changed

+13
-114
lines changed

2018/CopyDataProperties.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
var $TypeError = require('es-errors/type');
44
var isObject = require('es-object-atoms/isObject');
5-
65
var callBound = require('call-bound');
7-
var forEach = require('../helpers/forEach');
86
var OwnPropertyKeys = require('own-keys');
97

8+
var every = require('../helpers/every');
9+
var forEach = require('../helpers/forEach');
10+
1011
var $isEnumerable = callBound('Object.prototype.propertyIsEnumerable');
1112

1213
var CreateDataProperty = require('./CreateDataProperty');
@@ -25,14 +26,9 @@ module.exports = function CopyDataProperties(target, source, excludedItems) {
2526
throw new $TypeError('Assertion failed: "target" must be an Object');
2627
}
2728

28-
if (!IsArray(excludedItems)) {
29+
if (!IsArray(excludedItems) || !every(excludedItems, isPropertyKey)) {
2930
throw new $TypeError('Assertion failed: "excludedItems" must be a List of Property Keys');
3031
}
31-
for (var i = 0; i < excludedItems.length; i += 1) {
32-
if (!isPropertyKey(excludedItems[i])) {
33-
throw new $TypeError('Assertion failed: "excludedItems" must be a List of Property Keys');
34-
}
35-
}
3632

3733
if (typeof source === 'undefined' || source === null) {
3834
return target;
@@ -51,7 +47,7 @@ module.exports = function CopyDataProperties(target, source, excludedItems) {
5147
});
5248

5349
var enumerable = $isEnumerable(fromObj, nextKey) || (
54-
// this is to handle string keys being non-enumerable in older engines
50+
// this is to handle string keys being non-enumerable in older engines
5551
typeof source === 'string'
5652
&& nextKey >= 0
5753
&& IsInteger(ToNumber(nextKey))

2019/CopyDataProperties.js

+5-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2020/CopyDataProperties.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ var $TypeError = require('es-errors/type');
44
var isObject = require('es-object-atoms/isObject');
55
var callBound = require('call-bound');
66
var OwnPropertyKeys = require('own-keys');
7-
var forEach = require('../helpers/forEach');
7+
88
var every = require('../helpers/every');
9+
var forEach = require('../helpers/forEach');
910

1011
var $isEnumerable = callBound('Object.prototype.propertyIsEnumerable');
1112

@@ -46,7 +47,7 @@ module.exports = function CopyDataProperties(target, source, excludedItems) {
4647
});
4748

4849
var enumerable = $isEnumerable(from, nextKey) || (
49-
// this is to handle string keys being non-enumerable in older engines
50+
// this is to handle string keys being non-enumerable in older engines
5051
typeof source === 'string'
5152
&& nextKey >= 0
5253
&& IsInteger(ToNumber(nextKey))

test/tests.js

-94
Original file line numberDiff line numberDiff line change
@@ -8970,100 +8970,6 @@ var es2020 = function ES2020(ES, ops, expectedMissing, skips) {
89708970
t.end();
89718971
});
89728972

8973-
test('CopyDataProperties', function (t) {
8974-
t.test('first argument: target', function (st) {
8975-
forEach(v.primitives, function (primitive) {
8976-
st['throws'](
8977-
function () { ES.CopyDataProperties(primitive, {}, []); },
8978-
TypeError,
8979-
debug(primitive) + ' is not an Object'
8980-
);
8981-
});
8982-
st.end();
8983-
});
8984-
8985-
t.test('second argument: source', function (st) {
8986-
var frozenTarget = Object.freeze ? Object.freeze({}) : {};
8987-
forEach(v.nullPrimitives, function (nullish) {
8988-
st.equal(
8989-
ES.CopyDataProperties(frozenTarget, nullish, []),
8990-
frozenTarget,
8991-
debug(nullish) + ' "source" yields identical, unmodified target'
8992-
);
8993-
});
8994-
8995-
forEach(v.nonNullPrimitives, function (objectCoercible) {
8996-
var target = {};
8997-
var result = ES.CopyDataProperties(target, objectCoercible, []);
8998-
st.equal(result, target, 'result === target');
8999-
st.deepEqual(keys(result), keys(Object(objectCoercible)), 'target ends up with keys of ' + debug(objectCoercible));
9000-
});
9001-
9002-
st.test('enumerable accessor property', { skip: !$defineProperty }, function (s2t) {
9003-
var target = {};
9004-
var source = {};
9005-
defineProperty(source, 'a', {
9006-
enumerable: true,
9007-
get: function () { return 42; }
9008-
});
9009-
var result = ES.CopyDataProperties(target, source, []);
9010-
s2t.equal(result, target, 'result === target');
9011-
s2t.deepEqual(result, { a: 42 }, 'target ends up with enumerable accessor of source');
9012-
s2t.end();
9013-
});
9014-
9015-
st.end();
9016-
});
9017-
9018-
t.test('third argument: excludedItems', function (st) {
9019-
forEach(v.objects.concat(v.primitives), function (nonArray) {
9020-
st['throws'](
9021-
function () { ES.CopyDataProperties({}, {}, nonArray); },
9022-
TypeError,
9023-
debug(nonArray) + ' is not an Array'
9024-
);
9025-
});
9026-
9027-
forEach(v.nonPropertyKeys, function (nonPropertyKey) {
9028-
st['throws'](
9029-
function () { ES.CopyDataProperties({}, {}, [nonPropertyKey]); },
9030-
TypeError,
9031-
debug(nonPropertyKey) + ' is not a Property Key'
9032-
);
9033-
});
9034-
9035-
var result = ES.CopyDataProperties({}, { a: 1, b: 2, c: 3 }, ['b']);
9036-
st.deepEqual(keys(result).sort(), ['a', 'c'].sort(), 'excluded string keys are excluded');
9037-
9038-
st.test('excluding symbols', { skip: !v.hasSymbols }, function (s2t) {
9039-
var source = {};
9040-
forEach(v.symbols, function (symbol) {
9041-
source[symbol] = true;
9042-
});
9043-
9044-
var includedSymbols = v.symbols.slice(1);
9045-
var excludedSymbols = v.symbols.slice(0, 1);
9046-
var target = ES.CopyDataProperties({}, source, excludedSymbols);
9047-
9048-
forEach(includedSymbols, function (symbol) {
9049-
s2t.equal(hasOwn(target, symbol), true, debug(symbol) + ' is included');
9050-
});
9051-
9052-
forEach(excludedSymbols, function (symbol) {
9053-
s2t.equal(hasOwn(target, symbol), false, debug(symbol) + ' is excluded');
9054-
});
9055-
9056-
s2t.end();
9057-
});
9058-
9059-
st.end();
9060-
});
9061-
9062-
// TODO: CopyDataProperties throws when copying fails
9063-
9064-
t.end();
9065-
});
9066-
90678973
test('CreateListFromArrayLike', { skip: !hasBigInts }, function (t) {
90688974
t.deepEqual(
90698975
ES.CreateListFromArrayLike({ length: 2, 0: BigInt(1), 1: 'b', 2: 'c' }),

0 commit comments

Comments
 (0)