When writing tests in F# I am trying to generate useful messages about the state that caused errors. In python I would include all the locals(), so they are easily accessible in the test trace.
Is there a similar construct in F#?
I have been searching the web and the excellent fsharpforfunandprofit site, as well as gone through the list of reserved keywords.
Here is some code I would like to be able to do.
[<Property>]
let some_test x =
let example_inner_helper y =
let z = y + x
// Example of the meta-construction i am looking for
let scope = {|
local = {| y = y; z = z |};
capture = {|
local = {| x = x |};
capture = {| (* ... *) |};
|};
|}
x = z |@ sprintf "require %A=%A (%A)" x y scope
example_inner_helper (x+x)
Which would produce the very useful output
Tests.some_test
Source: Tests.fs line 105
Duration: 51 ms
Message:
FsCheck.Xunit.PropertyFailedException :
Falsifiable, after 1 test (1 shrink) (StdGen (387696160,296644521)):
Label of failing property: require 1=2 ({ capture = { capture = {}
local = { x = 1 } }
local = { y = 2
z = 3 } })
Original:
-1
Shrunk:
1
However, I am having to explicitly capture the information that a construct like "scope" could automatically provide. Resulting in ugly, error prone stuff like
let ``firstAscendingLastDescendingPartE Prop`` (a: Extent<int>) b =
let validateT ea eb =
let checkXbeforeY ex ey headx restx =
match restx with
| ValueNone -> // No rest
(ex = headx) |@ sprintf "no rest: ex = headx (%A = %A)" ex headx
.&. ((intersectE ex ey) = ValueNone) |@ sprintf "no rest: ex ∩ ey = ∅ (%A ∩ %A)" ex ey
| ValueSome rex -> // Some rest ->
// headx and restx combines to ex
((expandE headx rex) = ex) |@ sprintf "rest: headx+rex = ex (%A...%A)" headx rex
.&. (exactlyTouchesE headx rex) |@ sprintf "rest: headx ends where rex starts (ex=%A ey=%A headx=%A restx=%A)" ex ey headx restx
.&. (exactlyTouchesE headx ey) |@ sprintf "rest: headx ends where ey starts (ex=%A ey=%A headx=%A restx=%A)" ex ey headx restx
....
(Of course, I don't care about the actual type of the "scope" construct)
Regarding unquote
I already looked at unquote which is quite cute and looks about right. But it limits the code quite a bit:
- Error FS3155 A quotation may not involve an assignment to or taking the address of a captured local variable
- Error FS1230 Inner generic functions are not permitted in quoted expressions. Consider adding some type constraints until this function is no longer generic.
I have code that looks a bit like:
[<Struct>]
type X<'T when 'T: comparison and 'T: equality> =
val public first: 'T
val public last: 'T
new (first: 'T, last: 'T) = {
first =
(if last <= first then
invalidOp (sprintf "first < last required, (%A) is not < (%A)" first last)
else first)
last = last;
}
// ..
So I have issues with tests using the two constructs below (causing the above errors).
let quote_limits () =
let x first last = X(first, last)
let validate (x: X<'a>) = x.first < x.last
let a = X(0, 1)
<@ a.first = 0 @> |> test
<@ validate a @> |> test
The first one i can workaround with functions to access the struct parts, but the limitation on generics is PITA.