Replace Paper with Unit Tests: Code Snippets Edition

In my previous post about making mental notes about condition tests or new features through unit tests I offered an example of making a live template in ReSharper to automate some of the process.

This post is about achieving a similar thing in plain Visual Studio with the help of the Code Snippets feature.

Introduction to Code Snippets

In a nutshell, a code snippet in Visual Studio is either a template or a static chunk of code that is stored in a specific location and is available through IntelliSense. Visual Studio comes with some snippets pre-configured, such as “ctor“, which creates a default constructor for a class.

To use a snippet, start typing out its shortcut – either type the whole thing or wait for IntelliSense to pop up and complete it for you – and then press TAB key twice to expand the snippet into code.

There’s a detailed guide to using and creating snippets available on MSDN. For the rest of this post, I will just show what I did for my purposes.

Snippet Location and Creating New Snippets

Snippets are located in a specific folder under your installation of Visual Studio, so you’ll have a snippet collection per VS version. On my machine, where I used default installation paths, C# snippets for VS 2008 and 2010 are in the following folders:

C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#

C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#

You can use the Code Snippet Manager (available through Tools->Code Snippets Manager… menu item) to explore currently installed snippets for various languages.

To create a new snippet, you can simply write its XML definition (see below for my example or read the MSDN guide for more info), save the file with the .snippet extension and drop it in the appropriate folder. Visual Studio will see it without needing to restart.

You can also use a visual editor such as the open source Snippet Editor, which now includes support for Visual Studio 2010.

Unimplemented Unit Test Snippet

Here’s my code snippet in its entirety:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Unimplemented Unit Test</Title>
<Shortcut>test</Shortcut>
<Description>Code snippet for a failing test placeholder</Description>
<Author>Anna Lear (http://annalear.ca)</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>testName</ID>
<ToolTip>Descriptive test name</ToolTip>
<Default>NewTest</Default>
</Literal>
<Literal>
<ID>testBody</ID>
<ToolTip>Test contents</ToolTip>
<Default>Assert.Fail("Implement me!");</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
[Test]
public void $testName$()
{
$testBody$
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

I wrote it within Visual Studio 2010 and it helpfully provided IntelliSense support and guidance, which made writing the XML really easy.

Now when I type in “test” in a class and press TAB twice, the code within the CDATA portion of the snippet gets pasted into my code. “NewTest” and “Assert.Fail(“Implement me!”);” lines will be highlighted and I can navigate between then with TAB and Shift-TAB if I need to edit them. I typically always edit the name of the test, and only edit the body when I intend to implement the test at the same time.

Couple things to keep in mind:

1. When you add literals to a snippet, their IDs are case-sensitive.

2. If you want to preserve your formatting (multiple lines, etc.), make sure that you’re code is within a single CDATA element. Otherwise you’ll end up with all your code on the same line, which probably isn’t what you want.

Download

You can either copy the snippet above and do the legwork of placing it in a file and the correct location, or download the .snippet file or the .vsi installer, the latter of which will allow you to easily install the above snippet into any version of Visual Studio (2005 or newer) that you have on your machine.

Advertisements

Happy New Decade

I’ve seen some arguments going on around whether or not 2010 is the start of a new decade or the end of the “old” one. Among them was the best argument I’ve ever seen for zero-based counting: if 2010 is ending a decade, then by the same logic, 1980 is a part of the 70s and that’s just wrong. I really don’t think I can (or want to) argue with that, so I’m choosing to think of today as not only the beginning of a new year but also of a new decade.

Ten years is a bit too far in advance for me to even begin to contemplate planning. One year, however, is still doable. So, what do I think is on the docket for me?

  • Guitar Lessons — I’m going to continue taking guitar lessons and step up my actual practice time. I’m ashamed to admit that I have very little to show for the years I’ve been “playing” the instrument. That will change. When I do practice, I notice an almost immediate improvement, which tells me that I do have at least some talent for it. All that I need is practice and proficiency will come.
  • Side Projects — I will spend more time developing my own projects. I’m going to start with programming, of course, since that’s what I know best, but I’m sure others will come in time.
  • This Blog — I learned something obvious in 2009: without doing anything noteworthy, I don’t have anything noteworthy to write about. I will be expanding this blog to cover a few more topics, instead of trying to just blog about software development. At least until I do enough of the latter to be able to fill at least some regular posting schedule.
  • Debt Reduction/Conscious Living — I have some debt incurred during university, some for legitimate reasons, and some just because I didn’t know any better. It is well past time to deal with it all. My goal is to put thought behind everything I do. Even if I choose to spend my time on things that others might consider unproductive, I want it to be because I thought about it and consciously made the choice. It is time to reevaluate, and in some cases evaluate for the first time, what I spend my time on and why I do the things I do.
  • Theatre — I will continue my involvement with the Kitchener-Waterloo Little Theatre. I find it deeply fulfilling, and it’s good to have hobbies that get you out of the house once in a while.

Of course, it remains to be seen how much of the above will actually happen. I think the odds are in my favour. It will be interesting to look back at the end of next year and see how it all went.

The Nike Principle

I often find that I dawdle. I think of a great idea and then … never put it in action. I say I will do it “some day”. “When the time is right.” Trouble is, I never quite get around to defining what it means for the time to be right. To combat that, I now remind myself to either do what I said I would do or to come up with a set of well-defined criteria to determine when I’ll do it instead. No more excuses.

I call this the “Nike principle” because of their slogan: Just Do It.

That’s really all that matters. Waiting for the perfect moment to develop a new habit or try a new approach to something is often just a waste of time. When trying to learn something or make a change in your routine, don’t wait for the stars to align. Just do it. If it doesn’t work, refine your approach. Repeat that until you’re happy with what you’ve got. It’s as simple as that.