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

Javascript function String.prototype.localeCompare() does not support option ignorePunctuation within karate #2278

Closed
blaisn opened this issue Mar 16, 2023 · 4 comments
Labels

Comments

@blaisn
Copy link

blaisn commented Mar 16, 2023

Hello,
When comparing 2 strings for sorting purposes, we use the function localeCompare.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
This function takes 3 arguments:

  • the string to compare with
  • the locale to use
  • some options

The present issue concerns the "option" argument. One of the possible option is "ignorePunctuation" that can be true/false.
It seems that the documented behavior of this option (see link above) is not respected when we use it inside a karate scenario.
For example, running these scenarios with the latest release of karate.jar (1.3.1):

    Scenario: success and behaves as expected
        * def value1 = "abc";
        * def value2 = "abc";
        And match value1.localeCompare(value2, "en", { ignorePunctuation: true }) == 0
    Scenario: success but do not behaves as expected
        * def value1 = "abc";
        * def value2 = "a,bc";
        And match value1.localeCompare(value2, "en", { ignorePunctuation: true }) == 1

(both scenarios are passing)
We can see that introducing punctuation changes the result of the function but should not.
Trying with other locales or other punctuation signs does not have an effect.

I tried the function on nodejs v16.18.1:

{
    const value1 = "abc";
    const value2 = "abc";
    const compare = value1.localeCompare(value2, "fr", { ignorePunctuation: true });
    console.log(compare); // 0
}
{
    const value1 = "abc";
    const value2 = "a,bc";
    const compare = value1.localeCompare(value2, "fr", { ignorePunctuation: true });
    console.log(compare); // 0
}

and the results are as expected.
Why is the javascript called by karate behaves differently than the one we use outside?

Thanks

@ptrthomas
Copy link
Member

because the JS engine is Graal and it does have some quirks. for this use-case I recommend using the JDK locale helpers via Java interop. I am tagging this issue as help wanted

@blaisn
Copy link
Author

blaisn commented Mar 20, 2023

Thanks for your response. I'll try with JDK locale helpers.
For my curiosity, which version of GraalVM is embedded in karate 1.3.1?

@ptrthomas
Copy link
Member

@blaisn don't remember exactly but you can get details here: #2148

@ptrthomas
Copy link
Member

closing this as a wont fix PRs welcome against this ticket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants