-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMain3.purs
58 lines (53 loc) · 2.05 KB
/
Main3.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
-- | How to test:
-- |
-- | ```
-- | pulp test --main Test.Main3 -- <(head --bytes 1000000 /dev/zero)
-- | ```
module Test.Main3 where
import Prelude
import Data.Array as Array
import Data.Either (Either(..))
import Effect (Effect)
import Effect.Aff (Error, runAff_)
import Effect.Class (liftEffect)
import Effect.Class.Console as Console
import Node.Buffer (Buffer, concat)
import Node.Buffer as Buffer
import Node.Stream (Readable)
import Node.Stream.Aff (readAll, readN, readSome)
import Partial.Unsafe (unsafePartial)
import Test.Spec (describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Reporter (consoleReporter)
import Test.Spec.Runner (runSpec)
import Unsafe.Coerce (unsafeCoerce)
foreign import createReadStream :: String -> Effect (Readable ())
foreign import argv :: Effect (Array String)
completion :: Either Error (Effect Unit) -> Effect Unit
completion = case _ of
Left e -> Console.error (unsafeCoerce e)
Right f -> f
main :: Effect Unit
main = unsafePartial $ do
runAff_ completion do
runSpec [ consoleReporter ] do
describe "Node.Stream.Aff" do
it "reads 1" do
infile <- liftEffect $ createReadStream =<< pure <<< flip Array.unsafeIndex 2 =<< argv
{ buffers: inputs1 } <- readN infile 500000
bytesRead1 :: Int <- liftEffect $ Array.foldM (\a b -> (a + _) <$> Buffer.size b) 0 inputs1
shouldEqual 500000 bytesRead1
{ buffers: inputs2 } <- readSome infile
inputs3 <- readAll infile
let inputs = inputs1 <> inputs2 <> inputs3
-- TODO read after EOF will hang
-- inputs4 <- readAll infile
-- inputs4 <- readSome infile
-- inputs4 <- readN infile 10
-- let inputs = inputs1 <> inputs2 <> inputs3 <> inputs4
bytesRead :: Int <- liftEffect $ Array.foldM (\a b -> (a + _) <$> Buffer.size b) 0 inputs
shouldEqual 1000000 bytesRead
input :: Buffer <- liftEffect $ concat inputs
inputSize <- liftEffect $ Buffer.size input
shouldEqual 1000000 inputSize
pure (pure unit)