hspec-expectations-pretty (empty) → 0.1
raw patch · 4 files changed
+112/−0 lines, 4 filesdep +basedep +deepseqdep +hspec-expectationssetup-changed
Dependencies added: base, deepseq, hspec-expectations, wl-pprint-extras, wl-pprint-terminfo
Files
- LICENSE +19/−0
- Setup.hs +2/−0
- Test/Hspec/Expectations/Pretty.hs +57/−0
- hspec-expectations-pretty.cabal +34/−0
+ LICENSE view
@@ -0,0 +1,19 @@+Copyright (c) 2011-2013 Simon Hengel <sol@typeful.net>++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Test/Hspec/Expectations/Pretty.hs view
@@ -0,0 +1,57 @@+{-# LANGUAGE DeriveDataTypeable #-}+module Test.Hspec.Expectations.Pretty where++import Data.Dynamic (Typeable)+import Test.Hspec.Expectations (Expectation)+import Text.PrettyPrint.Free (pretty, renderPretty, displayS)+import System.Console.Terminfo.PrettyPrint (PrettyTerm(..))+import Control.Exception as E+import Control.DeepSeq (deepseq)+import Control.Monad (unless)++infix 1 `shouldBe`, `shouldSatisfy`, `shouldReturn`++data HspecFailure = HspecFailure String deriving (Typeable)+instance Show HspecFailure where show (HspecFailure msg) = msg+instance Exception HspecFailure++assertFailure :: String -- ^ A message that is displayed with the assertion failure + -> Expectation+assertFailure msg = msg `deepseq` E.throwIO (HspecFailure msg)++-- | Asserts that the specified actual value is equal to the expected value.+-- The output message will contain the prefix, the expected value, and the +-- actual value.+-- +-- If the prefix is the empty string (i.e., @\"\"@), then the prefix is omitted+-- and only the expected and actual values are output.+assertEqual :: (Eq a, PrettyTerm a) => String -- ^ The message prefix + -> a -- ^ The expected value + -> a -- ^ The actual value+ -> Expectation+assertEqual preface expected actual =+ unless (actual == expected) $ assertFailure msg+ where+ msg = (if null preface then "" else preface ++ "\n") +++ "expected: " ++ render (pretty expected) +++ "\n but got: " ++ render (pretty actual)+ render x = displayS (renderPretty 0.4 80 x) ""+ ++-- |+-- @actual \`shouldBe\` expected@ sets the expectation that @actual@ is equal+-- to @expected@ (this is just an alias for `@?=`).+shouldBe :: (Show a, PrettyTerm a, Eq a) => a -> a -> Expectation+actual `shouldBe` expected = assertEqual "" expected actual++-- |+-- @v \`shouldSatisfy\` p@ sets the expectation that @p v@ is @True@.+shouldSatisfy :: (Show a, PrettyTerm a) => a -> (a -> Bool) -> Expectation+v `shouldSatisfy` p = assertBool ("predicate failed on: " ++ show v) (p v)+ where assertBool msg b = unless b (assertFailure msg)++-- |+-- @action \`shouldReturn\` expected@ sets the expectation that @action@+-- returns @expected@.+shouldReturn :: (Show a, PrettyTerm a, Eq a) => IO a -> a -> Expectation+action `shouldReturn` expected = action >>= (`shouldBe` expected)
+ hspec-expectations-pretty.cabal view
@@ -0,0 +1,34 @@+name: hspec-expectations-pretty+version: 0.1+synopsis: hspec-expectations with pretty printing on failure+description: Use exactly the same as hspec-expectations. Require a PrettyTerm instance for anything under expectation. To start with you can define a Pretty instance and then just write: instance PrettyTerm YourData using wl-pprint-extras and wl-pprint-terminfo.++license: MIT+license-file: LICENSE+copyright: (c) 2013 Greg Weber+author: Daggerboard Inc, makers of docmunch.com+maintainer: Greg Weber <greg@gregweber.info>+build-type: Simple+category: Testing+cabal-version: >= 1.8+homepage: https://github.com/hspec/hspec-expectations#readme++source-repository head+ type: git+ location: https://github.com/hspec/hspec-expectations++library+ ghc-options:+ -Wall+ extensions:+ CPP+ build-depends:+ base < 4.8+ , hspec-expectations+ , wl-pprint-terminfo+ , wl-pprint-extras+ , deepseq+ hs-source-dirs:+ .+ exposed-modules:+ Test.Hspec.Expectations.Pretty