Skip to content

Commit 809477d

Browse files
gibson042anbaptomato
committed
Refactor dayPeriod formatting tests to be robust to CLDR changes
Taken from Richard's suggestion in tc39#4428 (review) Co-Authored-By: André Bargull <[email protected]> Co-Authored-By: Philip Chimento <[email protected]>
1 parent 4ba104a commit 809477d

File tree

6 files changed

+312
-459
lines changed

6 files changed

+312
-459
lines changed

test/intl402/DateTimeFormat/prototype/format/dayPeriod-long-en.js

+52-76
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,62 @@ features: [Intl.DateTimeFormat-dayPeriod]
88
locale: [en]
99
---*/
1010

11-
const d0000 = new Date(2017, 11, 12, 0, 0, 0, 0);
12-
const d0100 = new Date(2017, 11, 12, 1, 0, 0, 0);
13-
const d0200 = new Date(2017, 11, 12, 2, 0, 0, 0);
14-
const d0300 = new Date(2017, 11, 12, 3, 0, 0, 0);
15-
const d0400 = new Date(2017, 11, 12, 4, 0, 0, 0);
16-
const d0500 = new Date(2017, 11, 12, 5, 0, 0, 0);
17-
const d0600 = new Date(2017, 11, 12, 6, 0, 0, 0);
18-
const d0700 = new Date(2017, 11, 12, 7, 0, 0, 0);
19-
const d0800 = new Date(2017, 11, 12, 8, 0, 0, 0);
20-
const d0900 = new Date(2017, 11, 12, 9, 0, 0, 0);
21-
const d1000 = new Date(2017, 11, 12, 10, 0, 0, 0);
22-
const d1100 = new Date(2017, 11, 12, 11, 0, 0, 0);
23-
const d1200 = new Date(2017, 11, 12, 12, 0, 0, 0);
24-
const d1300 = new Date(2017, 11, 12, 13, 0, 0, 0);
25-
const d1400 = new Date(2017, 11, 12, 14, 0, 0, 0);
26-
const d1500 = new Date(2017, 11, 12, 15, 0, 0, 0);
27-
const d1600 = new Date(2017, 11, 12, 16, 0, 0, 0);
28-
const d1700 = new Date(2017, 11, 12, 17, 0, 0, 0);
29-
const d1800 = new Date(2017, 11, 12, 18, 0, 0, 0);
30-
const d1900 = new Date(2017, 11, 12, 19, 0, 0, 0);
31-
const d2000 = new Date(2017, 11, 12, 20, 0, 0, 0);
32-
const d2100 = new Date(2017, 11, 12, 21, 0, 0, 0);
33-
const d2200 = new Date(2017, 11, 12, 22, 0, 0, 0);
34-
const d2300 = new Date(2017, 11, 12, 23, 0, 0, 0);
11+
// Each expected dayPeriod value must be a) contiguous, and
12+
// b) represented in sequence.
13+
var expectedDayPeriods = [
14+
'in the morning',
15+
'noon',
16+
'in the afternoon',
17+
'in the evening',
18+
'at night'
19+
];
3520

36-
const long = new Intl.DateTimeFormat('en', {
21+
const formatter = new Intl.DateTimeFormat('en', {
3722
dayPeriod: 'long'
3823
});
39-
40-
assert.sameValue(long.format(d0000), 'at night', '00:00, long format');
41-
assert.sameValue(long.format(d0100), 'at night', '01:00, long format');
42-
assert.sameValue(long.format(d0200), 'at night', '02:00, long format');
43-
assert.sameValue(long.format(d0300), 'at night', '03:00, long format');
44-
assert.sameValue(long.format(d0400), 'at night', '04:00, long format');
45-
assert.sameValue(long.format(d0500), 'at night', '05:00, long format');
46-
assert.sameValue(long.format(d0600), 'in the morning', '06:00, long format');
47-
assert.sameValue(long.format(d0700), 'in the morning', '07:00, long format');
48-
assert.sameValue(long.format(d0800), 'in the morning', '08:00, long format');
49-
assert.sameValue(long.format(d0900), 'in the morning', '09:00, long format');
50-
assert.sameValue(long.format(d1000), 'in the morning', '10:00, long format');
51-
assert.sameValue(long.format(d1100), 'in the morning', '11:00, long format');
52-
assert.sameValue(long.format(d1200), 'noon', '12:00, long format');
53-
assert.sameValue(long.format(d1300), 'in the afternoon', '13:00, long format');
54-
assert.sameValue(long.format(d1400), 'in the afternoon', '14:00, long format');
55-
assert.sameValue(long.format(d1500), 'in the afternoon', '15:00, long format');
56-
assert.sameValue(long.format(d1600), 'in the afternoon', '16:00, long format');
57-
assert.sameValue(long.format(d1700), 'in the afternoon', '17:00, long format');
58-
assert.sameValue(long.format(d1800), 'in the evening', '18:00, long format');
59-
assert.sameValue(long.format(d1900), 'in the evening', '19:00, long format');
60-
assert.sameValue(long.format(d2000), 'in the evening', '20:00, long format');
61-
assert.sameValue(long.format(d2100), 'at night', '21:00, long format');
62-
assert.sameValue(long.format(d2200), 'at night', '22:00, long format');
63-
assert.sameValue(long.format(d2300), 'at night', '23:00, long format');
64-
65-
const longNumeric = new Intl.DateTimeFormat('en', {
24+
const numericFormatter = new Intl.DateTimeFormat('en', {
6625
dayPeriod: 'long',
6726
hour: 'numeric'
6827
});
6928

70-
assert.sameValue(longNumeric.format(d0000), '12 at night', '00:00, long-numeric');
71-
assert.sameValue(longNumeric.format(d0100), '1 at night', '01:00, long-numeric');
72-
assert.sameValue(longNumeric.format(d0200), '2 at night', '02:00, long-numeric');
73-
assert.sameValue(longNumeric.format(d0300), '3 at night', '03:00, long-numeric');
74-
assert.sameValue(longNumeric.format(d0400), '4 at night', '04:00, long-numeric');
75-
assert.sameValue(longNumeric.format(d0500), '5 at night', '05:00, long-numeric');
76-
assert.sameValue(longNumeric.format(d0600), '6 in the morning', '06:00, long-numeric');
77-
assert.sameValue(longNumeric.format(d0700), '7 in the morning', '07:00, long-numeric');
78-
assert.sameValue(longNumeric.format(d0800), '8 in the morning', '08:00, long-numeric');
79-
assert.sameValue(longNumeric.format(d0900), '9 in the morning', '09:00, long-numeric');
80-
assert.sameValue(longNumeric.format(d1000), '10 in the morning', '10:00, long-numeric');
81-
assert.sameValue(longNumeric.format(d1100), '11 in the morning', '11:00, long-numeric');
82-
assert.sameValue(longNumeric.format(d1200), '12 noon', '12:00, long-numeric');
83-
assert.sameValue(longNumeric.format(d1300), '1 in the afternoon', '13:00, long-numeric');
84-
assert.sameValue(longNumeric.format(d1400), '2 in the afternoon', '14:00, long-numeric');
85-
assert.sameValue(longNumeric.format(d1500), '3 in the afternoon', '15:00, long-numeric');
86-
assert.sameValue(longNumeric.format(d1600), '4 in the afternoon', '16:00, long-numeric');
87-
assert.sameValue(longNumeric.format(d1700), '5 in the afternoon', '17:00, long-numeric');
88-
assert.sameValue(longNumeric.format(d1800), '6 in the evening', '18:00, long-numeric');
89-
assert.sameValue(longNumeric.format(d1900), '7 in the evening', '19:00, long-numeric');
90-
assert.sameValue(longNumeric.format(d2000), '8 in the evening', '20:00, long-numeric');
91-
assert.sameValue(longNumeric.format(d2100), '9 at night', '21:00, long-numeric');
92-
assert.sameValue(longNumeric.format(d2200), '10 at night', '22:00, long-numeric');
93-
assert.sameValue(longNumeric.format(d2300), '11 at night', '23:00, long-numeric');
29+
var actualDayPeriods = [];
30+
for (var h = 0; h < 24; h++) {
31+
var input = new Date(2017, 11, 12, h, 0, 0, 0);
32+
var dayPeriod = formatter.format(input);
33+
34+
// Ensure any dayPeriod result is in the list of expected dayPeriods.
35+
assert.notSameValue(
36+
expectedDayPeriods.indexOf(dayPeriod), -1,
37+
'unexpected dayPeriod result: ' + dayPeriod);
38+
39+
actualDayPeriods.push(dayPeriod);
40+
41+
assert.sameValue(
42+
numericFormatter.format(input),
43+
// Hour "00" is represented as "12".
44+
((h % 12) || 12) + ' ' + dayPeriod,
45+
'numeric hour must precede dayPeriod');
46+
}
47+
48+
assert.sameValue(actualDayPeriods.length, 24);
49+
50+
// Ensure all expected dayPeriods have occurred.
51+
for (var index = 0; index < expectedDayPeriods.length; index++) {
52+
assert.notSameValue(actualDayPeriods.indexOf(expectedDayPeriods[index]), -1,
53+
'missed dayPeriod result: ' + expectedDayPeriods[index]);
54+
}
55+
56+
// Ensure all transitions are valid.
57+
for (var h = 1; h < 24; h++) {
58+
if (actualDayPeriods[h] !== actualDayPeriods[h - 1]) {
59+
var currentIndex = expectedDayPeriods.indexOf(actualDayPeriods[h]);
60+
var previousIndex = expectedDayPeriods.indexOf(actualDayPeriods[h - 1]);
61+
62+
// The previous-index plus one matches the current index, possibly by
63+
// wrapping around.
64+
assert.sameValue(
65+
currentIndex,
66+
(previousIndex + 1) % expectedDayPeriods.length
67+
);
68+
}
69+
}

test/intl402/DateTimeFormat/prototype/format/dayPeriod-narrow-en.js

+52-76
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,62 @@ features: [Intl.DateTimeFormat-dayPeriod]
88
locale: [en]
99
---*/
1010

11-
const d0000 = new Date(2017, 11, 12, 0, 0, 0, 0);
12-
const d0100 = new Date(2017, 11, 12, 1, 0, 0, 0);
13-
const d0200 = new Date(2017, 11, 12, 2, 0, 0, 0);
14-
const d0300 = new Date(2017, 11, 12, 3, 0, 0, 0);
15-
const d0400 = new Date(2017, 11, 12, 4, 0, 0, 0);
16-
const d0500 = new Date(2017, 11, 12, 5, 0, 0, 0);
17-
const d0600 = new Date(2017, 11, 12, 6, 0, 0, 0);
18-
const d0700 = new Date(2017, 11, 12, 7, 0, 0, 0);
19-
const d0800 = new Date(2017, 11, 12, 8, 0, 0, 0);
20-
const d0900 = new Date(2017, 11, 12, 9, 0, 0, 0);
21-
const d1000 = new Date(2017, 11, 12, 10, 0, 0, 0);
22-
const d1100 = new Date(2017, 11, 12, 11, 0, 0, 0);
23-
const d1200 = new Date(2017, 11, 12, 12, 0, 0, 0);
24-
const d1300 = new Date(2017, 11, 12, 13, 0, 0, 0);
25-
const d1400 = new Date(2017, 11, 12, 14, 0, 0, 0);
26-
const d1500 = new Date(2017, 11, 12, 15, 0, 0, 0);
27-
const d1600 = new Date(2017, 11, 12, 16, 0, 0, 0);
28-
const d1700 = new Date(2017, 11, 12, 17, 0, 0, 0);
29-
const d1800 = new Date(2017, 11, 12, 18, 0, 0, 0);
30-
const d1900 = new Date(2017, 11, 12, 19, 0, 0, 0);
31-
const d2000 = new Date(2017, 11, 12, 20, 0, 0, 0);
32-
const d2100 = new Date(2017, 11, 12, 21, 0, 0, 0);
33-
const d2200 = new Date(2017, 11, 12, 22, 0, 0, 0);
34-
const d2300 = new Date(2017, 11, 12, 23, 0, 0, 0);
11+
// Each expected dayPeriod value must be a) contiguous, and
12+
// b) represented in sequence.
13+
var expectedDayPeriods = [
14+
'in the morning',
15+
'n',
16+
'in the afternoon',
17+
'in the evening',
18+
'at night'
19+
];
3520

36-
const narrow = new Intl.DateTimeFormat('en', {
21+
const formatter = new Intl.DateTimeFormat('en', {
3722
dayPeriod: 'narrow'
3823
});
39-
40-
assert.sameValue(narrow.format(d0000), 'at night', '00:00, narrow format');
41-
assert.sameValue(narrow.format(d0100), 'at night', '01:00, narrow format');
42-
assert.sameValue(narrow.format(d0200), 'at night', '02:00, narrow format');
43-
assert.sameValue(narrow.format(d0300), 'at night', '03:00, narrow format');
44-
assert.sameValue(narrow.format(d0400), 'at night', '04:00, narrow format');
45-
assert.sameValue(narrow.format(d0500), 'at night', '05:00, narrow format');
46-
assert.sameValue(narrow.format(d0600), 'in the morning', '06:00, narrow format');
47-
assert.sameValue(narrow.format(d0700), 'in the morning', '07:00, narrow format');
48-
assert.sameValue(narrow.format(d0800), 'in the morning', '08:00, narrow format');
49-
assert.sameValue(narrow.format(d0900), 'in the morning', '09:00, narrow format');
50-
assert.sameValue(narrow.format(d1000), 'in the morning', '10:00, narrow format');
51-
assert.sameValue(narrow.format(d1100), 'in the morning', '11:00, narrow format');
52-
assert.sameValue(narrow.format(d1200), 'n', '12:00, narrow format');
53-
assert.sameValue(narrow.format(d1300), 'in the afternoon', '13:00, narrow format');
54-
assert.sameValue(narrow.format(d1400), 'in the afternoon', '14:00, narrow format');
55-
assert.sameValue(narrow.format(d1500), 'in the afternoon', '15:00, narrow format');
56-
assert.sameValue(narrow.format(d1600), 'in the afternoon', '16:00, narrow format');
57-
assert.sameValue(narrow.format(d1700), 'in the afternoon', '17:00, narrow format');
58-
assert.sameValue(narrow.format(d1800), 'in the evening', '18:00, narrow format');
59-
assert.sameValue(narrow.format(d1900), 'in the evening', '19:00, narrow format');
60-
assert.sameValue(narrow.format(d2000), 'in the evening', '20:00, narrow format');
61-
assert.sameValue(narrow.format(d2100), 'at night', '21:00, narrow format');
62-
assert.sameValue(narrow.format(d2200), 'at night', '22:00, narrow format');
63-
assert.sameValue(narrow.format(d2300), 'at night', '23:00, narrow format');
64-
65-
const narrowNumeric = new Intl.DateTimeFormat('en', {
24+
const numericFormatter = new Intl.DateTimeFormat('en', {
6625
dayPeriod: 'narrow',
6726
hour: 'numeric'
6827
});
6928

70-
assert.sameValue(narrowNumeric.format(d0000), '12 at night', '00:00, narrow-numeric');
71-
assert.sameValue(narrowNumeric.format(d0100), '1 at night', '01:00, narrow-numeric');
72-
assert.sameValue(narrowNumeric.format(d0200), '2 at night', '02:00, narrow-numeric');
73-
assert.sameValue(narrowNumeric.format(d0300), '3 at night', '03:00, narrow-numeric');
74-
assert.sameValue(narrowNumeric.format(d0400), '4 at night', '04:00, narrow-numeric');
75-
assert.sameValue(narrowNumeric.format(d0500), '5 at night', '05:00, narrow-numeric');
76-
assert.sameValue(narrowNumeric.format(d0600), '6 in the morning', '06:00, narrow-numeric');
77-
assert.sameValue(narrowNumeric.format(d0700), '7 in the morning', '07:00, narrow-numeric');
78-
assert.sameValue(narrowNumeric.format(d0800), '8 in the morning', '08:00, narrow-numeric');
79-
assert.sameValue(narrowNumeric.format(d0900), '9 in the morning', '09:00, narrow-numeric');
80-
assert.sameValue(narrowNumeric.format(d1000), '10 in the morning', '10:00, narrow-numeric');
81-
assert.sameValue(narrowNumeric.format(d1100), '11 in the morning', '11:00, narrow-numeric');
82-
assert.sameValue(narrowNumeric.format(d1200), '12 n', '12:00, narrow-numeric');
83-
assert.sameValue(narrowNumeric.format(d1300), '1 in the afternoon', '13:00, narrow-numeric');
84-
assert.sameValue(narrowNumeric.format(d1400), '2 in the afternoon', '14:00, narrow-numeric');
85-
assert.sameValue(narrowNumeric.format(d1500), '3 in the afternoon', '15:00, narrow-numeric');
86-
assert.sameValue(narrowNumeric.format(d1600), '4 in the afternoon', '16:00, narrow-numeric');
87-
assert.sameValue(narrowNumeric.format(d1700), '5 in the afternoon', '17:00, narrow-numeric');
88-
assert.sameValue(narrowNumeric.format(d1800), '6 in the evening', '18:00, narrow-numeric');
89-
assert.sameValue(narrowNumeric.format(d1900), '7 in the evening', '19:00, narrow-numeric');
90-
assert.sameValue(narrowNumeric.format(d2000), '8 in the evening', '20:00, narrow-numeric');
91-
assert.sameValue(narrowNumeric.format(d2100), '9 at night', '21:00, narrow-numeric');
92-
assert.sameValue(narrowNumeric.format(d2200), '10 at night', '22:00, narrow-numeric');
93-
assert.sameValue(narrowNumeric.format(d2300), '11 at night', '23:00, narrow-numeric');
29+
var actualDayPeriods = [];
30+
for (var h = 0; h < 24; h++) {
31+
var input = new Date(2017, 11, 12, h, 0, 0, 0);
32+
var dayPeriod = formatter.format(input);
33+
34+
// Ensure any dayPeriod result is in the list of expected dayPeriods.
35+
assert.notSameValue(
36+
expectedDayPeriods.indexOf(dayPeriod), -1,
37+
'unexpected dayPeriod result: ' + dayPeriod);
38+
39+
actualDayPeriods.push(dayPeriod);
40+
41+
assert.sameValue(
42+
numericFormatter.format(input),
43+
// Hour "00" is represented as "12".
44+
((h % 12) || 12) + ' ' + dayPeriod,
45+
'numeric hour must precede dayPeriod');
46+
}
47+
48+
assert.sameValue(actualDayPeriods.length, 24);
49+
50+
// Ensure all expected dayPeriods have occurred.
51+
for (var index = 0; index < expectedDayPeriods.length; index++) {
52+
assert.notSameValue(actualDayPeriods.indexOf(expectedDayPeriods[index]), -1,
53+
'missed dayPeriod result: ' + expectedDayPeriods[index]);
54+
}
55+
56+
// Ensure all transitions are valid.
57+
for (var h = 1; h < 24; h++) {
58+
if (actualDayPeriods[h] !== actualDayPeriods[h - 1]) {
59+
var currentIndex = expectedDayPeriods.indexOf(actualDayPeriods[h]);
60+
var previousIndex = expectedDayPeriods.indexOf(actualDayPeriods[h - 1]);
61+
62+
// The previous-index plus one matches the current index, possibly by
63+
// wrapping around.
64+
assert.sameValue(
65+
currentIndex,
66+
(previousIndex + 1) % expectedDayPeriods.length
67+
);
68+
}
69+
}

0 commit comments

Comments
 (0)