Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Created February 12, 2014 03:41
Show Gist options
  • Save aaronpowell/8949709 to your computer and use it in GitHub Desktop.
Save aaronpowell/8949709 to your computer and use it in GitHub Desktop.
Convert `Assert.IsTrue` to a useful assert statement

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 caprutes 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})
//Or with numbered captures
Assert.AreEqual(${2}, ${1})

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

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