]> code.delx.au - offlineimap/blobdiff - testsrc/runtests.hs
Believe allChanges is working
[offlineimap] / testsrc / runtests.hs
index dbeda57e293c8531887dd34bd706f51c65eb826b..8e7d8c4cb87a744de78f7013e588f5f33ac3619d 100644 (file)
@@ -62,7 +62,7 @@ prop_allChangesToChild master child =
         expectedResChild = sort $
             (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference master $ child) ++
             (map DeleteItem . Map.keys . Map.difference child $ master) ++
-            (map (\(k, v) -> ModifyContent k v) changeList)
+            (map (pairToFunc ModifyContent) changeList)
         changeList = foldl changefunc [] (Map.toList child)
         changefunc accum (k, v) =
             case Map.lookup k master of
@@ -70,16 +70,25 @@ prop_allChangesToChild master child =
               Just x -> if x /= v
                         then (k, x) : accum
                         else accum
-        in ([], expectedResChild) @=?
-           (resMaster, sort resChild)
+        masterChanges = map (pairToFunc ModifyContent) . catMaybes .
+                        map checkIt . Map.toList . Map.intersection child
+                            $ master
+            where checkIt (k, v) = 
+                      case Map.lookup k master of
+                        Nothing -> Nothing
+                        Just v' -> if v /= v'
+                                   then Just (k, v)
+                                   else Nothing
+        in (sort masterChanges, expectedResChild) @=?
+           (sort resMaster, sort resChild)
 
 prop_allChangesToMaster :: SyncCollection Int Float -> SyncCollection Int Float -> Result
 prop_allChangesToMaster master child =
     let (resMaster, resChild) = syncBiDir master child master
         expectedResMaster = sort $
-            (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference child $ master) ++
+            (map (pairToFunc CopyItem) . Map.toList . Map.difference child $ master) ++
             (map DeleteItem . Map.keys . Map.difference master $ child) ++
-            (map (\(k, v) -> ModifyContent k v) changeList)
+            (map (pairToFunc ModifyContent) changeList)
         changeList = foldl changefunc [] (Map.toList child)
         changefunc accum (k, v) =
             case Map.lookup k master of
@@ -90,29 +99,53 @@ prop_allChangesToMaster master child =
         in (expectedResMaster, []) @=?
            (sort resMaster, resChild)
 
+-- FIXME: test findModified
+
 prop_allChanges :: SyncCollection Int Float -> SyncCollection Int Float -> SyncCollection Int Float -> Result
 prop_allChanges master child lastchild =
     let (resMaster, resChild) = syncBiDir master child lastchild
+
+        masterMods = catMaybes . map procKV $ (Map.toList master)
+            where procKV (k, m) =
+                      case (Map.lookup k child, Map.lookup k lastchild) of
+                        (Just c, Just lc) -> 
+                            if c == lc -- child didn't change
+                               then Nothing
+                               else if c == m -- child and master changed
+                                    then Nothing
+                                    else Just (k, c) -- child changed, master didn't
+                        (Nothing, Just lc) -> Nothing -- deleted on child
+                        (Just c, Nothing) -> -- New on both c and m
+                            if c == m        -- Added the same
+                               then Nothing
+                               else Just (k, c) -- Added but differ
+                        (Nothing, Nothing) -> Nothing -- New to master only
+
+        childMods = catMaybes . map procKV $ (Map.toList child)
+            where procKV (k, c) =
+                      case (Map.lookup k master, Map.lookup k lastchild) of
+                        (Just m, Just lc) -> 
+                            if lc == c 
+                               then if c == m
+                                       then Nothing
+                                       else Just (k, m)
+                               else Nothing
+                        (Nothing, Just lc) ->        -- deleted; nothing to see here
+                                              Nothing
+                        (Just m, Nothing) -> -- New on both; child takes precedence
+                           Nothing
+                        (Nothing, Nothing) -> Nothing -- New to child only
+
         expectedResMaster = sort $
-            (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference child $ Map.union master lastchild) ++
+            (map (pairToFunc CopyItem) . Map.toList . Map.difference child $ Map.union master lastchild) ++
                                                                                             (map DeleteItem . Map.keys . Map.intersection master $ Map.difference lastchild child) ++
-            (map (\(k, v) -> ModifyContent k v) masterChanges)
+            (map (pairToFunc ModifyContent) masterMods)
 
         expectedResChild = sort $
-            (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference master $ Map.union child lastchild) ++
+            (map (pairToFunc CopyItem) . Map.toList . Map.difference master $ Map.union child lastchild) ++
                                                                                             (map DeleteItem . Map.keys . Map.intersection child $ Map.difference lastchild master) ++
-            (map (\(k, v) -> ModifyContent k v) childChanges)
+            (map (pairToFunc ModifyContent) childMods)
 
-        childChanges = foldl (changefunc True) [] (Map.toList child)
-        masterChanges = foldl (changefunc False) [] (Map.toList child)
-        changefunc useMaster accum (k, v) =
-            case Map.lookup k master of
-              Nothing -> accum
-              Just x -> if x /= v
-                        then if useMaster
-                             then (k, x) : accum
-                             else (k, v) : accum
-                        else accum
     in (expectedResMaster, expectedResChild) @=?
        (sort resMaster, sort resChild)