Archive for the ‘Tools’ Category

Red line at 80th column in VS

Wednesday, October 14th, 2009

vs_redline_at80There is a hidden feature in Visual Studio. You can add a nice, red, vertical line to your editor.
All you need to do is to create a .reg file with the following content:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Text Editor]
“Guides”=”RGB(255,230,230), 80″

And add it to registry (double click). That’s it!

How to permanently remove a SVN folder

Wednesday, October 7th, 2009

svnRecently I wanted to permanently remove a folder from Subversion repository.

Basically it was a folder with large amount of test data, and I decided to rename every single file inside. Doing it with ‘SVN rename…’ for 3000 items was out of question.

The solution was quite simple:

First dump the whole repository:
svnadmin dump "D:\My projects\SVN Data" > dumpfile.txt

Second filter the folder I wanted to remove:
svndumpfilter exclude Mail\eml < dumpfile.txt > filtered-dumpfile.txt

Then delete and create a new repository:
Delete repository
Create repository

Finally load the filtered dump into SVN repository:
svnadmin load "D:\My projects\SVN Data" > filtered-dumpfile.txt

You can also use this procedure for upgrading SVN repository version.

How to test email sending?

Friday, September 25th, 2009

Nearly every web application these days sends some notifications to its users.

Many times before, I came across people who said that it’s not possible to test such things.
Well, they are wrong!

There is a very cool project on sourceforge called nDumbster:
http://ndumbster.sourceforge.net

nDumbster is a simple fake SMTP server designed especially to enable unit testing.

[TestFixture]
public class SmtpClientTest
{
    private const int _port = 25;
    private SimpleSmtpServer _smtpServer;

    [SetUp]
    public void SetUp()
    {
        _smtpServer = SimpleSmtpServer.Start(_port);
    }

    [TearDown]
    public void TearDown()
    {
        _smtpServer.Stop();
    }

    [Test]
    public void SendMessage_SendsMessage()
    {
        Mail.Text(&quot;Some text&quot;)
            .Subject(&quot;Some subject&quot;)
            .From(new MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;))
            .To(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))
            .UsingNewSmtp()
            .Server(&quot;localhost&quot;)
            .OnPort(_port)
            .Send();

        Assert.AreEqual(1, _smtpServer.ReceivedEmailCount);
        SmtpMessage mail = _smtpServer.ReceivedEmail[0];
        Assert.AreEqual(&quot;\&quot;Alice\&quot; &lt;alice@mail.com&gt;&quot;, mail.Headers[&quot;From&quot;]);
        Assert.AreEqual(&quot;\&quot;Bob\&quot; &lt;bob@mail.com&gt;&quot;, mail.Headers[&quot;To&quot;]);
        Assert.AreEqual(&quot;Some subject&quot;, mail.Headers[&quot;Subject&quot;]);
        Assert.AreEqual(&quot;Some text&quot;, mail.Body);
    }
};

GoToTest macro for VisualStudio

Wednesday, September 23rd, 2009

4
When you are doing Test Driven Development (TDD) you are constantly switching back and forth between production code and test classes.

As it’s good idea to have those files in separate projects, it’s sometimes very hard to find test code for a class and vice-versa.

Some time ago I decided to write a simple Visual Studio macro, that solves this problem.
It’s based on the convention that all your test classes have Test or Tests suffix (e.g. CSVReader.cs and CSVReaderTests.cs)

I’m far from being VB expert, so the code is not perfect (Why does VS use Visual Basic for Macros?):

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

Public Module MainModule
    Dim _patterns As String() = {"{0}Test", "{0}Tests"}
    Dim _reversePatterns As String() = {"Tests", "Test"}

    Sub GoToTest()
        If DTE.SelectedItems.Count = 0 Then
            Return
        End If

        Dim fullFileName As String = DTE.ActiveDocument.Name
        Dim fileName As String = Path.GetFileNameWithoutExtension(fullFileName)
        Dim extension As String = Path.GetExtension(fullFileName)

        If IsTestFile(fileName) Then
            For Each reversePattern As String In _reversePatterns
                If fileName.Contains(reversePattern) Then
                    TryOpen(fileName.Replace(reversePattern, "") + extension)
                End If
            Next
        Else
            For Each pattern As String In _patterns
                TryOpen(String.Format(pattern, fileName) + extension)
            Next pattern
        End If
    End Sub

    Function IsTestFile(ByVal fileName As String)
        For Each reversePattern As String In _reversePatterns
            If fileName.Contains(reversePattern) Then Return True
        Next
        Return False
    End Function

    Function TryOpen(ByVal fileName As String) As Boolean
        Dim item As EnvDTE.ProjectItem
        item = FindItem(FileName)
        If Not (item Is Nothing) Then
            OpenItem(item)
            Return True
        End If
        Return False
    End Function

    Function FindItem(ByVal fileName As String) As EnvDTE.ProjectItem
        If String.IsNullOrEmpty(fileName) Then
            Return Nothing
        End If
        Dim item As EnvDTE.ProjectItem = DTE.Solution.FindProjectItem(fileName)
        Return item
    End Function

    Sub OpenItem(ByVal item As EnvDTE.ProjectItem)
        item.Open()
        item.Document.Activate()
    End Sub

End Module

How to install:

Start Visual Studio and go to Tools/Macros/Load Macro Project…:
1

Right click on the Visual Studio toolbar, and select customize:
2

Select ‘Macros’ category and find ‘GoToTest’ Macro:
3

Of course you can change the name of the button.
4

And finally the macro itself:
GotoTestMacro

My ReSharper templates for Unit Testing

Monday, September 21st, 2009

As I always have problem synchronizing my office and home machine’s templates I thought this would be good place to store them.

Inline templates (LiveTemplates.xml)

test

[Test]
public void Method_Condition_Result()
{
	$END$
}

setup

[SetUp]
public void SetUp()
{
	$END$
}

record

using(mocks.Record())
{
	$END$
}

play

using(mocks.Playback())
{
	$END$
}

File templates (FileTemplates.xml)

NUnitTestFile

using NUnit.Framework;

namespace $Namespace$
{
    [TestFixture]
    public class $FileName$
    {
        [Test]
        public void Method_Condition_Result()
        {
        }
    };
}

FileTemplates
LiveTemplates