Anyways, I started manually changing the calls, but that was just too tedious. It was time to get sed out again, but soon I realized that sed was not going to cut it this time, because there is no way do non greedy regular expressions . The expression also had to take care of single and multi line statements. An example for a multi line statement would be:
assertEquals(
"Wrong propertyPath",
propertyPath,
violation.getPropertyPath()
);
Time to get good old perl out again. Here is what I came up with (the whole expression is one line!):
perl -pi -0777 -e 's/(assertEquals.|\n*?)(".*?"),[ ,\n,\t]*(\w*?,)([ ,\n,\t]*.*?)([ ,\n,\t]*)\);/$1$3$4,$5$2\ );/g' MyClass.java
Well, it did not solve all my problems. In some cases the formatting was a little off, but that could be easily fixed with my IDE. Some comments about the expression:
- the -i option allows editing of the file in-line. A very nice feature of perl and something I miss a lot in sed
- -0777 specified the line break character in octa-decimal. Assuming 777 does not appear in your file this is an easy way to match across lines
- there are a lot of \n matches. Even when matching across the whole file .* won't match the new line character. It has to be explicitly specified
- using regular expression buffers I am able to capture the groups of the statement an put the expression together in a different order using $x notation for referring to the matching buffers
- and I almost forgot the ? after the operators ?, + and * turns on non greedy machting
enjoy!
5 comments:
You could also have used the AssertJUnit class, which is a copy of JUnit's class, so it's just a one line change per file...
WTF indeed! The whole Java thing seems to be a complexity conspiracy. (Don't get me started on portlets....)
Right. I wish the TestNG documentation would have made AssertJunit more visible. Not only a little node at the end of the documentation. Or what's about adding a comment to the migration guide? What can we learn from this? There are many ways to skin a cat! And of course there is a big Java complexity conspiracy going on ;-)
Good point about the migration guide, I updated it to reflect this.
Thanks!
Thanks to you :)
Post a Comment