I started using FsUnit to test F# code. It makes it possible to express assertion in F# style, for example:
[<Test>]
member this.``Portugal voted for 23 countries in 2001 Eurovision contest``() =
this.totalVotes
|> getYearVotesFromCountry "Portugal" 2001
|> Seq.length
|> should equal 23
Note "sould equal 23" that I get from FsUnit. Here's how FsUnit define it:
let equal x = new EqualConstraint(x)
With floating point numbers it's not that simple. I have to use EqualConstraint with Within method. It naturally fits C#:
Assert.That(result).Is.EqualTo(1).Within(0.05);
Of course I'd like to be able to write in F#:
result |> should equal 1 within 0.05
But that does not work. I ended up defining a new function:
let almostEqual x = (new EqualConstraint(x)).Within(0.01)
or if I want to parameterize precision, I can specify it as a second argument:
let equalWithin x y = (new EqualConstraint(x)).Within(y)
But none of them are pretty. I would like to define "within" function in a more natural way for F#, so it can be used together with equal. But F# does not support method overloading, so it looks like I can't define it in such a way so "equal" can be used either alone or together with "within".
Any great ideas?