Skip to content

Instantly share code, notes, and snippets.

@jstangroome
Forked from aaronpowell/regex.md
Last active August 29, 2015 13:56
Show Gist options
  • Save jstangroome/8951324 to your computer and use it in GitHub Desktop.
Save jstangroome/8951324 to your computer and use it in GitHub Desktop.

So I really hate when people use Assert.IsTrue(a == b) as an assertion in a unit test (I blogged my rant) so I decided to find a way to easily convert large test files to be more normal.

Search Regex

You'll want to search with this:

Assert\.IsTrue\((?<Actual>.*)\s*==\s*(?<Expected>.*)\)
//or if your language doesn't support named captures use:
Assert\.IsTrue\((.*)\s*==\s*(.*)\)

Maybe tweak the casing for your language (I've done this for .NET)

Replace Regex

And do a replacement using:

Assert.AreEqual(${Expected}, ${Actual}, "WAS: Assert.IsTrue( ${Actual} == ${Expected} )")
//Or with numbered captures
Assert.AreEqual(${2}, ${1}, "WAS: Assert.IsTrue( ${1} == ${2} )")

//How about NUnit's Assert.That
Assert.That(${Actual}, Is.EqualTo(${Expected}))

Run that through the Replace tool in your editor and it's all cleaned up, no more crappy assertions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment