Skip to content

Commit 6aa5eb4

Browse files
committed
test: port over most of the remaining Locale tests
1 parent 97a6a28 commit 6aa5eb4

27 files changed

+937
-14
lines changed

NOTICE

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Copyright (c) 2018 Igalia, S.L. All rights reserved.
1313
Copyright (c) 2018 Rick Waldron. All rights reserved.
1414
Copyright (c) 2019 Google Inc. All rights reserved.
1515
Copyright (c) 2020 Apple Inc. All rights reserved.
16+
Copyright (c) 2020 Jeff Walden, Mozilla Corporation. All rights reserved.
1617

1718
The << Software identified by reference to the Ecma Standard* ("Software)">> is protected by copyright and is being
1819
made available under the "BSD License", included below. This Software may be subject to third party rights (rights
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
7+
$invalidLanguageTags = [
8+
// Unicode extension sequence is incomplete.
9+
'da-u',
10+
'da-u-',
11+
'da-u--',
12+
'da-u-t-latn',
13+
'da-u-x-priv',
14+
15+
// Duplicate 'u' singleton.
16+
'da-u-ca-gregory-u-ca-buddhist',
17+
];
18+
19+
it('throws ValueError for invalid language options', function (string $test): void {
20+
expect(fn () => new Locale($test))
21+
->toThrow(ValueError::class);
22+
})->with($invalidLanguageTags);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
7+
$validLanguageTags = [
8+
// Duplicate keywords are removed.
9+
['da-u-ca-gregory-ca-buddhist', 'da-u-ca-gregory'],
10+
11+
// Keywords currently used in Intl specs are reordered in US-ASCII order.
12+
['zh-u-nu-hans-ca-chinese', 'zh-u-ca-chinese-nu-hans'],
13+
['zh-u-ca-chinese-nu-hans', 'zh-u-ca-chinese-nu-hans'],
14+
15+
// Even keywords currently not used in Intl specs are reordered in US-ASCII order.
16+
['de-u-cu-eur-nu-latn', 'de-u-cu-eur-nu-latn'],
17+
['de-u-nu-latn-cu-eur', 'de-u-cu-eur-nu-latn'],
18+
19+
// Attributes in Unicode extensions are reordered in US-ASCII order.
20+
['pt-u-attr-ca-gregory', 'pt-u-attr-ca-gregory'],
21+
['pt-u-attr1-attr2-ca-gregory', 'pt-u-attr1-attr2-ca-gregory'],
22+
['pt-u-attr2-attr1-ca-gregory', 'pt-u-attr1-attr2-ca-gregory'],
23+
];
24+
25+
it('canonicalizes specific tags', function (string $tag, string $canonical): void {
26+
expect((string) new Locale($tag))
27+
->toBe($canonical);
28+
})->with($validLanguageTags);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
use Ecma\Intl\Locale\Options;
7+
8+
$testData = [
9+
// Regular grandfathered without modern replacement.
10+
[
11+
'tag' => 'cel-gaulish',
12+
'options' => [
13+
'language' => 'fr',
14+
'script' => 'Cyrl',
15+
'region' => 'FR',
16+
'numberingSystem' => 'latn',
17+
],
18+
'canonical' => 'fr-Cyrl-FR-u-nu-latn',
19+
],
20+
21+
// Regular grandfathered with modern replacement.
22+
[
23+
'tag' => 'art-lojban',
24+
'options' => [
25+
'language' => 'fr',
26+
'script' => 'Cyrl',
27+
'region' => 'ZZ',
28+
'numberingSystem' => 'latn',
29+
],
30+
'canonical' => 'fr-Cyrl-ZZ-u-nu-latn',
31+
],
32+
];
33+
34+
it('verifies handling of options with grandfathered tags', function (
35+
string $tag,
36+
array $options,
37+
string $canonical,
38+
): void {
39+
$loc = new Locale($tag, new Options(...$options));
40+
expect((string) $loc)->toBe($canonical);
41+
42+
foreach ($options as $property => $value) {
43+
expect($loc->$property)->toBe($value);
44+
}
45+
})->with($testData);
46+
47+
$invalidTestData = [
48+
[
49+
'tag' => 'i-default',
50+
'options' => [
51+
'language' => 'fr',
52+
'script' => 'Cyrl',
53+
'region' => 'DE',
54+
'numberingSystem' => 'latn',
55+
],
56+
],
57+
[
58+
'tag' => 'en-gb-oed',
59+
'options' => [
60+
'language' => 'fr',
61+
'script' => 'Cyrl',
62+
'region' => 'US',
63+
'numberingSystem' => 'latn',
64+
],
65+
],
66+
[
67+
'tag' => 'zh-min',
68+
'options' => [],
69+
],
70+
];
71+
72+
it('throws ValueError for invalid grandfather tags with valid options', function (
73+
string $tag,
74+
array $options,
75+
): void {
76+
expect(fn () => new Locale($tag, new Options(...$options)))
77+
->toThrow(ValueError::class);
78+
})->with($invalidTestData);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
use Ecma\Intl\Locale\Options;
7+
8+
$invalidTestData = [
9+
[
10+
'tag' => 'x-default',
11+
'options' => [
12+
'language' => 'fr',
13+
'script' => 'Cyrl',
14+
'region' => 'DE',
15+
'numberingSystem' => 'latn',
16+
],
17+
],
18+
];
19+
20+
it('throws ValueError for invalid private use tags with valid options', function (
21+
string $tag,
22+
array $options,
23+
): void {
24+
expect(fn () => new Locale($tag, new Options(...$options)))
25+
->toThrow(ValueError::class);
26+
})->with($invalidTestData);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
7+
it('verifies getters with grandfathered tags for "cel-gaulish"', function (): void {
8+
$loc = new Locale('cel-gaulish');
9+
10+
expect($loc->baseName)
11+
->toBe('xtg')
12+
->and($loc->language)
13+
->toBe('xtg')
14+
->and($loc->script)
15+
->toBeNull()
16+
->and($loc->region)
17+
->toBeNull();
18+
});
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
7+
$testData = [
8+
[
9+
'tag' => 'sv',
10+
'expected' => [
11+
'baseName' => 'sv',
12+
'language' => 'sv',
13+
'script' => null,
14+
'region' => null,
15+
],
16+
],
17+
[
18+
'tag' => 'sv-Latn',
19+
'expected' => [
20+
'baseName' => 'sv-Latn',
21+
'language' => 'sv',
22+
'script' => 'Latn',
23+
'region' => null,
24+
],
25+
],
26+
[
27+
'tag' => 'sv-SE',
28+
'expected' => [
29+
'baseName' => 'sv-SE',
30+
'language' => 'sv',
31+
'script' => null,
32+
'region' => 'SE',
33+
],
34+
],
35+
[
36+
'tag' => 'de-1901',
37+
'expected' => [
38+
'baseName' => 'de-1901',
39+
'language' => 'de',
40+
'script' => null,
41+
'region' => null,
42+
],
43+
],
44+
];
45+
46+
it('verifies getters with missing tags', function (
47+
string $tag,
48+
array $expected,
49+
): void {
50+
$loc = new Locale($tag);
51+
52+
foreach ($expected as $property => $value) {
53+
expect($loc->$property)->toBe($value);
54+
}
55+
})->with($testData);

tests/intl402/Locale/getters.php

+16-10
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,64 @@
1212
->toBe('de-Latn-DE-u-ca-gregory-co-phonebk-hc-h23-kf-kn-false-nu-latn')
1313
->and($loc->baseName)->toBe('de-Latn-DE')
1414
->and($loc->calendar)->toBe('gregory')
15+
->and($loc->calendars)->toBe(['gregory'])
1516
->and($loc->caseFirst)->toBe('yes')
1617
->and($loc->collation)->toBe('phonebk')
1718
->and($loc->hourCycle)->toBe('h23')
1819
->and($loc->language)->toBe('de')
1920
->and($loc->numberingSystem)->toBe('latn')
2021
->and($loc->numeric)->toBeFalse()
2122
->and($loc->region)->toBe('DE')
22-
->and($loc->script)->toBe('Latn');
23+
->and($loc->script)->toBe('Latn')
24+
->and($loc->getCalendars())->toBe($loc->calendars);
2325

2426
$loc = new Locale($langtag, new Locale\Options(
25-
language: 'ja',
26-
script: 'jpan',
27-
region: 'jp',
2827
calendar: 'japanese',
28+
caseFirst: 'false',
2929
collation: 'search',
3030
hourCycle: 'h24',
31-
caseFirst: 'false',
32-
numeric: true,
31+
language: 'ja',
3332
numberingSystem: 'jpanfin',
33+
numeric: true,
34+
region: 'jp',
35+
script: 'jpan',
3436
));
3537

3638
test('all getters return the expected results after replacing all components through option values')
3739
->expect((string) $loc)
3840
->toBe('ja-Jpan-JP-u-ca-japanese-co-search-hc-h24-kf-false-kn-nu-jpanfin')
3941
->and($loc->baseName)->toBe('ja-Jpan-JP')
4042
->and($loc->calendar)->toBe('japanese')
43+
->and($loc->calendars)->toBe(['japanese'])
4144
->and($loc->caseFirst)->toBe('false')
4245
->and($loc->collation)->toBe('search')
4346
->and($loc->hourCycle)->toBe('h24')
4447
->and($loc->language)->toBe('ja')
4548
->and($loc->numberingSystem)->toBe('jpanfin')
4649
->and($loc->numeric)->toBeTrue()
4750
->and($loc->region)->toBe('JP')
48-
->and($loc->script)->toBe('Jpan');
51+
->and($loc->script)->toBe('Jpan')
52+
->and($loc->getCalendars())->toBe($loc->calendars);
4953

5054
$loc = new Locale($langtag, new Locale\Options(
51-
language: 'fr',
52-
region: 'ca',
5355
collation: 'standard',
5456
hourCycle: 'h11',
57+
language: 'fr',
58+
region: 'ca',
5559
));
5660

5761
test('all getters return the expected results after replacing only some components through option values')
5862
->expect((string) $loc)
5963
->toBe('fr-Latn-CA-u-ca-gregory-co-standard-hc-h11-kf-kn-false-nu-latn')
6064
->and($loc->baseName)->toBe('fr-Latn-CA')
6165
->and($loc->calendar)->toBe('gregory')
66+
->and($loc->calendars)->toBe(['gregory'])
6267
->and($loc->caseFirst)->toBe('yes')
6368
->and($loc->collation)->toBe('standard')
6469
->and($loc->hourCycle)->toBe('h11')
6570
->and($loc->language)->toBe('fr')
6671
->and($loc->numberingSystem)->toBe('latn')
6772
->and($loc->numeric)->toBeFalse()
6873
->and($loc->region)->toBe('CA')
69-
->and($loc->script)->toBe('Latn');
74+
->and($loc->script)->toBe('Latn')
75+
->and($loc->getCalendars())->toBe($loc->calendars);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
7+
$reflected = new ReflectionClass(Locale::class);
8+
9+
it('allows extension')
10+
->expect($reflected->isFinal())
11+
->toBeFalse();

tests/intl402/Locale/instance.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
7+
$reflected = new ReflectionClass(Locale::class);
8+
9+
it('allows instantiation')
10+
->expect($reflected->isInstantiable())
11+
->toBeTrue();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Ecma\Intl\Locale;
6+
7+
it('throws ValueError for invalid language tags', function (string $tag): void {
8+
expect(fn () => new Locale($tag))
9+
->toThrow(ValueError::class);
10+
})->with(getInvalidLanguageTags());

0 commit comments

Comments
 (0)