Jean-Paul S. Boodhoo

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 09-04-2007 11:20 AM by bitwisejp
Filed under:

[Advertisement]

Comments

JP Junkie wrote re: Macro to aid BDD test naming style
on 09-04-2007 4:50 PM

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

DeeJay Bettes wrote re: Macro to aid BDD test naming style
on 09-04-2007 5:13 PM

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

Jean-Paul S. Boodhoo wrote re: Macro to aid BDD test naming style
on 09-04-2007 9:57 PM

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

Style » Macro to aid BDD test naming style wrote Style » Macro to aid BDD test naming style
on 09-28-2007 11:55 AM

Pingback from  Style » Macro to aid BDD test naming style