1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/Object/toLocaleString.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/licenses/publicdomain/ 1.7 + */ 1.8 + 1.9 +var gTestfile = 'toLocaleString.js'; 1.10 +var BUGNUMBER = 653789; 1.11 +var summary = "Object.prototype.toLocaleString"; 1.12 + 1.13 +print(BUGNUMBER + ": " + summary); 1.14 + 1.15 +/************** 1.16 + * BEGIN TEST * 1.17 + **************/ 1.18 + 1.19 +function expectThrowTypeError(fun) 1.20 +{ 1.21 + try 1.22 + { 1.23 + var r = fun(); 1.24 + throw "didn't throw TypeError, returned " + r; 1.25 + } 1.26 + catch (e) 1.27 + { 1.28 + assertEq(e instanceof TypeError, true, 1.29 + "didn't throw TypeError, got: " + e); 1.30 + } 1.31 +} 1.32 + 1.33 +var toLocaleString = Object.prototype.toLocaleString; 1.34 + 1.35 +/* 1.36 + * 1. Let O be the result of calling ToObject passing the this value as the 1.37 + * argument. 1.38 + */ 1.39 +expectThrowTypeError(function() { toLocaleString.call(null); }); 1.40 +expectThrowTypeError(function() { toLocaleString.call(undefined); }); 1.41 +expectThrowTypeError(function() { toLocaleString.apply(null); }); 1.42 +expectThrowTypeError(function() { toLocaleString.apply(undefined); }); 1.43 + 1.44 + 1.45 +/* 1.46 + * 2. Let toString be the result of calling the [[Get]] internal method of O 1.47 + * passing "toString" as the argument. 1.48 + */ 1.49 +try 1.50 +{ 1.51 + toLocaleString.call({ get toString() { throw 17; } }); 1.52 + throw new Error("didn't throw"); 1.53 +} 1.54 +catch (e) 1.55 +{ 1.56 + assertEq(e, 17); 1.57 +} 1.58 + 1.59 + 1.60 +/* 3. If IsCallable(toString) is false, throw a TypeError exception. */ 1.61 +expectThrowTypeError(function() { toLocaleString.call({ toString: 12 }); }); 1.62 +expectThrowTypeError(function() { toLocaleString.call({ toString: 0.3423423452352e9 }); }); 1.63 +expectThrowTypeError(function() { toLocaleString.call({ toString: undefined }); }); 1.64 +expectThrowTypeError(function() { toLocaleString.call({ toString: false }); }); 1.65 +expectThrowTypeError(function() { toLocaleString.call({ toString: [] }); }); 1.66 +expectThrowTypeError(function() { toLocaleString.call({ toString: {} }); }); 1.67 +expectThrowTypeError(function() { toLocaleString.call({ toString: new String }); }); 1.68 +expectThrowTypeError(function() { toLocaleString.call({ toString: new Number(7.7) }); }); 1.69 +expectThrowTypeError(function() { toLocaleString.call({ toString: new Boolean(true) }); }); 1.70 +expectThrowTypeError(function() { toLocaleString.call({ toString: JSON }); }); 1.71 + 1.72 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: 12 }); }); 1.73 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: 0.3423423452352e9 }); }); 1.74 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: undefined }); }); 1.75 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: false }); }); 1.76 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: [] }); }); 1.77 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: {} }); }); 1.78 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new String }); }); 1.79 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new Number(7.7) }); }); 1.80 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new Boolean(true) }); }); 1.81 +expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: JSON }); }); 1.82 + 1.83 + 1.84 +/* 1.85 + * 4. Return the result of calling the [[Call]] internal method of toString 1.86 + * passing O as the this value and no arguments. 1.87 + */ 1.88 +assertEq(toLocaleString.call({ get toString() { return function() { return "foo"; } } }), 1.89 + "foo"); 1.90 + 1.91 +var obj = { toString: function() { assertEq(this, obj); assertEq(arguments.length, 0); return 5; } }; 1.92 +assertEq(toLocaleString.call(obj), 5); 1.93 + 1.94 +assertEq(toLocaleString.call({ toString: function() { return obj; } }), obj); 1.95 + 1.96 +assertEq(toLocaleString.call({ toString: function() { return obj; }, 1.97 + valueOf: function() { return "abc"; } }), 1.98 + obj); 1.99 + 1.100 +/******************************************************************************/ 1.101 + 1.102 +if (typeof reportCompare === "function") 1.103 + reportCompare(true, true); 1.104 + 1.105 +print("All tests passed!");