Skip to content

Commit 017eadc

Browse files
committed
Add hunit and hegdehog test suite with roundtrip test for patchThatChangesMap
1 parent bbcfe70 commit 017eadc

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

patch.cabal

+13
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ library
6464
build-depends: these >= 0.4 && <0.9
6565
, monoidal-containers == 0.4.0.0
6666

67+
test-suite tests
68+
default-language: Haskell2010
69+
type: exitcode-stdio-1.0
70+
main-is: tests.hs
71+
hs-source-dirs: test
72+
build-depends: base
73+
, patch
74+
, containers
75+
, hedgehog
76+
, HUnit
77+
if impl(ghcjs)
78+
buildable: False
79+
6780
test-suite hlint
6881
default-language: Haskell2010
6982
type: exitcode-stdio-1.0

test/tests.hs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module Main where
3+
4+
import Test.HUnit (runTestTT, (~:), assertEqual, errors, failures, test)
5+
import Data.Patch ( Patch(apply) )
6+
import Data.Patch.MapWithMove ( patchThatChangesMap )
7+
import Data.Map as Map ( Map, fromList, singleton )
8+
import Hedgehog (checkParallel, discover, Property, property, forAll, PropertyT, (===))
9+
import Hedgehog.Gen as Gen ( int )
10+
import Hedgehog.Range as Range ( linear )
11+
import Control.Monad (replicateM)
12+
import System.Exit (exitFailure, exitSuccess)
13+
import Data.Sequence as Seq ( foldMapWithIndex, replicateM )
14+
15+
main :: IO ()
16+
main = 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
27+
28+
propertyChecks :: IO Bool
29+
propertyChecks = checkParallel $$(discover)
30+
31+
prop_patchThatChangesMap :: Property
32+
prop_patchThatChangesMap = property $ do
33+
mapBefore <- makeRandomIntMap
34+
mapAfter <- makeRandomIntMap
35+
let patch = patchThatChangesMap mapBefore mapAfter
36+
Just mapAfter === apply patch mapBefore
37+
38+
makeRandomIntMap :: Monad m => PropertyT m (Map Int Int)
39+
makeRandomIntMap = 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

0 commit comments

Comments
 (0)