~ubuntu-branches/ubuntu/trusty/mozjs24/trusty-proposed

1 by Tim Lunn
Import upstream version 24.2.0
1
/*
2
 * Any copyright is dedicated to the Public Domain.
3
 * http://creativecommons.org/licenses/publicdomain/
4
 */
5
6
var gTestfile = 'toLocaleString.js';
7
var BUGNUMBER = 653789;
8
var summary = "Object.prototype.toLocaleString";
9
10
print(BUGNUMBER + ": " + summary);
11
12
/**************
13
 * BEGIN TEST *
14
 **************/
15
16
function expectThrowTypeError(fun)
17
{
18
  try
19
  {
20
    var r = fun();
21
    throw "didn't throw TypeError, returned " + r;
22
  }
23
  catch (e)
24
  {
25
    assertEq(e instanceof TypeError, true,
26
             "didn't throw TypeError, got: " + e);
27
  }
28
}
29
30
var toLocaleString = Object.prototype.toLocaleString;
31
32
/*
33
 * 1. Let O be the result of calling ToObject passing the this value as the
34
 *    argument.
35
 */
36
expectThrowTypeError(function() { toLocaleString.call(null); });
37
expectThrowTypeError(function() { toLocaleString.call(undefined); });
38
expectThrowTypeError(function() { toLocaleString.apply(null); });
39
expectThrowTypeError(function() { toLocaleString.apply(undefined); });
40
41
42
/*
43
 * 2. Let toString be the result of calling the [[Get]] internal method of O
44
 *    passing "toString" as the argument.
45
 */
46
try
47
{
48
  toLocaleString.call({ get toString() { throw 17; } });
49
  throw new Error("didn't throw");
50
}
51
catch (e)
52
{
53
  assertEq(e, 17);
54
}
55
56
57
/* 3. If IsCallable(toString) is false, throw a TypeError exception. */
58
expectThrowTypeError(function() { toLocaleString.call({ toString: 12 }); });
59
expectThrowTypeError(function() { toLocaleString.call({ toString: 0.3423423452352e9 }); });
60
expectThrowTypeError(function() { toLocaleString.call({ toString: undefined }); });
61
expectThrowTypeError(function() { toLocaleString.call({ toString: false }); });
62
expectThrowTypeError(function() { toLocaleString.call({ toString: [] }); });
63
expectThrowTypeError(function() { toLocaleString.call({ toString: {} }); });
64
expectThrowTypeError(function() { toLocaleString.call({ toString: new String }); });
65
expectThrowTypeError(function() { toLocaleString.call({ toString: new Number(7.7) }); });
66
expectThrowTypeError(function() { toLocaleString.call({ toString: new Boolean(true) }); });
67
expectThrowTypeError(function() { toLocaleString.call({ toString: JSON }); });
68
69
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: 12 }); });
70
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: 0.3423423452352e9 }); });
71
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: undefined }); });
72
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: false }); });
73
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: [] }); });
74
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: {} }); });
75
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new String }); });
76
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new Number(7.7) }); });
77
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: new Boolean(true) }); });
78
expectThrowTypeError(function() { toLocaleString.call({ valueOf: 0, toString: JSON }); });
79
80
81
/*
82
 * 4. Return the result of calling the [[Call]] internal method of toString
83
 *    passing O as the this value and no arguments.
84
 */
85
assertEq(toLocaleString.call({ get toString() { return function() { return "foo"; } } }),
86
         "foo");
87
88
var obj = { toString: function() { assertEq(this, obj); assertEq(arguments.length, 0); return 5; } };
89
assertEq(toLocaleString.call(obj), 5);
90
91
assertEq(toLocaleString.call({ toString: function() { return obj; } }), obj);
92
93
assertEq(toLocaleString.call({ toString: function() { return obj; },
94
                               valueOf: function() { return "abc"; } }),
95
         obj);
96
97
/******************************************************************************/
98
99
if (typeof reportCompare === "function")
100
  reportCompare(true, true);
101
102
print("All tests passed!");