Skip to content

Commit c659700

Browse files
committed
tests
1 parent c9a379e commit c659700

File tree

4 files changed

+98
-9
lines changed

4 files changed

+98
-9
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"url": "git://github.com/purescript-node/purescript-node-streams.git"
1515
},
1616
"devDependencies": {
17-
"purescript-console": "^0.1.0"
17+
"purescript-console": "^0.1.0",
18+
"purescript-assert": "~0.1.1"
1819
},
1920
"dependencies": {
2021
"purescript-eff": "~0.1.1",

src/Node/Stream.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ exports.onDataImpl = function(s) {
1515
return function(f) {
1616
return function() {
1717
s.on('data', function(chunk) {
18+
if (!(chunk instanceof Buffer)) {
19+
throw new Error("Node.Stream.onDataImpl: stream encoding should not be set");
20+
}
1821
f(chunk)();
1922
});
2023
};

test/Main.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,29 @@
22

33
// module Test.Main
44

5-
exports.gzip = require('zlib').createGzip;
5+
6+
exports.writableStreamBuffer = function() {
7+
var W = require('stream-buffers').WritableStreamBuffer;
8+
return new W;
9+
};
10+
11+
exports.getContentsAsString = function(w) {
12+
return function() {
13+
return w.getContentsAsString('utf8');
14+
};
15+
};
16+
17+
exports.readableStreamBuffer = function() {
18+
var R = require('stream-buffers').ReadableStreamBuffer;
19+
return new R;
20+
};
21+
22+
exports.putImpl = function(str) {
23+
return function(enc) {
24+
return function(r) {
25+
return function() {
26+
r.put(str, enc);
27+
};
28+
};
29+
};
30+
};

test/Main.purs

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,80 @@ module Test.Main where
22

33
import Prelude
44

5+
import Node.Buffer as Buffer
6+
import Node.Encoding
57
import Node.Stream
8+
import Node.Stream.StdIO
69

710
import Control.Monad.Eff
811
import Control.Monad.Eff.Console
912

10-
foreign import data GZIP :: !
13+
import Test.Assert
1114

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)
1318

14-
foreign import stdout :: forall eff. Writable () (console :: CONSOLE | eff)
19+
foreign import data STREAM_BUFFER :: !
1520

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)
1731

1832
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\nBergentrü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

Comments
 (0)