All posts tagged 'Spec'
Technorati Tags: ,,

In a previous post, I took JP Boodhoo’s test rename macro and fine tuned it.  Now that I have fully adopted MSpec (and it rocks), the macro needed some much needed refactoring.  Keep in mind, I’m not a wiz at VB.NET, so it could probably due with some window dressing, (improvements are more than welcome), but it does what it’s supposed to.  To wire it up, I set the key combination of “Alt+=” to run the macro (go to Tools->Options->Keyboard to do this).  When your edit point is on a line that needs modification, then just hit your selected key combination.  It will replace spaces with underscores and also add underscores on camel casing.  So, both

  • Should Do Something
  • ShouldDoSomething

will be converted to:

  • Should_Do_Something.

So, here is my latest rendition:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module TDD
    Public Sub ReplaceSpacesInTestNameWithUnderscores()
        If DTE.ActiveDocument Is Nothing Then Return
        'Use "Basic" for VB.Net            
        Dim wrCS As Boolean = DTE.Properties("TextEditor", "CSharp").Item("WordWrap").Value
            If wrCS = True Then
                DTE.Properties("TextEditor", "CSharp").Item("WordWrap").Value = False
            End If
            Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
            Dim linetext As String = selection.Text
            If linetext = "" Then Return
            linetext = linetext.Substring(0, linetext.IndexOf(vbCrLf))
            Dim prefix As String = linetext.Substring(0, GetPrefixLength(linetext))
            If Trim(prefix) = "" Then Return
            Dim suffix As String = linetext.Substring(GetSuffixPosition(linetext))
            Dim description As String = Trim(GetBody(prefix.Length, linetext, suffix.Length))
            If description = "" Then Return
            description = SplitOnCamelCasing(description)
            selection.Text = prefix + _ 
                description.Replace(" ", "_").Replace("'", "").Replace(Chr(146), "") _ 
                + suffix + vbCrLf
        Catch ex As Exception
            If wrCS = True Then
                DTE.Properties("TextEditor", "CSharp").Item("WordWrap").Value = wrCS
            End If
        End Try
    End Sub
    Private Function GetBody(ByVal prefixLength As Integer, ByVal selectedText As String, _ 
         ByVal suffixLength As Integer) As String
        Return selectedText.Substring(prefixLength, _ 
         selectedText.Length - prefixLength - suffixLength)
    End Function
    Private Function GetSuffixPosition(ByVal selectedText As String) As Integer
        Dim indexOf As Integer = selectedText.IndexOf("=")
        If indexOf > 0 Then Return indexOf
        indexOf = selectedText.IndexOf("(")
        If indexOf > 0 Then Return indexOf
        indexOf = selectedText.IndexOf(";")
        If indexOf > 0 Then Return indexOf
        Return selectedText.Length
    End Function
    Private Function GetPrefixLength(ByVal selectedText As String) As Integer
        Dim words As String() = selectedText.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
        For x As Integer = 0 To words.Length
            Select Case words(x)
                Case "public", "private", "internal", "static", "class", "It", "Because", "void"
                    If x = words.Length - 1 Then Return String.Empty
                Case Else
                    Return selectedText.IndexOf(words(x))
            End Select
        Return String.Empty
    End Function
    Private Function SplitOnCamelCasing(ByVal description As String)
        For x As Integer = description.Length - 1 To 0 Step -1
            If x <> 0 And x <> description.Length Then
                Dim c As String = description.Substring(x, 1)
                Dim pc As String = description.Substring(x - 1, 1)
                If c <> "_" And c <> " " And c.ToUpper() = c And pc <> "_" And pc <> " " Then
                    description = description.Insert(x, " ")
                End If
            End If
        Return description
    End Function
End Module



Happy Coding!

.NET Musings

Wandering thoughts of a developer, architect, speaker, and trainer

Managed Windows Shared Hosting by OrcsWeb