-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMain.purs
168 lines (137 loc) · 3.83 KB
/
Main.purs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
module Test.Main where
import Prelude
import Data.Maybe (fromJust, isJust, isNothing)
import Effect (Effect)
import Effect.Console (log)
import Effect.Exception (error)
import Node.Buffer as Buffer
import Node.Encoding (Encoding(..))
import Node.EventEmitter (on_)
import Node.Stream (Duplex, dataH, dataHStr, destroy', end, end', errorH, newPassThrough, pipe, read, readString, readableH, setDefaultEncoding, setEncoding, writeString, writeString')
import Partial.Unsafe (unsafePartial)
import Test.Assert (assert, assert')
assertEqual :: forall a. Show a => Eq a => a -> a -> Effect Unit
assertEqual x y =
assert' (show x <> " did not equal " <> show y) (x == y)
main :: Effect Unit
main = do
log "setDefaultEncoding should not affect writing"
testSetDefaultEncoding
log "setEncoding should not affect reading"
testSetEncoding
log "test pipe"
testPipe
log "test write"
testWrite
log "test end"
testEnd
log "test manual reads"
testReads
testString :: String
testString = "üöß💡"
testReads :: Effect Unit
testReads = do
testReadString
testReadBuf
where
testReadString = do
sIn <- newPassThrough
v <- readString sIn UTF8
assert (isNothing v)
sIn # on_ readableH do
str <- readString sIn UTF8
assert (isJust str)
assertEqual (unsafePartial (fromJust str)) testString
pure unit
void $ writeString sIn UTF8 testString
testReadBuf = do
sIn <- newPassThrough
v <- read sIn
assert (isNothing v)
sIn # on_ readableH do
buf <- read sIn
assert (isJust buf)
_ <- assertEqual <$> (Buffer.toString UTF8 (unsafePartial (fromJust buf)))
<*> pure testString
pure unit
void $ writeString sIn UTF8 testString
testSetDefaultEncoding :: Effect Unit
testSetDefaultEncoding = do
w1 <- newPassThrough
check w1
w2 <- newPassThrough
setDefaultEncoding w2 UCS2
check w2
where
check w = do
w # on_ dataH \buf -> do
str <- Buffer.toString UTF8 buf
assertEqual testString str
void $ writeString w UTF8 testString
testSetEncoding :: Effect Unit
testSetEncoding = do
check UTF8
check UTF16LE
check UCS2
where
check enc = do
r1 <- newPassThrough
void $ writeString r1 enc testString
r2 <- newPassThrough
void $ writeString r1 enc testString
setEncoding r2 enc
r1 # on_ dataH \buf -> do
r2 # on_ dataHStr \str -> do
join $ assertEqual <$> Buffer.toString enc buf <*> pure testString
assertEqual str testString
testPipe :: Effect Unit
testPipe = do
sIn <- newPassThrough
sOut <- newPassThrough
zip <- createGzip
unzip <- createGunzip
log "pipe 1"
_ <- sIn `pipe` zip
log "pipe 2"
_ <- zip `pipe` unzip
log "pipe 3"
_ <- unzip `pipe` sOut
void $ writeString' sIn UTF8 testString \_ -> do
end' sIn \_ -> do
sOut # on_ dataH \buf -> do
str <- Buffer.toString UTF8 buf
assertEqual str testString
foreign import createGzip :: Effect Duplex
foreign import createGunzip :: Effect Duplex
testWrite :: Effect Unit
testWrite = do
hasError
noError
where
hasError = do
w1 <- newPassThrough
w1 # on_ errorH (const $ pure unit)
end w1
void $ writeString' w1 UTF8 "msg" \err -> do
assert' "writeString - should have error" $ isJust err
noError = do
w1 <- newPassThrough
void $ writeString' w1 UTF8 "msg1" \err -> do
assert' "writeString - should have no error" $ isNothing err
end w1
testEnd :: Effect Unit
testEnd = do
hasError
noError
where
hasError = do
w1 <- newPassThrough
w1 # on_ errorH (const $ pure unit)
void $ writeString' w1 UTF8 "msg" \_ -> do
_ <- destroy' w1 $ error "Problem"
end' w1 \err -> do
assert' "end - should have error" $ isJust err
noError = do
w1 <- newPassThrough
end' w1 \err -> do
assert' "end - should have no error" $ isNothing err