v8/test/intl/number-format/format-range-string.js
Frank Tang fe53fc04d2 [intl] Implement NumberFormat v8 string input handling
Design Document:
https://docs.google.com/document/d/14zxGub6Os6nARzH6XstOZX05w2537sZo_ZSSlGjGpBM/edit#


Bug: v8:10776
Change-Id: I506eadcf70f6855a79601a7cb1ce47849cfc066a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3594118
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81168}
2022-06-15 04:23:14 +00:00

117 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2022 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-number-format-v3
// Test the throw in formatRange
let df = new Intl.NumberFormat();
// https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/diff.html#sec-partitionnumberrangepattern
// 2. If x is not-a-number or y is not-a-number, throw a RangeError exception.
assertThrows(() => { df.formatRange("xyz", "123") }, RangeError);
assertThrows(() => { df.formatRange("123", "xyz") }, RangeError);
assertThrows(() => { df.formatRange("1", "-0b1111") }, RangeError);
assertThrows(() => { df.formatRange("1", "-0o7654") }, RangeError);
assertThrows(() => { df.formatRange("1", "-0xabcde") }, RangeError);
// 2. If x is a mathematical value, then
// 2a. If y is a mathematical value and y < x, throw a RangeError exception.
assertThrows(() => { df.formatRange(
" +1234567890123456789012345678901234567890123456789012345678901 ",
" +123456789012345678901234567890123456789012345678901234567890 ") }, RangeError);
assertThrows(() => { df.formatRange(
" +123456789012345678901234567890.123456789012345678901234567890e25 ",
" +12345678901234567890.1234567890123456789012345678901234567890e25 ") }, RangeError);
assertThrows(() => { df.formatRange(
" +12345678901234567890.1234567890123456789012345678901234567890e35 ",
" +123456789012345678901234567890.123456789012345678901234567890e24 ") }, RangeError);
assertThrows(() => { df.formatRange(
" -123456789012345678901234567890123456789012345678901234567890 ",
" -1234567890123456789012345678901234567890123456789012345678901 ") }, RangeError);
assertThrows(() => { df.formatRange(
" -12345678901234567890.1234567890123456789012345678901234567890e25 ",
" -123456789012345678901234567890.123456789012345678901234567890e25 ") }, RangeError);
assertThrows(() => { df.formatRange(
" -123456789012345678901234567890.123456789012345678901234567890e24 ",
" -12345678901234567890.1234567890123456789012345678901234567890e35 ") }, RangeError);
assertThrows(() => { df.formatRange(
" +.1234567890123456789012345678901234567890123456789012345678901 ",
" +.123456789012345678901234567890123456789012345678901234567890 ") }, RangeError);
assertThrows(() => { df.formatRange(
" +.123456789012345678901234567890123456789012345678901234567890 ",
" -.1234567890123456789012345678901234567890123456789012345678901 ") }, RangeError);
assertThrows(() => { df.formatRange(
" +.12e3 ", " +.12e2 ") }, RangeError);
assertThrows(() => { df.formatRange(
" +123 ", " +.12e2 ") }, RangeError);
assertThrows(() => { df.formatRange(
" -123 ", " -.12e4 ") }, RangeError);
// 2b. Else if y is negative-infinity, throw a RangeError exception.
assertThrows(() => { df.formatRange( " 123 ", " -Infinity ") }, RangeError);
// 2c. Else if y is negative-zero and x ≥ 0, throw a RangeError exception.
assertThrows(() => { df.formatRange( " 123 ", " -0 ") }, RangeError);
// other case which won't throw under 2
assertDoesNotThrow(() => { df.formatRange( " 123 ", " Infinity ") })
assertEquals("123∞", df.formatRange( " 123 ", " Infinity "));
assertDoesNotThrow(() => { df.formatRange(
" +.123456789012345678901234567890123456789012345678901234567890 ", " Infinity ") })
assertEquals("0.123–∞", df.formatRange(
" +.123456789012345678901234567890123456789012345678901234567890 ",
" Infinity "));
assertDoesNotThrow(() => { df.formatRange(
" +.123456789012345678901234567890123456789012345678901234567890 ",
" +.123456789012345678901234567890123456789012345678901234567890 ")})
assertDoesNotThrow(() => { df.formatRange(
" +.123456789012345678901234567890123456789012345678901234567890 ",
" +.1234567890123456789012345678901234567890123456789012345678901 ")})
assertDoesNotThrow(() => { df.formatRange(
" +12345678901234567890.123456789012345678901234567890123456789000000001e20 ",
" +1234567890.12345678901234567890123456789012345678901234567890e31 ")})
// 3. Else if x is positive-infinity, then
// 3a. If y is a mathematical value, throw a RangeError exception.
assertThrows(() => { df.formatRange( " Infinity ", " 123 ") }, RangeError);
assertThrows(() => { df.formatRange( " +Infinity ", " 123 ") }, RangeError);
// 3b. Else if y is negative-infinity, throw a RangeError exception.
assertThrows(() => { df.formatRange( " Infinity ", " -Infinity ") }, RangeError);
assertThrows(() => { df.formatRange( " +Infinity ", " -Infinity ") }, RangeError);
// 3c. Else if y is negative-zero, throw a RangeError exception.
assertThrows(() => { df.formatRange( " Infinity ", " -0 ") }, RangeError);
assertThrows(() => { df.formatRange( " +Infinity ", " -0 ") }, RangeError);
// other case which won't throw under 3
assertDoesNotThrow(() => { df.formatRange( " Infinity ", " Infinity ") })
assertEquals("~∞", df.formatRange(" Infinity ", " Infinity "));
// 4. Else if x is negative-zero, then
// 4a. If y is a mathematical value and y < 0, throw a RangeError exception.
assertThrows(() => { df.formatRange( " -0 ", " -1e-30 ") }, RangeError);
assertThrows(() => { df.formatRange( " -0.000e200 ", " -1e-30 ") }, RangeError);
// 4b. Else if y is negative-infinity, throw a RangeError exception.
assertThrows(() => { df.formatRange( " -0 ", " -Infinity ") }, RangeError);
// other case which won't throw under 4
assertDoesNotThrow(() => { df.formatRange( " -0 ", " Infinity ") })
assertEquals("-0 ∞", df.formatRange(" -0 ", " Infinity "));
assertDoesNotThrow(() => { df.formatRange( " -0 ", " -0 ") })
assertDoesNotThrow(() => { df.formatRange( " -0 ", " 12345 ") })
assertDoesNotThrow(() => { df.formatRange( " -0 ", " 12345e-30 ") })
assertEquals("-0 0", df.formatRange(" -0 ", " 12345e-30 "));
assertDoesNotThrow(() => { df.formatRange( " -0 ", " .12345e-30 ") })
assertDoesNotThrow(() => { df.formatRange( " -0 ", " .12345e34 ") })
assertEquals("-0 12,345,000,000,000,000,000,000,000,000,000",
df.formatRange(" -0 ", " .12345e32 "));
// other cases which won't throw not under 2-4
assertDoesNotThrow(() => { df.formatRange( " -Infinity ", " -Infinity ") })
assertEquals("~-∞", df.formatRange(" -Infinity ", " -Infinity "));
assertDoesNotThrow(() => { df.formatRange( " -Infinity ", " -3e20 ") })
assertDoesNotThrow(() => { df.formatRange( " -Infinity ", " -3e20 ") })
assertDoesNotThrow(() => { df.formatRange( " -Infinity ", " -0 ") })
assertDoesNotThrow(() => { df.formatRange( " -Infinity ", " 0 ") })
assertDoesNotThrow(() => { df.formatRange( " -Infinity ", " .3e20 ") })
assertDoesNotThrow(() => { df.formatRange( " -Infinity ", " Infinity ") })
assertEquals("-∞ ∞", df.formatRange(" -Infinity ", " Infinity "));