[INTL] Implement Date.prototype.toLocaleDateString in ECMA-402
[WebKit-https.git] / LayoutTests / js / script-tests / date-toLocaleString.js
1 description("This test checks the behavior of Date.prototype.toLocaleString as described in the ECMAScript Internationalization API Specification (ECMA-402 2.0).");
2
3 shouldBe("Date.prototype.toLocaleString.length", "0");
4 shouldBeFalse("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleString').enumerable");
5 shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleString').configurable");
6 shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleString').writable");
7
8 // Test thisTimeValue abrupt completion.
9 shouldNotThrow("Date.prototype.toLocaleString.call(new Date)");
10 shouldThrow("Date.prototype.toLocaleString.call()");
11 shouldThrow("Date.prototype.toLocaleString.call(undefined)");
12 shouldThrow("Date.prototype.toLocaleString.call(null)");
13 shouldThrow("Date.prototype.toLocaleString.call(0)");
14 shouldThrow("Date.prototype.toLocaleString.call(NaN)");
15 shouldThrow("Date.prototype.toLocaleString.call(Infinity)");
16 shouldThrow("Date.prototype.toLocaleString.call('1')");
17 shouldThrow("Date.prototype.toLocaleString.call({})");
18 shouldThrow("Date.prototype.toLocaleString.call([])");
19 shouldThrow("Date.prototype.toLocaleString.call(Symbol())");
20
21 shouldBeTrue("typeof new Date().toLocaleString() === 'string'");
22
23 shouldBeEqualToString("new Date(NaN).toLocaleString()", "Invalid Date");
24
25 // Test for DateTimeFormat behavior.
26 // Test that locale parameter is passed through properly.
27 shouldThrow("new Date().toLocaleString('i')");
28 shouldBeEqualToString("new Date(0).toLocaleString('zh-Hans-CN-u-nu-hanidec', { timeZone: 'UTC' })", "一九七〇/一/一 上午一二:〇〇:〇〇");
29
30 // Defaults to mdy, hms
31 shouldBeEqualToString("new Date(0).toLocaleString('en', { timeZone: 'UTC' })", "1/1/1970, 12:00:00 AM");
32
33 // Test that options parameter is passed through properly.
34 shouldThrow("new Date(0).toLocaleString('en', null)", "'TypeError: null is not an object'");
35 shouldBeEqualToString("new Date(0).toLocaleString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' })", "12:00 AM");
36 shouldBeEqualToString("new Date(0).toLocaleString('en', { timeZone: 'UTC', year:'numeric', month:'long' })", "January 1970");
37
38 // Test toLocaleDateString ()
39 shouldBe("Date.prototype.toLocaleDateString.length", "0");
40 shouldBeFalse("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleDateString').enumerable");
41 shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleDateString').configurable");
42 shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleDateString').writable");
43
44 // Test thisTimeValue abrupt completion.
45 shouldNotThrow("Date.prototype.toLocaleDateString.call(new Date)");
46 shouldThrow("Date.prototype.toLocaleDateString.call()");
47 shouldThrow("Date.prototype.toLocaleDateString.call(undefined)");
48 shouldThrow("Date.prototype.toLocaleDateString.call(null)");
49 shouldThrow("Date.prototype.toLocaleDateString.call(0)");
50 shouldThrow("Date.prototype.toLocaleDateString.call(NaN)");
51 shouldThrow("Date.prototype.toLocaleDateString.call(Infinity)");
52 shouldThrow("Date.prototype.toLocaleDateString.call('1')");
53 shouldThrow("Date.prototype.toLocaleDateString.call({})");
54 shouldThrow("Date.prototype.toLocaleDateString.call([])");
55 shouldThrow("Date.prototype.toLocaleDateString.call(Symbol())");
56
57 shouldBeTrue("typeof new Date().toLocaleDateString() === 'string'");
58
59 shouldBeEqualToString("new Date(NaN).toLocaleDateString()", "Invalid Date");
60
61 // Test for DateTimeFormat behavior.
62 // Test that locale parameter is passed through properly.
63 shouldThrow("new Date().toLocaleDateString('i')");
64 shouldBeEqualToString("new Date(0).toLocaleDateString('zh-Hans-CN-u-nu-hanidec', { timeZone: 'UTC' })", "一九七〇/一/一");
65
66 // Defaults to mdy
67 shouldBeEqualToString("new Date(0).toLocaleDateString('en', { timeZone: 'UTC' })", "1/1/1970");
68
69 // Test that options parameter is passed through properly.
70 shouldThrow("new Date(0).toLocaleDateString('en', null)", "'TypeError: null is not an object'");
71 // Adds mdy if no date formats specified.
72 shouldBeEqualToString("new Date(0).toLocaleDateString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' })", "1/1/1970, 12:00 AM");
73 // If any date formats specified, just use them.
74 shouldBeEqualToString("new Date(0).toLocaleDateString('en', { timeZone: 'UTC', year:'numeric', month:'long' })", "January 1970");