michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 304828; michael@0: var summary = 'Array Generic Methods'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: var value; michael@0: michael@0: // use Array methods on a String michael@0: // join michael@0: value = '123'; michael@0: expect = '1,2,3'; michael@0: try michael@0: { michael@0: actual = Array.prototype.join.call(value); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': join'); michael@0: michael@0: // reverse michael@0: value = '123'; michael@0: expect = 'TypeError: Array.prototype.reverse.call(...) is read-only'; michael@0: try michael@0: { michael@0: actual = Array.prototype.reverse.call(value) + ''; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': reverse'); michael@0: michael@0: // sort michael@0: value = 'cba'; michael@0: expect = 'TypeError: Array.prototype.sort.call(...) is read-only'; michael@0: try michael@0: { michael@0: actual = Array.prototype.sort.call(value) + ''; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': sort'); michael@0: michael@0: // push michael@0: value = 'abc'; michael@0: expect = 6; michael@0: try michael@0: { michael@0: Array.prototype.push.call(value, 'd', 'e', 'f'); michael@0: throw new Error("didn't throw"); michael@0: } michael@0: catch(e) michael@0: { michael@0: reportCompare(true, e instanceof TypeError, michael@0: "push on a string primitive should throw TypeError"); michael@0: } michael@0: reportCompare('abc', value, summary + ': push string primitive'); michael@0: michael@0: value = new String("abc"); michael@0: expect = 6; michael@0: try michael@0: { michael@0: Array.prototype.push.call(value, 'd', 'e', 'f'); michael@0: throw new Error("didn't throw"); michael@0: } michael@0: catch(e) michael@0: { michael@0: reportCompare(true, e instanceof TypeError, michael@0: "push on a String object should throw TypeError"); michael@0: } michael@0: reportCompare("d", value[3], summary + ': push String object index 3'); michael@0: reportCompare("e", value[4], summary + ': push String object index 4'); michael@0: reportCompare("f", value[5], summary + ': push String object index 5'); michael@0: michael@0: // pop michael@0: value = 'abc'; michael@0: expect = "TypeError: property Array.prototype.pop.call(...) is non-configurable and can't be deleted"; michael@0: try michael@0: { michael@0: actual = Array.prototype.pop.call(value); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': pop'); michael@0: reportCompare('abc', value, summary + ': pop'); michael@0: michael@0: // unshift michael@0: value = 'def'; michael@0: expect = 'TypeError: Array.prototype.unshift.call(...) is read-only'; michael@0: try michael@0: { michael@0: actual = Array.prototype.unshift.call(value, 'a', 'b', 'c'); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': unshift'); michael@0: reportCompare('def', value, summary + ': unshift'); michael@0: michael@0: // shift michael@0: value = 'abc'; michael@0: expect = 'TypeError: Array.prototype.shift.call(...) is read-only'; michael@0: try michael@0: { michael@0: actual = Array.prototype.shift.call(value); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': shift'); michael@0: reportCompare('abc', value, summary + ': shift'); michael@0: michael@0: // splice michael@0: value = 'abc'; michael@0: expect = 'TypeError: Array.prototype.splice.call(...) is read-only'; michael@0: try michael@0: { michael@0: actual = Array.prototype.splice.call(value, 1, 1) + ''; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': splice'); michael@0: michael@0: // concat michael@0: value = 'abc'; michael@0: expect = 'abc,d,e,f'; michael@0: try michael@0: { michael@0: actual = Array.prototype.concat.call(value, 'd', 'e', 'f') + ''; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': concat'); michael@0: michael@0: // slice michael@0: value = 'abc'; michael@0: expect = 'b'; michael@0: try michael@0: { michael@0: actual = Array.prototype.slice.call(value, 1, 2) + ''; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': slice'); michael@0: michael@0: // indexOf michael@0: value = 'abc'; michael@0: expect = 1; michael@0: try michael@0: { michael@0: actual = Array.prototype.indexOf.call(value, 'b'); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': indexOf'); michael@0: michael@0: // lastIndexOf michael@0: value = 'abcabc'; michael@0: expect = 4; michael@0: try michael@0: { michael@0: actual = Array.prototype.lastIndexOf.call(value, 'b'); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': lastIndexOf'); michael@0: michael@0: // forEach michael@0: value = 'abc'; michael@0: expect = 'ABC'; michael@0: actual = ''; michael@0: try michael@0: { michael@0: Array.prototype.forEach.call(value, michael@0: function (v, index, array) michael@0: {actual += array[index].toUpperCase();}); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': forEach'); michael@0: michael@0: // map michael@0: value = 'abc'; michael@0: expect = 'A,B,C'; michael@0: try michael@0: { michael@0: actual = Array.prototype.map.call(value, michael@0: function (v, index, array) michael@0: {return v.toUpperCase();}) + ''; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': map'); michael@0: michael@0: // filter michael@0: value = '1234567890'; michael@0: expect = '2,4,6,8,0'; michael@0: try michael@0: { michael@0: actual = Array.prototype.filter.call(value, michael@0: function (v, index, array) michael@0: {return array[index] % 2 == 0;}) + ''; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': filter'); michael@0: michael@0: // every michael@0: value = '1234567890'; michael@0: expect = false; michael@0: try michael@0: { michael@0: actual = Array.prototype.every.call(value, michael@0: function (v, index, array) michael@0: {return array[index] % 2 == 0;}); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': every'); michael@0: michael@0: michael@0: