lsmor
lsmor

Reputation: 5063

Alternative instance for Tests?. HSpec

I'd like to write a test suite which expresses the following: function f either is not implemented or it is implemented with the some tests.

So I can check both things separatelly. I'd like to do something like spec1 <|> spec2 where the alternative instance is similar to Maybe's: fail if everything fails.

So let me give an examples

-- if function is not implemented
average :: Float -> Float -> Float
average x y = error "function not implemented"

-- This test passes
average 1 2 `shouldThrow` errorCall "function not implemented"



-- if function is implemented
average :: Float -> Float -> Float
average x y = (x + y) / 2

-- This test passes
average 1 2 `shouldBe` 0.5

The context of the problem is that I would like to write an exercise sheet with automated tests, and I'd like the tests to pass if the student hasn't implemented the exercise or if she has done it correctly. Therefore, I would like to write something like

-- Using alternative
spec = describe "test group 1" $ do
   it "testing average" $
     (average 1 2 `shouldThrow` errorCall "function not implemented")
     <|>
     (average 1 2 `shouldBe` 0.5)

-- Or maybe using a built-in combinator I'm not aware of.
spec = describe "test group 1" $ do
   it "testing average" $ anySuccess
     [ (average 1 2 `shouldThrow` errorCall "function not implemented")
     , (average 1 2 `shouldBe` 0.5)
     ]

Upvotes: 3

Views: 88

Answers (0)

Related Questions