CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jean-Paul S. Boodhoo

Develop With Passion

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


Published Sep 04 2007, 11:20 AM by bitwisejp
Filed under:

Comments

DeeJay Bettes said:

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

# September 4, 2007 5:13 PM

Jean-Paul S. Boodhoo said:

@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.

# September 4, 2007 9:57 PM

Style » Macro to aid BDD test naming style said:

Pingback from  Style » Macro to aid BDD test naming style

# September 28, 2007 11:55 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!