FsUnit icon indicating copy to clipboard operation
FsUnit copied to clipboard

throwWithRegexMessage

Open codingedgar opened this issue 5 years ago • 6 comments

I find myself using regex to match dynamic errors, could this be added?

let throwWithRegexMessage (m:string) (t:System.Type) = CustomMatcher<obj>(sprintf "%s \"%s\"" (string t) m,
                                                                    fun f -> match f with
                                                                             | :? (unit -> unit) as testFunc ->
                                                                                 try
                                                                                   testFunc()
                                                                                   false
                                                                                 with
                                                                                 | ex -> ex.GetType() = t && Regex.IsMatch (ex.Message, m)
                                                                             | _ -> false )

(fun () -> AsyncWrite2 (streamName) (int64(EventStore.ClientAPI.ExpectedVersion.Any)) (event)
               |> Async.RunSynchronously
               |> ignore)
    |> should (throwWithRegexMessage @"EventStoreConnection 'ES-.{36}' is not active.") typeof<System.AggregateException>

Maybe there could be a better message I can make the PR if worth including.

codingedgar avatar Feb 21 '20 02:02 codingedgar

Also, if throwWithMessage fails, then it prints:

Expected: System.AggregateException "EventStoreConnection 'ES-.{36}' is not active."
Actual:   was ProgramTest+try async write 2@49

Would expect Actual to have the same form as Expected instead the function id

codingedgar avatar Feb 21 '20 02:02 codingedgar

@sergey-tihon Suggestions or ideas?

CaptnCodr avatar Jun 19 '20 19:06 CaptnCodr

could this be added?

I do not mind if throwWithRegexMessage will be added with tests for all framework

Would expect Actual to have the same form as Expected instead the function id

This is quite tricky, xUnit and MSUnit mismatch description builder rely on .ToString() that is defined for F# types in a way now very pleasant for our use case (would be way more useful to use %A)

I am not sure that there is a short of simple fix for this, that does not break error messages for other operators.

sergey-tihon avatar Jun 20 '20 08:06 sergey-tihon

that is defined for F# types in a way now very pleasant for our use case (would be way more useful to use %A)

Then maybe I'm not using it correctly? because idk how to work with Actual: was ProgramTest+try async write 2@49, I expected something more like this:

image

codingedgar avatar Jun 20 '20 19:06 codingedgar

I am not aware about such diffs for .net test frameworks. I believe it cannot be added to DSL library (like FsUnit), It should be supposrted by framework (xUnit, NUnit, MSUnit) and runner (console runner) that will provide colorful output.

AFAIK, Expecto support string diff (but for a git-like diff for objects) https://github.com/haf/expecto/releases/tag/v2.2.0

sergey-tihon avatar Jun 25 '20 05:06 sergey-tihon

Diffract can show you a diff of your F# types etc., see: https://github.com/d-edge/Diffract

CaptnCodr avatar Oct 19 '21 05:10 CaptnCodr