Update: this blog is no longer active. For new posts and RSS subscriptions, please go to http://saintgimp.org.
Over the years several people have shared the Visual Studio macros they use to make the BDD boxcar naming style easier to work with. I thought I’d add my own, not because it’s any better than the others but because it’s built for a slightly different workflow and someone might find it useful.
First, here’s the macro:
Public Module BDDNaming
Public Sub ReplaceSpacesInTestNameWithUnderscores()
If DTE.ActiveDocument Is Nothing Then Return
Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
If selection.IsEmpty Then
Private Sub ReplaceSpacesInSelection(ByVal selection As TextSelection)
Dim text As String = selection.Text
text = text.ToLower()
text = text.Replace(" ", "_")
text = text.Replace("""", String.Empty)
selection.Text = text
Private Sub ReplaceSpacesAtEditPoint(ByVal selection As TextSelection)
While selection.Text(0) <> """" AndAlso (Not selection.ActivePoint.AtStartOfLine)
If selection.Text(0) = """" Then
Private Sub DeleteTrailingQuote(ByVal selection As TextSelection)
I usually bind this macro to Alt-- (Alt-[dash]) because it’s easy to remember and it’s not bound by default to anything important.
To use it, I start by typing an open quote mark where I want to type a BDD context or spec name. I use Resharper so it automatically inserts two quote marks for me, but the macro works equally well without Resharper. The quotes prevent Intellisense from freaking out as I start to type the context or spec name:
public class ""
Then I type the context or spec name as a normal sentence using the space bar like so:
public class "when a message with no eligible listeners is sent"
Then I hit Alt-- to convert the sentence to a proper boxcar-style identifier:
public class when_a_message_with_no_eligible_listeners_is_sent
I can also highlight any arbitrary piece of text and hit Alt-- to convert the spaces to underscores.
Other people like to put a lot of boilerplate code into the macro to make it easier to set up contexts and specs quickly, but I prefer this macro that does just one thing and does it well. Hopefully someone else will find it useful too!