This issue occurs to me many times while sending a review request, For example when I use Assert.AreEqual() method and the parameters are not in the order ( expected value, actual value) is got swapped. It's boring and long vexing job to correct all the Assert.AreEqual() parameter order. While searching on the Internet found this easy way to swap the parameters using Regular Expression.
// One of the common place to swap parmeter
public static void AreEqual(object expected, object actual)
For example, I wrote code like :
Assert.AreEqual("ActualString1", CretedString1);
Assert.AreEqual("ActualString2", CretedString2);
Assert.AreEqual("ActualString3", CretedString3);
The parameter is reversed order, it's not right and For sure I will get a review comment to change this.
To swap this parameter we can use find and replace with the regular expression.
Note: Regular Expression in Visual Studio is bit different.
Add this as Search Term
\((".*"),([^\)]*)
Add this as Replace Term
($2, $1
How its works
For information about regular expressions that are used in replacement patterns, see Substitutions in Regular Expressions. To use a numbered capture group, the syntax is $1 to specify the numbered group and (x) to specify the group in question. For example, the grouped regular expression (\d)([a-z]) finds four matches in the following string: 1a 2b 3c 4d. The replacement string z$1 converts that string to z1 z2 z3 z4.
Example Screenshots
Before :
After :
- Be careful to select code part you want to swap parameter. Don't apply for whole Document or Solution, It might do some harmfull effects.
- You can tweak the regex for other use cases where parameter pattern is different.
- There exist some shortcuts to reorder the parameter in VS but it didn't work for me and even if it work, We need to select each AreEqual method and apply those shortcut.
More Reference:
Assert.AreEqual Method
Using Regular Expressions in Visual Studio
Is there a shortcut to swap/reorder parameters in visual studio IDE? - Stack Overflow
Top comments (0)