@@ -2,20 +2,80 @@ module Test.Main where
2
2
3
3
import Prelude
4
4
5
+ import Node.Buffer as Buffer
6
+ import Node.Encoding
5
7
import Node.Stream
8
+ import Node.Stream.StdIO
6
9
7
10
import Control.Monad.Eff
8
11
import Control.Monad.Eff.Console
9
12
10
- foreign import data GZIP :: !
13
+ import Test.Assert
11
14
12
- foreign import stdin :: forall eff . Readable () (console :: CONSOLE | eff )
15
+ assertEqual :: forall e a . (Show a , Eq a ) => a -> a -> Eff (assert :: ASSERT | e ) Unit
16
+ assertEqual x y =
17
+ assert' (show x <> " did not equal " <> show y) (x == y)
13
18
14
- foreign import stdout :: forall eff . Writable () ( console :: CONSOLE | eff )
19
+ foreign import data STREAM_BUFFER :: !
15
20
16
- foreign import gzip :: forall eff . Eff (gzip :: GZIP | eff ) (Duplex (gzip :: GZIP | eff ))
21
+ foreign import writableStreamBuffer :: forall eff . Eff (sb :: STREAM_BUFFER | eff ) (Writable () (sb :: STREAM_BUFFER | eff ))
22
+
23
+ foreign import getContentsAsString :: forall r eff . Writable r (sb :: STREAM_BUFFER | eff ) -> Eff (sb :: STREAM_BUFFER | eff ) String
24
+
25
+ foreign import readableStreamBuffer :: forall eff . Eff (sb :: STREAM_BUFFER | eff ) (Readable () (sb :: STREAM_BUFFER | eff ))
26
+
27
+ foreign import putImpl :: forall r eff . String -> String -> Readable r (sb :: STREAM_BUFFER | eff ) -> Eff (sb :: STREAM_BUFFER | eff ) Unit
28
+
29
+ put :: forall r eff . String -> Encoding -> Readable r (sb :: STREAM_BUFFER | eff ) -> Eff (sb :: STREAM_BUFFER | eff ) Unit
30
+ put str enc = putImpl str (show enc)
17
31
18
32
main = do
19
- z <- gzip
20
- stdin `pipe` z
21
- z `pipe` stdout
33
+ log " setDefaultEncoding should not affect writing"
34
+ testSetDefaultEncoding
35
+
36
+ log " setEncoding should not affect reading"
37
+ testSetEncoding
38
+
39
+ testString :: String
40
+ testString = " Liebe Grüße\n Bergentrückung\n 💡"
41
+
42
+ testSetDefaultEncoding = do
43
+ w1 <- writableStreamBuffer
44
+ check w1
45
+
46
+ w2 <- writableStreamBuffer
47
+ setDefaultEncoding w2 UCS2
48
+ check w2
49
+
50
+ where
51
+ check w = do
52
+ writeString w UTF8 testString do
53
+ c <- getContentsAsString w
54
+ assertEqual testString c
55
+
56
+ testSetEncoding = do
57
+ check readableStreamBuffer
58
+
59
+ check do
60
+ r2 <- readableStreamBuffer
61
+ setEncoding r2 UTF8
62
+ pure r2
63
+
64
+ check do
65
+ r3 <- readableStreamBuffer
66
+ setEncoding r3 UCS2
67
+ pure r3
68
+
69
+ where
70
+ check makeR = do
71
+ r1 <- makeR
72
+ put testString UTF8 r1
73
+
74
+ r2 <- makeR
75
+ put testString UTF8 r2
76
+
77
+ onData r1 \buf -> do
78
+ onDataString r2 UTF8 \str -> do
79
+ assertEqual <$> Buffer .toString UTF8 buf <*> pure testString
80
+ assertEqual str testString
81
+ log " ok."
0 commit comments