From c451a3329947ccc70eb73328f3e02608a4d2d856 Mon Sep 17 00:00:00 2001 From: Matthew Meyers Date: Mon, 7 Jul 2014 11:37:47 -0700 Subject: [PATCH] Adds tests for Array.prototype.find --- ...Array.prototype.find_callable-predicate.js | 30 +++++++++++++++++ ...ay.prototype.find_empty-array-undefined.js | 15 +++++++++ .../Array.prototype.find_length-property.js | 11 +++++++ ...Array.prototype.find_modify-after-start.js | 28 ++++++++++++++++ ....prototype.find_non-returning-predicate.js | 13 ++++++++ ...ay.prototype.find_noncallable-predicate.js | 33 +++++++++++++++++++ ...rray.prototype.find_predicate-arguments.js | 24 ++++++++++++++ .../Array.prototype.find_push-after-start.js | 14 ++++++++ ...Array.prototype.find_remove-after-start.js | 29 ++++++++++++++++ ...Array.prototype.find_return-found-value.js | 29 ++++++++++++++++ .../Array.prototype.find_skip-empty.js | 20 +++++++++++ .../Array.prototype.find_this-defined.js | 18 ++++++++++ .../Array.prototype.find_this-is-object.js | 25 ++++++++++++++ .../Array.prototype.find_this-undefined.js | 15 +++++++++ 14 files changed, 304 insertions(+) create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_callable-predicate.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_empty-array-undefined.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_length-property.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_modify-after-start.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_non-returning-predicate.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_noncallable-predicate.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_predicate-arguments.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_push-after-start.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_remove-after-start.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_return-found-value.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_skip-empty.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_this-defined.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_this-is-object.js create mode 100644 test/suite/es6/Array.prototype.find/Array.prototype.find_this-undefined.js diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_callable-predicate.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_callable-predicate.js new file mode 100644 index 0000000000..ba66238e91 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_callable-predicate.js @@ -0,0 +1,30 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Array.prototype.find shouldn't throw a TypeError if IsCallable(predicate) is true + */ + +var callableValues = [ + function () {}, + Array.prototype.forEach, + Proxy, + new Proxy(function () {}, function () {}), + x => x +]; + +function testcase() { + for (var i = 0, len = callableValues.length; i < len; i++) { + try { + [].find(callableValues[i]); + } catch (e) { + if (e instanceof TypeError) { + return false; + } + } + } + return true; +} + +runTestCase(testcase); diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_empty-array-undefined.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_empty-array-undefined.js new file mode 100644 index 0000000000..19293c9124 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_empty-array-undefined.js @@ -0,0 +1,15 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Find on empty array should return undefined + */ + +var a = [].find(function () { + return true; +}); + +if (a !== undefined) { + $ERROR('#1: a !== undefined. Actual: ' + typeof a); +} diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_length-property.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_length-property.js new file mode 100644 index 0000000000..5184bd9cf5 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_length-property.js @@ -0,0 +1,11 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description The length property of the find method is 1 + */ + +if ([].find.length !== 1) { + $ERROR('1: [].find.length !== 1. Actual: ' + [].find.length); +} diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_modify-after-start.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_modify-after-start.js new file mode 100644 index 0000000000..2fc5f4303c --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_modify-after-start.js @@ -0,0 +1,28 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Array may be mutated by calls to the predicate + */ + +[1, 2, 3].find(function (v, i, arr) { + arr[i + 1] = arr[i + 1] + 1; + switch (i) { + case 0: + if (arr[i] !== 1) { + $ERROR('#1: arr[0] !== 1. Actual: ' + arr[i]); + } + break; + case 1: + if (arr[i] !== 3) { + $ERROR('#2: arr[1] !== 3. Actual: ' + arr[i]); + } + break; + case 2: + if (arr[i] !== 4) { + $ERROR('#3: arr[1] !== 4. Actual: ' + arr[i]); + } + break; + } +}); diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_non-returning-predicate.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_non-returning-predicate.js new file mode 100644 index 0000000000..0b63d32adb --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_non-returning-predicate.js @@ -0,0 +1,13 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Find with a predicate with no return value should return undefined + */ + +var a = [1, 2, 3].find(function () {}); + +if (a !== undefined) { + $ERROR('#1: a !== undefined. Actual: ' + typeof a); +} diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_noncallable-predicate.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_noncallable-predicate.js new file mode 100644 index 0000000000..1fcda5f492 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_noncallable-predicate.js @@ -0,0 +1,33 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Array.prototype.find should throw a TypeError if IsCallable(predicate) is false + */ + +var uncallableValues = [ + undefined, + null, + true, + this, + {}, + 'string', + 0 +]; + +function testcase() { + for (var i = 0, len = uncallableValues.length; i < len; i++) { + try { + [].find(uncallableValues[i]); + return false; + } catch (e) { + if (!(e instanceof TypeError)) { + return false; + } + } + } + return true; +} + +runTestCase(testcase); diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_predicate-arguments.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_predicate-arguments.js new file mode 100644 index 0000000000..8ce1217958 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_predicate-arguments.js @@ -0,0 +1,24 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description predicate is called with three arguments: the value of the element, the index of the element, and the object being traversed. + */ + +var a = [1]; + +var b = a.find(function (v, i, arr) { + if (arguments.length !== 3) { + $ERROR('#1: arguments.length !== 3. Actual: ' + arguments.length); + } + if (v !== 1) { + $ERROR('#2: element value !== 1. Actual: ' + v); + } + if (i !== 0) { + $ERROR('#3: index !== 0. Actual: ' + i); + } + if (arr !== a) { + $ERROR('#4: object being traversed !== a'); + } +}); diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_push-after-start.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_push-after-start.js new file mode 100644 index 0000000000..0ed0138de3 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_push-after-start.js @@ -0,0 +1,14 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Elements added to array after find has been called should not be visited + */ + +[1].find(function (v, i, arr) { + arr.push('string'); + if (v === 'string') { + $ERROR('#' + i + ': \'string\' should not be visited'); + } +}); diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_remove-after-start.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_remove-after-start.js new file mode 100644 index 0000000000..169e6fdebf --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_remove-after-start.js @@ -0,0 +1,29 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Elements removed from array after find has been called should not be visited + */ + +[1, 'string', 2].find(function (v, i, arr) { + var stringIndex = arr.indexOf('string'); + if (stringIndex !== -1) delete arr[stringIndex]; + if (v === 'string') { + $ERROR('#1: \'string\' should not exist, it has been deleted'); + } + if (v === undefined) { + $ERROR('#2: deleted element should not be visited'); + } +}); + +[1, 'string', 2].find(function (v, i, arr) { + var stringIndex = arr.indexOf('string'); + if (stringIndex !== -1) arr.splice(stringIndex, 1); + if (v === 'string') { + $ERROR('#3: \'string\' should not exist, it has been deleted'); + } + if (v === undefined) { + $ERROR('#4: deleted element should not be visited'); + } +}); diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_return-found-value.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_return-found-value.js new file mode 100644 index 0000000000..b65ead1c32 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_return-found-value.js @@ -0,0 +1,29 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Find should return value if predicate returns true + */ + +var testVals = [ + undefined, + null, + 0, + 'string', + String, + this, + true, + [1,2,3] +]; + +var a; + +for (var i = 0, len = testVals.length; i < len; i++) { + a = testVals.find(function (v) { + return v === testVals[i]; + }); + if (a !== testVals[i]) { + $ERROR('#' + (i + 1) + ': a !== testVals[' + i + ']. Actual: ' + a); + } +} diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_skip-empty.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_skip-empty.js new file mode 100644 index 0000000000..a24b0b8301 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_skip-empty.js @@ -0,0 +1,20 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description predicate is called only for elements of the array which actually exist; it is not called for missing elements of the array + */ + +var a = []; + +a[10] = 1; +a[11] = 2; + +var b = a.find(function (v) { + return v !== 1; +}); + +if (b !== 2) { + $ERROR('#1: b !== 2. Actual: ' + b); +} diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_this-defined.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_this-defined.js new file mode 100644 index 0000000000..f28f6fc4a5 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_this-defined.js @@ -0,0 +1,18 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description thisArg should be bound to this if provided + */ + +var globalThis = this; + +[1].find(function () { + if (this !== Array) { + $ERROR('#1: this !== Array'); + } + if (this === globalThis) { + $ERROR('#2: this === globalThis. Should be Array'); + } +}, Array); diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_this-is-object.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_this-is-object.js new file mode 100644 index 0000000000..7897090a66 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_this-is-object.js @@ -0,0 +1,25 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description Array.prototype.find should convert thisArg into an object + */ +var dataTypes = [ + undefined, + null, + true, + this, + {}, + 'string', + 0, + function () {} +] + +for (var i = 0, len = dataTypes.length; i < len; i++) { + [1].find(function () { + if (!(this instanceof Object)) { + $ERROR('#' + i + ': !(this instanceof Object). Actual: ' + typeof this); + } + }, dataTypes[i]) +} diff --git a/test/suite/es6/Array.prototype.find/Array.prototype.find_this-undefined.js b/test/suite/es6/Array.prototype.find/Array.prototype.find_this-undefined.js new file mode 100644 index 0000000000..54a2c9fd42 --- /dev/null +++ b/test/suite/es6/Array.prototype.find/Array.prototype.find_this-undefined.js @@ -0,0 +1,15 @@ +// Copyright (c) 2014 Matthew Meyers. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/** + * @path + * @description thisArg should be undefined if not provided + */ + +var globalThis = this; + +[1].find(function () { + if (this !== globalThis) { + $ERROR('#1: this !== globalThis'); + } +});