Search And Destroy

Look out honey, ’cause I’m using technology!

Archive for April, 2011

QuickDotNetCheck 0.1 Released

Posted by kilfour on April 23, 2011

Available here.

There’s a couple of examples included :

1. The ListDeleteTests.

This one’s carried over from quicknet.
Here’s the requirement :

Write a function which takes a list of values and a value to remove as arguments, and return a copy of the inputlist that does not contain the value to remove.

Here’s the unit-test :

public class UnitTesting
{
    [Fact]
    public void Result_Does_Not_Contain_Removed_Value()
    {
        var inputList = new List<int> {1, 2, 3, 4, 5};
        var resultList = new ListDeleter().DoingMyThing(inputList, 4);
        Assert.DoesNotContain(4, resultList);
    }
}

Here’s a first implementation :
public class ListDeleter
{
    public IList<int> DoingMyThing(IList<int> theList, int iNeedToBeRemoved)
    {
        var result = theList.ToList();
        result.Remove(iNeedToBeRemoved);
        return result;
    }            
}

Unit test output :
1 passed, 0 failed, 0 skipped, took 1,21 seconds (xunit).

Here’s the (still quite verbose) QuickDotNetCheck test :
public class PropertyBasedTesting : Fixture
{
    private readonly ListDeleter listDeleter = new ListDeleter();
    private readonly IntGenerator intGen = new IntGenerator(1, 50);

    private IList<int> inputList { get; set; }
    private IList<int> resultList;
    private int toRemove;

    public override void Arrange()
    {
        toRemove = intGen.GetRandomValue();
        inputList = new DomainGenerator().With(() => intGen).Many<int>(1, 10).ToList();
    }

    protected override void Act()
    {
        resultList = listDeleter.DoingMyThing(inputList, toRemove);
    }

    [Spec]
    public void Result_Does_Not_Contain_Removed_Value()
    {
        Ensure.False(resultList.Contains(toRemove));
    }

    [Fact]
    public void VerifyAll()
    {
        new Suite(100, 20)
            .Register(() => new PropertyBasedTesting())
            .Run();
    }

    public override void Shrink(Func<bool> runFunc)
    {
        new ListShrinkingStrategy<PropertyBasedTesting, int>(
            this, e => e.inputList, new[] { -1, 0, 1 })
                .Shrink(runFunc);
    }

    public override string ToString()
    {
        var sb = new StringBuilder();
        sb.AppendLine(GetType().Name);
        sb.Append("inputList : ");
        inputList.ForEach(i => sb.AppendFormat("{0}, ", i));
        sb.Remove(sb.Length - 2, 2);
        sb.Append(".");
        sb.AppendLine();
        sb.AppendFormat("toRemove : {0}.", toRemove);
        return sb.ToString();
    }
}

Which outputs (f.i.) :
Test 'QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll' failed: QuickDotNetCheck.RunReport : 
--------------------Simplest Fail Case--------------------
1 : PropertyBasedTesting
inputList : 45, 45.
toRemove : 45.
----------------------------------------------------------

---- QuickDotNetCheck.Exceptions.FalsifiableException : 
Expected : False.
Actual : True.

	Suite.cs(151,0): at QuickDotNetCheck.Suite.Run()
	ListDeleteTests.cs(63,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll()
	----- Inner Stack Trace -----
	Ensure.cs(34,0): at QuickDotNetCheck.Ensure.False(Boolean flag)
	ListDeleteTests.cs(57,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.Result_Does_Not_Contain_Removed_Value()
	Fixture.cs(58,0): at QuickDotNetCheck.Fixture.AssertSpec(MethodInfo info)
	Implementation\EnumerableForEachExtension.cs(12,0): at QuickDotNetCheck.Implementation.EnumerableForEachExtension.ForEach[T](IEnumerable`1 enumerable, Action`1 action)
	Fixture.cs(63,0): at QuickDotNetCheck.Fixture.Assert()
	Suite.cs(138,0): at QuickDotNetCheck.Suite.Run()

0 passed, 1 failed, 0 skipped, took 1,78 seconds (xunit).

Or :
Test 'QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll' failed: QuickDotNetCheck.RunReport : 
--------------------Simplest Fail Case--------------------
1 : PropertyBasedTesting
inputList : 18, 18.
toRemove : 18.
----------------------------------------------------------

---- QuickDotNetCheck.Exceptions.FalsifiableException : 
Expected : False.
Actual : True.

	Suite.cs(151,0): at QuickDotNetCheck.Suite.Run()
	ListDeleteTests.cs(63,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll()
	----- Inner Stack Trace -----
	Ensure.cs(34,0): at QuickDotNetCheck.Ensure.False(Boolean flag)
	ListDeleteTests.cs(57,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.Result_Does_Not_Contain_Removed_Value()
	Fixture.cs(58,0): at QuickDotNetCheck.Fixture.AssertSpec(MethodInfo info)
	Implementation\EnumerableForEachExtension.cs(12,0): at QuickDotNetCheck.Implementation.EnumerableForEachExtension.ForEach[T](IEnumerable`1 enumerable, Action`1 action)
	Fixture.cs(63,0): at QuickDotNetCheck.Fixture.Assert()
	Suite.cs(138,0): at QuickDotNetCheck.Suite.Run()

0 passed, 1 failed, 0 skipped, took 1,76 seconds (xunit).

1. The BugHouseTest.
Also carried over from quicknet.
The BugHouse :

public class BugHouse
{
    private int count;
    public bool Run(int a)
    {
        if (count++ >= 3 && a == 6)
            throw new Exception();
        return true;
    }
}

Here’s the test code :
public class BugHouseTest
{
    [Fact]
    public void VerifyAll()
    {
        var suite = new Suite(50, 20);
        suite
            .Using(() => new BugHouseFixtureState())
            .Register(() => new BugHouseFixture(suite))
            .Run();
    }
}

public class BugHouseFixtureState
{
    public BugHouse BugHouse { get; private set; }
    public BugHouseFixtureState()
    {
        BugHouse = new BugHouse();
    }
}

public class BugHouseFixture : Fixture
{
    private readonly Suite suite;
    private int input { get; set; }
    private bool output;

    public BugHouseFixture(Suite suite)
    {
        this.suite = suite;
    }

    public override void Arrange()
    {
        input = new IntGenerator(0, 20).GetRandomValue();
    }

    protected override void Act()
    {
        output = suite.Get<BugHouseFixtureState>().BugHouse.Run(input);
    }

    private SimpleValuesShrinkingStrategy<BugHouseFixture> shrunk;

    public override void Shrink(Func<bool> runFunc)
    {
        shrunk =
            new SimpleValuesShrinkingStrategy<BugHouseFixture>(this, e => e.input);
        shrunk.AddValues(new object[] { -1, 0, 1 });
        shrunk.Shrink(runFunc);
    }

    public override string ToString()
    {
        var sb = new StringBuilder();
        sb.AppendLine(GetType().Name);
        if(!shrunk.Shrunk())
            sb.AppendFormat("input : {0}.", input);
        return sb.ToString();
    }

    [Spec]
    public void Always_Returns_True()
    {
        Ensure.True(output);
    }
}

Resulting in something like :
Test 'QuickDotNetCheck.Examples.BugHouseTest.VerifyAll' failed: QuickDotNetCheck.RunReport : 
--------------------Simplest Fail Case--------------------
1 : BugHouseFixture
2 : BugHouseFixture
3 : BugHouseFixture
4 : BugHouseFixture
input : 6.
----------------------------------------------------------

---- QuickDotNetCheck.Exceptions.FalsifiableException : 
Expected : True.
Actual : False.

	Suite.cs(151,0): at QuickDotNetCheck.Suite.Run()
	BugHouseTest.cs(26,0): at QuickDotNetCheck.Examples.BugHouseTest.VerifyAll()
	----- Inner Stack Trace -----
	Ensure.cs(27,0): at QuickDotNetCheck.Ensure.True(Boolean flag)
	BugHouseTest.cs(85,0): at QuickDotNetCheck.Examples.BugHouseFixture.Always_Returns_True()
	Fixture.cs(58,0): at QuickDotNetCheck.Fixture.AssertSpec(MethodInfo info)
	Implementation\EnumerableForEachExtension.cs(12,0): at QuickDotNetCheck.Implementation.EnumerableForEachExtension.ForEach[T](IEnumerable`1 enumerable, Action`1 action)
	Fixture.cs(63,0): at QuickDotNetCheck.Fixture.Assert()
	Suite.cs(138,0): at QuickDotNetCheck.Suite.Run()

0 passed, 1 failed, 0 skipped, took 1,30 seconds (xunit).

3. The ElaborateExample.
Experiments.
Not quite working at the moment, but interesting nonetheless.

Maintaining consistency :
As promised, the project on google code is up for deletion.

Posted in QuickDotNetCheck | Leave a Comment »

QuickGenerate 0.4.4 Released

Posted by kilfour on April 22, 2011

In a different place.

This one’s about moving to github mostly.

Squashed out a (small) bug here and there.
0.4.3. was already pretty stable.
The, much downloaded, 0.4.2. unfortunately contains a hell of a nasty bug :-( .

New features :
GeneratorOptions.Counter() : supplying starting value and step size :

public class CustomizeTypeLongCounterTests
{
    [Fact]
    public void SimpleAppend()
    {
        var domainGenerator =
            new DomainGenerator()
                .With<SomethingToGenerate>(g => g.Counter(e => e.MyProperty));

        Assert.Equal(1, domainGenerator.One<SomethingToGenerate>().MyProperty);
        Assert.Equal(2, domainGenerator.One<SomethingToGenerate>().MyProperty);
        Assert.Equal(3, domainGenerator.One<SomethingToGenerate>().MyProperty);
    }

    [Fact]
    public void SupplyingStartingValue()
    {
        var domainGenerator =
            new DomainGenerator()
                .With<SomethingToGenerate>(g => g.Counter(e => e.MyProperty, 5));

        Assert.Equal(5, domainGenerator.One<SomethingToGenerate>().MyProperty);
        Assert.Equal(6, domainGenerator.One<SomethingToGenerate>().MyProperty);
        Assert.Equal(7, domainGenerator.One<SomethingToGenerate>().MyProperty);
    }

    [Fact]
    public void SupplyingStartingValueAndStep()
    {
        var domainGenerator =
            new DomainGenerator()
                .With<SomethingToGenerate>(g => g.Counter(e => e.MyProperty, 5, 2));

        Assert.Equal(5, domainGenerator.One<SomethingToGenerate>().MyProperty);
        Assert.Equal(7, domainGenerator.One<SomethingToGenerate>().MyProperty);
        Assert.Equal(9, domainGenerator.One<SomethingToGenerate>().MyProperty);
    }

    public class SomethingToGenerate
    {
        public long MyProperty { get; set; }
    }
}

Components :
public class ComponentsTests
{
    private readonly DomainGenerator domainGenerator;

    public ComponentsTests()
    {
        domainGenerator =
            new DomainGenerator()
                .Component<SomeComponent>()
                .With(42);
    }

    [Fact]
    public void GeneratorIsApplied()
    {
        Assert.Equal(42, domainGenerator.One<SomethingToGenerate>().One.Answer);
        Assert.Equal(42, domainGenerator.One<SomethingToGenerate>().Two.Answer);
        Assert.Equal(42, domainGenerator.One<SomethingElseToGenerate>().Three.Answer);
    }

    public class SomethingToGenerate
    {
        public SomeComponent One { get; set; }
        public SomeComponent Two { get; set; }
    }

    public class SomethingElseToGenerate
    {
        public SomeComponent Three { get; set; }
    }

    public class SomeComponent
    {
        public int Answer { get; set; }
    }
}

As promised, the project on google code is up for deletion.

Posted in QuickGenerate | Leave a Comment »

My Favourite Language : Anything You Can Do Haskell Can Do Better

Posted by kilfour on April 21, 2011

F.i. : one of the canonical ‘wax on/wax off’ TDD exercises : ‘The Bowling Game

The Haskell solution :

score ([]) = 0
score (x:[]) = x
score (x:y:[]) = x + y
score (x:y:z:[]) = x + y + z
score (x:y:z:xs) = if  (x == 10)             then x + y + z +  score(y:z:xs)
                   else if (((x + y) == 10)) then x + y + z +  score(z:xs)
                   else x + y + score(z:xs)

Posted in My Favourite | Leave a Comment »

QuickDotNetCheck Moved to GitHub

Posted by kilfour on April 21, 2011

Which is here.

Maintaining consistency :
Seeing as how I’ve never been in favour of looking back, the google code project will be deleted as soon as there’s a new release.

Posted in QuickDotNetCheck | Leave a Comment »

QuickGenerate : The Competition

Posted by kilfour on April 21, 2011

As I stated before, the QuickGenerate lib is a side effect from my interest in property based testing.

At the time I could not find anything suitable to fill in for the part of QuickCheck’s Arbitrary class.

So I rolled out my own.

And for me, it does all it that it is supposed to do.
Keep in mind that I’m not using this to populate databases,
Keep in mind that I’m not using this as builders in my unit tests.
I am using it to supply quickdotnetcheck with shrinkable input.

Well, I lied, I do use it to populate databases and all that, … these days.
But the point is, that was not the goal.

A while ago I said to a colleague : ‘If I had known about NBuilder while I was writing QuickNet, QuickGenerate would probably not exist’.
Today I reckon that ain’t true.
I would have run into an issue sooner or later because the goals of both libs are too different.
That applies too all others mentioned below aswell.

NBuilder :
Cool api, mature lib. Probably the easiest one to use. Struggles when used in tests (I think it’s likely that this wasn’t the main focus of the project).

AutoFixture :
Really aimed at simplifying your test code. It offers (amongst a lot of other things such as auto-mocking, f.i.) random data generation in order to implement the test data builder pattern (shines in unit tests).

AutoPoco :
A co-worker of mine recently pointed out this one. It looks really powerfull. And thanks to aforementioned co-worker, who submitted a patch recently, it now has a feature which quickgenerate just can’t produce. Once again, … thank you Jan ;-) .

There’s propably others.

I think most, if not all features of ‘the competition’ can also be achieved reasonably painlessly with quickgenerate.
And most, if not all quickgenerator features can also be achieved reasonably painlessly using one of ‘the competition’ libs.

So whichever one you prefer using, depends on your goal.
And on how close that relates to the goals of the respective lib.

Posted in Rants | 1 Comment »

QuickGenerate moved to GitHub

Posted by kilfour on April 20, 2011

Which is here.

A move not made, solely in order to be one of the cool kids on the block.

First of all, I’m currently using Agatha at work and it is hosted on GitHub so I really needed to know how to get the latest version ;-) .

But also, it is an invitation : “Go forth, and fork at will, … “.

Seeing as how I’ve never been in favour of looking back, the google code project will be deleted as soon as there’s a new release.

Posted in QuickGenerate | Leave a Comment »

And Broken Guitar Strings

Posted by kilfour on April 19, 2011

Recently I had to write a rather tricky data migration.
Everything got developed tdd style and coverage was almost a 100%.

But it failed in the wild.

Trying to write unit tests in order to smoke out all the edge cases wasn’t working.
Looking up the culprit record (in a huge dbase IV db), tracing down all the relations and trying to recreate the arrange part was a pain.

So I tried a different approach.
I took me some effort to write a property based test (and even more effort in order to produce the ‘SimplestPossibleFailCase’), but once that was done the test pointed out each and every bug out to me with impunity.

Writing a unit test reproducing the bug after seeing it once, was trivial.

Fixing the bug, once it was identified, also, … trivial.

That always makes me happy, … hurray, …

Posted in Property Based Testing | Leave a Comment »

Real Bleeding Fingers …

Posted by kilfour on April 19, 2011

I started working on a new project, earlier this week.

It is very similar to the one I was involved with when I first came up with quicknet.

Even though there’s not that much (reusable) code to show for my bleeding fingers, learning and implementing property based testing techniques, has improved the quality of my work.

I reckon this project will allow me to consolidate some of ‘this code/these techniques’ into quickdotnetcheck.

Posted in Property Based Testing | Leave a Comment »

Fasterflect

Posted by kilfour on April 13, 2011

Through the ditto project, which I haven’t really tried out in the wild, but I must say, I do like the approach outlined, I came across Fasterflect.

Really nice lib.
It works (i.e. : it has automated tests).
It’s clean.
It’s fast.

Expect some QuickDotNetCheck updates in a month or so.
One of the main reasons that has been holding me back, regarding quickdotnetcheck, is having to write nasty reflection heavy methods.

Fasterflect really makes that kind of stuff a breeze.

And the DeepClone method truly helps out when writing properties involving pre- or postconditions.

Experiments are going on as we speak ;-) .

Posted in C# Musings, Checking Stuff Out | Leave a Comment »

QuickGenerate : State of the Test Suite

Posted by kilfour on April 6, 2011

Number of tests :

Coverage :

The uncovered bits :

public static class Maybe
{
    public static void Do(Action action)
    {
        if (new BoolGenerator().GetRandomValue())
            action();
    }
}

And :
public static class HardCodeThatDate
{
    public static DateTime January(this int day, int year) { return new DateTime(year, 1, day); }
    public static DateTime February(this int day, int year) { return new DateTime(year, 2, day); }
    public static DateTime March(this int day, int year) { return new DateTime(year, 3, day); }
    public static DateTime April(this int day, int year) { return new DateTime(year, 4, day); }
    public static DateTime May(this int day, int year) { return new DateTime(year, 5, day); }
    public static DateTime June(this int day, int year) { return new DateTime(year, 6, day); }
    public static DateTime July(this int day, int year) { return new DateTime(year, 7, day); }
    public static DateTime August(this int day, int year) { return new DateTime(year, 8, day); }
    public static DateTime September(this int day, int year) { return new DateTime(year, 9, day); }
    public static DateTime October(this int day, int year) { return new DateTime(year, 10, day); }
    public static DateTime November(this int day, int year) { return new DateTime(year, 11, day); }
    public static DateTime December(this int day, int year) { return new DateTime(year, 12, day); }
}

The Complex namespace contains the fully tested NumericStringGenerator, the badly tested StringBuilderGenerator and the totally untested EmailGenerator and WordGenerator.
Above sentence would have been completely redundant if I had thought about the screenshot slightly longer, I now realize.

The Primitives namespace contain the PrimitiveGenerators.
Most of these are carried over from the QuickNet, and then later the QuickNet.Generators, project.
In both those projects they were tested using QuickNet.
These tests are still running, but they’re no longer publicly available, because I change my mind (about syntax and stuff) far too often.

The Modifying namespace is something that needs to be worked on.

Conclusion :
Not a lot of tests (only a mere 105), and still, acceptable coverage for all of the important stuff such as the DomainGenerator.
And most of the tests really are unit-tests.
I’ve posted examples in the past but here’s another recent and typical DomainGenerator test :

public class IgnoreConventionTypedTests
{
    private readonly DomainGenerator domainGenerator;

    public IgnoreConventionTypedTests()
    {
        domainGenerator =
            new DomainGenerator()
                .Ignore<SomethingToGenerate, int>(something => something.Id)
                .Ignore<SomethingElseToGenerate, string>(somethingElse => somethingElse.Id);
    }

    [Fact]
    public void StaysDefaultvalue()
    {
        10.Times(
            () =>
                {
                    var something = domainGenerator.One<SomethingToGenerate>();
                    Assert.Equal(0, something.Id);
                });

        10.Times(
            () =>
                {
                    var something = domainGenerator.One<SomethingElseToGenerate>();
                    Assert.Null(something.Id);
                });
    }

    public class SomethingToGenerate
    {
        public int Id { get; set; }
    }

    public class SomethingElseToGenerate
    {
        public string Id { get; set; }
    }
}

So there seems to be very little code that’s doing quite a bit of work.

That always makes me happy, … hurray, …

Posted in Rants | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.