0

Code Snippets

by volkanuzun 15. April 2009 19:00

If you are using Microsoft Visual Studio, you are probably used to code snippets, which is kind alike code generation with a few keystrokes. Visual studio comes with a hand full of code snippets, however there are still sometimes where you may want to have your own code snippets. Unfortunately I never found writing code snippets easy, so I usually was skipping my attempts to write a code snippet.

Today i found this open source project at codeplex: Snippet Designer.
It is exactly what I was expecting to have for a long time :) as creating a code snippet is very easy now. I am also practicing TDD (test driven development) for some time,and below is my most popular line of code :

throw new NotImplementedException("Write a unit test");
 

I write this simple line to compile the code but also fail the test, and I can't tell you how many times I write this simple 1 line.
Today, I installed Snippet Designer tool, and I was checking out a simple way to create a snippet for the above line, and boom it was right there :) I wrote the line, select the line, right click , and the menu was there for me: “Export as Snippet”. I clicked on this, and the next step was this:

image

It is a straightforward window to fill. The most important part for me is the shortcut section, in my example I wrote “tddnot” so that whenever I write “tddnot” in the visual studio editor and hit tab, I get my most popular line :)

What if you want to have a place holder? Such as you want to have a codesnippet for a test function, and as you are using NUnit you want to have [Test] decoration around your function. I opened one of my existing test functions, highlighted the function, right click on it and click Export as Snippet menu.

This time I highlighted my function name, right click, and select  Mark as Replacement. So whenever you write your code snippet shortcut, you can tab to your Mark as Replacements and change them quickly.

Below is my code snippet for Nunit Test Class that I created using Snippet Designer, and believe or not, it took me less than 1 minute to create this file :)

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Keywords>
        <Keyword>NUnit Test Template Project</Keyword>
      </Keywords>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>SnippetFile1</Title>
      <Author>vuzun</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>tddnu</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>SomeNamepace</ID>
          <ToolTip>SomeNamepace</ToolTip>
          <Default>SomeNamepace</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>SomeTestClass</ID>
          <ToolTip>SomeTestClass</ToolTip>
          <Default>SomeTestClass</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>InitializeTest</ID>
          <ToolTip>InitializeTest</ToolTip>
          <Default>InitializeTest</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>SomeTestFunction</ID>
          <ToolTip>SomeTestFunction</ToolTip>
          <Default>SomeTestFunction</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="method decl"><![CDATA[using System;
using NUnit.Framework;
 
namespace $SomeNamepace$
{
    [TestFixture]
    public class $SomeTestClass$
    {
        
 
        [TestFixtureSetUp]
        public void $InitializeTest$()
        {
         
        }
        
        [Test]
        public void $SomeTestFunction$()
        {
            //Arrange
            
            //Act
            
            //Assert
        }
    }
}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Tags:

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Original Design by Laptop Geek, Adapted by onesoft