-
Notifications
You must be signed in to change notification settings - Fork 39
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
Introduce NewStringFromCharArray{,SubSequence}
Refaster rules
#1012
Introduce NewStringFromCharArray{,SubSequence}
Refaster rules
#1012
Conversation
@rickie @Stephan202 Please review. I have added the rule to copy string characters with an offset and a count. I tried the below way to support both copying all characters and copying characters with an offset[1]. But it gives the following error[2] [1]
[2] Is there a specific way to overcome this? Or do we need another class to be initiated? Thanks, |
Looks good. No mutations were possible for these changes. |
Hey @BLasan! Thanks; I'll have a look this weekend. To answer your question: Refaster has some constraints. Relevant to your example are:
The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | ||
* Prefer direct invocation of {@link String#copyValueOf(char[])} over the indirection introduced | ||
* by {@link String#copyValueOf(char[])}. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence contains a copy-paste error 👀
static final class StringCopyValueOf { | ||
@BeforeTemplate | ||
String before(char[] data, int offset, int count) { | ||
return String.copyValueOf(data, offset, count); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that we can also rewrite a String.valueOf
variant; will propose something.
|
||
String testStringCopyValueOf() { | ||
return String.copyValueOf(new char[] {'f', 'o', 'o'}, 0, 3); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally order the test methods the same as the rules; I'll move these test methods up :)
* Prefer direct invocation of {@link String#copyValueOf(char[])} over the indirection introduced | ||
* by {@link String#copyValueOf(char[])}. | ||
*/ | ||
static final class StringCopyValueOf { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally name the rule after the @AfterTemplate
code. I this case it's a bit tricky, but I'll propose something.
Looks good. No mutations were possible for these changes. |
Hi @Stephan202 , Thank you for the review. Are there any resources to learn the procedure of adding new rules? Also, any other issues that I could work on :) ? Thanks, |
NewStringFromCharArray{,SubSequence}
Refaster rules
That's a good question. I think that right now there's no documentation for this. What we instead try to do, is add Error Prone checks that validate and (when possible) attempt to auto-fix rules to match our guidelines. PR #1002 moves some of those rules around, but there are (vague) plans to add more such rules. For now the best I can offer is to employ careful pattern matching by looking at existing rules 😄
Certainly our work isn't done 😉. The list of currently open issues could serve as inspiration, but note that most/all of those will require writing an Error Prone But let me offer another idea: are there aspects of your company's code base that you'd like to see different, such as things that you (perhaps repeatedly) point out during code review? If any of those are generally applicable and solvable using Refaster, we're certainly open to considering rules for that. (This suggestion does come with a caveat: we're kind of opinionated on some topics, such as preferring immutablility. But when in doubt: just ask!) |
Hi @Stephan202 , Sure, will take a look at some other issues opened. I asked specifically for an issue since someone internally (from your team) might have started working on such issues :) I need to recall some of the ideas (which are solvable using Refaster) pointed out in a code review as well as during the implementations. I'll just revisit some of the feature implementations and open issues if found any :) Thanks, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏻
fd71853
to
605805e
Compare
Looks good. No mutations were possible for these changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the Refaster rules @BLasan! 🚀
Also, any other issues that I could work on :) ?
@Stephan202 and I discussed something that could be a nice thing to work on. In this PR #770 that we are about to merge, we want to make clear for type migrations which methods are not (yet) migrated. We of course want to minimize the number of methods that are not migrated, see an example of the migration from JUnit Assertions to AssertJ here.
If working on Refaster rules increase the coverage of an JUnit Assertions -> AssertJ assertions migration sound interesting to you, you can take a few of those list. Add a few of the Refaster rules, create a PR and we can review them. If we do a few rules per PR the PR will not become too big. We can start with a few simple ones to go through this process the first time, we make sure to review it in an elaborate way to help you better understand Refaster 😄. That will make it easier for you to add new things and on our side easier to reviewing following rules.
For example the following methods could be a nice start to migrate from org.junit.jupiter.api.Assertions
to their AssertJ equivalent:
"assertFalse(BooleanSupplier)",
"assertFalse(BooleanSupplier, String)",
"assertFalse(BooleanSupplier, Supplier<String>)",
"assertTrue(BooleanSupplier)",
"assertTrue(BooleanSupplier, String)",
"assertTrue(BooleanSupplier, Supplier<String>)",
Something that might not be clear is that in the JUnitToAssertJRules.java
is that the Refaster rules are sorted by the order in which the methods we migrate away from are in the org.junit.jupiter.api.Assertions
class.
But, don't worry we are of course here to help and guide you if you think this sounds like a nice thing! Don't hesitate to open a discussion, draft PR or an answer on this PR 😄.
Let us know what you think :).
605805e
to
f5b3697
Compare
Looks good. No mutations were possible for these changes. |
f5b3697
to
a82950a
Compare
Looks good. No mutations were possible for these changes. |
fixes: #1001