unit testing TypeScript

Few days ago TypeScript 0.97 was promoted to version 1.0RC which means that there will be no more changes for version 1.0, just bug fixes. At the same time Visual Studio 2013 Update 2 RC was announced with full TypeScript support out of the box, without having to separately download the tools. It is obvious that Microsoft are pushing TypeScript to be a first class citizen in the Visual Studio eco-system. What’s missing? Unit testing! Did they not notice? How could they call it 1.0 without a viable unit testing framework out of the box?

Here comes ReSharper 8.2 (which is not yet released, beta bits available on the JetBrains website). Best new feature – TypeScript unit testing of course!

Here is my setup:

  • Visual Studio 2013 Update 2 (RC)
  • ReSharper 8.2 (Beta)
  • PhantomJS (from PhantomJS.org) installed and configured
  • QUnit definition file – qunit.d.ts (from DefinitelyTyped)

Create a new HTML TypeScript Application project in Visual Studio and delete index.html, app.ts and app.css files (like to start clean).

Create a folder “libs” and put qunit.d.ts there.

Create a folder “tests” and put a file helloWorld.ts like this:

///<reference path="../libs/qunit.d.ts"/>

test("hello world test", ()=>
{
    ok(true, "hello world test");
});

The test icon appears in front of the test method. Run the test to make sure all is configured correctly.

Now create a folder “app” and put a file MyClass.ts in it:

module MyModule
{
    export class MyClass
    {
        private x: number;

        constructor(x: number)
        {
            this.x = x;
        }

        getX(): number
        {
            return this.x;
        }
    }
}

Finally create a test class MyClass.Tests for that class in folder “tests”:

///<reference path="../libs/qunit.d.ts"/>
///<reference path="../app/MyClass.ts"/>

module MyModule.Tests
{
    test("my test", ()=>
    {
        // arrange
        var myClass = new MyModule.MyClass(5);

        // act
        var result = myClass.getX();

        // assert 
        equal(result, 5, "result is 5");
    });
}

Magic! Love it or hate it ReSharper saves the day (again)…

Roman numbers kata

This exercise is from vNext_OC mob programming session today. The goal of this kata is to convert a roman number (XLVII) to a decimal number (48). In the spirit of TDD I will start with a test for the number one and the simplest possible code that satisfies that test:

[TestMethod]
public void I()
{
    Assert.AreEqual(1, Roman.ToDecimal("I"));
}

public class Roman
{
    public static int ToDecimal(string roman)
    {
        return 1;
    }
}

The second test I’ll write is for the number two:

[TestMethod]
public void II()
{
    Assert.AreEqual(2, Roman.ToDecimal("II"));
}

That test fails. One possible solution is to return the length of the passed string:

public class Roman
{
    public static int ToDecimal(string roman)
    {
        return roman.Length;
    }
}

Test succeeds. Next test – three. That succeeds as well. Next one – four. Fails. I don’t quite know what to do with four yet so I’ll do the simplest possible thing – will explicitly check for IV:

public class Roman
{
    public static int ToDecimal(string roman)
    {
        if (roman = "IV") return 4;

        return roman.Length;
    }
}

Green. Next one – five. I have 2 different digits now. What I’ll do is I’ll assign value 1 to I, value 5 to V and will add those values:

public class Roman
{
    public static int ToDecimal(string roman)
    {
        if (roman = "IV") return 4;

        int result = 0;

        foreach (var c in roman)
        {
            switch(c)
            {
                case "I": 
                    result += 1;
                    break;
                case "V":
                    result += 5;
                    break;
            }
        }

        return result;
    }
}

All tests are green again. VI, VII, VIII are green too.

Now is a good time to introduce ApprovalTests. I’ll install the package from nuget and I’ll write a test like this:

[TestMethod]
public void TestAll()
{
    var romans = new[] { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" };
    var results = romans.Select(r => new { roman = r, number = Roman.ToDecimal(r) });
    Approvals.VerifyAll(results, "");
}

That test will fail but will bring a file that looks like this:

[0] => { roman: "I", number: 1 }
[1] => { roman: "II", number: 2 }
[2] => { roman: "III", number: 3 }
[3] => { roman: "IV", number: 4 }
[4] => { roman: "V", number: 5 }
[5] => { roman: "VI", number: 6 }
[6] => { roman: "VII", number: 7 }
[7] => { roman: "VIII", number: 8 }
[8] => { roman: "IX", number: 1 }
[9] => { roman: "X", number: 0 }

I will fix the last two results of to be 9 and 10 and save the approved “gold master”. Test fails again (as expected) but we can replace all previous tests with just one more powerful and more visual test. To satisfy the test I’ll add another “exception” for IX and I’ll add a value for the digit 10.

public class Roman
{
    public static int ToDecimal(string roman)
    {
        if (roman = "IV") return 4;
        if (roman = "IX") return 9;

        int result = 0;

        foreach (var c in roman)
        {
            switch(c)
            {
                case "I": 
                    result += 1;
                    break;
                case "V":
                    result += 5;
                    break;
                case "X":
                    result += 10;
                    break;
            }
        }

        return result;
    }
}

Green. I’ll refactor the code now to include two dictionaries for the digits and the exceptions:

public class Roman
{
    static var exceptions = new Dictionary() { { "IV", 4 }, { "IX", 9 } }; 

    static var digits = new Dictionary() { { 'I', 1 }, { 'V', 5 }, { 'X', 10 } };

    public static int ToDecimal(string roman)
    {
        int result = 0;

        foreach (var x in exceptions)
        {
            if (roman.Contains(x.Key))
            {
                result += x.Value;
                roman = roman.Replace(x.Key, "");
            }
        }

        foreach (var c in roman)
        {
            if (digits.ContainsKey(c))
            {
                result += digits[c];
            }
        }

        return result;
    }
}

Run the test – all green. Now I’ll add the numbers from XI to XX to the test. First time it will fail. I’ll approve the result (since it is correct) and run the test again. Green. Repeat with XXI to XXX. Red, approve, green. XXXI to XL – all looks good except the number 40. I’ll manually edit the result file and approve it. Run the test again – red. To fix the code I need to add XL to the list of exceptions. Green. XL to L – to make the test pass need to add L to the digits. Then we are good all the way to XC. Adding XC to the exceptions and C to the digits and we have all the numbers to 100 working.

I’ll stop here. There is room to refactor but this is a good demonstration of the power of TDD. Without sitting down and designing the one algorithm that solves all roman numbers, in half an hour we have a very simple code that solves the problem for all numbers up to 100. Good enough to translate the number of the superbowl (XLVIII) that is coming next weekend.

AgileThought hackathon

AT-hackathon-logo

This week I took part in the inaugural AgileThought hackathon. My team consisted of Esteban Garcia (ALM MVP) , Eric Landes (also ALM MVP) and me (what am I doing with those two?). We wanted to do “something mobile” and/or “something with TFS”. Our initial proposal was for a Windows 8 TFS dashboard app but it was rejected since another team submitted a similar idea before us. We ended up writing a Windows 8 client for the TFS team rooms using the newly released REST API. It turned out pretty good (both visually and functionally) and we won the Cool Use of Technology award at the end.

SharePoint 2010 on Windows 7

For those about to rock, we salute you! For those about to get into SharePoint development, I solute you too! With a bit of mixed feelings, but still… salute!

SharePoint requires Windows Server to run but most developers do not use Windows Server on their dev machines. You can always mess with virtual machines but I am not a big fan of that either.

Then I found out that (for dev purposes only!) you can manually install SharePoint 2010 on a Windows 7 machine (or even Vista). Luckily there is a free 180 day trial which should be plenty of time to get your feet wet. Here is what you need to do:

Download SharePoint 2010 Foundation:

http://www.microsoft.com/en-us/download/details.aspx?id=5970

Download SharePoint Trial:

http://technet.microsoft.com/en-us/evalcenter/ee388573.aspx

Now follow this instruction:

http://msdn.microsoft.com/en-us/library/ee554869(v=office.14).aspx

My advice is to follow the instruction as closely as possible and do not skip the steps that are “optional” (because they are not!). Also after installing and before running the setup wizard run Windows update several times + several restarts to get all available updates installed.

The road is certainly rocky but google bing is your friend and you’ll get there… took me about 2 hours to setup… And when you get the “Unable to create configuration database” error, install the MSChart control… Duh!?!

And at last, optionally download and install the SharePoint 2010 Designer which is free:

http://www.microsoft.com/en-us/download/details.aspx?id=16573 (32-bit)

http://www.microsoft.com/en-us/download/details.aspx?id=24309 (64-bit)

Now off to Pluralsight to watch SharePoint videos… 57 courses at 4-6h average equals to a lot…