-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Apostrophe in display name breaks name generation for @ParameterizedTest
#3235
Comments
JUnit uses Here's an excerpt from the User Guide:
That means if you renamed your test method to I say "should", because I have not tested it myself (since I don't use Kotlin). Please let us know if that works for you. |
Yep that works as expected, but imagine test names like Could the name be pre-escaped somehow for
I think that part of the user guide should be more explicit that if you have a |
Glad to hear that works with Kotlin. Thanks for confirming. 👍
I'm not so sure that would work, but feel free to try it and report back.
Interesting idea. We'll discuss it amongst the team.
Indeed, the documentation should be improved, both in User Guide and in the Javadoc. |
Team Decision: Ensure replacing Implementation hint: Try replacing |
@TWiStErRob Would you be interested in submitting a PR for this? |
Happy to let anyone take a stab, please go ahead! (I might do it in the future, but I'll comment here if/when I start.) |
Hello, i'm interested in solving this issue, could someone help me get started? I'm new to contributing :) |
@alemendoza-v Sure! The relevant code is in |
Thanks! I'm assuming i'll have to install Kotlin right? I've only worked with Java before |
You only need JDK 17. The Gradle build will take care of everything else when you import the project into IntelliJ IDEA. |
Perfect, I was planning on using vscode but ill use intelliJ to work |
@TWiStErRob Hello, I think I found a possible solution, If the Doing that I got this output using the |
That would solve the issue regarding single apostrophes; however, it would not be a general purpose solution. Please see the tip in #3235 (comment):
Doing that should theoretically cover all use cases. |
Does the evaluation of the |
The The |
Thanks! |
I have a problem, to replace |
@sbrannen OK so I think I found the solution. In the String result = pattern//
.replace(DISPLAY_NAME_PLACEHOLDER, "__DISPLAY_NAME__")//
.replace(INDEX_PLACEHOLDER, String.valueOf(invocationIndex)); This allows the Then the String formatted = format.format(humanReadableArguments);
return formatted.replace("__DISPLAY_NAME__", this.displayName); Using the tests provided by @TWiStErRob I get the following output names: Is this the right approach? |
Should I créate a PR for it? |
I assumed that Feel free to submit a PR. If you would like to get it included in JUnit 5.10, please do so in the coming days or week since we are now finalizing the feature set for 5.10. In your PR, please include Java-based tests in addition to Kotlin-based tests. |
@ParameterizedTest
Prior to this commit, if a @ParameterizedTest used the {displayName} placeholder to generate a display name and the value of the displayName contained an apostrophe (') or something resembling a MessageFormat element (such as {data}), JUnit threw an exception due to failure to parse the display name. This is applicable to method names in Kotlin-based tests or custom display names in general. To fix this bug, instead of replacing the DISPLAY_NAME_PLACEHOLDER before the MessageFormat is evaluated, the DISPLAY_NAME_PLACEHOLDER is now replaced with another temporary placeholder, which is then replaced after the MessageFormat has been evaluated. Closes #3235 Closes #3264
Steps to reproduce
gradlew test
build/reports/tests/test/index.html
Note: the repro project is a bit more complex than the above:
TestK
is a pure Kotlin versionTestJ
is a pure Java versionTest
is the problematic version with Kotlin apostrophes.I made them really similar so it's easy to compare their outputs.
Expected
No
{0}
anywhere.It works correctly without apostrophes:
build/reports/tests/test/classes/com.example.MyTestK.html
Actual
When the method name contains
'
, the{0}
is not substituted.It fails formatting the name when apostrophes are added:
build/reports/tests/test/classes/com.example.MyTest.html
Context
Deliverables
I think the problem might be around
ParameterizedTestNameFormatter
, but didn't debug it.The text was updated successfully, but these errors were encountered: