From b517ca711973e02fa5269fc5c11489a0b60c7baa Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 22 Nov 2015 00:52:49 -0600 Subject: [PATCH] Add Object.entries tests --- .../entries/exception-during-enumeration.js | 21 +++++++++++++ .../entries/exception-not-object-coercible.js | 15 +++++++++ .../object/entries/function-length.js | 14 +++++++++ .../built-ins/object/entries/function-name.js | 18 +++++++++++ .../entries/function-property-descriptor.js | 12 +++++++ .../object/entries/getter-adding-key.js | 31 +++++++++++++++++++ .../getter-making-future-key-nonenumerable.js | 31 +++++++++++++++++++ .../entries/getter-removing-future-key.js | 29 +++++++++++++++++ .../entries/inherited-properties-omitted.js | 28 +++++++++++++++++ .../object/entries/primitive-booleans.js | 17 ++++++++++ .../object/entries/primitive-numbers.js | 14 +++++++++ .../object/entries/primitive-strings.js | 19 ++++++++++++ .../object/entries/primitive-symbols.js | 13 ++++++++ .../object/entries/symbols-omitted.js | 27 ++++++++++++++++ .../entries/tamper-with-global-object.js | 18 +++++++++++ .../object/entries/tamper-with-object-keys.js | 17 ++++++++++ 16 files changed, 324 insertions(+) create mode 100644 test/built-ins/object/entries/exception-during-enumeration.js create mode 100644 test/built-ins/object/entries/exception-not-object-coercible.js create mode 100644 test/built-ins/object/entries/function-length.js create mode 100644 test/built-ins/object/entries/function-name.js create mode 100644 test/built-ins/object/entries/function-property-descriptor.js create mode 100644 test/built-ins/object/entries/getter-adding-key.js create mode 100644 test/built-ins/object/entries/getter-making-future-key-nonenumerable.js create mode 100644 test/built-ins/object/entries/getter-removing-future-key.js create mode 100644 test/built-ins/object/entries/inherited-properties-omitted.js create mode 100644 test/built-ins/object/entries/primitive-booleans.js create mode 100644 test/built-ins/object/entries/primitive-numbers.js create mode 100644 test/built-ins/object/entries/primitive-strings.js create mode 100644 test/built-ins/object/entries/primitive-symbols.js create mode 100644 test/built-ins/object/entries/symbols-omitted.js create mode 100644 test/built-ins/object/entries/tamper-with-global-object.js create mode 100644 test/built-ins/object/entries/tamper-with-object-keys.js diff --git a/test/built-ins/object/entries/exception-during-enumeration.js b/test/built-ins/object/entries/exception-during-enumeration.js new file mode 100644 index 0000000000..69a30d4ed8 --- /dev/null +++ b/test/built-ins/object/entries/exception-during-enumeration.js @@ -0,0 +1,21 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries should terminate if getting a value throws an exception +includes: [Test262Error.js] +author: Jordan Harband +---*/ + +var trappedKey = { + get a() { + throw new Test262Error('This error should be re-thrown'); + }, + get b() { + $ERROR('Should not try to get the second element'); + } +}; + +assert.throws(Test262Error, function () { + Object.entries(trappedKey); +}); diff --git a/test/built-ins/object/entries/exception-not-object-coercible.js b/test/built-ins/object/entries/exception-not-object-coercible.js new file mode 100644 index 0000000000..56d668709d --- /dev/null +++ b/test/built-ins/object/entries/exception-not-object-coercible.js @@ -0,0 +1,15 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries should fail if given a null or undefined value +author: Jordan Harband +---*/ + +assert.throws(TypeError, function () { + Object.entries(null); +}); + +assert.throws(TypeError, function () { + Object.entries(undefined); +}); diff --git a/test/built-ins/object/entries/function-length.js b/test/built-ins/object/entries/function-length.js new file mode 100644 index 0000000000..6905e8f448 --- /dev/null +++ b/test/built-ins/object/entries/function-length.js @@ -0,0 +1,14 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries should have length 1 +author: Jordan Harband +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Object.entries.length, 1, 'Expected Object.entries.length to be 1'); + +verifyNotEnumerable(Object.entries, 'length'); +verifyNotWritable(Object.entries, 'length'); +verifyConfigurable(Object.entries, 'length'); diff --git a/test/built-ins/object/entries/function-name.js b/test/built-ins/object/entries/function-name.js new file mode 100644 index 0000000000..794462db5a --- /dev/null +++ b/test/built-ins/object/entries/function-name.js @@ -0,0 +1,18 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries should have name property with value 'entries' +author: Jordan Harband +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Object.entries.name, + 'entries', + 'Expected Object.entries.name to be "entries"' +); + +verifyNotEnumerable(Object.entries, 'name'); +verifyNotWritable(Object.entries, 'name'); +verifyConfigurable(Object.entries, 'name'); diff --git a/test/built-ins/object/entries/function-property-descriptor.js b/test/built-ins/object/entries/function-property-descriptor.js new file mode 100644 index 0000000000..d207f0cd53 --- /dev/null +++ b/test/built-ins/object/entries/function-property-descriptor.js @@ -0,0 +1,12 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries should be writable, non-enumerable, and configurable +author: Jordan Harband +includes: [propertyHelper.js] +---*/ + +verifyConfigurable(Object, 'entries'); +verifyNotEnumerable(Object, 'entries'); +verifyWritable(Object, 'entries'); diff --git a/test/built-ins/object/entries/getter-adding-key.js b/test/built-ins/object/entries/getter-adding-key.js new file mode 100644 index 0000000000..add9bc6f37 --- /dev/null +++ b/test/built-ins/object/entries/getter-adding-key.js @@ -0,0 +1,31 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries sees a new element added by a getter that is hit during iteration +author: Jordan Harband +---*/ + +var bAddsC = { + a: 'A', + get b() { + this.c = 'C'; + return 'B'; + } +}; + +var result = Object.entries(bAddsC); + +assert.sameValue(Array.isArray(result), true, 'result is an array'); +assert.sameValue(result.length, 3, 'result has 3 items'); + +assert.sameValue(Array.isArray(result[0]), true, 'first entry is an array'); +assert.sameValue(Array.isArray(result[1]), true, 'second entry is an array'); +assert.sameValue(Array.isArray(result[2]), true, 'third entry is an array'); + +assert.sameValue(result[0][0], 'a', 'first entry has key "a"'); +assert.sameValue(result[0][1], 'A', 'first entry has value "A"'); +assert.sameValue(result[1][0], 'b', 'second entry has key "b"'); +assert.sameValue(result[1][1], 'B', 'second entry has value "B"'); +assert.sameValue(result[2][0], 'c', 'third entry has key "c"'); +assert.sameValue(result[2][1], 'C', 'third entry has value "C"'); diff --git a/test/built-ins/object/entries/getter-making-future-key-nonenumerable.js b/test/built-ins/object/entries/getter-making-future-key-nonenumerable.js new file mode 100644 index 0000000000..cf8e1a061a --- /dev/null +++ b/test/built-ins/object/entries/getter-making-future-key-nonenumerable.js @@ -0,0 +1,31 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries does not see an element made non-enumerable by a getter that is hit during iteration +author: Jordan Harband +---*/ + +var bDeletesC = { + a: 'A', + get b() { + Object.defineProperty(this, 'c', { + enumerable: false + }); + return 'B'; + }, + c: 'C' +}; + +var result = Object.entries(bDeletesC); + +assert.sameValue(Array.isArray(result), true, 'result is an array'); +assert.sameValue(result.length, 2, 'result has 2 items'); + +assert.sameValue(Array.isArray(result[0]), true, 'first entry is an array'); +assert.sameValue(Array.isArray(result[1]), true, 'second entry is an array'); + +assert.sameValue(result[0][0], 'a', 'first entry has key "a"'); +assert.sameValue(result[0][1], 'A', 'first entry has value "A"'); +assert.sameValue(result[1][0], 'b', 'second entry has key "b"'); +assert.sameValue(result[1][1], 'B', 'second entry has value "B"'); diff --git a/test/built-ins/object/entries/getter-removing-future-key.js b/test/built-ins/object/entries/getter-removing-future-key.js new file mode 100644 index 0000000000..c95f7d7ec1 --- /dev/null +++ b/test/built-ins/object/entries/getter-removing-future-key.js @@ -0,0 +1,29 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries does not see an element removed by a getter that is hit during iteration +author: Jordan Harband +---*/ + +var bDeletesC = { + a: 'A', + get b() { + delete this.c; + return 'B'; + }, + c: 'C' +}; + +var result = Object.entries(bDeletesC); + +assert.sameValue(Array.isArray(result), true, 'result is an array'); +assert.sameValue(result.length, 2, 'result has 2 items'); + +assert.sameValue(Array.isArray(result[0]), true, 'first entry is an array'); +assert.sameValue(Array.isArray(result[1]), true, 'second entry is an array'); + +assert.sameValue(result[0][0], 'a', 'first entry has key "a"'); +assert.sameValue(result[0][1], 'A', 'first entry has value "A"'); +assert.sameValue(result[1][0], 'b', 'second entry has key "b"'); +assert.sameValue(result[1][1], 'B', 'second entry has value "B"'); diff --git a/test/built-ins/object/entries/inherited-properties-omitted.js b/test/built-ins/object/entries/inherited-properties-omitted.js new file mode 100644 index 0000000000..b31f221ee8 --- /dev/null +++ b/test/built-ins/object/entries/inherited-properties-omitted.js @@ -0,0 +1,28 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries does not see inherited properties. +author: Jordan Harband +---*/ + +var F = function G() {}; +F.prototype.a = {}; +F.prototype.b = {}; + +var f = new F(); +f.b = {}; // shadow the prototype +f.c = {}; // solely an own property + +var result = Object.entries(f); + +assert.sameValue(Array.isArray(result), true, 'result is an array'); +assert.sameValue(result.length, 2, 'result has 2 items'); + +assert.sameValue(Array.isArray(result[0]), true, 'first entry is an array'); +assert.sameValue(Array.isArray(result[1]), true, 'second entry is an array'); + +assert.sameValue(result[0][0], 'b', 'first entry has key "b"'); +assert.sameValue(result[0][1], f.b, 'first entry has value f.b'); +assert.sameValue(result[1][0], 'c', 'second entry has key "c"'); +assert.sameValue(result[1][1], f.c, 'second entry has value f.c'); diff --git a/test/built-ins/object/entries/primitive-booleans.js b/test/built-ins/object/entries/primitive-booleans.js new file mode 100644 index 0000000000..d56e25215b --- /dev/null +++ b/test/built-ins/object/entries/primitive-booleans.js @@ -0,0 +1,17 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries accepts boolean primitives. +author: Jordan Harband +---*/ + +var trueResult = Object.entries(true); + +assert.sameValue(Array.isArray(trueResult), true, 'trueResult is an array'); +assert.sameValue(trueResult.length, 0, 'trueResult has 0 items'); + +var falseResult = Object.entries(false); + +assert.sameValue(Array.isArray(falseResult), true, 'falseResult is an array'); +assert.sameValue(falseResult.length, 0, 'falseResult has 0 items'); diff --git a/test/built-ins/object/entries/primitive-numbers.js b/test/built-ins/object/entries/primitive-numbers.js new file mode 100644 index 0000000000..14f00a0d02 --- /dev/null +++ b/test/built-ins/object/entries/primitive-numbers.js @@ -0,0 +1,14 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries accepts number primitives. +author: Jordan Harband +---*/ + +assert.sameValue(Object.entries(0).length, 0, '0 has zero entries'); +assert.sameValue(Object.entries(-0).length, 0, '-0 has zero entries'); +assert.sameValue(Object.entries(Infinity).length, 0, 'Infinity has zero entries'); +assert.sameValue(Object.entries(-Infinity).length, 0, '-Infinity has zero entries'); +assert.sameValue(Object.entries(NaN).length, 0, 'NaN has zero entries'); +assert.sameValue(Object.entries(Math.PI).length, 0, 'Math.PI has zero entries'); diff --git a/test/built-ins/object/entries/primitive-strings.js b/test/built-ins/object/entries/primitive-strings.js new file mode 100644 index 0000000000..63fafcfa21 --- /dev/null +++ b/test/built-ins/object/entries/primitive-strings.js @@ -0,0 +1,19 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries accepts string primitives. +author: Jordan Harband +---*/ + +var result = Object.entries('abc'); + +assert.sameValue(Array.isArray(result), true, 'result is an array'); +assert.sameValue(result.length, 3, 'result has 3 items'); + +assert.sameValue(result[0][0], '0', 'first entry has key "0"'); +assert.sameValue(result[0][1], 'a', 'first entry has value "a"'); +assert.sameValue(result[1][0], '1', 'second entry has key "1"'); +assert.sameValue(result[1][1], 'b', 'second entry has value "b"'); +assert.sameValue(result[2][0], '2', 'second entry has key "2"'); +assert.sameValue(result[2][1], 'c', 'second entry has value "c"'); diff --git a/test/built-ins/object/entries/primitive-symbols.js b/test/built-ins/object/entries/primitive-symbols.js new file mode 100644 index 0000000000..cbcec1855f --- /dev/null +++ b/test/built-ins/object/entries/primitive-symbols.js @@ -0,0 +1,13 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries accepts Symbol primitives. +author: Jordan Harband +features: [Symbol] +---*/ + +var result = Object.entries(Symbol()); + +assert.sameValue(Array.isArray(result), true, 'result is an array'); +assert.sameValue(result.length, 0, 'result has 0 items'); diff --git a/test/built-ins/object/entries/symbols-omitted.js b/test/built-ins/object/entries/symbols-omitted.js new file mode 100644 index 0000000000..7024ee25ae --- /dev/null +++ b/test/built-ins/object/entries/symbols-omitted.js @@ -0,0 +1,27 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Object.entries does not include Symbol keys. +author: Jordan Harband +features: [Symbol] +---*/ + +var value = {}; +var enumSym = Symbol('enum'); +var nonEnumSym = Symbol('nonenum'); +var symValue = Symbol('value'); + +var obj = { key: symValue }; +obj[enumSym] = value; +Object.defineProperty(obj, nonEnumSym, { enumerable: false, value: value }); + +var result = Object.entries(obj); + +assert.sameValue(Array.isArray(result), true, 'result is an array'); +assert.sameValue(result.length, 1, 'result has 1 item'); + +assert.sameValue(Array.isArray(result[0]), true, 'first entry is an array'); + +assert.sameValue(result[0][0], 'key', 'first entry has key "key"'); +assert.sameValue(result[0][1], symValue, 'first entry has value `symValue`'); diff --git a/test/built-ins/object/entries/tamper-with-global-object.js b/test/built-ins/object/entries/tamper-with-global-object.js new file mode 100644 index 0000000000..3bd08d0fd6 --- /dev/null +++ b/test/built-ins/object/entries/tamper-with-global-object.js @@ -0,0 +1,18 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Object.entries should not have its behavior impacted by modifications to the global property Object +author: Jordan Harband +---*/ + +function fakeObject() { + $ERROR('The overriden version of Object was called!'); +} + +var global = Function('return this;')(); +global.Object = fakeObject; + +assert.sameValue(Object, fakeObject, 'Sanity check failed: could not modify the global Object'); +assert.sameValue(Object.entries(1).length, 0, 'Expected number primitive to have zero entries'); diff --git a/test/built-ins/object/entries/tamper-with-object-keys.js b/test/built-ins/object/entries/tamper-with-object-keys.js new file mode 100644 index 0000000000..a0552a2344 --- /dev/null +++ b/test/built-ins/object/entries/tamper-with-object-keys.js @@ -0,0 +1,17 @@ +// Copyright (C) 2015 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Object.entries should not have its behavior impacted by modifications to Object.keys +author: Jordan Harband +---*/ + +function fakeObjectKeys() { + $ERROR('The overriden version of Object.keys was called!'); +} + +Object.keys = fakeObjectKeys; + +assert.sameValue(Object.keys, fakeObjectKeys, 'Sanity check failed: could not modify the global Object.keys'); +assert.sameValue(Object.entries({ a: 1 }).length, 1, 'Expected object with 1 key to have 1 entry');