michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: var gTestfile = 'toLocaleString.js'; michael@0: var BUGNUMBER = 653789; michael@0: var summary = "Object.prototype.toLocaleString"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: function expectThrowTypeError(fun) michael@0: { michael@0: try michael@0: { michael@0: var r = fun(); michael@0: throw "didn't throw TypeError, returned " + r; michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "didn't throw TypeError, got: " + e); michael@0: } michael@0: } michael@0: michael@0: var toLocaleString = Object.prototype.toLocaleString; michael@0: michael@0: /* michael@0: * 1. Let O be the result of calling ToObject passing the this value as the michael@0: * argument. michael@0: */ michael@0: expectThrowTypeError(function() { toLocaleString.call(null); }); michael@0: expectThrowTypeError(function() { toLocaleString.call(undefined); }); michael@0: expectThrowTypeError(function() { toLocaleString.apply(null); }); michael@0: expectThrowTypeError(function() { toLocaleString.apply(undefined); }); michael@0: michael@0: michael@0: /* michael@0: * 2. Let toString be the result of calling the [[Get]] internal method of O michael@0: * passing "toString" as the argument. michael@0: */ michael@0: try michael@0: { michael@0: toLocaleString.call({ get toString() { throw 17; } }); michael@0: throw new Error("didn't throw"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e, 17); michael@0: } michael@0: michael@0: michael@0: /* 3. If IsCallable(toString) is false, throw a TypeError exception. */ michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: 12 }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: 0.3423423452352e9 }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: undefined }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: false }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: [] }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: {} }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: new String }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: new Number(7.7) }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: new Boolean(true) }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ toString: JSON }); }); michael@0: michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: 12 }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: 0.3423423452352e9 }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: undefined }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: false }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: [] }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: {} }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new String }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new Number(7.7) }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new Boolean(true) }); }); michael@0: expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: JSON }); }); michael@0: michael@0: michael@0: /* michael@0: * 4. Return the result of calling the [[Call]] internal method of toString michael@0: * passing O as the this value and no arguments. michael@0: */ michael@0: assertEq(toLocaleString.call({ get toString() { return function() { return "foo"; } } }), michael@0: "foo"); michael@0: michael@0: var obj = { toString: function() { assertEq(this, obj); assertEq(arguments.length, 0); return 5; } }; michael@0: assertEq(toLocaleString.call(obj), 5); michael@0: michael@0: assertEq(toLocaleString.call({ toString: function() { return obj; } }), obj); michael@0: michael@0: assertEq(toLocaleString.call({ toString: function() { return obj; }, michael@0: valueOf: function() { return "abc"; } }), michael@0: obj); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("All tests passed!");