Wednesday 21 November 2012

TestCaseData mock verifications

It is common practice to use an the TestCaseSource attribute to specify test data and the expected result in the format of IEnumerable<TestCaseData> like the example below.

[Test, TestCaseSource("TestCases")]
public int ShouldReturnTheExpectedValueWhenCalled(int testCase)
{
 context.Setup(c => c.Number).Returns(testCase);

 return context.CalculateSomething();
}

private IEnumerable<TestCaseData> TestCases
{
 get
 {
  yield return new TestCaseData(4).Returns(16);
  yield return new TestCaseData(5).Returns(25);
  yield return new TestCaseData(-1).Throws(typeof( Exception));
 }
}

For some tests the method shown above is sufficient but what about when you are testing a method that does not return a value and executes a command? In this circumstance you will be using mocking to assert that the method is operating correctly. The only problem with this is that I could not find any documentation for using a mock verification with TestCaseData, but I was able to get the functionality required from the example below.

[Test, TestCaseSource(&quot;TestCases&quot;)]
public void ShouldAlwaysCallSaveMethod(int testCase, Action[] verifications)
{
 // Act
 context.DoSomething(testCase);

 // Assert
 foreach (var verification in verifications)
 {
  verification.Invoke();
 }
}

private IEnumerable&lt;TestCaseData&gt; TestCases
{
 get
 {
  yield return new TestCaseData(random.Next(), 
   new Action[] 
   {
    () =&gt; service.Verify(s =&gt; s.SomeAction(It.IsAny<int>()), Times.Once()),
    () =&gt; service2.Verify(s =&gt; s.SomeAction(It.IsAny<int>()), Times.Once())
   });

  yield return new TestCaseData(0,
   new Action[]
   {
    () =&gt; service.Verify(s =&gt; s.SomeAction(It.IsAny<int>()), Times.Once()),
    () =&gt; service2.Verify(s =&gt; s.SomeAction(It.IsAny<int>()), Times.Never())
   });

  yield return new TestCaseData(-(random.Next()), 
   new Action[]
   {
    () =&gt; service.Verify(s =&gt; s.SomeAction(It.IsAny<int>()), Times.Never()),
    () =&gt; service2.Verify(s =&gt; s.SomeAction(It.IsAny<int>()), Times.Never())
   });
 }
}

Friday 2 November 2012

TypeScript QuadTree part 2

This is the second part of my series on implementing a QuadTree in TypeScript, the first post can be found here. This posts focuses on another prerequisite of my QuadTree, a list of Quads (as the QuadTree itself doesn't really care about how they are stored). There is currently no generic type support in TypeScript, but they are planning it in the future. So I have made a basic list implementation that can be later modified to become a generic list like that provided by C#, this class could even potentially implement the iterator pattern (IEnumerable in C#) in the future.

interface IQuadList {
    add(quad: IQuad);
    count(): number;
    clear();
    getQuad(index: number): IQuad;
}

class QuadList implements IQuadList {
    private list: IQuad[];
    private size: number;

    constructor (startSize : number = 1) {
        if (startSize < 1)
            startSize = 1;

        this.size = 0;
        this.list = new Array(startSize);
    }

    add(quad: IQuad) {
        if (!this.isRoomInList()) {
            var newArray = new Array(this.list.length * 2);
            this.list = this.copyToArray(newArray);
        }

        this.list[this.size] = quad;
        this.size += 1;
    }

    clear() {
        for (var i = 0; i < this.list.length; i++) {
            this.list[i] = null;
        }

        this.size = 0;
    }

    copyToArray(destination : IQuad[]) {
        for (var i = 0; i < this.size; i++) {
            destination[i] = this.list[i];
        }

        return destination;
    }

    count(): number {
        return this.size;
    }

    getQuad(index: number): IQuad {
        return this.list[index];
    }

    isRoomInList() : bool {
        return this.size < this.list.length;
    }
}

Now I run tsc.exe over the TypeScipt and I get my JavaScript.

var QuadList = (function () {
    function QuadList(startSize) {
        if (typeof startSize === "undefined") { startSize = 1; }
        if(startSize < 1) {
            startSize = 1;
        }
        this.size = 0;
        this.list = new Array(startSize);
    }
    QuadList.prototype.add = function (quad) {
        if(!this.isRoomInList()) {
            var newArray = new Array(this.list.length * 2);
            this.list = this.copyToArray(newArray);
        }
        this.list[this.size] = quad;
        this.size += 1;
    };
    QuadList.prototype.clear = function () {
        for(var i = 0; i < this.list.length; i++) {
            this.list[i] = null;
        }
        this.size = 0;
    };
    QuadList.prototype.copyToArray = function (destination) {
        for(var i = 0; i < this.size; i++) {
            destination[i] = this.list[i];
        }
        return destination;
    };
    QuadList.prototype.count = function () {
        return this.size;
    };
    QuadList.prototype.getQuad = function (index) {
        return this.list[index];
    };
    QuadList.prototype.isRoomInList = function () {
        return this.size < this.list.length;
    };
    return QuadList;
})();

Now that we have got a basic list implemented my next post I will get started on the actual quad tree class. The TypeScript so far has enabled me to have type checking at compile time and the joy of not worrying about got-ya's in JavaScript.