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 = 312385; michael@0: var summary = 'Generic methods with null or undefined |this|'; michael@0: var actual = ''; michael@0: var expect = true; michael@0: var voids = [null, undefined]; michael@0: michael@0: michael@0: function noop() { } michael@0: michael@0: var generics = { michael@0: String: [{ quote: [] }, michael@0: { substring: [] }, michael@0: { toLowerCase: [] }, michael@0: { toUpperCase: [] }, michael@0: { charAt: [] }, michael@0: { charCodeAt: [] }, michael@0: { indexOf: [] }, michael@0: { lastIndexOf: [] }, michael@0: { toLocaleLowerCase: [] }, michael@0: { toLocaleUpperCase: [] }, michael@0: { localeCompare: [] }, michael@0: { match: [/(?:)/] }, // match(regexp) michael@0: { search: [] }, michael@0: { replace: [] }, michael@0: { split: [] }, michael@0: { substr: [] }, michael@0: { concat: [] }, michael@0: { slice: [] }], michael@0: michael@0: Array: [{ join: [] }, michael@0: { reverse: [] }, michael@0: { sort: [] }, michael@0: // { push: [0] }, // push(item1, ...) michael@0: // { pop: [] }, michael@0: // { shift: [] }, michael@0: { unshift: [] }, michael@0: // { splice: [0, 0, 1] }, // splice(start, deleteCount, item1, ...) michael@0: { concat: [] }, michael@0: { indexOf: [] }, michael@0: { lastIndexOf: [] }, michael@0: // forEach is excluded since it does not return a value... michael@0: /* { forEach: [noop] }, // forEach(callback, thisObj) */ michael@0: { map: [noop] }, // map(callback, thisObj) michael@0: { filter: [noop] }, // filter(callback, thisObj) michael@0: { some: [noop] }, // some(callback, thisObj) michael@0: { every: [noop] } // every(callback, thisObj) michael@0: ] michael@0: }; michael@0: michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: var global = this; michael@0: michael@0: for (var c in generics) michael@0: { michael@0: var methods = generics[c]; michael@0: for (var i = 0; i < methods.length; i++) michael@0: { michael@0: var method = methods[i]; michael@0: michael@0: for (var methodname in method) michael@0: { michael@0: for (var v = 0; v < voids.length; v++) michael@0: { michael@0: var Constructor = global[c] michael@0: michael@0: var argsLen = method[methodname].length; michael@0: assertEq(argsLen === 0 || argsLen === 1, true, "not all arities handled"); michael@0: michael@0: var generic = Constructor[methodname]; michael@0: var prototypy = Constructor.prototype[methodname]; michael@0: michael@0: assertEq(typeof generic, "function"); michael@0: assertEq(typeof prototypy, "function"); michael@0: michael@0: // GENERIC METHOD TESTING michael@0: michael@0: try michael@0: { michael@0: switch (method[methodname].length) michael@0: { michael@0: case 0: michael@0: generic(voids[v]); michael@0: break; michael@0: michael@0: case 1: michael@0: generic(voids[v], method[methodname][0]); michael@0: break; michael@0: } michael@0: throw new Error(c + "." + methodname + " must throw for null or " + michael@0: "undefined first argument"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "Didn't get a TypeError for " + c + "." + methodname + michael@0: " called with null or undefined first argument"); michael@0: } michael@0: michael@0: michael@0: // PROTOTYPE METHOD TESTING michael@0: michael@0: try michael@0: { michael@0: prototypy.apply(voids[v], method[methodname][0]); michael@0: throw new Error(c + ".prototype." + methodname + " must throw " + michael@0: "for null or undefined this"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: c + ".prototype." + methodname + "didn't throw a " + michael@0: "TypeError when called with null or undefined this"); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("Tests finished.");