haskellhunit

Tasty HUnit test against Either


I am fairly new to Haskell, and I was wondering if there is a way to test a pattern match, as in erlang. An example is a function that returns an Either a b, Can I match against Left b, whilst ignoring b itself?

In my case, I have Either String Error. I simply want to test if an error occurs, but it's a bit tedious to write out the entire error text for each test case, so I simply want to assert that it in fact does return a Right error.


Solution

  • Sure! You can do something like

    assertEqual “This will return Right” True ( isRight result )
    

    https://hackage.haskell.org/package/tasty-hunit-0.10.0.2/docs/Test-Tasty-HUnit.html#v:assertEqual

    https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Either.html#v:isRight