Using assertion messages with Unquote in F#?

Viewed 86

Some test assertion frameworks allow you to add a custom message to your assertion, such as the following using NUnit:

Assert.AreEqual(1, result, "the result should be one")

Is it possible to do the same when using Unquote in F#?

test <@ result = 1 @>

Update

The closest I have got to this is to add a simple comment inside the quoted expression. As my motivation for this was to document what is being verified (I tend to assert more than once!), this is quite adequate for my needs.

test <@ 
        // the result should be one
        result = 1 
     @>

Another Update

I've been using Stephen's suggestion to use ignore "description here";, which I really like. I find it easier to read if I declare my own function like so:

> let inline checking _ = ()
> let result = 2;;
> test <@ checking "the result should be one"; result = 1 @>;;

Test failed:

checking "the result should be one"; result = 1
(); result = 1
result = 1
2 = 1
false
1 Answers

You could do something like this (using F# Interactive)

> let result = 2;;
> test <@ ignore "the result should be one"; result = 1 @>;;

Test failed:

ignore "the result should be one"; result = 1
(); result = 1
result = 1
2 = 1
false

But if you use variable names like expected and actual, Unquote's output can be rich enough to obviate the need for such supplementary comments

> let actual = 2;;
> let expected = 1;;            
> test <@ actual = expected @>;; 

Test failed:

actual = expected
2 = 1
false
Related