0

For a project I'm working on I'm trying to create a script that will go over all files with name *Test*.java (ex. AccountServiceTest.java) . Each file contains Java Test methods. During the years the naming conventions have been interpreted by each team member differently and we ended with a mess of Test method names. As a refactoring task I'm trying to rename all those method names correctly. Manually it will take too long, so I'm trying to do it with Bash scripts. This script should change the name of the method based on rules discussed in the team.

for example :

...
public void givensomethingWhensomethingElseThensomethingElse() {
...

should end up like :

...
public void given_something_when_somethingElse_then_somethingElse() {
...

These are the steps that I now think that should do the trick:

  1. Pick line that contains "public void"

  2. catch given and make sure it's in lowercase (GIVEN_something results in given_something)

  3. catch when and make sure it's in lowercase

  4. catch then and make sure it's in lowercase

  5. catch string between given and when (or then as sometimes there is no when statement in method name)

     - String found for example is _SomeRandomText
     - remove all _ --> SomeRandomText
     - change first character to lowercase --> someRandomText
     - add _ in front and in back --> _someRandomText_ 
     - result is : ...given_someRandomText_when...
    
  6. catch string between when and then

     - String found for example is _SomeRandomText   
     - remove all _ --> SomeRandomText
     - change first character to lowercase --> someRandomText
     - add _ in front and in back --> _someRandomText_
     - result is : ...when_someRandomText_then... 
    
  7. catch string between then and the parenthesis

     - String found for example is _SomeRandomText 
     - remove all _ --> SomeRandomText
     - change first character to lowercase --> someRandomText
     - add _ in front --> _someRandomText
     - result is : ...then_someRandomText(... 
    

I think sed will be the solution here, but I'm just not seeing how I can create it. Anyone an idea?

1 Answer 1

0

Like this:

$ sed -E '/public void.*given.*when.*then/s/_(\w)/\U\1/g' file
public void givenSomethingWhenSomethingElseThenSomethingElse() {

I don't respect 5,6,7 points, but now you have more than a start to adapt it a bit by yourself.

What I do here is to replace _[a-z] by the matched character in UPPER case.

If you need to replace in-place, add -i switch

3
  • Maybe sed on mac works differently, but it's not working for me. It doesn't change anything. sed -E '/public void.*given.*when.*then/s/_(\w)/\U\1/g' testFileTest.java public void givenSomethingWhenSomethingElseThensomethingeElse() {
    – Nungu
    Commented Nov 22, 2022 at 16:05
  • After some searching I've downloaded gsed for my mac. Later with these sed commands it's working fine. Thanks! gsed -E -i '/public void/s/_//g' $x gsed -E -i '/public void/s/given(\w)/given\l\1/g' $x gsed -E -i '/public void/s/given(\w)/given_\1/gi' $x gsed -E -i '/public void/s/when(\w)/when\l\1/gi' $x gsed -E -i '/public void/s/when(\w)/_when_\1/gi' $x gsed -E -i '/public void/s/then(\w)/then\l\1/gi' $x gsed -E -i '/public void/s/then(\w)/_then_\1/gi' $x
    – Nungu
    Commented Nov 22, 2022 at 17:19
  • The way to thanks here: Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. Please see the relevant help-center article Commented Nov 22, 2022 at 21:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .