mardi 29 novembre 2016

Trying to put together a few assertion functions and i cannot get a try with to work

I am just learning F#, so i am trying a few things out(i do know could just use XUnit or something else)

I have the following assertion method, and the idea is that it should take an expected exception and the function that it expects to throw this exception, then execute the function and inside of the with test if the exception thrown is the same as the expected one.

let assertException (testName : string) (expected : 'a when 'a :> Exception) functionToBeTested =
    try
        functionToBeTested
        (false)
    with
    | :? Exception as someException when someException :? expected ->
            printTestResultInMiddle (sprintf "Test: %s PASSED: Raised expected exception %A" testName expected) true 
            (true)
    | _ ->
        printTestResultInMiddle (sprintf "Test: %s FAILED: expected exception %A" testName expected) false
        (false)

It gives me the error Unexpected symbol '(' in pattern matching. Expected '->' or other token. in the line where i try to call a print method. Shouldn't i be able to treat this try ... with as a

match ... with

??

And another question, could i do this alot easier or?

Aucun commentaire:

Enregistrer un commentaire