-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOldFileIOTests.hs
117 lines (102 loc) · 3.45 KB
/
OldFileIOTests.hs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-|
Module : OldFileIOTests
Description : Example use cases of the runners for file IO from `Control.Runner.OldFileIO`
Copyright : (c) Danel Ahman, 2019
License : MIT
Maintainer : danel.ahman@eesti.ee
Stability : experimental
This module provides some example use cases of the
runners for file IO from `Control.Runner.OldFileIO`.
-}
module OldFileIOTests where
import Control.Runner
import Control.Runner.OldFileIO
import System.IO
test1 :: User '[File] ()
test1 =
do s <- performU Read;
if s == "foo"
then (performU (Write "contents was foo"))
else (performU (Write "contents was not foo"))
writeLines :: Member File iface => [String] -> User iface ()
writeLines [] = return ()
writeLines (l:ls) = do performU (Write l);
performU (Write "\n");
writeLines ls
exampleLines = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Cras sit amet felis arcu.",
"Maecenas ac mollis mauris, vel fermentum nibh."]
test2 :: User '[IO] ()
test2 = -- in IO signature
run
fioRunner
ioFioInitialiser
( -- in FileIO signature
run
fhRunner
(fioFhInitialiser "./out.txt")
( -- in File signature, with FH runner
writeLines exampleLines -- this runner appends to the existing file
)
fioFhFinaliser
)
ioFioFinaliser
test3 = ioTopLevel test2
test4 :: User '[IO] ()
test4 = -- in IO signature
run
fioRunner
ioFioInitialiser
( -- in FileIO signature
run
fcRunner
(fioFcInitialiser "./out2.txt")
( -- in File signature, with FC runner
writeLines exampleLines -- this runner appends to the existing file
)
(fioFcFinaliser "./out2.txt")
)
ioFioFinaliser
test5 = ioTopLevel test4
test6 :: User '[IO] ()
test6 = -- in IO signature
run
fioRunner
ioFioInitialiser
( -- in FileIO signature
run
fcOwRunner
(fioFcOwInitialiser "./out3.txt")
( -- in File signature, with FC+OW runner
writeLines exampleLines -- this runner overwrites the existing file
)
(fioFcOwFinaliser "./out3.txt")
)
ioFioFinaliser
test7 = ioTopLevel test6
test8 :: User '[IO] ()
test8 = -- in IO signature
run
fioRunner
ioFioInitialiser
( -- in FileIO signature
run
fcOwRunner
(fioFcOwInitialiser "./out4.txt")
( -- in File signature, with FC+OW runner
do s <- performU Read;
performU (Write s);
performU (Write "\n");
performU (Write s);
if not (s == "foo")
then (do performU Clean; -- selectively empties file contents
performU (Write "foo"))
else (return ())
)
(fioFcOwFinaliser "./out4.txt")
)
ioFioFinaliser
test9 = ioTopLevel test8