Permalink
Browse files

Object.fromEntries: add basic tests

  • Loading branch information...
bakkot authored and rwaldron committed Aug 11, 2018
1 parent 8ae2a51 commit 7f694d4718a713e065eef5286cedd75b0345ab5e
Showing with 553 additions and 0 deletions.
  1. +4 −0 features.txt
  2. +74 −0 test/built-ins/Object/fromEntries/evaluation-order.js
  3. +39 −0 test/built-ins/Object/fromEntries/iterator-closed-for-null-entry.js.js
  4. +39 −0 test/built-ins/Object/fromEntries/iterator-closed-for-string-entry.js.js
  5. +45 −0 test/built-ins/Object/fromEntries/iterator-closed-for-throwing-entry-accessor.js
  6. +47 −0 test/built-ins/Object/fromEntries/iterator-closed-for-throwing-entry-key-tostring.js
  7. +25 −0 test/built-ins/Object/fromEntries/iterator-not-closed-for-next-returning-non-object.js
  8. +32 −0 test/built-ins/Object/fromEntries/iterator-not-closed-for-throwing-done-accessor.js
  9. +27 −0 test/built-ins/Object/fromEntries/iterator-not-closed-for-throwing-next.js
  10. +23 −0 test/built-ins/Object/fromEntries/iterator-not-closed-for-uncallable-next.js
  11. +22 −0 test/built-ins/Object/fromEntries/key-order.js
  12. +15 −0 test/built-ins/Object/fromEntries/length.js
  13. +15 −0 test/built-ins/Object/fromEntries/name.js
  14. +12 −0 test/built-ins/Object/fromEntries/requires-argument.js
  15. +14 −0 test/built-ins/Object/fromEntries/simple-properties.js
  16. +11 −0 test/built-ins/Object/fromEntries/string-entry-object-succeeds.js
  17. +12 −0 test/built-ins/Object/fromEntries/string-entry-primitive-throws.js
  18. +12 −0 test/built-ins/Object/fromEntries/supports-symbols.js
  19. +17 −0 test/built-ins/Object/fromEntries/to-property-key.js
  20. +20 −0 test/built-ins/Object/fromEntries/uses-define-semantics.js
  21. +48 −0 test/built-ins/Object/fromEntries/uses-keys-not-iterator.js
View
@@ -7,6 +7,10 @@
#
# https://github.com/tc39/process-document
# Object.fromEntries
# https://github.com/tc39/proposal-object-from-entries
Object.fromEntries
# BigInt
# https://github.com/tc39/proposal-bigint
BigInt
@@ -0,0 +1,74 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Evaluation order is iterator.next(), get '0', get '1', toPropertyKey, repeat.
esid: sec-object.fromentries
includes: [compareArray.js]
features: [Symbol.iterator, Object.fromEntries]
---*/
var effects = [];
function makeEntry(label) {
return {
get '0'() {
effects.push('access property "0" of ' + label + ' entry');
return {
toString: function() {
effects.push('toString of ' + label + ' key');
return label + ' key';
},
};
},
get '1'() {
effects.push('access property "1" of ' + label + ' entry');
return label + ' value';
},
};
}
var iterable = {
[Symbol.iterator]: function() {
effects.push('get Symbol.iterator');
var count = 0;
return {
next: function() {
effects.push('next ' + count);
if (count === 0) {
++count;
return {
done: false,
value: makeEntry('first', 'first key', 'first value'),
};
} else if (count === 1) {
++count;
return {
done: false,
value: makeEntry('second', 'second key', 'second value'),
};
} else {
return {
done: true,
};
}
},
};
},
};
var result = Object.fromEntries(iterable);
assert.compareArray(effects, [
'get Symbol.iterator',
'next 0',
'access property "0" of first entry',
'access property "1" of first entry',
'toString of first key',
'next 1',
'access property "0" of second entry',
'access property "1" of second entry',
'toString of second key',
'next 2',
], 'Object.fromEntries evaluation order');
assert.sameValue(result['first key'], 'first value');
assert.sameValue(result['second key'], 'second value');
@@ -0,0 +1,39 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Closes iterators when they return entries which are null.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
var returned = false;
var iterable = {
[Symbol.iterator]: function() {
var advanced = false;
return {
next: function() {
if (advanced) {
throw new Test262Error('should only advance once');
}
advanced = true;
return {
done: false,
value: 'null',
};
},
return: function() {
if (returned) {
throw new Test262Error('should only return once');
}
returned = true;
},
};
},
};
assert.throws(TypeError, function() {
Object.fromEntries(iterable);
});
assert(returned, 'iterator should be closed when entry is null');
@@ -0,0 +1,39 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Closes iterators when they return entries which are strings.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
var returned = false;
var iterable = {
[Symbol.iterator]: function() {
var advanced = false;
return {
next: function() {
if (advanced) {
throw new Test262Error('should only advance once');
}
advanced = true;
return {
done: false,
value: 'ab',
};
},
return: function() {
if (returned) {
throw new Test262Error('should only return once');
}
returned = true;
},
};
},
};
assert.throws(TypeError, function() {
Object.fromEntries(iterable);
});
assert(returned, 'iterator should be closed when entry is a string');
@@ -0,0 +1,45 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Closes iterators when accessing an entry's properties throws.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
function DummyError() {}
var returned = false;
var iterable = {
[Symbol.iterator]: function() {
var advanced = false;
return {
next: function() {
if (advanced) {
throw new Test262Error('should only advance once');
}
advanced = true;
return {
done: false,
value: {
get '0'() {
throw new DummyError();
},
},
};
},
return: function() {
if (returned) {
throw new Test262Error('should only return once');
}
returned = true;
},
};
},
};
assert.throws(DummyError, function() {
Object.fromEntries(iterable);
});
assert(returned, 'iterator should be closed when entry property access throws');
@@ -0,0 +1,47 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Closes iterators when toString on a key throws.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
function DummyError() {}
var returned = false;
var iterable = {
[Symbol.iterator]: function() {
var advanced = false;
return {
next: function() {
if (advanced) {
throw new Test262Error('should only advance once');
}
advanced = true;
return {
done: false,
value: {
0: {
toString: function() {
throw new DummyError();
},
},
},
};
},
return: function() {
if (returned) {
throw new Test262Error('should only return once');
}
returned = true;
},
};
},
};
assert.throws(DummyError, function() {
Object.fromEntries(iterable);
});
assert(returned, 'iterator should be closed when key toString throws');
@@ -0,0 +1,25 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Does not close iterators with a `next` method which returns a non-object.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
var iterable = {
[Symbol.iterator]: function() {
return {
next: function() {
return null;
},
return: function() {
throw new Test262Error('should not call return');
},
};
},
};
assert.throws(TypeError, function() {
Object.fromEntries(iterable);
});
@@ -0,0 +1,32 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Does not close iterators with a `done` accessor which throws.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
function DummyError() {}
var returned = false;
var iterable = {
[Symbol.iterator]: function() {
return {
next: function() {
return {
get done() {
throw new DummyError();
},
};
},
return: function() {
throw new Test262Error('should not call return');
},
};
},
};
assert.throws(DummyError, function() {
Object.fromEntries(iterable);
});
@@ -0,0 +1,27 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Does not close iterators with a `next` method which throws.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
function DummyError() {}
var iterable = {
[Symbol.iterator]: function() {
return {
next: function() {
throw new DummyError();
},
return: function() {
throw new Test262Error('should not call return');
},
};
},
};
assert.throws(DummyError, function() {
Object.fromEntries(iterable);
});
@@ -0,0 +1,23 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Does not close iterators with an uncallable `next` property.
esid: sec-object.fromentries
features: [Symbol.iterator, Object.fromEntries]
---*/
var iterable = {
[Symbol.iterator]: function() {
return {
next: null,
return: function() {
throw new Test262Error('should not call return');
},
};
},
};
assert.throws(TypeError, function() {
Object.fromEntries(iterable);
});
@@ -0,0 +1,22 @@
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Key enumeration order of result objects matches the order of entries in the iterable.
esid: sec-object.fromentries
includes: [compareArray.js]
features: [Object.fromEntries]
---*/
var entries = [
['z', 1],
['y', 2],
['x', 3],
['y', 4],
];
var result = Object.fromEntries(entries);
assert.sameValue(result['z'], 1);
assert.sameValue(result['y'], 4);
assert.sameValue(result['x'], 3);
assert.compareArray(Object.keys(result), ['z', 'y', 'x']);
Oops, something went wrong.

0 comments on commit 7f694d4

Please sign in to comment.