Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NumberFormat.resolvedOptions on Android #1565

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,10 @@ public Map<String, Object> 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) {

Expand Down
65 changes: 65 additions & 0 deletions test/hermes/intl/number-format-significant-digits.js
Original file line number Diff line number Diff line change
@@ -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 === 21);

resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 21 }).resolvedOptions();
assert(resolvedOptions.minimumSignificantDigits === 21);
assert(resolvedOptions.maximumSignificantDigits === 21);

//
// 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 === 1);
assert(resolvedOptions.maximumSignificantDigits === 1);

resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', maximumSignificantDigits: 21 }).resolvedOptions();
assert(resolvedOptions.minimumSignificantDigits === 1);
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('maximumSignificantDigits value is invalid.')) }

resolvedOptions = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumSignificantDigits: 3, maximumSignificantDigits: 5 }).resolvedOptions();
assert(resolvedOptions.minimumSignificantDigits === 3);
assert(resolvedOptions.maximumSignificantDigits === 5);
Loading