-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileIOTests.hs
136 lines (120 loc) · 4.56 KB
/
FileIOTests.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-|
Module : FileIOTests
Description : Example use cases of the runners for file IO from `Control.Runner.FileIO`
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.FileIO`.
-}
module FileIOTests where
import Control.Runner
import Control.Runner.FileIO
import System.IO hiding (withFile)
writeLines :: Member File sig => [String] -> User sig ()
writeLines [] = return ()
writeLines (l:ls) = do fWrite l;
fWrite "\n";
writeLines ls
exampleLines = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Cras sit amet felis arcu.",
"Maecenas ac mollis mauris, vel fermentum nibh."]
test1 :: User '[IO] ()
test1 = -- in IO signature, using IO container
run
fioRunner
ioFioInitialiser
( -- in FileIO signature, using FIO runner
run
fhRunner
(fioFhInitialiser "./out.txt")
( -- in File signature, using FH runner
writeLines exampleLines
)
fioFhFinaliser
)
ioFioFinaliser
test2 = ioTopLevel test1
test3 :: User '[IO] ()
test3 = -- in IO signature, using IO container
run
fioRunner
ioFioInitialiser
( -- in FileIO signature, using FIO runner
run
fhRunner
(fioFhInitialiser "./out2.txt")
( -- in File signature, using FH runner
do s <- fRead; -- contents of the file at the point of entering the FH runner
fWrite s; -- write the contents back to the file
writeLines exampleLines -- write additional lines to the file
)
fioFhFinaliser
)
ioFioFinaliser
test4 = ioTopLevel test3
test5 :: User '[IO] ()
test5 = -- in IO signature, using IO container
run
fioRunner
ioFioInitialiser
( -- in FileIO signature, using FIO runner
run
fhRunner
(fioFhInitialiser "./out3.txt")
( -- in File signature, using FH runner
do run
fcRunner
fhFcInitialiser
( -- in File signature, using FC runner
writeLines exampleLines -- writing example lines using FC runner
)
fhFcFinaliser;
fWrite "Proin eu porttitor enim." -- writing another line using FH runner
)
fioFhFinaliser
)
ioFioFinaliser
test6 = ioTopLevel test5
test7 :: User '[IO] ()
test7 = -- in IO signature, using IO container
run
fioRunner
ioFioInitialiser
( -- in FileIO signature, using FIO runner
run
fhRunner
(fioFhInitialiser "./out3.txt")
( -- in File signature, using FH runner
do run
fcRunner
fhFcInitialiser
( -- in File signature, using FC runner
writeLines exampleLines -- writing example lines using FC runner
)
(\ x s -> do fWrite "Finalising FC runner\n";
fhFcFinaliser x s);
fWrite "Proin eu porttitor enim.\n" -- writing another line using FH runner
)
(\ x (s,fh) -> do fWriteOS fh "Finalising FH runner\n";
fioFhFinaliser x (s,fh))
)
ioFioFinaliser
test8 = ioTopLevel test7
test9 :: User '[IO] ()
test9 = -- in IO signature, using IO container
withFile
"./out4.txt"
( -- in File signature, using FIO,FH,FC runners
do writeLines exampleLines;
fWrite "\nProin eu porttitor enim.\n\n";
s <- fRead; -- the initial contents of the file
fWrite "\nInitial contents of the file [START]\n";
fWrite s;
fWrite "\nInitial contents of the file [END]\n"
)
test10 = ioTopLevel test9