Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit fdced1d

Browse files
committed
Add ICU tests
1 parent b7b8de8 commit fdced1d

File tree

3 files changed

+233
-3
lines changed

3 files changed

+233
-3
lines changed

tests/.v8-helpers.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public function __construct(PhpV8Testsuite $testsuite) {
3333
$this->testsuite = $testsuite;
3434
}
3535

36-
public function getPrintFunctionTemplate (\V8\Isolate $isolate) {
37-
$print_func_tpl = new \V8\FunctionTemplate($isolate, function (\V8\FunctionCallbackInfo $args) {
36+
public function getPrintFunctionTemplate(\V8\Isolate $isolate, $newline = false)
37+
{
38+
$print_func_tpl = new \V8\FunctionTemplate($isolate, function (\V8\FunctionCallbackInfo $args) use ($newline) {
3839

3940
$context = $args->GetContext();
4041

@@ -44,12 +45,21 @@ public function getPrintFunctionTemplate (\V8\Isolate $isolate) {
4445
$out[] = $this->toString($arg, $context);
4546
}
4647

47-
echo implode('', $out);
48+
echo implode('', $out), ($newline ? PHP_EOL : false);
4849
});
4950

5051
return $print_func_tpl;
5152
}
5253

54+
public function injectConsoleLog(\V8\Context $context) {
55+
$isolate = $context->GetIsolate();
56+
$obj_tpl = new \V8\ObjectTemplate($isolate);
57+
$obj_tpl->Set(new \V8\StringValue($isolate, 'log'), $this->getPrintFunctionTemplate($isolate, true));
58+
59+
$console_obj = $obj_tpl->NewInstance($context);
60+
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate, 'console'), $console_obj);
61+
}
62+
5363
/**
5464
* @param \V8\Value | \V8\ObjectValue | \V8\SymbolValue | \V8\StringValue | \V8\NumberValue $arg
5565
* @param \V8\Context $context
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
--TEST--
2+
ICU - Intl.DateTimeFormat()
3+
--SKIPIF--
4+
<?php if (!extension_loaded("v8")) console.log "skip"; ?>
5+
--ENV--
6+
TZ=UTC
7+
--INI--
8+
date.timezone = "UTC"
9+
--FILE--
10+
<?php
11+
/** @var \Phpv8Testsuite $helper */
12+
$helper = require '.testsuite.php';
13+
14+
require '.v8-helpers.php';
15+
$v8_helper = new PhpV8Helpers($helper);
16+
17+
// Tests:
18+
19+
$isolate = new \V8\Isolate();
20+
$context = new \V8\Context($isolate);
21+
$v8_helper->injectConsoleLog($context);
22+
23+
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat#Using_locales
24+
$source = /** @lang JavaScript 1.8 */
25+
<<<HEREDOC
26+
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
27+
28+
// formats below assume the local time zone of the locale;
29+
// America/Los_Angeles for the US
30+
31+
// US English uses month-day-year order
32+
console.log(new Intl.DateTimeFormat('en-US').format(date));
33+
// → "12/19/2012"
34+
35+
// British English uses day-month-year order
36+
console.log(new Intl.DateTimeFormat('en-GB').format(date));
37+
// → "20/12/2012"
38+
39+
// Korean uses year-month-day order
40+
console.log(new Intl.DateTimeFormat('ko-KR').format(date));
41+
// → "2012. 12. 20."
42+
43+
// Arabic in most Arabic speaking countries uses real Arabic digits
44+
console.log(new Intl.DateTimeFormat('ar-EG').format(date));
45+
// → "٢٠‏/١٢‏/٢٠١٢"
46+
47+
// for Japanese, applications may want to use the Japanese calendar,
48+
// where 2012 was the year 24 of the Heisei era
49+
console.log(new Intl.DateTimeFormat('ja-JP-u-ca-japanese').format(date));
50+
// → "24/12/20"
51+
52+
// when requesting a language that may not be supported, such as
53+
// Balinese, include a fallback language, in this case Indonesian
54+
console.log(new Intl.DateTimeFormat(['ban', 'id']).format(date));
55+
// → "20/12/2012"
56+
HEREDOC;
57+
58+
(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context);
59+
60+
$helper->line();
61+
62+
63+
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat#Using_options
64+
// with en-AU am/AM hack as it causes troubles in CI and inconsistent across different ICU versions
65+
$source = /** @lang JavaScript 1.8 */
66+
<<<HEREDOC
67+
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
68+
69+
// request a weekday along with a long date
70+
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
71+
console.log(new Intl.DateTimeFormat('de-DE', options).format(date));
72+
// → "Donnerstag, 20. Dezember 2012"
73+
74+
// an application may want to use UTC and make that visible
75+
options.timeZone = 'UTC';
76+
options.timeZoneName = 'short';
77+
console.log(new Intl.DateTimeFormat('en-US', options).format(date));
78+
// → "Thursday, December 20, 2012, GMT"
79+
80+
// sometimes you want to be more precise
81+
var options = {
82+
hour: 'numeric', minute: 'numeric', second: 'numeric',
83+
timeZoneName: 'short'
84+
};
85+
console.log(new Intl.DateTimeFormat('en-AU', options).format(date).replace('AM', 'am'));
86+
// → "2:00:00 pm AEDT"
87+
88+
// sometimes even the US needs 24-hour time
89+
options = {
90+
year: 'numeric', month: 'numeric', day: 'numeric',
91+
hour: 'numeric', minute: 'numeric', second: 'numeric',
92+
hour12: false
93+
};
94+
console.log(date.toLocaleString('en-US', options));
95+
// → "12/19/2012, 19:00:00"
96+
HEREDOC;
97+
98+
(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context);
99+
100+
101+
$helper->line();
102+
103+
$source = /** @lang JavaScript 1.8 */
104+
<<<HEREDOC
105+
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
106+
107+
// request a weekday along with a long date
108+
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
109+
110+
options.timeZone = 'America/New_York';
111+
console.log(new Intl.DateTimeFormat('en-US', options).format(date));
112+
options.timeZone = 'Europe/Paris';
113+
console.log(new Intl.DateTimeFormat('fr-FR', options).format(date));
114+
options.timeZone = 'Europe/Berlin';
115+
console.log(new Intl.DateTimeFormat('de-DE', options).format(date));
116+
117+
HEREDOC;
118+
119+
(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context);
120+
121+
122+
?>
123+
--EXPECT--
124+
12/20/2012
125+
20/12/2012
126+
2012. 12. 20.
127+
٢٠‏/١٢‏/٢٠١٢
128+
平成24/12/20
129+
20/12/2012
130+
131+
Donnerstag, 20. Dezember 2012
132+
Thursday, December 20, 2012, GMT
133+
3:00:00 am GMT
134+
12/20/2012, 03:00:00
135+
136+
Wednesday, December 19, 2012
137+
jeudi 20 décembre 2012
138+
Donnerstag, 20. Dezember 2012

tests/004-ICU-Intl_NumberFormat.phpt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
--TEST--
2+
ICU - Intl.NumberFormat()
3+
--SKIPIF--
4+
<?php if (!extension_loaded("v8")) console.log "skip"; ?>
5+
--FILE--
6+
<?php
7+
/** @var \Phpv8Testsuite $helper */
8+
$helper = require '.testsuite.php';
9+
10+
require '.v8-helpers.php';
11+
$v8_helper = new PhpV8Helpers($helper);
12+
13+
// Tests:
14+
15+
$isolate = new \V8\Isolate();
16+
$context = new \V8\Context($isolate);
17+
$v8_helper->injectConsoleLog($context);
18+
19+
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat#Using_locales
20+
$source = /** @lang JavaScript 1.8 */
21+
<<<HEREDOC
22+
var number = 123456.789;
23+
24+
// German uses comma as decimal separator and period for thousands
25+
console.log(new Intl.NumberFormat('de-DE').format(number));
26+
// → 123.456,789
27+
28+
// Arabic in most Arabic speaking countries uses real Arabic digits
29+
console.log(new Intl.NumberFormat('ar-EG').format(number));
30+
// → ١٢٣٤٥٦٫٧٨٩
31+
32+
// India uses thousands/lakh/crore separators
33+
console.log(new Intl.NumberFormat('en-IN').format(number));
34+
// → 1,23,456.789
35+
36+
// the nu extension key requests a numbering system, e.g. Chinese decimal
37+
console.log(new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec').format(number));
38+
// → 一二三,四五六.七八九
39+
40+
// when requesting a language that may not be supported, such as
41+
// Balinese, include a fallback language, in this case Indonesian
42+
console.log(new Intl.NumberFormat(['ban', 'id']).format(number));
43+
// → 123.456,789
44+
HEREDOC;
45+
46+
(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context);
47+
48+
$helper->line();
49+
50+
51+
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat#Using_options
52+
$source = /** @lang JavaScript 1.8 */
53+
<<<HEREDOC
54+
var number = 123456.789;
55+
56+
// request a currency format
57+
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
58+
// → 123.456,79 €
59+
60+
// the Japanese yen doesn't use a minor unit
61+
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
62+
// → ¥123,457
63+
64+
// limit to three significant digits
65+
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
66+
// → 1,23,000
67+
HEREDOC;
68+
69+
(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context);
70+
71+
72+
?>
73+
--EXPECT--
74+
123.456,789
75+
١٢٣٬٤٥٦٫٧٨٩
76+
1,23,456.789
77+
一二三,四五六.七八九
78+
123.456,789
79+
80+
123.456,79 €
81+
¥123,457
82+
1,23,000

0 commit comments

Comments
 (0)