Search code examples
f#functional-programmingnunitfunctional-testing

Using List.map with should throw Exception


let d5=["IIII";"VVVV";"XXXX";"LLLL";"MMMM"] 

[<Test; ExpectedException(typeof<System.Exception>)>]
    member this.
        ``More than 3 times repetive characters `` ()=
        //Missing part

This test isn't working. I have converter which parsers Rome numbers. But it works wrong for

["IIII";"VVVV";"XXXX";"LLLL";"MMMM"]

When you send convert("IIII") it returns 4 , but it should have given System.Exception error. I need to write NUnit test (not fix converter) which maps every string in the list and passes when every single of them returns error. Otherwise fails. The test I want to write is as following.

d5=["IIII";"VVVV";"XXXX";"LLLL";"MMMM"]
convert each d5 element
if each of them is giving system.exception error 
then test is successfull
else test is failed

Any solution? Any idea. . If you are interested convert method is here Convert Method


Solution

  • What you want is to test all values separately:

    open NUnit.Framework
    open FsUnit
    
    type ConvertTest() = 
    
        let convert s = 
            if String.length s > 3 then failwith "Something's wrong"
            else String.length s
    
        [<Theory>]
        member this.``More than 3 characters throws``([<Values("IIII", "VVVVVV", "XXXX", "LLLL", "MMMM")>] s) = 
            (fun () -> convert s |> ignore) |> should throw typeof<System.Exception>
    
        [<Theory>]
        member this.``Less than 4 characters returns length``([<Values("II", "VV", "XXX", "LLL", "MMM")>] s) = 
            convert s |> should equal s.Length
    

    I deliberately changed the convert method to a slightly different implementation (as you gave none) but it should be obvious how to proceed.