packages feed

hspec-expectations-match 0.1.0.0 → 0.1.1.0

raw patch · 7 files changed

+84/−31 lines, 7 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Test.Hspec.Expectations.Match: shouldReturnAndMatch :: Q Exp -> Q Pat -> Q Exp

Files

CHANGELOG.md view
@@ -1,3 +1,8 @@+# 0.1.1.0 (November 15th, 2017)++- Added `shouldReturnAndMatch`, which combines the action-running behavior of `shouldReturn` with the pattern-matching behavior of `shouldMatch`.+- Added a missing `HasCallStack` constraint to properly provide source location information for failed expectations.+ # 0.1.0.0 (November 15th, 2017)  - Initial release.
+ LICENSE view
@@ -0,0 +1,13 @@+Copyright 2017 CJ Affiliate by Conversant++Permission to use, copy, modify, and/or distribute this software for any purpose+with or without fee is hereby granted, provided that the above copyright notice+and this permission notice appear in all copies.++THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND+FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS+OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER+TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF+THIS SOFTWARE.
README.md view
@@ -1,6 +1,6 @@ # hspec-expectations-match [![Build Status](https://travis-ci.org/cjdev/hspec-expectations-match.svg?branch=master)](https://travis-ci.org/cjdev/hspec-expectations-match) -`hspec-expectations-match` is a Haskell package that provides a single expectation for use with [`hspec`][hspec], `shouldMatch`. The `shouldMatch` expectation uses Template Haskell to assert that a value matches a particular pattern.+`hspec-expectations-match` is a Haskell package that provides expectations for use with [`hspec`][hspec] that use Template Haskell to assert that a value matches a particular pattern. The primary expectation is `shouldMatch`:  ```haskell {-# LANGUAGE TemplateHaskell #-}
hspec-expectations-match.cabal view
@@ -3,11 +3,11 @@ -- see: https://github.com/sol/hpack  name:           hspec-expectations-match-version:        0.1.0.0+version:        0.1.1.0 synopsis:       An hspec expectation that asserts a value matches a pattern.-description:    This package provides a single expectation for use with @hspec@,-                'shouldMatch'. The 'shouldMatch' expectation uses Template Haskell to assert-                that a value matches a particular pattern.+description:    This package provides expectations for use with @hspec@ that use Template+                Haskell to assert that a value matches a particular pattern.+                The primary expectation is 'shouldMatch':                 .                 >>> $([|Just True|] `shouldMatch` [p|Just _|])                 >>> $([|Nothing|] `shouldMatch` [p|Just _|])@@ -27,6 +27,7 @@ maintainer:     Alexis King <lexi.lambda@gmail.com> copyright:      2017 CJ Affiliate by Conversant license:        ISC+license-file:   LICENSE build-type:     Simple cabal-version:  >= 1.10 
package.yaml view
@@ -1,11 +1,11 @@ name: hspec-expectations-match-version: 0.1.0.0+version: 0.1.1.0 category: Testing synopsis: An hspec expectation that asserts a value matches a pattern. description: |-  This package provides a single expectation for use with @hspec@,-  'shouldMatch'. The 'shouldMatch' expectation uses Template Haskell to assert-  that a value matches a particular pattern.+  This package provides expectations for use with @hspec@ that use Template+  Haskell to assert that a value matches a particular pattern.+  The primary expectation is 'shouldMatch':    >>> $([|Just True|] `shouldMatch` [p|Just _|])   >>> $([|Nothing|] `shouldMatch` [p|Just _|])
src/Test/Hspec/Expectations/Match.hs view
@@ -2,26 +2,29 @@ {-# LANGUAGE TemplateHaskellQuotes #-}  {-|-  This module implements an @hspec@ expectation, 'shouldMatch'. This is a-  Template Haskell function that makes it possible to assert that a value-  matches a particular pattern, and it even allows values bound by the pattern-  to be extracted.+  This package provides expectations for use with @hspec@ that use Template+  Haskell to assert that a value matches a particular pattern. Furthermore,+  any bindings created by the pattern will be returned if the pattern is+  successfully matched, making it easier to extract the result of some assertion+  and use it to make further assertions. -  The function should be used with Template Haskell’s expression and pattern+  These functions should be used with Template Haskell’s expression and pattern   quoters, notated by @[| ... |]@ and @[p| ... |]@, respectively. -} module Test.Hspec.Expectations.Match   ( shouldMatch+  , shouldReturnAndMatch   ) where  import Control.Monad.Base (MonadBase, liftBase) import Data.Maybe (fromMaybe)+import GHC.Stack (HasCallStack) import Test.Hspec.Expectations (expectationFailure)  import Language.Haskell.TH.Ppr import Language.Haskell.TH.Syntax -assertPatternMatchFailure :: (MonadBase IO m, Show a) => String -> a -> m b+assertPatternMatchFailure :: (HasCallStack, MonadBase IO m, Show a) => String -> a -> m b assertPatternMatchFailure pat val =     liftBase (expectationFailure (showsPrec 11 val "" ++ " failed to match pattern " ++ pat))     -- expectationFailure should always throw, but it returns IO (), not IO a,@@ -62,6 +65,27 @@     [ Match pat (NormalB successExpr) []     , Match (VarP valName) (NormalB failureExpr) []     ]++-- | Like 'Test.Hspec.Expectations.shouldReturn' combined with 'shouldMatch'.+-- Like 'Test.Hspec.Expectations.shouldReturn', the provided expression should+-- produce an action that, once run, produces a value. Like 'shouldMatch', the+-- resulting value will be matched against the provided pattern.+shouldReturnAndMatch :: Q Exp -> Q Pat -> Q Exp+shouldReturnAndMatch qExpr qPat = do+  expr <- qExpr+  pat <- qPat+  patStr <- showsPat 11 pat++  valName <- newName "val"++  let successExpr = VarE 'pure `AppE` patBindingsToTupleExp pat+  let failureExpr = VarE 'assertPatternMatchFailure `AppE` LitE (StringL (patStr "")) `AppE` VarE valName++  pure $ VarE '(>>=) `AppE` expr `AppE` LamE [VarP valName]+    (CaseE (VarE valName)+      [ Match pat (NormalB successExpr) []+      , Match WildP (NormalB failureExpr) []+      ])  patBindingsToTupleExp :: Pat -> Exp patBindingsToTupleExp = TupE . map VarE . patBindings
test/Test/Hspec/Expectations/MatchSpec.hs view
@@ -7,24 +7,34 @@ import Test.Hspec.Expectations.Match  spec :: Spec-spec = describe "shouldMatch" $ do-  it "succeeds when a value matches a pattern" $ example $ do-    $([|Just True|] `shouldMatch` [p|Just True|])-    $([|Just True|] `shouldMatch` [p|Just _|])-    $([|Just False|] `shouldMatch` [p|Just _|])+spec = do+  describe "shouldMatch" $ do+    it "succeeds when a value matches a pattern" $ example $ do+      $([|Just True|] `shouldMatch` [p|Just True|])+      $([|Just True|] `shouldMatch` [p|Just _|])+      $([|Just False|] `shouldMatch` [p|Just _|]) -    $([|17 :: Integer|] `shouldMatch` [p|((>= 15) -> True)|])+      $([|17 :: Integer|] `shouldMatch` [p|((>= 15) -> True)|]) -  it "fails when a value does not match a pattern" $ do-    $([|Just True|] `shouldMatch` [p|Nothing|]) `shouldThrow` anyException-    $([|Nothing|] `shouldMatch` [p|Just True|]) `shouldThrow` anyException+    it "fails when a value does not match a pattern" $ do+      $([|Just True|] `shouldMatch` [p|Nothing|]) `shouldThrow` anyException+      $([|Nothing|] `shouldMatch` [p|Just True|]) `shouldThrow` anyException -    $([|17 :: Integer|] `shouldMatch` [p|((<= 15) -> True)|]) `shouldThrow` anyException+      $([|17 :: Integer|] `shouldMatch` [p|((<= 15) -> True)|]) `shouldThrow` anyException -  it "returns any bindings matched by a successful pattern" $ do-    a <- $([|Just True|] `shouldMatch` [p|Just x|])-    a `shouldBe` True+    it "returns any bindings matched by a successful pattern" $ do+      a <- $([|Just True|] `shouldMatch` [p|Just x|])+      a `shouldBe` True -    (b, c) <- $([|['x', 'y']|] `shouldMatch` [p|[x, y]|])-    b `shouldBe` 'x'-    c `shouldBe` 'y'+      (b, c) <- $([|['x', 'y']|] `shouldMatch` [p|[x, y]|])+      b `shouldBe` 'x'+      c `shouldBe` 'y'++  describe "shouldReturnAndMatch" $+    it "matches the result of an action against a pattern" $ example $ do+      $([|pure (Just True)|] `shouldReturnAndMatch` [p|Just _|])++      $([|pure Nothing|] `shouldReturnAndMatch` [p|Just True|]) `shouldThrow` anyException++      a <- $([|pure (Just True)|] `shouldReturnAndMatch` [p|Just x|])+      a `shouldBe` True