Debian Codegraph @

js/src/jsapi-tests/testDateToLocaleString.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include "js/LocaleSensitive.h"
9 #include "jsapi-tests/tests.h"
10
11 BEGIN_TEST(testDateToLocaleString) {
12 JSRuntime* rt = JS_GetRuntime(cx);
13
14 // This test should only attempt to run if we have Intl support: necessary
15 // to properly assume that changes to the default locale will predictably
16 // affect the behavior of the locale-sensitive Date methods tested here.
17 JS::Rooted<JS::Value> haveIntl(cx);
18 EVAL("typeof Intl !== 'undefined'", &haveIntl);
19 if (!haveIntl.toBoolean()) {
20 return true;
21 }
22
23 // Pervasive assumption: our Intl support includes "de" (German) and
24 // "en" (English) and treats them differently for purposes of
25 // Date.prototype.toLocale{,Date,Time}String behavior.
26
27 // Start with German.
28 CHECK(JS_SetDefaultLocale(rt, "de"));
29
30 // The (constrained) Date object we'll use to test behavior.
31 EXEC("var d = new Date(Date.UTC(2015, 9 - 1, 17));");
32
33 // Test that toLocaleString behavior changes with default locale changes.
34 EXEC("var deAll = d.toLocaleString();");
35
36 CHECK(JS_SetDefaultLocale(rt, "en"));
37 EXEC(
38 "if (d.toLocaleString() === deAll) \n"
39 " throw 'toLocaleString results should have changed with system locale "
40 "change';");
41
42 // Test that toLocaleDateString behavior changes with default locale changes.
43 EXEC("var enDate = d.toLocaleDateString();");
44
45 CHECK(JS_SetDefaultLocale(rt, "de"));
46 EXEC(
47 "if (d.toLocaleDateString() === enDate) \n"
48 " throw 'toLocaleDateString results should have changed with system "
49 "locale change';");
50
51 // Test that toLocaleTimeString behavior changes with default locale changes.
52 EXEC("var deTime = d.toLocaleTimeString();");
53
54 CHECK(JS_SetDefaultLocale(rt, "en"));
55 EXEC(
56 "if (d.toLocaleTimeString() === deTime) \n"
57 " throw 'toLocaleTimeString results should have changed with system "
58 "locale change';");
59
60 JS_ResetDefaultLocale(rt);
61 return true;
62 }
63 END_TEST(testDateToLocaleString)