From c3198135a0a16be0dce55d778a1520a4848ed337 Mon Sep 17 00:00:00 2001 From: Julian Hall Date: Thu, 5 May 2016 21:30:20 +0200 Subject: [PATCH] Generalise list matchers to accept any Foldable instance. This relies on the updated definitions of various functions in the base-4.8.0.0 Prelude, so have updated dependencies and bumped version number. It may be possible to revert to base-4.5.0.0 if desired by explicitly using Foldable functions rather than defaulting to list functions, but as I don't have an appropriate installation to test it on I've not done so. --- core/Control/Rematch.hs | 14 ++++++++------ core/rematch.cabal | 9 +++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/core/Control/Rematch.hs b/core/Control/Rematch.hs index d32a57c..bf95ccb 100644 --- a/core/Control/Rematch.hs +++ b/core/Control/Rematch.hs @@ -44,6 +44,7 @@ import Control.Applicative (liftA2) import Data.List ( nub , intercalate ) import qualified Data.Maybe as M +import qualified Data.Foldable as F import Control.Rematch.Run import Control.Rematch.Formatting @@ -129,25 +130,26 @@ andAlso m m' = Matcher { | otherwise = "You've found a bug in rematch!" -- |Matches if every item in the input list passes a matcher -everyItem :: Matcher a -> Matcher [a] +everyItem :: Foldable f => Matcher a -> Matcher (f a) everyItem m = Matcher { match = all (match m) , description = "everyItem(" ++ description m ++ ")" - , describeMismatch = describeList "" . map (describeMismatch m) . filter (not . match m) + , describeMismatch = describeList "" . fmap (describeMismatch m) . + filter (not . match m) . F.toList } -- |Matches if any of the items in the input list passes the provided matcher -hasItem :: Matcher a -> Matcher [a] +hasItem :: Foldable f => Matcher a -> Matcher (f a) hasItem m = Matcher { match = any (match m) , description = "hasItem(" ++ description m ++ ")" - , describeMismatch = go + , describeMismatch = go . F.toList } where go [] = "got an empty list: []" go as = describeList "" (map (describeMismatch m) as) -- |Matches if the input list is empty -isEmpty :: (Show a) => Matcher [a] +isEmpty :: (Show (f a), Foldable f) => Matcher (f a) isEmpty = Matcher { match = null , description = "isEmpty" @@ -155,7 +157,7 @@ isEmpty = Matcher { } -- |Matches if the input list has the required size -hasSize :: (Show a) => Int -> Matcher [a] +hasSize :: (Show (f a), Foldable f) => Int -> Matcher (f a) hasSize n = Matcher { match = ((== n) . length) , description = "hasSize(" ++ show n ++ ")" diff --git a/core/rematch.cabal b/core/rematch.cabal index cfd3115..4714edd 100644 --- a/core/rematch.cabal +++ b/core/rematch.cabal @@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: rematch -version: 0.2.0.0 +version: 0.2.1.0 synopsis: A simple api for matchers description: Rematch is a simple library of matchers, which express rules @@ -26,9 +26,10 @@ build-type: Simple cabal-version: >=1.8 library - exposed-modules: Control.Rematch, Control.Rematch.Formatting, Control.Rematch.Run - build-depends: base >= 4.5.0 && < 5 + exposed-modules: Control.Rematch, Control.Rematch.Formatting, + Control.Rematch.Run, Control.Rematch.Traversable + build-depends: base >= 4.8 && < 5 test-suite tests - build-depends: base >= 4.5.0 && < 5, hspec >= 1.4, HUnit >= 1.2 + build-depends: base >= 4.8 && < 5, hspec >= 1.4, HUnit >= 1.2 type: exitcode-stdio-1.0 main-is: Main.hs