Pipes for Windows 8

The Windows 8 version of Pipes is in the store and got a 1000 downloads in the first week. Working on the first update already with a “big orange button”™ for rate and review as well as few smaller improvements.

Download from Windows Store

#dvlupday in San Diego

I attended a DVLUP day in San Diego this weekend. The event started pretty rough with an ex-military security guard yelling at poor unwoken devs to order in lines before 8:00 AM. Besides that it was a lot of fun. I quite miss the energy of those events. Over a hundred devs sitting in a room in the Nokia office in San Diego, laptops open, coding… Several good presentations most notably on appstudio, unity and maps.

I worked on a video poker app for windows phone. I started with a code from a poker kata we did at work few weeks ago. I also had a control for a playing card that flips when tapped with a nice animation. And I build a working video poker game in less than 8h and it turned out pretty nice. I’m going to submit it to the app store in the following days for a DVLUP challenge.

I’m also planning to create an universal app based on that code base trying to reuse as much code as possible between winphone and win8 apps.

Update (5/5): the app just got published, download from the link below.

258x67_WPS_Download_cyan

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.