on develop help sign in
patch/test/tests.hs
43 lines 1.6 kB history raw
1{-# LANGUAGE TemplateHaskell #-}
2module Main where
4import Test.HUnit (runTestTT, (~:), assertEqual, errors, failures, test)
5import Data.Patch ( Patch(apply) )
6import Data.Patch.MapWithMove ( patchThatChangesMap )
7import Data.Map as Map ( Map, fromList, singleton )
8import Hedgehog (checkParallel, discover, Property, property, forAll, PropertyT, (===))
9import Hedgehog.Gen as Gen ( int )
10import Hedgehog.Range as Range ( linear )
11import Control.Monad (replicateM)
12import System.Exit (exitFailure, exitSuccess)
13import Data.Sequence as Seq ( foldMapWithIndex, replicateM )
15main :: IO ()
16main = do
17 counts <- runTestTT $ test [
18 "Simple Move" ~: (do
19 let mapBefore = Map.fromList [(0,1)]
20 mapAfter = Map.fromList [(0,0),(1,1)]
21 patch = patchThatChangesMap mapBefore mapAfter
22 afterPatch = apply patch mapBefore
23 assertEqual "Patch creates the same Map" (Just mapAfter) afterPatch),
24 "Property Checks" ~: propertyChecks
25 ]
26 if errors counts + failures counts == 0 then exitSuccess else exitFailure
28propertyChecks :: IO Bool
29propertyChecks = checkParallel $$(discover)
31prop_patchThatChangesMap :: Property
32prop_patchThatChangesMap = property $ do
33 mapBefore <- makeRandomIntMap
34 mapAfter <- makeRandomIntMap
35 let patch = patchThatChangesMap mapBefore mapAfter
36 Just mapAfter === apply patch mapBefore
38makeRandomIntMap :: Monad m => PropertyT m (Map Int Int)
39makeRandomIntMap = do
40 let genNum = Gen.int (Range.linear 0 100)
41 length <- forAll genNum
42 listOfNumbers <- forAll $ Seq.replicateM length genNum
43 pure $ Seq.foldMapWithIndex Map.singleton listOfNumbers