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: signDisplay plus sign behaviour fixed for NumberFormat #1483

Closed

Conversation

shubhamguptadream11
Copy link
Contributor

@shubhamguptadream11 shubhamguptadream11 commented Aug 17, 2024

Summary

Fixes following issue:

Why this change is made?

There are few cases where signDisplay is not being handled correctly.
Examples:

1.
new Intl.NumberFormat('de-DE', {
 style: 'currency',
 currency: 'EUR',
 signDisplay: 'exceptZero',
}).format(8537.71)

Output: +8,537,71+
Expected: +8.537,71 €

2.
new Intl.NumberFormat('ja-JP', {
 style: 'currency', 
 currency: 'JPY', 
 signDisplay: 'exceptZero' 
}).format(123456.789)

Output: +123,457
Expected: +¥123,457

ChangeLog

This PR updates the implementation and testing of the signDisplay functionality in the DecimalFormat class within the Hermes engine, specifically for Android API level 31 and above.

Key Changes:

Implementation:

  • Integrated the setSignAlwaysShown method of DecimalFormat for API level 31 and above to control the display of the sign (+ or -) based on the signDisplay option. [For more detail about setSignAlwaysShown check here]
  • For API levels below 31, maintained the existing logic for handling sign display, ensuring backward compatibility.

Test Plan

  • Added a comprehensive set of test cases in HermesIntlAndroidTest.java to validate the behaviour of the signDisplay functionality, specifically for API level 31 and above.
  • Test cases cover scenarios for signDisplay: NEVER | ALWAYS | EXCEPTZERO

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Aug 17, 2024
@shubhamguptadream11 shubhamguptadream11 marked this pull request as ready for review August 17, 2024 07:28
@shubhamguptadream11
Copy link
Contributor Author

shubhamguptadream11 commented Aug 20, 2024

@tmikov Whenever you have time, can you please check this PR? It solves the signDisplay issue reported many times on the platform.

@neildhar
Copy link
Contributor

Hey @shubhamguptadream11, thank you for looking into this and submitting this PR!

Could you describe the exact change here in more detail? It looks like there is more going on with this change, with regards to whether the positive/negative strings are checked and whether things are added to the suffix or prefix.

Alternatively, it looks like Android API level 31 adds native support for this functionality. I think it would be preferable to add an API version check and use the newer API when possible, which should be available on most devices.

@neildhar
Copy link
Contributor

neildhar commented Aug 21, 2024

Looks great!

It looks like CI is failing, likely because the compile/target SDK version specified in our build.gradle is too low. Could you bump that to 31, and add a test for the desired behaviour in HermesIntlAndroidTest.java?

@shubhamguptadream11
Copy link
Contributor Author

shubhamguptadream11 commented Aug 22, 2024

Hi @neildhar,

I attempted to upgrade the compileSdkVersion to 31, but I encountered several issues due to deprecated APIs. For example, InstrumentationTestCase is deprecated as of API level 24 and needs to be replaced with InstrumentationRegistry. Given the scope of these changes, it would be more appropriate to handle this upgrade in a separate PR.

Could someone from the Hermes team take on the task of upgrading the compile/target version to the latest supported? or help me in upgrading that. Once that's done, I'll be able to raise my PR.

Till then I am trying to fix deprecated API's.

Thanks!

@shubhamguptadream11
Copy link
Contributor Author

shubhamguptadream11 commented Aug 22, 2024

Hi @neildhar ,

I've finally fixed all the errors that emerged due to the compile SDK upgrade. Below is a summary of the changes made:

Migration to JUnit4 and AndroidJUnitRunner:

Replaced the deprecated InstrumentationTestCase with the recommended JUnit4 and AndroidJUnitRunner.
Updated the test classes to reflect these changes.

Updated Hermes Tests:

Addressed issues related to missing JSRuntime references by correctly importing and using it within test files.

Please take a moment to review these changes and let me know if any further adjustments are needed.

Thanks for your guidance and support throughout this process!

@neildhar
Copy link
Contributor

@shubhamguptadream11, thanks for bumping the SDK version and fixing up the deprecated APIs. Everything looks good, please add a test and update the summary, and I'll merge this.

@shubhamguptadream11
Copy link
Contributor Author

@neildhar Added test cases and updated PR summary as well. Please check and let me know if anything else needed from my end.
Thanks again!

@shubhamguptadream11
Copy link
Contributor Author

Hi @neildhar

Just wanted to follow up on this PR. I’ve added the test cases and updated the summary as discussed. Could you please review it when you get a chance? Let me know if there’s anything else you need from my side.

Thanks again for your time!

if (Build.VERSION.SDK_INT >= 31) {
try (JSRuntime rt = JSRuntime.makeHermesRuntime()) {
rt.evaluateJavaScript(
"var nf = new Intl.NumberFormat('de-DE', { exceptZero: 'never', currency: 'EUR' });\n" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems odd, is it correct? I'm not aware of exceptZero being an option to NumberFormat (as opposed to signDisplay: "exceptZero"), and I don't believe the currency is printed unless you set style: "currency".

If this is incorrect, we should understand why this test didn't fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've corrected the test case to properly reflect the intended behaviour by using signDisplay: 'exceptZero' and including the style: 'currency' option.

@shubhamguptadream11
Copy link
Contributor Author

Hi @neildhar ,

Could we please check and review this PR when you have a moment?

@neildhar
Copy link
Contributor

@shubhamguptadream11 Could you clarify if the test is actually running or if you ran it locally? It seems odd that the previous version of the test was working.

@shubhamguptadream11
Copy link
Contributor Author

shubhamguptadream11 commented Aug 30, 2024

@neildhar
I am able to run the test cases locally now.

Command Used:

./gradlew :intltest:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.facebook.hermes.test.HermesIntlAndroidTest

With previous faulty test cases

@Test
  public void testSignDisplayExceptZeroForApiLevelAbove31() {
    if (Build.VERSION.SDK_INT >= 31) {
      try (JSRuntime rt = JSRuntime.makeHermesRuntime()) {
        rt.evaluateJavaScript(
            "var nf = new Intl.NumberFormat('de-DE', { exceptZero: 'never', currency: 'EUR' });\n" +
                       "var result = nf.format(8537.71);");

        String result = rt.getGlobalStringProperty("result");
        assertThat(result).isEqualTo("+8.537,71 €");
      }
    }
  }

Test cases are failing in local:

Screenshot 2024-08-30 at 8 42 47 PM

With updated test cases:
Test cases are passing

Screenshot 2024-08-30 at 8 46 11 PM

@shubhamguptadream11
Copy link
Contributor Author

@neildhar Please check the PR once. I had shared few screenshots above about test cases results.

@facebook-github-bot
Copy link
Contributor

@neildhar has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@shubhamguptadream11
Copy link
Contributor Author

Hi @neildhar
I hope you're doing well. I wanted to follow up on the status of PR. If there's anything you'd like me to adjust, just let me know.

Thank you for your time and support!

@facebook-github-bot
Copy link
Contributor

@shubhamguptadream11 has updated the pull request. You must reimport the pull request before landing.

@shubhamguptadream11
Copy link
Contributor Author

Hi @neildhar, I hope you are doing well. Can you please take a moment to review this PR.
Thanks!

@facebook-github-bot
Copy link
Contributor

@neildhar merged this pull request in a75e14a.

facebook-github-bot pushed a commit that referenced this pull request Sep 17, 2024
…1483)

Summary:
Original Author: [email protected]
Original Git: a75e14a
Original Reviewed By: avp
Original Revision: D62153166

Fixes following issue:
- #1466
- #1138
- #789

**Why this change is made?**

There are few cases where signDisplay is not being handled correctly.
Examples:
```
1.
new Intl.NumberFormat('de-DE', {
 style: 'currency',
 currency: 'EUR',
 signDisplay: 'exceptZero',
}).format(8537.71)

Output: +8,537,71+
Expected: +8.537,71 €

2.
new Intl.NumberFormat('ja-JP', {
 style: 'currency',
 currency: 'JPY',
 signDisplay: 'exceptZero'
}).format(123456.789)

Output: +123,457
Expected: +¥123,457
```

**ChangeLog**

This PR updates the implementation and testing of the signDisplay functionality in the DecimalFormat class within the Hermes engine, specifically for Android API level 31 and above.

**Key Changes:**

Implementation:
- Integrated the **setSignAlwaysShown** method of DecimalFormat for API level 31 and above to control the display of the sign (+ or -) based on the signDisplay option. [For more detail about setSignAlwaysShown check [here](https://developer.android.com/reference/android/icu/text/DecimalFormat#setSignAlwaysShown(boolean))]
- For API levels below 31, maintained the existing logic for handling sign display, ensuring backward compatibility.

Pull Request resolved: #1483

Pulled By: neildhar

Reviewed By: neildhar

Differential Revision: D62883944

fbshipit-source-id: df41029b45400b9db038f32727293d5ccda27211
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants