Skip to content

Commit 93e9a11

Browse files
authored
Merge pull request #81 from Risto-Stevcev/master
Added some more helper comparison operations
2 parents ced9e99 + 9992bb4 commit 93e9a11

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

bower.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@
3333
"purescript-strings": "^3.0.0",
3434
"purescript-transformers": "^3.0.0",
3535
"purescript-generics-rep": "^5.0.0"
36+
},
37+
"devDependencies": {
38+
"purescript-assert": "^3.0.0"
3639
}
3740
}

src/Test/QuickCheck.purs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ module Test.QuickCheck
2828
, (<?>)
2929
, assertEquals
3030
, (===)
31+
, (==?)
3132
, assertNotEquals
3233
, (/==)
34+
, (/=?)
35+
, assertLessThan
36+
, (<?)
37+
, assertLessThanEq
38+
, (<=?)
39+
, assertGreaterThan
40+
, (>?)
41+
, assertGreaterThanEq
42+
, (>=?)
3343
, module Test.QuickCheck.LCG
3444
, module Test.QuickCheck.Arbitrary
3545
) where
@@ -41,15 +51,13 @@ import Control.Monad.Eff.Console (CONSOLE, log)
4151
import Control.Monad.Eff.Exception (EXCEPTION, throwException, error)
4252
import Control.Monad.Eff.Random (RANDOM)
4353
import Control.Monad.Rec.Class (Step(..), tailRec)
44-
4554
import Data.Foldable (for_)
4655
import Data.List (List)
4756
import Data.Maybe (Maybe(..))
4857
import Data.Maybe.First (First(..))
4958
import Data.Monoid (mempty)
5059
import Data.Tuple (Tuple(..))
5160
import Data.Unfoldable (replicateA)
52-
5361
import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary, class Coarbitrary, coarbitrary)
5462
import Test.QuickCheck.Gen (Gen, evalGen, runGen)
5563
import Test.QuickCheck.LCG (Seed, runSeed, randomSeed)
@@ -161,14 +169,40 @@ withHelp false msg = Failed msg
161169

162170
infix 2 withHelp as <?>
163171

172+
-- | Self-documenting comparison operation
173+
assertOp :: forall a. Eq a => Show a => (a -> a -> Boolean) -> String -> a -> a -> Result
174+
assertOp op failString a b = a `op` b <?> show a <> failString <> show b
175+
164176
-- | Self-documenting equality assertion
165177
assertEquals :: forall a. Eq a => Show a => a -> a -> Result
166-
assertEquals a b = a == b <?> show a <> " /= " <> show b
178+
assertEquals = assertOp (==) " /= "
167179

168180
infix 2 assertEquals as ===
181+
infix 2 assertEquals as ==?
169182

170183
-- | Self-documenting inequality assertion
171184
assertNotEquals :: forall a. Eq a => Show a => a -> a -> Result
172-
assertNotEquals a b = a /= b <?> show a <> " == " <> show b
185+
assertNotEquals = assertOp (/=) " == "
173186

174187
infix 2 assertNotEquals as /==
188+
infix 2 assertNotEquals as /=?
189+
190+
assertLessThan :: forall a. Ord a => Show a => a -> a -> Result
191+
assertLessThan = assertOp (<) " >= "
192+
193+
infix 2 assertLessThan as <?
194+
195+
assertLessThanEq :: forall a. Ord a => Show a => a -> a -> Result
196+
assertLessThanEq = assertOp (<=) " > "
197+
198+
infix 2 assertLessThanEq as <=?
199+
200+
assertGreaterThan :: forall a. Ord a => Show a => a -> a -> Result
201+
assertGreaterThan = assertOp (>) " <= "
202+
203+
infix 2 assertGreaterThan as >?
204+
205+
assertGreaterThanEq :: forall a. Ord a => Show a => a -> a -> Result
206+
assertGreaterThanEq = assertOp (>=) " < "
207+
208+
infix 2 assertGreaterThanEq as >=?

test/Main.purs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@ import Prelude
44

55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log, logShow)
7+
import Control.Monad.Eff.Exception (try, EXCEPTION)
78
import Control.Monad.Eff.Random (RANDOM)
8-
99
import Data.Array.Partial (head)
10+
import Data.Either (isLeft)
1011
import Data.Foldable (sum)
1112
import Data.Generic.Rep (class Generic)
1213
import Data.Generic.Rep.Show (genericShow)
13-
1414
import Partial.Unsafe (unsafePartial)
15-
15+
import Test.Assert (assert, ASSERT)
16+
import Test.QuickCheck (class Testable, quickCheck, (/=?), (<=?), (<?), (==?), (>=?), (>?))
1617
import Test.QuickCheck.Arbitrary (arbitrary, genericArbitrary, class Arbitrary)
1718
import Test.QuickCheck.Gen (Gen, vectorOf, randomSample')
1819

1920
data Foo a = F0 a | F1 a a | F2 { foo :: a, bar :: Array a }
2021
derive instance genericFoo :: Generic (Foo a) _
2122
instance showFoo :: Show a => Show (Foo a) where show = genericShow
22-
instance arbitraryFoo :: Arbitrary a => Arbitrary (Foo a) where arbitrary = genericArbitrary
23+
instance arbitraryFoo :: Arbitrary a => Arbitrary (Foo a) where arbitrary = genericArbitrary
24+
2325

24-
main :: Eff (console :: CONSOLE, random :: RANDOM) Unit
26+
quickCheckFail
27+
:: forall t e
28+
. Testable t
29+
=> t
30+
-> Eff (assert :: ASSERT, console :: CONSOLE, random :: RANDOM | e) Unit
31+
quickCheckFail = assert <=< map isLeft <<< try <<< quickCheck
32+
33+
34+
main :: Eff (assert :: ASSERT, console :: CONSOLE, random :: RANDOM, exception :: EXCEPTION) Unit
2535
main = do
2636
log "Try with some little Gens first"
2737
logShow =<< go 10
@@ -36,6 +46,26 @@ main = do
3646
log "Generating via Generic"
3747
logShow =<< randomSample' 10 (arbitrary :: Gen (Foo Int))
3848

49+
quickCheck \(x :: Int) -> x <? x + 1
50+
quickCheck \(x :: Int) -> x <=? x + 1
51+
quickCheck \(x :: Int) -> x >=? x - 1
52+
quickCheck \(x :: Int) -> x >? x - 1
53+
quickCheck \(x :: Int) -> x + x ==? x * 2
54+
quickCheck \(x :: Int) -> x + x /=? x * 3
55+
56+
quickCheck $ 1 ==? 1
57+
quickCheckFail $ 1 /=? 1
58+
quickCheck $ 1 <? 2
59+
quickCheckFail $ 1 >=? 2
60+
quickCheck $ 3 <=? 3
61+
quickCheckFail $ 3 >? 3
62+
quickCheck $ 3 >=? 3
63+
quickCheckFail $ 3 <? 3
64+
quickCheck $ 4 /=? 3
65+
quickCheckFail $ 4 ==? 3
66+
quickCheck $ 4 >? 3
67+
quickCheckFail $ 4 <=? 3
68+
3969
where
4070
go n = map (sum <<< unsafeHead) $ randomSample' 1 (vectorOf n (arbitrary :: Gen Int))
4171

0 commit comments

Comments
 (0)