Blog Archive

Friday, September 15

Visual Studio Macro

Never thought I'd be posting VB to my blog, but here's a little Visual Studio macro to switch between a Class file and it's associated TestFixture. The macro assumes a ClassNameFixture naming convention. Also sorry if my VB sucks, it literally took me 3 minutes plus asking a coworker to figure out how test for null.


Imports System
Imports System.IO
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module TestFixtures

    Public Sub SwitchBetweenClassAndFixture()
        Dim active As Document, filename As String, extension As String
        Const fixtureSuffix = "Fixture"

        active = DTE.ActiveDocument
        If Not active Is Nothing Then
            filename = Path.GetFileNameWithoutExtension(active.Name)
            extension = Path.GetExtension(active.Name)
            If filename.EndsWith(fixtureSuffix) Then
                DTE.ExecuteCommand("Edit.OpenFile", filename.Substring(0, filename.Length - fixtureSuffix.Length) + extension)
                DTE.ExecuteCommand("Edit.OpenFile", filename + fixtureSuffix + extension)
            End If
        End If
    End Sub

End Module

No comments: