From fa065e74d783ddf97d3505719f30462b079256b6 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 12 Nov 2024 14:20:11 +0100 Subject: [PATCH 1/5] fix `NumberFormat.resolvedOptions` --- .../Intl/java/com/facebook/hermes/intl/NumberFormat.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Platform/Intl/java/com/facebook/hermes/intl/NumberFormat.java b/lib/Platform/Intl/java/com/facebook/hermes/intl/NumberFormat.java index c83ada557fc..4433c02ab1b 100644 --- a/lib/Platform/Intl/java/com/facebook/hermes/intl/NumberFormat.java +++ b/lib/Platform/Intl/java/com/facebook/hermes/intl/NumberFormat.java @@ -626,10 +626,10 @@ public Map resolvedOptions() throws JSRangeErrorException { if (mRoundingType == IPlatformNumberFormatter.RoundingType.SIGNIFICANT_DIGITS) { if (mResolvedMaximumSignificantDigits != -1) - finalResolvedOptions.put("minimumSignificantDigits", mResolvedMaximumSignificantDigits); + finalResolvedOptions.put("maximumSignificantDigits", mResolvedMaximumSignificantDigits); if (mResolvedMinimumSignificantDigits != -1) - finalResolvedOptions.put("maximumSignificantDigits", mResolvedMinimumSignificantDigits); + finalResolvedOptions.put("minimumSignificantDigits", mResolvedMinimumSignificantDigits); } else if (mRoundingType == IPlatformNumberFormatter.RoundingType.FRACTION_DIGITS) { From 9ff6f4953d5a796cab0ac66c24dde2d95c886233 Mon Sep 17 00:00:00 2001 From: Daniel Lindenkreuz Date: Tue, 12 Nov 2024 14:32:11 +0100 Subject: [PATCH 2/5] add test for resolved options / significant digits --- .../intl/number-format-significant-digits.js | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/hermes/intl/number-format-significant-digits.js diff --git a/test/hermes/intl/number-format-significant-digits.js b/test/hermes/intl/number-format-significant-digits.js new file mode 100644 index 00000000000..a53e2f5d068 --- /dev/null +++ b/test/hermes/intl/number-format-significant-digits.js @@ -0,0 +1,65 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// RUN: %hermes %s +// REQUIRES: intl + +function assert(pred, str) { + if (!pred) { + throw new Error('assertion failed' + (str === undefined ? '' : (': ' + str))); + } +} + +let resolvedOptions; + +resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD' }).resolvedOptions(); +assert(resolvedOptions.minimumSignificantDigits === undefined); +assert(resolvedOptions.maximumSignificantDigits === undefined); + +// +// Validate minimumSignificantDigits logic +// +try { new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: -1 }) } +catch (e) { assert(e.message.includes('minimumSignificantDigits value is invalid.')) } + +try { new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 22 }) } +catch (e) { assert(e.message.includes('minimumSignificantDigits value is invalid.')) } + +resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 1 }).resolvedOptions(); +assert(resolvedOptions.minimumSignificantDigits === 1); +assert(resolvedOptions.maximumSignificantDigits === undefined); + +resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 21 }).resolvedOptions(); +assert(resolvedOptions.minimumSignificantDigits === 21); +assert(resolvedOptions.maximumSignificantDigits === undefined); + +// +// Validate maximumSignificantDigits logic +// +try { new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximumSignificantDigits: -1 }) } +catch (e) { assert(e.message.includes('maximumSignificantDigits value is invalid.')) } + +try { new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximumSignificantDigits: 22 }) } +catch (e) { assert(e.message.includes('maximumSignificantDigits value is invalid.')) } + +resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximumSignificantDigits: 1 }).resolvedOptions(); +assert(resolvedOptions.minimumSignificantDigits === undefined); +assert(resolvedOptions.maximumSignificantDigits === 1); + +resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximumSignificantDigits: 21 }).resolvedOptions(); +assert(resolvedOptions.minimumSignificantDigits === undefined); +assert(resolvedOptions.maximumSignificantDigits === 21); + +// +// Validate when both are set +// +try { new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 5, maximumSignificantDigits: 2 }) } +catch (e) { assert(e.message.includes('minimumSignificantDigits is greater than maximumSignificantDigits')) } + +resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 3, maximumSignificantDigits: 5 }).resolvedOptions(); +assert(resolvedOptions.minimumSignificantDigits === 3); +assert(resolvedOptions.maximumSignificantDigits === 5); From 5701fd951eb4fd17c30f72e23c101f2c09886e1d Mon Sep 17 00:00:00 2001 From: Daniel Lindenkreuz Date: Tue, 12 Nov 2024 14:38:34 +0100 Subject: [PATCH 3/5] actually run test --- .../facebook/hermes/test/HermesIntlAndroidTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java b/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java index 03637ba89e9..8b1a89bc0ae 100644 --- a/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +++ b/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java @@ -48,6 +48,18 @@ public void testNumberFormatFractionDigitsFromAsset() throws IOException { } } + @Test + public void testNumberFormatSignificantDigitsFromAsset() throws IOException { + AssetManager assets = + InstrumentationRegistry.getInstrumentation().getTargetContext().getAssets(); + InputStream is = assets.open("number-format-significant-digits.js"); + String script = + new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n")); + try (JSRuntime rt = JSRuntime.makeHermesRuntime()) { + rt.evaluateJavaScript(script); + } + } + @Test public void testDateTimeFormat() { try (JSRuntime rt = JSRuntime.makeHermesRuntime()) { From dc569ffbf80ac24835b632fcb7c25565c7d6166a Mon Sep 17 00:00:00 2001 From: Daniel Lindenkreuz Date: Wed, 13 Nov 2024 23:31:02 +0100 Subject: [PATCH 4/5] fix tests --- test/hermes/intl/number-format-significant-digits.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/hermes/intl/number-format-significant-digits.js b/test/hermes/intl/number-format-significant-digits.js index a53e2f5d068..81b28e168ef 100644 --- a/test/hermes/intl/number-format-significant-digits.js +++ b/test/hermes/intl/number-format-significant-digits.js @@ -31,11 +31,11 @@ catch (e) { assert(e.message.includes('minimumSignificantDigits value is invalid resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 1 }).resolvedOptions(); assert(resolvedOptions.minimumSignificantDigits === 1); -assert(resolvedOptions.maximumSignificantDigits === undefined); +assert(resolvedOptions.maximumSignificantDigits === 21); resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 21 }).resolvedOptions(); assert(resolvedOptions.minimumSignificantDigits === 21); -assert(resolvedOptions.maximumSignificantDigits === undefined); +assert(resolvedOptions.maximumSignificantDigits === 21); // // Validate maximumSignificantDigits logic @@ -47,11 +47,11 @@ try { new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximu catch (e) { assert(e.message.includes('maximumSignificantDigits value is invalid.')) } resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximumSignificantDigits: 1 }).resolvedOptions(); -assert(resolvedOptions.minimumSignificantDigits === undefined); +assert(resolvedOptions.minimumSignificantDigits === 1); assert(resolvedOptions.maximumSignificantDigits === 1); resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximumSignificantDigits: 21 }).resolvedOptions(); -assert(resolvedOptions.minimumSignificantDigits === undefined); +assert(resolvedOptions.minimumSignificantDigits === 1); assert(resolvedOptions.maximumSignificantDigits === 21); // From b7516ff0b7b2acae20c45b3a2bb79e931af70371 Mon Sep 17 00:00:00 2001 From: Daniel Lindenkreuz Date: Wed, 13 Nov 2024 23:36:57 +0100 Subject: [PATCH 5/5] fix error assertion --- test/hermes/intl/number-format-significant-digits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hermes/intl/number-format-significant-digits.js b/test/hermes/intl/number-format-significant-digits.js index 81b28e168ef..bf90124094c 100644 --- a/test/hermes/intl/number-format-significant-digits.js +++ b/test/hermes/intl/number-format-significant-digits.js @@ -58,7 +58,7 @@ assert(resolvedOptions.maximumSignificantDigits === 21); // Validate when both are set // try { new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 5, maximumSignificantDigits: 2 }) } -catch (e) { assert(e.message.includes('minimumSignificantDigits is greater than maximumSignificantDigits')) } +catch (e) { assert(e.message.includes('maximumSignificantDigits value is invalid.')) } resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 3, maximumSignificantDigits: 5 }).resolvedOptions(); assert(resolvedOptions.minimumSignificantDigits === 3);