Skip to content

Commit 07c115a

Browse files
committed
Add failing test
1 parent 3b2e4a7 commit 07c115a

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/Data/Patch/MapWithPatchingMove.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Data.Patch.MapWithPatchingMove
2323
, patchMapWithPatchingMoveInsertAll
2424
, insertMapKey
2525
, moveMapKey
26+
, patchMapKey
2627
, swapMapKey
2728
, deleteMapKey
2829
, unsafePatchMapWithPatchingMove
@@ -135,6 +136,18 @@ moveMapKey src dst
135136
, (src, NodeInfo From_Delete (Just dst))
136137
]
137138

139+
patchMapKey
140+
:: ( DecidablyEmpty p
141+
#if !MIN_VERSION_base(4,11,0)
142+
, Semigroup p
143+
#endif
144+
)
145+
=> k -> p -> PatchMapWithPatchingMove k p
146+
patchMapKey k p
147+
| isEmpty p = PatchMapWithPatchingMove Map.empty
148+
| otherwise =
149+
PatchMapWithPatchingMove $ Map.singleton k $ NodeInfo (From_Move k p) (Just k)
150+
138151
-- |Make a @'PatchMapWithPatchingMove' k p@ which has the effect of swapping two keys in the mapping, equivalent to:
139152
--
140153
-- @

test/tests.hs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,35 @@ module Main where
44
import Test.HUnit (runTestTT, (~:), assertEqual, errors, failures, test)
55
import Data.Patch ( Patch(apply) )
66
import Data.Patch.MapWithMove ( patchThatChangesMap )
7+
import Data.Patch.MapWithPatchingMove (PatchMapWithPatchingMove)
8+
import qualified Data.Patch.MapWithPatchingMove as PatchMapWithPatchingMove
79
import Data.Map as Map ( Map, fromList, singleton )
810
import Hedgehog (checkParallel, discover, Property, property, forAll, PropertyT, (===))
911
import Hedgehog.Gen as Gen ( int )
1012
import Hedgehog.Range as Range ( linear )
1113
import Control.Monad (replicateM)
1214
import System.Exit (exitFailure, exitSuccess)
1315
import Data.Sequence as Seq ( foldMapWithIndex, replicateM )
16+
import Data.Semigroup (Sum (..))
1417

1518
main :: IO ()
1619
main = do
17-
counts <- runTestTT $ test [
18-
"Simple Move" ~: (do
20+
counts <- runTestTT $ test
21+
[ "Simple Move" ~: do
1922
let mapBefore = Map.fromList [(0,1)]
2023
mapAfter = Map.fromList [(0,0),(1,1)]
2124
patch = patchThatChangesMap mapBefore mapAfter
2225
afterPatch = apply patch mapBefore
23-
assertEqual "Patch creates the same Map" (Just mapAfter) afterPatch),
24-
"Property Checks" ~: propertyChecks
25-
]
26+
assertEqual "Patch creates the same Map" (Just mapAfter) afterPatch
27+
, "Property Checks" ~: propertyChecks
28+
, "Insert and Patch" ~: do
29+
let i :: PatchMapWithPatchingMove () (Sum Int)
30+
i = PatchMapWithPatchingMove.insertMapKey () 1
31+
p = PatchMapWithPatchingMove.patchMapKey () (Sum 2)
32+
pAfterI = PatchMapWithPatchingMove.insertMapKey () 3
33+
assertEqual "Insert after patch is the same as insert" (i <> p) i
34+
assertEqual "Patch after insert is a patched insert" (p <> i) pAfterI
35+
]
2636
if errors counts + failures counts == 0 then exitSuccess else exitFailure
2737

2838
propertyChecks :: IO Bool

0 commit comments

Comments
 (0)