0

Ihm struggling to implement a seemingly simple testing problem with HSpec: I'd like to test a function

myFunc :: (Exception e) a -> Either e MyRecord

In one test case, I'd like to first assert that the return value is a Right value, then unwrap the value to assert its contents. I haven't figured out a way to do this without cumbersome case expressions. Is there an assertion operator that simultaneously allows for pattern matching?

What I am looking for is something along the following lines (pseudo-Haskell):

describe "myFunc" $ do
    it "should return funky stuff" $ do
      let result = myFunc <testArgument>
      result `shouldBe` (Right testRecord)
      testRecord `shouldBe` <expectedRecord>

This is just to illustrate the idea, pattern matching on the RHS doesn't work, of course.

Ulrich Schuster
  • 1,670
  • 15
  • 24
  • 2
    Why isn't `result \`shouldBe\` (Right testRecord)` enough? The `shouldBe` function has an `Eq` constraint, and `Either` is only an `Eq` instance when both `a` and `b` are `Eq` instances. Thus, that assertion should only pass if the content of `Right` is equal to the expected result. – Mark Seemann May 03 '21 at 05:24
  • @MarkSeemann Isn't the very problem that the `Left` type of the `Either` doesn't have an `Eq` instance? – Joseph Sible-Reinstate Monica Aug 08 '21 at 18:09

1 Answers1

1

Use a custom function with shouldSatisfy, like this: result `shouldSatisfy` either (const False) (<expectedRecord> ==)