SwensenSoftware / unquote

Write F# unit test assertions as quoted expressions, get step-by-step failure messages for free
http://www.swensensoftware.com/unquote
Apache License 2.0
285 stars 26 forks source link

Improve evaluation when using array/list/seq functions #162

Closed cmeeren closed 6 months ago

cmeeren commented 1 year ago

(Not sure this is easily possible. Also, it may apply to all lambdas, not just in the case I describe below.)

Consider this code:

let width = 2

test
    <@
        "a test".Split(" ")
        |> Array.forall (fun s -> s.Length <= width)
    @>

This is displayed as:

"a test".Split(" ", None) |> Array.forall (fun s -> s.Length <= width)
[|"a"; "test"|] |> Array.forall (fun s -> s.Length <= width)
false

With two items in the list, it's no problem, but if the list contains many items, I may have to hunt around a bit to find the failing item.

It would be nice if the evaluation step could display the failing lamba:

"a test".Split(" ", None) |> Array.forall (fun s -> s.Length <= width)
[|"a"; "test"|] |> Array.forall (fun s -> s.Length <= width)
"test".Length <= width
4 <= 2
false
stephen-swensen commented 6 months ago

Hi @cmeeren - I don't think this is really possible / practical, since Unquote can't really know what Array.forall does in the general sense. However, there are ways you could rewrite your test to gain greater insight. For example:

    let width = 2
    test
        <@
            "a test".Split(" ")
            |> Array.map (fun s -> s.Length)
            |> Array.forall (fun l -> l <= width)
        @>

produces the following output:

"a test".Split(" ", None) |> Array.map (fun s -> s.Length) |> Array.forall (fun l -> l <= width)
[|"a"; "test"|] |> Array.map (fun s -> s.Length) |> Array.forall (fun l -> l <= width)
[|1; 4|] |> Array.forall (fun l -> l <= width)
false