Coder Social home page Coder Social logo

hspec-expectations's Introduction

Catchy combinators for HUnit

(inspired by ScalaTest's ShouldMatchers)

The three main primitives are shouldBe, shouldSatisfy and shouldThrow. They can be used with HUnit, or any framework that integrates with HUnit, like test-framework or Hspec.

An introductory example

Here is an example that uses Hspec. It's a partial specification of itself.

import Test.Hspec
import Control.Exception

main :: IO ()
main = hspec $ do
  describe "shouldBe" $ do
    it "asserts equality" $ do
      "foo" `shouldBe` "foo"

  describe "shouldSatisfy" $ do
    it "asserts that a predicate holds" $ do
      "bar" `shouldSatisfy` (not . null)

  describe "shouldThrow" $ do
    it "asserts that an exception is thrown" $ do
      evaluate (1 `div` 0 :: Int) `shouldThrow` (== DivideByZero)

shouldBe

shouldBe is just an alias for HUnit's @?=.

shouldSatisfy

shouldSatisfy asserts that some predicate holds for a given value.

"bar" `shouldSatisfy` (not . null)

It is similar to HUnit's assertBool, but gives a useful error message.

>>> 23 `shouldSatisfy` (> 42)
*** Exception: HUnitFailure "23 did not satisfy predicate!"

shouldReturn

shouldReturn asserts that an action returns a given value.

launchMissiles `shouldReturn` Left "permission error"

shouldThrow

shouldThrow asserts that an exception is thrown. The precise nature of that exception is described with a Selector.

error "foobar" `shouldThrow` anyException

A Selector is a predicate, it can simultaneously constrain the type and value of an exception.

throw DivideByZero `shouldThrow` (== DivideByZero)

To select all exceptions of a given type, const True can be used.

error "foobar" `shouldThrow` (const True :: Selector ErrorCall)

For convenience, predefined selectors for some standard exceptions are provided.

error "foobar" `shouldThrow` anyErrorCall

Some exceptions (like ErrorCall) have no Eq instance, so checking for a specific value requires pattern matching.

error "foobar" `shouldThrow` (\e -> case e of
    ErrorCall "foobar" -> True
    _ -> False
    )

For such exceptions, combinators that construct selectors are provided. Each combinator corresponds to a constructor; it takes the same arguments, and has the same name (but starting with a lower-case letter).

error "foobar" `shouldThrow` errorCall "foobar"

hspec-expectations's People

Contributors

sol avatar qoelet avatar supki avatar soareschen avatar adinapoli avatar erkan-yilmaz avatar jefffrey avatar soenkehahn avatar

Watchers

Oleg Grenrus avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.