0 | module Control.RIO.Mock.Console
 1 |
 2 | import Control.RIO.Console
 3 | import Data.IORef
 4 |
 5 | %default total
 6 |
 7 | ||| A mock output console where err out and std out are mutable refs
 8 | public export
 9 | record MockOut where
10 |   constructor MkMockOut
11 |   stdOut : IORef (SnocList String)
12 |   errOut : IORef (SnocList String)
13 |
14 | ||| Creates a mock console
15 | export
16 | mkMockOut : IO MockOut
17 | mkMockOut = [| MkMockOut (newIORef [<]) (newIORef [<]) |]
18 |
19 | ||| A mock output console, which uses the
20 | ||| the wrapped mutable refs for simulating
21 | ||| output.
22 | export
23 | consoleOut : MockOut -> ConsoleOut
24 | consoleOut m =
25 |   MkConsoleOut
26 |     (\s => modifyIORef m.stdOut (:< s))
27 |     (\s => modifyIORef m.errOut (:< s))
28 |
29 | ||| A mock console where err out and std out are mutable refs
30 | ||| of snoc lists and `getChar` and `getLine` are simulated
31 | ||| via mutable refs of streams.
32 | public export
33 | record MockIn where
34 |   constructor MkMockIn
35 |   charIn : IORef (Stream Char)
36 |   lineIn : IORef (Stream String)
37 |
38 | ||| Creates a mock console
39 | export
40 | mkMockIn : Stream Char -> Stream String -> IO MockIn
41 | mkMockIn cs ss = [| MkMockIn (newIORef cs) (newIORef ss) |]
42 |
43 | getHead : IORef (Stream a) -> IO a
44 | getHead ref = readIORef ref >>= \(h :: t) => writeIORef ref t $> h
45 |
46 | ||| A mock input console, which uses the given streams for
47 | ||| simulating input
48 | export
49 | console : MockIn -> ConsoleIn
50 | console m =
51 |   MkConsoleIn
52 |     (getHead m.charIn)
53 |     (getHead m.lineIn)
54 |