Adding tests to cover 'fractionalSecondDigits' on DateTimeFormat.prototype.formatRangeToParts.

This commit is contained in:
Caio Lima 2020-01-27 12:45:06 -03:00 committed by Rick Waldron
parent 72c4433687
commit 1337f11e21
1 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,134 @@
// Copyright 2020 Google Inc, Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: Checks basic handling of fractionalSecondDigits.
features: [Intl.DateTimeFormat-fractionalSecondDigits, Intl.DateTimeFormat-formatRange]
locale: [en-US]
---*/
function compare(actual, expected) {
for (const [i, actualEntry, expectedEntry] of zip(actual, expected)) {
assert.sameValue(actualEntry.type, expectedEntry.type, `type for entry ${i}`);
assert.sameValue(actualEntry.value, expectedEntry.value, `value for entry ${i}`);
assert.sameValue(actualEntry.source, expectedEntry.source, `source for entry ${i}`);
}
}
const d1 = new Date(2019, 7, 10, 1, 2, 3, 234);
const d2 = new Date(2019, 7, 10, 1, 2, 3, 567);
const d3 = new Date(2019, 7, 10, 1, 2, 13, 987);
let dtf = new Intl.DateTimeFormat(
'en', { minute: "numeric", second: "numeric", fractionalSecondDigits: 0});
compare(dtf.formatRangeToParts(d1, d2), [
{ type: "minute", value: "02", source: "shared" },
{ type: "literal", value: ":", source: "shared" },
{ type: "second", value: "03", source: "shared" }
]);
compare(dtf.formatRangeToParts(d1, d3), [
{ type: "minute", value: "02", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
{ type: "second", value: "03", source: "startRange" },
{ type: "literal", value: " - ", source: "shared" },
{ type: "minute", value: "02", source: "endRange" },
{ type: "literal", value: ":", source: "endRange" },
{ type: "second", value: "13", source: "endRange" }
]);
dtf = new Intl.DateTimeFormat(
'en', { minute: "numeric", second: "numeric", fractionalSecondDigits: 1});
compare(dtf.formatRangeToParts(d1, d2), [
{ type: "minute", value: "02", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
{ type: "second", value: "03", source: "startRange" },
{ type: "literal", value: ".", source: "startRange" },
{ type: "fractionalSecond", value: "2", source: "startRange" },
{ type: "literal", value: " - ", source: "shared" },
{ type: "minute", value: "02", source: "endRange" },
{ type: "literal", value: ":", source: "endRange" },
{ type: "second", value: "03", source: "endRange" },
{ type: "literal", value: ".", source: "endRange" },
{ type: "fractionalSecond", value: "5", source: "endRange" }
]);
compare(dtf.formatRangeToParts(d1, d3), [
{ type: "minute", value: "02", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
{ type: "second", value: "03", source: "startRange" },
{ type: "literal", value: ".", source: "startRange" },
{ type: "fractionalSecond", value: "2", source: "startRange" },
{ type: "literal", value: " - ", source: "shared" },
{ type: "minute", value: "02", source: "endRange" },
{ type: "literal", value: ":", source: "endRange" },
{ type: "second", value: "13", source: "endRange" },
{ type: "literal", value: ".", source: "endRange" },
{ type: "fractionalSecond", value: "9", source: "endRange" }
]);
dtf = new Intl.DateTimeFormat(
'en', { minute: "numeric", second: "numeric", fractionalSecondDigits: 2});
compare(dtf.formatRangeToParts(d1, d2), [
{ type: "minute", value: "02", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
{ type: "second", value: "03", source: "startRange" },
{ type: "literal", value: ".", source: "startRange" },
{ type: "fractionalSecond", value: "23", source: "startRange" },
{ type: "literal", value: " - ", source: "shared" },
{ type: "minute", value: "02", source: "endRange" },
{ type: "literal", value: ":", source: "endRange" },
{ type: "second", value: "03", source: "endRange" },
{ type: "literal", value: ".", source: "endRange" },
{ type: "fractionalSecond", value: "56", source: "endRange" }
]);
compare(dtf.formatRangeToParts(d1, d3), [
{ type: "minute", value: "02", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
{ type: "second", value: "03", source: "startRange" },
{ type: "literal", value: ".", source: "startRange" },
{ type: "fractionalSecond", value: "23", source: "startRange" },
{ type: "literal", value: " - ", source: "shared" },
{ type: "minute", value: "02", source: "endRange" },
{ type: "literal", value: ":", source: "endRange" },
{ type: "second", value: "13", source: "endRange" },
{ type: "literal", value: ".", source: "endRange" },
{ type: "fractionalSecond", value: "98", source: "endRange" }
]);
dtf = new Intl.DateTimeFormat(
'en', { minute: "numeric", second: "numeric", fractionalSecondDigits: 3});
compare(dtf.formatRangeToParts(d1, d2), [
{ type: "minute", value: "02", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
{ type: "second", value: "03", source: "startRange" },
{ type: "literal", value: ".", source: "startRange" },
{ type: "fractionalSecond", value: "234", source: "startRange" },
{ type: "literal", value: " - ", source: "shared" },
{ type: "minute", value: "02", source: "endRange" },
{ type: "literal", value: ":", source: "endRange" },
{ type: "second", value: "03", source: "endRange" },
{ type: "literal", value: ".", source: "endRange" },
{ type: "fractionalSecond", value: "567", source: "endRange" }
]);
compare(dtf.formatRangeToParts(d1, d3), [
{ type: "minute", value: "02", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
{ type: "second", value: "03", source: "startRange" },
{ type: "literal", value: ".", source: "startRange" },
{ type: "fractionalSecond", value: "234", source: "startRange" },
{ type: "literal", value: " - ", source: "shared" },
{ type: "minute", value: "02", source: "endRange" },
{ type: "literal", value: ":", source: "endRange" },
{ type: "second", value: "13", source: "endRange" },
{ type: "literal", value: ".", source: "endRange" },
{ type: "fractionalSecond", value: "987", source: "endRange" }
]);