Fitnesse and C#

Here are some sample fitnesse tests and fixtures.

!define COMMAND_PATTERN {%m -r fitnesse.fitServer.FitServer,dotnet2fit.dll %p}
!define TEST_RUNNER {dotnet2Runner.exe}
!path C:developfitnessedotnet2fittestfittestbinReleasefittest.dll

!|Import|
|fittest|

!|fittest.OurFirstTest|
|string1|string2|Concatenate?|
|Hello|World|HelloWorld|

!|fittest.SecondTest|
|a|b|Output?|
|One|Two|a = One;b = Two;|

!|ActionFixture|
|start|ActionFixtureTest|
|enter|firstPart|Hello|
|enter|secondPart|World|
|press|join|
|check|together|Hello, World|

namespace fittest
{
public class OurFirstTest : fit.ColumnFixture
{
public string string1;
public string string2;
public string Concatenate()
{
return string1 + string2;
}
}

public class SecondTest : fit.ColumnFixture
{
public string a;
public string b;
public string c;

public string Output()
{
StringBuilder sb = new StringBuilder();

if (a != null) sb.AppendFormat(“a = {0};”, a);
if (b != null) sb.AppendFormat(“b = {0};”, b);
if (c != null) sb.AppendFormat(“c = {0};”, c);
return sb.ToString();
}
}

public class ActionFixtureTest : fit.Fixture
{
public string FirstPart;
public string SecondPart;
public string Together;
public void Join()
{
Together = string.Format(“{0}, {1}”, FirstPart, SecondPart);
}

}
}

vim searches

vim is a very powerful editor once you can remember a few command combinations.

The basic search is invoked with / (or ? if you want to go backwards).

Of late I have been using the character find command f (especially in visual mode, vf[char] is extremely useful as would yf[char].

For the uninitiated these select until the next character (or with y copy [yank] into the default buffer).

Yesterday I found in the help files the * and # commands.

* is search forward for the word under the current cursor (# is the backwards version).

Another useful option is t which searches for a character but stops one character before. So yt) will copy upto the next closing bracket or y2t) will copy til the second following bracket!

This is also useful for extracting the contents of a parameter list:

vb%bd

Enter visual mode, back one word go to matching brace, back one word then delete (and copy into default buffer).

This is a lightweight refactoring in itself.

FileHash generator to match silverlight manifest

The above is the source of the below code.  I needed to use this to check that the generated Hash used in a silverlight manifest was indeed correct.  One of my customers was having trouble installing on a win7 64 bit system.  This same build works fine elsewhere.  In the end we used a different whitelabel (same code different graphics and therefore checksums).

using System;
using System.Security.Cryptography;
using System.IO;

namespace FileHashSample
{
public class FileHash
{
public FileHash()
{
return;
}

        public string ComputeHash(string filePath)
{
string filePathNormalized = System.IO.Path.GetFullPath(filePath);
SHA1 sha = new SHA1Managed();
FileStream fs = new FileStream(filePathNormalized, FileMode.Open, FileAccess.Read);
byte[] byteHash = sha.ComputeHash(fs);
fs.Close();
return Convert.ToBase64String(byteHash, 0, byteHash.Length);
}

        public static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine(“Please Enter a File Path”);
return;
}
string filePath = System.IO.Path.GetFullPath(args[0]);
FileHash objFileHash = new FileHash();
Console.WriteLine(“File Path is {0}”, filePath);
Console.WriteLine(“File Hash is {0}”, objFileHash.ComputeHash(filePath));
return;
}
}

}

How to simulate a tfs build locally

When tfs is used as a build server it builds the visual studio projects differently to the way the IDE does.
Typically it will dump all of the build artifacts into a single folder (which breaks almost any post-build scripts).
However if you are building web applications it has a clever option that builds a useful web applications folder with everything that is needed to be deployed.
This happens for any web project where the output directory is redirected.

msbuild Whatever.sln /p:configuration=Release /p:OutDir=c:builds

This is useful when you have a web site project in visual studio 2010 (say a silverlight web project) that you would like to deploy but don’t have enough space on the build server to install the right tools.
This is quite powerful but would be useful if the documentation for this was more obvious.

Simple Powershell database script

I know that sql server 2008 has a powershell scriptlet that does something similar to this.
However this works without any additional code.
It also works really well in a psake script.
This is a sql script runner that treats each file as a single group of transactions.
$a = new-module script-block {
function Exec-Sql
{
param([System.String] $filename, [System.String] $connectionString)
if (![System.IO.File]::Exists($filename))
{
Write-Host “No such file as $filename”
}
else
{
Write-Host $filename
$connection = new-object System.Data.SqlClient.SqlConnection -argumentList $connectionString
$connection.Open()
$command = “”
foreach ($str2 in [System.IO.File]::ReadAllLines($filename))
{
if (($str2.Trim().ToLower() -eq “go”))
{
$cmd = $connection.CreateCommand();
$cmd.CommandText = $command
$b=$cmd.ExecuteNonQuery()
$command = “”
}
else
{
$command += “`n$str2”
}
}
if ($command -ne “”)
{
$cmd = $connection.CreateCommand();
$cmd.CommandText = $command
$b=$cmd.ExecuteNonQuery()
}
$connection.Close();
}
}
}