From c3198135a0a16be0dce55d778a1520a4848ed337 Mon Sep 17 00:00:00 2001 From: Julian Hall Date: Thu, 5 May 2016 21:30:20 +0200 Subject: [PATCH 1/5] 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 From f7b6c82db559a51e18f22c5407f8e8df20c4f0b7 Mon Sep 17 00:00:00 2001 From: Julian Hall Date: Thu, 5 May 2016 22:52:15 +0200 Subject: [PATCH 2/5] New Foldable matcher combinator: isSingleton --- core/Control/Rematch.hs | 23 +++++++++++++++++++++++ core/Control/Rematch/Specs.hs | 13 +++++++++++++ 2 files changed, 36 insertions(+) diff --git a/core/Control/Rematch.hs b/core/Control/Rematch.hs index bf95ccb..c0a4712 100644 --- a/core/Control/Rematch.hs +++ b/core/Control/Rematch.hs @@ -12,6 +12,7 @@ module Control.Rematch( , equalTo -- ** Matchers on lists , isEmpty + , isSingleton , hasSize , everyItem , hasItem @@ -35,6 +36,7 @@ module Control.Rematch( , anyOf , on , andAlso + --, followedBy -- ** Utility functions for writing your own matchers , matcherOn , matchList @@ -129,6 +131,27 @@ andAlso m m' = Matcher { | match2 x = describeMismatch2 x | otherwise = "You've found a bug in rematch!" +-- |Matches if the matcher in the first argument matches the first item in the +-- input and the second argument matches the remaining items. Designed to be +-- used infix, e.g. 'is 5 `followedBy` is 6 `followedBy` isEmpty' can be used to +-- match the list [5,6]. +--followedBy :: Foldable f => Matcher a -> Matcher [a] -> Matcher (f a) +--followedBy l r = Matcher { +-- match = doMatch +-- , description = description l ++ " followed by " ++ description r + +-- |Matches a Foldable instance if it contains exactly one item that passes a +-- matcher +isSingleton :: (Show a, Foldable f) => Matcher a -> Matcher (f a) +isSingleton m = Matcher { + match = \x -> (length x == 1) && all (match m) x + , description = "isSingleton(" ++ description m ++ ")" + , describeMismatch = go . F.toList + } + where go [] = "got an empty list: []" + go (x:[]) = describeMismatch m x + go (x:xs) = "got a list with multiple items: " ++ show (x:xs) + -- |Matches if every item in the input list passes a matcher everyItem :: Foldable f => Matcher a -> Matcher (f a) everyItem m = Matcher { diff --git a/core/Control/Rematch/Specs.hs b/core/Control/Rematch/Specs.hs index 0cc8e19..cb50839 100644 --- a/core/Control/Rematch/Specs.hs +++ b/core/Control/Rematch/Specs.hs @@ -110,6 +110,19 @@ listMatcherSpecs = describe "list matchers" $ do it "hasSize" pending + describe "isSingleton" $ do + it "does not match when there are no items" $ + checkMatch (isSingleton $ equalTo 1) [] @?= Just ("isSingleton(equalTo 1)", "got an empty list: []") + + it "matches when one item matches" $ + checkMatch (isSingleton $ equalTo 1) [1] @?= Nothing + + it "does not match when one item does not match" $ + checkMatch (isSingleton $ equalTo 1) [2] @?= Just ("isSingleton(equalTo 1)", "was 2") + + it "does not match when there are two items" $ + checkMatch (isSingleton $ equalTo 1) [1, 1] @?= Just ("isSingleton(equalTo 1)", "got a list with multiple items: [1,1]") + comparableSpecs :: Spec comparableSpecs = describe "comparables" $ do describe "greaterThan" $ do From 292171a90abd5a6b526e57b9037e28ee8e363c53 Mon Sep 17 00:00:00 2001 From: Julian Hall Date: Fri, 6 May 2016 00:04:36 +0200 Subject: [PATCH 3/5] New matcher: followedBy (matches head and tail of list or Foldable separately) --- core/Control/Rematch.hs | 22 ++++++++++++++++------ core/Control/Rematch/Specs.hs | 12 ++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/core/Control/Rematch.hs b/core/Control/Rematch.hs index c0a4712..2896668 100644 --- a/core/Control/Rematch.hs +++ b/core/Control/Rematch.hs @@ -36,7 +36,7 @@ module Control.Rematch( , anyOf , on , andAlso - --, followedBy + , followedBy -- ** Utility functions for writing your own matchers , matcherOn , matchList @@ -135,11 +135,21 @@ andAlso m m' = Matcher { -- input and the second argument matches the remaining items. Designed to be -- used infix, e.g. 'is 5 `followedBy` is 6 `followedBy` isEmpty' can be used to -- match the list [5,6]. ---followedBy :: Foldable f => Matcher a -> Matcher [a] -> Matcher (f a) ---followedBy l r = Matcher { --- match = doMatch --- , description = description l ++ " followed by " ++ description r - +followedBy :: (Show a, Foldable f) => Matcher a -> Matcher [a] -> Matcher (f a) +followedBy l r = Matcher { + match = doMatch . F.toList + , description = description l ++ " followed by " ++ description r + , describeMismatch = doDescribe . F.toList + } + where doMatch [] = False + doMatch (x:xs) = match l x && match r xs + doDescribe [] = "got an empty list: []" + doDescribe (x:[]) | match l x = "matched " ++ show x + | otherwise = standardMismatch x + doDescribe (x:xs) | match l x = "matched " ++ show x ++ ", " ++ describeMismatch r xs + | otherwise = standardMismatch x ++ ", " ++ describeMismatch r xs +infixr 0 `followedBy` + -- |Matches a Foldable instance if it contains exactly one item that passes a -- matcher isSingleton :: (Show a, Foldable f) => Matcher a -> Matcher (f a) diff --git a/core/Control/Rematch/Specs.hs b/core/Control/Rematch/Specs.hs index cb50839..1a91302 100644 --- a/core/Control/Rematch/Specs.hs +++ b/core/Control/Rematch/Specs.hs @@ -123,6 +123,18 @@ listMatcherSpecs = describe "list matchers" $ do it "does not match when there are two items" $ checkMatch (isSingleton $ equalTo 1) [1, 1] @?= Just ("isSingleton(equalTo 1)", "got a list with multiple items: [1,1]") + describe "followedBy" $ do + it "does not match if there are no items" $ + checkMatch (is 1 `followedBy` isEmpty) [] @?= Just ("equalTo 1 followed by isEmpty", "got an empty list: []") + it "matches if the inputs match its arguments" $ + checkMatch (is 1 `followedBy` isEmpty) [1] @?= Nothing + it "fails if the first argument does not match the head" $ + checkMatch (is 1 `followedBy` isEmpty) [2] @?= Just ("equalTo 1 followed by isEmpty", "was 2") + it "fails if the second argument does not match the tail" $ + checkMatch (is 1 `followedBy` isSingleton $ equalTo 2) [1,4] @?= Just ("equalTo 1 followed by isSingleton(equalTo 2)", "matched 1, was 4") + it "includes match description of the tail when first argument does not match" $ + checkMatch (is 1 `followedBy` is 2 `followedBy` isEmpty) [2,2] @?= Just ("equalTo 1 followed by equalTo 2 followed by isEmpty", "was 2, matched 2") + comparableSpecs :: Spec comparableSpecs = describe "comparables" $ do describe "greaterThan" $ do From 86eed20bc6c01c91ab8f35bed6b12de495bc45ef Mon Sep 17 00:00:00 2001 From: Julian Hall Date: Fri, 6 May 2016 10:51:19 +0200 Subject: [PATCH 4/5] New module based for Tasty's HUnit integration (Basically just a copy of the hunit-rematch module with the imports and dependencies altered to use Tasty rather than HUnit.) --- tasty-hunit-rematch/LICENSE | 7 ++++++ tasty-hunit-rematch/Main.hs | 4 ++++ tasty-hunit-rematch/Setup.hs | 2 ++ .../Test/Rematch/Specs/TastyHUnit.hs | 17 ++++++++++++++ .../Test/Rematch/TastyHUnit.hs | 18 +++++++++++++++ tasty-hunit-rematch/script/test | 3 +++ tasty-hunit-rematch/tasty-hunit-rematch.cabal | 23 +++++++++++++++++++ 7 files changed, 74 insertions(+) create mode 100644 tasty-hunit-rematch/LICENSE create mode 100644 tasty-hunit-rematch/Main.hs create mode 100644 tasty-hunit-rematch/Setup.hs create mode 100644 tasty-hunit-rematch/Test/Rematch/Specs/TastyHUnit.hs create mode 100644 tasty-hunit-rematch/Test/Rematch/TastyHUnit.hs create mode 100755 tasty-hunit-rematch/script/test create mode 100644 tasty-hunit-rematch/tasty-hunit-rematch.cabal diff --git a/tasty-hunit-rematch/LICENSE b/tasty-hunit-rematch/LICENSE new file mode 100644 index 0000000..5d24e87 --- /dev/null +++ b/tasty-hunit-rematch/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2013 Tom Crayford + +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. diff --git a/tasty-hunit-rematch/Main.hs b/tasty-hunit-rematch/Main.hs new file mode 100644 index 0000000..a0b6c2b --- /dev/null +++ b/tasty-hunit-rematch/Main.hs @@ -0,0 +1,4 @@ +import qualified Test.Rematch.Specs.TastyHUnit as S + +main :: IO () +main = S.main diff --git a/tasty-hunit-rematch/Setup.hs b/tasty-hunit-rematch/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/tasty-hunit-rematch/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/tasty-hunit-rematch/Test/Rematch/Specs/TastyHUnit.hs b/tasty-hunit-rematch/Test/Rematch/Specs/TastyHUnit.hs new file mode 100644 index 0000000..4dfbcda --- /dev/null +++ b/tasty-hunit-rematch/Test/Rematch/Specs/TastyHUnit.hs @@ -0,0 +1,17 @@ +module Test.Rematch.Specs.TastyHUnit where +import Test.Hspec +import Test.Hspec.HUnit() +import Test.Rematch.HUnit +import Control.Rematch(is) + +main :: IO () +main = hspec $ specs + +specs :: Spec +specs = do + describe "expect" $ do + -- it isn't clear how to run a tasty test programatically and extract its result, + -- so I'm not sure how to write a test for this. + it "is used as a tasty-hunit test" pending + + diff --git a/tasty-hunit-rematch/Test/Rematch/TastyHUnit.hs b/tasty-hunit-rematch/Test/Rematch/TastyHUnit.hs new file mode 100644 index 0000000..e5e3ecd --- /dev/null +++ b/tasty-hunit-rematch/Test/Rematch/TastyHUnit.hs @@ -0,0 +1,18 @@ +module Test.Rematch.TastyHUnit where +import Test.Tasty.HUnit(Assertion, assertFailure) +import Control.Rematch +import Control.Rematch.Run + +-- |Run a matcher as a Tasty HUnit assertion +-- +-- Example output: +-- +-- @ +--Expected: equalTo "a" +-- but: was "b" +-- @ +expect :: a -> Matcher a -> Assertion +expect a matcher = case res of + MatchSuccess -> return () + (MatchFailure msg) -> assertFailure msg + where res = runMatch matcher a diff --git a/tasty-hunit-rematch/script/test b/tasty-hunit-rematch/script/test new file mode 100755 index 0000000..a657304 --- /dev/null +++ b/tasty-hunit-rematch/script/test @@ -0,0 +1,3 @@ +#!/bin/bash + +runghc Test/Rematch/Specs/HUnit.hs diff --git a/tasty-hunit-rematch/tasty-hunit-rematch.cabal b/tasty-hunit-rematch/tasty-hunit-rematch.cabal new file mode 100644 index 0000000..2821e92 --- /dev/null +++ b/tasty-hunit-rematch/tasty-hunit-rematch.cabal @@ -0,0 +1,23 @@ +name: tasty-hunit-rematch +version: 0.1.0.1 +synopsis: Tasty-HUnit support for rematch +-- description: +homepage: http://github.com/tcrayford/rematch +license: MIT +license-file: LICENSE +author: Tom Crayford +maintainer: tcrayford@googlemail.com +-- copyright: +category: Testing +build-type: Simple +cabal-version: >=1.8 + +library + -- exposed-modules: + -- other-modules: + build-depends: base >= 4.5.0 && < 5, rematch >= 0.2, tasty-hunit >= 0.9 + exposed-modules: Test.Rematch.TastyHUnit +test-suite tests + build-depends: base >= 4.5.0 && < 5, hspec >= 1.4, tasty-hunit >= 0.9, rematch >= 0.2 + type: exitcode-stdio-1.0 + main-is: Main.hs From 0052e30c803b0fa20391bfe0ff9067513ed2f66f Mon Sep 17 00:00:00 2001 From: Julian Hall Date: Sat, 20 Aug 2016 10:38:13 +0100 Subject: [PATCH 5/5] New module: Control.Rematch.Data, for data type generic matchers using the Data.Data.Data typeclass, isDataConstr and isDataValue --- core/Control/Rematch/Data.hs | 69 ++++++++++++++++++++++++++++++++++++ core/rematch.cabal | 3 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 core/Control/Rematch/Data.hs diff --git a/core/Control/Rematch/Data.hs b/core/Control/Rematch/Data.hs new file mode 100644 index 0000000..a969805 --- /dev/null +++ b/core/Control/Rematch/Data.hs @@ -0,0 +1,69 @@ +{-# LANGUAGE DeriveDataTypeable, FlexibleInstances, OverlappingInstances #-} +{-# LANGUAGE NoMonomorphismRestriction, UndecidableInstances, RankNTypes #-} +{-# LANGUAGE ScopedTypeVariables #-} + +-- | Matchers based on the 'Data' type class defined in @Data.Data@. +-- +-- Examples below are based on a sample data type defined as +-- @data MyType = First | Second Int | Third String Bool deriving(Data,Show)@. + +module Control.Rematch.Data (Constrable, isDataConstr, isDataValue) where + +import Data.Data +import Control.Rematch + +-- | class Constrable allows us to convert an algebraic data type constructor +-- function (e.g. 'Maybe') to a 'Constr' object that represents the values +-- it can create. Based on code provided by Tikhon Jelvis in a Stack Overflow +-- answer (see http://stackoverflow.com/a/25588663/441899) +class Constrable a where + constr :: a -> Constr + +instance Data a => Constrable a where + constr = toConstr + +instance Constrable a => Constrable (b -> a) where + constr f = constr (f undefined) + +-- | Match if and only if a value was constructed using a specific constructor +-- version, e.g. @isDataConstr Second@ produces a matcher for @MyType@ that +-- accepts values produced via @Second@ but rejects those produced via @First@ +-- or @Third@ +isDataConstr :: (Data d, Constrable c) => c -> Matcher d +isDataConstr f = Matcher match + ("value with constructor " ++ show expectedC) + (\v -> "had constructor " ++ (show $ toConstr v)) + where + expectedC = constr f + match v = toConstr v == expectedC + +-- | Match if and only if a value was specified using a specific constructor +-- and contains an argument at a specified zero-based index. For example, +-- @isDataValue Second 0 (equalTo (2::Int))@ is a matcher for @MyType@ that will +-- first check that the value was constructed via the @Second@ constructor, then +-- that the type of value contained is an 'Int', and finally that it is equal to +-- 3. +-- +-- Note that types of contained values are checked *dynamically* not +-- *statically*, as the 'Data' type class does not provide static type +-- information about constructor arguments. For this reason, when checking +-- against literals that may have multiple types, it is important to +-- specify their types explicitly, otherwise they may be defaulted to the +-- wrong the type. +isDataValue :: forall c v d . (Data d, Constrable c, Data v, Show d) => + c -> Int -> Matcher v -> Matcher d +isDataValue f i argMatch = + Matcher doMatch descr standardMismatch + where + expectedC = constr f + descr = "value with constructor " ++ (show expectedC) ++ " with " + ++ (description argMatch) ++ " at index " ++ (show i) + doMatch v = toConstr v == expectedC && + gmapQi i doSubmatch v + doSubmatch :: forall ad . Data ad => ad -> Bool + doSubmatch av = case (cast av) :: Maybe v of + Just avx -> (match argMatch) avx + Nothing -> False + + +-- FIXME need tests for both matchers defined in this module diff --git a/core/rematch.cabal b/core/rematch.cabal index 4714edd..e2d0e68 100644 --- a/core/rematch.cabal +++ b/core/rematch.cabal @@ -27,7 +27,8 @@ cabal-version: >=1.8 library exposed-modules: Control.Rematch, Control.Rematch.Formatting, - Control.Rematch.Run, Control.Rematch.Traversable + Control.Rematch.Run, Control.Rematch.Traversable, + Control.Rematch.Data build-depends: base >= 4.8 && < 5 test-suite tests build-depends: base >= 4.8 && < 5, hspec >= 1.4, HUnit >= 1.2