-
-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathProgress.hs
More file actions
61 lines (55 loc) · 2.08 KB
/
Progress.hs
File metadata and controls
61 lines (55 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE PackageImports #-}
module Progress (tests) where
import Control.Concurrent.STM
import Data.Foldable (for_)
import qualified Data.HashMap.Strict as Map
import Development.IDE
import Development.IDE.Core.ProgressReporting
import qualified "list-t" ListT
import qualified StmContainers.Map as STM
import Test.Tasty
import Test.Tasty.HUnit
tests :: TestTree
tests = testGroup "Progress"
[ reportProgressTests
]
data InProgressModel = InProgressModel {
done, todo :: Int,
current :: Map.HashMap NormalizedUri Int
}
reportProgressTests :: TestTree
reportProgressTests = testGroup "recordProgress"
[ test "addNew" addNew
, test "increase" increase
, test "decrease" decrease
, test "done" done
]
where
p0 = pure $ InProgressModel 0 0 mempty
aUri = filePathToUri' "A"
addNew = recordProgressModel aUri succ p0
increase = recordProgressModel aUri succ addNew
decrease = recordProgressModel aUri succ increase
done = recordProgressModel aUri pred decrease
recordProgressModel key change state =
model state $ \st -> recordProgress st key change
model stateModelIO k = do
state <- fromModel =<< stateModelIO
_ <- k state
toModel state
test name p = testCase name $ do
InProgressModel{..} <- p
(done, todo) @?= (length (filter (==0) (Map.elems current)), Map.size current)
fromModel :: InProgressModel -> IO InProgressState
fromModel InProgressModel{..} = do
doneVar <- newTVarIO done
todoVar <- newTVarIO todo
currentVar <- STM.newIO
atomically $ for_ (Map.toList current) $ \(k,v) -> STM.insert v k currentVar
return InProgressState{..}
toModel :: InProgressState -> IO InProgressModel
toModel InProgressState{..} = atomically $ do
done <- readTVar doneVar
todo <- readTVar todoVar
current <- Map.fromList <$> ListT.toList (STM.listT currentVar)
return InProgressModel{..}