Unit Test Live Templates for ReSharper

Attached are Live templates that can be imported into ReSharper to help with writing unit tests. There are templates for Test, Setup, TearDown, TestFixtureSetUp and TestFixtureTearDown methods.

I find that failing test cases are really good reminders of things that need to get done, so often I will add test methods with Assert.Fail(); as the body, usually the method name is enough detail for me to remember what needs to be done.

Lastly, I’ve started using the ConditionalAttribute on my test class methods so they won’t be compiled in release mode. If this is not your style you may want to modify the templates.

ReSharperUnitTestLiveTemplates.xml (2.63 KB)

**Update:
** I’ve decided against using the ConditionalAttribute in favor of adding compiler directives to the test files. I quickly found that not using the attribute became a source for failing tests that shouldn’t have been run and/or simple code leaks. I know that many people prefer a different class library for tests, but I think that only makes sense for non unit tests. Anyway, here’s the updated file.

ReSharperUnitTestLiveTemplates1.xml (2.48 KB)


Setting up VS 2005 for nant build files

To setup nant intellisense in VS 2005 you can copy the schema from the install to the Visual Studio Install\xml\schema directory, however this won’t get all available tasks. As such, I prefer to create a nant file which calls the nantschema task (http://nant.sourceforge.net/release/0.85/help/tasks/nantschema.html). This task requires an output attribute be set, which I just set to my Visual Studio Install\xml\schema directory. Below is the build script I use:

<?xml version="1.0" encoding="utf-8"?>
<project name="Build nant schema to VS 2005" default="build" basedir=".">
    <target name="build">
        <nantschema 
output="C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\NAnt.xsd"
target-ns="http://tempuri.org/nant-donotuse.xsd" /> </target> </project>

You can run this after updating Nant or adding contrib projects to your installation and get the latest tasks.

Finally, in your other build files add the xmlns attribute to the project element pointing to the name space you used as the target-ns, http://tempuri.org/nant-donotuse.xsd in the above example. You may also need to tell VS to open .build files with the xml editor, you can do this by right clicking on the .build file and choosing ‘open with’.


Sum a column in a tab-delimited file using Powershell

Assuming you have a bunch of tab delimited files w/out headers and the second column as a number, you could use the following powershell command to sum the values and output the result.

PS > $sum=0; ls | cat | %{ $.split(“`t”)[1] } | %{ $sum+=[int]$ }; $sum

Sample File:

a-count    7
b-count 233
c-count 32
</p>


Reflection.Emit.DeclareLocal and Reflection.Emit.DefineLabel

Working with Reflection.Emit today I ran into two thing that I couldn’t

quickly find answers to. First, was regarding the br.s (OpCodes.Br_S)

code. Secondly, was regarding an error ‘Common Language Runtime

detected an invalid program’ which didn’t provide much help and the

cause was not obvious. After a lot of research and trial and error I

found some answers.

Regarding the first issue, I found this page on MSDN which also show’s

how to create a switch statement.

http://msdn2.microsoft.com/en-us/library/csx7wsz2(vs.80).aspx. The

solution is to create a Label object and MakeLabel method when Emitting

the IL.

Label endOfMethod = il.DefineLabel();
...
il.Emit(OpCodes.Br_S, endOfMethod);
il.MarkLabel(endOfMethod);

The above will create IL like this:

IL_0017:  br.s       IL_001
IL_0019:  ldloc.1

The second problem maybe should have been obvious, but most of what I
read didn’t cover this. If you need to store something you need to
create local storage before trying to store objects there. Not doing
this will result in a System.InvalidProgramException. You need to
create the storage with the il.DeclareLocal method passing in the Type
of the object you will store there.

il.DeclareLocal(stringBuilderType);
il.DeclareLocal(typeof(string));

The above will create IL like:

.locals init (class [mscorlib]System.Text.StringBuilder V_0,
            string V_1)

Working sample command line application (.net2.0) flie – Program.cs.txt (2.45 KB)


Deleting files recursively with PowerShell

This is not the hardest script to come up with, but I can think of a few times I could have used something like this.

ls -rec -inc .svn -fo | foreach {del $_ -rec -fo}</font></pre>


It will delete any file or folder named </em>.svn in the current directory or below.


NHibernate access strategy names

Here are the naming strategies for the NHibernate access attribute.

  • property
  • field
  • field.camelcase
  • field.camelcase-underscore
  • field.pascalcase-m-underscore”
  • field.lowercase-underscore
  • nosetter.camelcase
  • nosetter.camelcase-underscore
  • nosetter.pascalcase-m-underscore
  • nosetter.lowercase-underscore
[Id(Column = “id”, Name = “Id”, 
Type = “Int32”, UnsavedValue = “0”,
Access = “nosetter.camelcase-underscore”),
Generator(1, Class = “native”)]

Switch statement and the goto command.

The goto command can be used to within a switch statement to transfer control to other case statements or the default case statement. Using **goto case ** will transfer control to that case statement. Using **goto default** will transfer execute to the default statement. I’m not sure it produces the cleanest code, but here’s an example anyway.

public void SwitchMe(StatesEnum state) 
{
switch (state)
{
case StatesEnum.Stopped:
Start();
goto case StatesEnum.Starting;
case StatesEnum.Started:
Stop();
goto case StatesEnum.Stopping;
case StatesEnum.Stopping:
state = StatesEnum.Stopping;
goto default;
case StatesEnum.Starting:
state = StatesEnum.Starting;
goto default;
default:
Log(state);
break;
}
}

public enum StatesEnum
{
Stopped,
Starting,
Started,
Stopping
}

So, looking above, if state = Stopped and was passed to the SwitchMe method, the code would execute as follows:

  1. the Stopped case would execute calling the Start method
  2. the Starting case would execute setting the value of state to Starting
  3. the default case would execute calling the Log method

Brutalist Framework