v8/test/intl/number-format/unified/style-unit.js
Frank Tang 5efc4d0b74 [Intl] Intl.NumberFormat Unified API Proposal
Design Doc: https://goo.gl/ZAtL1f

Bug: v8:8515
Change-Id: I543ab704fd3f8b41e396879ebbc581977ec0ff10
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1612325
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61709}
2019-05-21 17:23:21 +00:00

181 lines
6.4 KiB
JavaScript

// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-intl-numberformat-unified
// Test default.
let nf = new Intl.NumberFormat();
assertEquals(undefined, nf.resolvedOptions().unit);
nf = new Intl.NumberFormat("en");
assertEquals(undefined, nf.resolvedOptions().unit);
nf = new Intl.NumberFormat("en", {style: 'decimal'});
assertEquals(undefined, nf.resolvedOptions().unit);
nf = new Intl.NumberFormat("en", {style: 'currency', currency: 'TWD'});
assertEquals(undefined, nf.resolvedOptions().unit);
nf = new Intl.NumberFormat("en", {style: 'percent'});
assertEquals('percent', nf.resolvedOptions().unit);
assertThrows(() => new Intl.NumberFormat("en", {style: 'unit'}), TypeError);
const validUnits = [
'acre',
'bit',
'byte',
'celsius',
'centimeter',
'day',
'degree',
'fahrenheit',
'foot',
'gigabit',
'gigabyte',
'gram',
'hectare',
'hour',
'inch',
'kilobit',
'kilobyte',
'kilogram',
'kilometer',
'megabit',
'megabyte',
'meter',
'mile-scandinavian',
'mile',
'millimeter',
'millisecond',
'minute',
'month',
'ounce',
'petabyte',
'pound',
'second',
'stone',
'terabit',
'terabyte',
'week',
'yard',
'year',
'percent',
'kilometer-per-hour',
'mile-per-hour',
'meter-per-second',
'yard-per-second',
'yard-per-hour',
];
for (const unit of validUnits) {
let resolved = new Intl.NumberFormat(
"en", {style: 'unit', unit}).resolvedOptions();
assertEquals('unit', resolved.style);
assertEquals(resolved.unit, unit);
}
function c(u) {
return new Intl.NumberFormat('en', { style: 'unit', unit: u});
}
assertThrows(() => c('acre-foot'), RangeError);
assertThrows(() => c('ampere'), RangeError);
assertThrows(() => c('arc-minute'), RangeError);
assertThrows(() => c('arc-second'), RangeError);
assertThrows(() => c('astronomical-unit'), RangeError);
assertThrows(() => c('bushel'), RangeError);
assertThrows(() => c('calorie'), RangeError);
assertThrows(() => c('carat'), RangeError);
assertThrows(() => c('centiliter'), RangeError);
assertThrows(() => c('century'), RangeError);
assertThrows(() => c('cubic-centimeter'), RangeError);
assertThrows(() => c('cubic-foot'), RangeError);
assertThrows(() => c('cubic-inch'), RangeError);
assertThrows(() => c('cubic-kilometer'), RangeError);
assertThrows(() => c('cubic-meter'), RangeError);
assertThrows(() => c('cubic-mile'), RangeError);
assertThrows(() => c('cubic-yard'), RangeError);
assertThrows(() => c('cup-metric'), RangeError);
assertThrows(() => c('cup'), RangeError);
assertThrows(() => c('day-person'), RangeError);
assertThrows(() => c('deciliter'), RangeError);
assertThrows(() => c('decimeter'), RangeError);
assertThrows(() => c('fathom'), RangeError);
assertThrows(() => c('fluid-ounce'), RangeError);
assertThrows(() => c('foodcalorie'), RangeError);
assertThrows(() => c('furlong'), RangeError);
assertThrows(() => c('g-force'), RangeError);
assertThrows(() => c('gallon-imperial'), RangeError);
assertThrows(() => c('gallon'), RangeError);
assertThrows(() => c('generic'), RangeError);
assertThrows(() => c('gigahertz'), RangeError);
assertThrows(() => c('gigawatt'), RangeError);
assertThrows(() => c('hectoliter'), RangeError);
assertThrows(() => c('hectopascal'), RangeError);
assertThrows(() => c('hertz'), RangeError);
assertThrows(() => c('horsepower'), RangeError);
assertThrows(() => c('inch-hg'), RangeError);
assertThrows(() => c('joule'), RangeError);
assertThrows(() => c('karat'), RangeError);
assertThrows(() => c('kelvin'), RangeError);
assertThrows(() => c('kilocalorie'), RangeError);
assertThrows(() => c('kilohertz'), RangeError);
assertThrows(() => c('kilojoule'), RangeError);
assertThrows(() => c('kilowatt-hour'), RangeError);
assertThrows(() => c('kilowatt'), RangeError);
assertThrows(() => c('knot'), RangeError);
assertThrows(() => c('light-year'), RangeError);
assertThrows(() => c('liter-per-100kilometers'), RangeError);
assertThrows(() => c('liter-per-kilometer'), RangeError);
assertThrows(() => c('liter'), RangeError);
assertThrows(() => c('lux'), RangeError);
assertThrows(() => c('megahertz'), RangeError);
assertThrows(() => c('megaliter'), RangeError);
assertThrows(() => c('megawatt'), RangeError);
assertThrows(() => c('meter-per-second-squared'), RangeError);
assertThrows(() => c('metric-ton'), RangeError);
assertThrows(() => c('microgram'), RangeError);
assertThrows(() => c('micrometer'), RangeError);
assertThrows(() => c('microsecond'), RangeError);
assertThrows(() => c('mile-per-gallon-imperial'), RangeError);
assertThrows(() => c('mile-per-gallon'), RangeError);
assertThrows(() => c('milliampere'), RangeError);
assertThrows(() => c('millibar'), RangeError);
assertThrows(() => c('milligram-per-deciliter'), RangeError);
assertThrows(() => c('milligram'), RangeError);
assertThrows(() => c('milliliter'), RangeError);
assertThrows(() => c('millimeter-of-mercury'), RangeError);
assertThrows(() => c('millimole-per-liter'), RangeError);
assertThrows(() => c('milliwatt'), RangeError);
assertThrows(() => c('month-person'), RangeError);
assertThrows(() => c('nanometer'), RangeError);
assertThrows(() => c('nanosecond'), RangeError);
assertThrows(() => c('nautical-mile'), RangeError);
assertThrows(() => c('ohm'), RangeError);
assertThrows(() => c('ounce-troy'), RangeError);
assertThrows(() => c('parsec'), RangeError);
assertThrows(() => c('part-per-million'), RangeError);
assertThrows(() => c('picometer'), RangeError);
assertThrows(() => c('pint-metric'), RangeError);
assertThrows(() => c('pint'), RangeError);
assertThrows(() => c('pound-per-square-inch'), RangeError);
assertThrows(() => c('quart'), RangeError);
assertThrows(() => c('radian'), RangeError);
assertThrows(() => c('revolution'), RangeError);
assertThrows(() => c('square-centimeter'), RangeError);
assertThrows(() => c('square-foot'), RangeError);
assertThrows(() => c('square-inch'), RangeError);
assertThrows(() => c('square-kilometer'), RangeError);
assertThrows(() => c('square-meter'), RangeError);
assertThrows(() => c('square-mile'), RangeError);
assertThrows(() => c('square-yard'), RangeError);
assertThrows(() => c('tablespoon'), RangeError);
assertThrows(() => c('teaspoon'), RangeError);
assertThrows(() => c('ton'), RangeError);
assertThrows(() => c('volt'), RangeError);
assertThrows(() => c('watt'), RangeError);
assertThrows(() => c('week-person'), RangeError);
assertThrows(() => c('year-person'), RangeError);