rematch 0.1.0.2 → 0.1.1.0
raw patch · 2 files changed
+59/−8 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Control.Rematch: no :: Matcher a -> Matcher a
+ Control.Rematch: hasJust :: Matcher a -> Matcher (Maybe a)
+ Control.Rematch: hasLeft :: (Show a, Show b) => Matcher a -> Matcher (Either a b)
+ Control.Rematch: hasRight :: (Show a, Show b) => Matcher b -> Matcher (Either a b)
+ Control.Rematch: isNot :: Matcher a -> Matcher a
+ Control.Rematch: on :: Matcher b -> (a -> b, String) -> Matcher a
Files
- Control/Rematch.hs +58/−7
- rematch.cabal +1/−1
Control/Rematch.hs view
@@ -23,14 +23,18 @@ , lessThanOrEqual -- ** Matchers on Maybe , isJust+ , hasJust , isNothing -- ** Matchers on Either , isRight+ , hasRight , isLeft+ , hasLeft -- ** Matcher combinators- , no+ , isNot , allOf , anyOf+ , on -- ** Utility functions for writing your own matchers , matcherOn , matchList@@ -56,9 +60,8 @@ -- Example output: -- -- @--- Expected:--- equalTo \"a\"--- but: was \"b\"+--Expected: equalTo "a"+-- but: was "b" -- @ expect :: a -> Matcher a -> Assertion expect a matcher = case res of@@ -68,14 +71,14 @@ -- |Inverts a matcher, so success becomes failure, and failure -- becomes success-no :: Matcher a -> Matcher a-no (Matcher m desc mismatch) = Matcher (not . m) ("not " ++ desc) mismatch+isNot :: Matcher a -> Matcher a+isNot (Matcher m desc mismatch) = Matcher (not . m) ("isNot " ++ desc) mismatch -- |Run a matcher, producing a Match with a good error string runMatch :: Matcher a -> a -> Match runMatch m a = if match m a then MatchSuccess- else MatchFailure $ "Expected:\n " ++ description m ++ "\n but: " ++ describeMismatch m a+ else MatchFailure $ "\nExpected: " ++ description m ++ "\n but: " ++ describeMismatch m a -- |Matcher on equality is :: (Show a, Eq a) => a -> Matcher a@@ -103,6 +106,19 @@ , describeMismatch = \a -> describeList "" (map (`describeMismatch` a) matchers) } +-- |A combinator that translates Matcher a to Matcher b using+-- a function :: (a -> b)+-- Takes a name of the function for better error messages+--+-- Using this as an infix operator gets you some nice syntax:+-- expect ((is 1) `on` (length, "length")) []+on :: Matcher b -> ((a -> b), String) -> Matcher a+on m (f, name) = Matcher {+ match = match m . f+ , description = name ++ " " ++ (description m)+ , describeMismatch = describeMismatch m . f+ }+ -- |Matches if every item in the input list passes a matcher everyItem :: Matcher a -> Matcher [a] everyItem m = Matcher {@@ -170,6 +186,18 @@ , describeMismatch = standardMismatch } +-- |Matcher combinator, turns Matcher a to Matcher (Maybe a)+-- Fails if the Maybe is Nothing, otherwise tries the original+-- matcher on the content of the Maybe+hasJust :: Matcher a -> Matcher (Maybe a)+hasJust matcher = Matcher {+ match = (\a -> M.isJust a && (match matcher (M.fromJust a)))+ , description = "hasJust(" ++ description matcher ++ ")"+ , describeMismatch = mismatchDescription+ }+ where mismatchDescription (Just x) = matcher `describeMismatch` x+ mismatchDescription Nothing = "but was Nothing"+ -- |Matches if the input is Nothing isNothing :: (Show a) => Matcher (Maybe a) isNothing = Matcher {@@ -188,6 +216,17 @@ where go (Right _) = True go (Left _) = False +-- |Matcher combinator: turns a Matcher b into a Matcher on the+-- Right side of an Either a b+hasRight :: (Show a, Show b) => Matcher b -> Matcher (Either a b)+hasRight matcher = Matcher {+ match = (\e -> case e of+ (Right a) -> match matcher a+ (Left _) -> False)+ , description = "hasRight(" ++ description matcher ++ ")"+ , describeMismatch = standardMismatch+ }+ -- |Matches if an Either is Left isLeft :: (Show a, Show b) => Matcher (Either a b) isLeft = Matcher {@@ -197,6 +236,18 @@ } where go (Left _) = True go (Right _) = False++-- |Matcher combinator: turns a Matcher a into a Matcher on the+-- Left side of an Either a b+hasLeft :: (Show a, Show b) => Matcher a -> Matcher (Either a b)+hasLeft matcher = Matcher {+ match = (\e -> case e of+ (Left a) -> match matcher a+ (Right _) -> False)+ , description = "hasRight(" ++ description matcher ++ ")"+ , describeMismatch = standardMismatch+ }+ -- |Utility function for running a list of matchers matchList :: [Matcher a] -> a -> [Bool]
rematch.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: rematch-version: 0.1.0.2+version: 0.1.1.0 synopsis: A simple api for matchers description: Rematch is a simple library of matchers, which express rules