Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions core/Control/Rematch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -129,33 +130,34 @@ 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"
, describeMismatch = standardMismatch
}

-- |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 ++ ")"
Expand Down
9 changes: 5 additions & 4 deletions core/rematch.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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