Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Macro to aid BDD test naming style

During the DevTeach seminar that occured in Montreal in May I got an awesome opportunity to pair deliver a TDD workshop with Scott Bellware. One of the things he got me hooked on was using natural sentences as a test naming convention. So instead of this:

ShouldBeAbleToTransferFundsBetweenAccounts

You write it out like this:

Should be able to transfer funds between accounts.

Of course, only one of these is legitimate from a compilation perspective. So Scott had written a macro that would replace all of the spaces in the test name with underscore. You invoked it by highlighting the name of the test and hitting a key combination that was bound to the macro. Which would convert the natural sentence to the following:

Should_be_able_to_transfer_funds_between_accounts.

During the last course, Terry Hughes improved upon the macro so that now all I have to do is type out the name of the test and hit ALT + _ (that is what I have the macro bound to). I don’t need to select the name of the test.

It is a subtle change, but one that allows me to just write out the name of my test without worrying about camel casing or underscores.

Here is the macro:

 

Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Public Module CodeEditor Sub ReplaceSpacesInTestNameWithUnderscores() If DTE.ActiveDocument Is Nothing Then Return Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection) selection.SelectLine() If selection.Text = "" Then Return Dim prefix As String = "public void " Dim index As Integer = selection.Text.IndexOf(prefix) prefix = selection.Text.Substring(0, index) + prefix Dim description As String = selection.Text.Replace(prefix, String.Empty) selection.Text = prefix + description.Replace(" ", "_").Replace("'", "_") selection.LineDown() selection.EndOfLine() End Sub End Module

This video shows the macro in action.

 

Enjoy

This entry was posted in Programming. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

3 Responses to Macro to aid BDD test naming style

  1. @DeeJay,

    I have configured my ReSharper so that the intellisense only pops up when I explicitly type ALT + Space. This works a lot better for thestyle that I code.

    I don’t use any of studios intellisense features. ReSharper more than meets my needs in this and other arenas of Studio.

  2. DeeJay Bettes says:

    Did you have to turn off Intellisense so that it didn’t try to auto-complete while you were typing the test name?

  3. JP Junkie says:

    Welcome back JP !! We have all been suffering withdrawal systems….

Leave a Reply