Permalink
Browse files

Test suite

  • Loading branch information...
snoyberg committed Dec 9, 2011
1 parent 019375e commit 1f5d6ed95bdfbebc080478292f757a8086f011be
Showing with 66 additions and 0 deletions.
  1. +11 −0 conduit/Data/Conduit/List.hs
  2. +14 −0 conduit/conduit.cabal
  3. +41 −0 conduit/test/main.hs
@@ -6,6 +6,7 @@ module Data.Conduit.List
, map
, concatMap
, head
+ , consume
) where
import qualified Prelude
@@ -68,3 +69,13 @@ map f a = return $ ConduitResult [] $ fmap f a
concatMap :: Monad m => (a -> [b]) -> Conduit a m b
concatMap _ EOF = return $ ConduitResult [] EOF
concatMap f (Chunks l) = return $ ConduitResult [] $ Chunks $ Prelude.concatMap f l
+
+consume :: MonadBaseControl IO m => Sink a m [a]
+consume = Sink $ do
+ ifront <- liftBase $ I.newIORef id
+ return $ SinkData $ go ifront
+ where
+ go ifront EOF = SinkResult [] . Just . ($ []) <$> liftBase (I.readIORef ifront)
+ go ifront (Chunks cs) = do
+ liftBase $ I.atomicModifyIORef ifront $ \front -> (front . (cs ++), ())
+ return $ SinkResult [] Nothing
View
@@ -10,6 +10,7 @@ Category: Data
Build-type: Simple
Cabal-version: >=1.8
Homepage: http://github.com/yesodweb/wai
+extra-source-files: test/main.hs
Library
Exposed-modules: Data.Conduit
@@ -25,6 +26,19 @@ Library
, system-fileio >= 0.3.3 && < 0.4
, system-filepath >= 0.4.3 && < 0.5
, bytestring >= 0.9
+ ghc-options: -Wall
+
+test-suite test
+ hs-source-dirs: test
+ main-is: main.hs
+ type: exitcode-stdio-1.0
+ cpp-options: -DTEST
+ build-depends: conduit
+ , base
+ , hspec
+ , HUnit
+ , QuickCheck
+ ghc-options: -Wall
source-repository head
type: git
View
@@ -0,0 +1,41 @@
+{-# LANGUAGE OverloadedStrings #-}
+import Test.Hspec.Monadic
+import Test.Hspec.HUnit ()
+import Test.Hspec.QuickCheck (prop)
+import Test.HUnit
+
+import qualified Data.Conduit as C
+import qualified Data.Conduit.List as CL
+import qualified Data.Conduit.Binary as CB
+import Control.Monad.Trans.Resource (runResourceT)
+import System.IO.Unsafe (unsafePerformIO)
+import Data.Monoid
+import qualified Data.ByteString as S
+
+main :: IO ()
+main = hspecX $ do
+ describe "sum" $ do
+ prop "is idempotent" $ \list ->
+ (unsafePerformIO $ runResourceT $ CL.fromList list C.$$ CL.fold (+) (0 :: Int))
+ == sum list
+ describe "Monoid instance for Source" $ do
+ it "mappend" $ do
+ x <- runResourceT $ (CL.fromList [1..5 :: Int] `mappend` CL.fromList [6..10]) C.$$ CL.fold (+) 0
+ x @?= sum [1..10]
+ it "mconcat" $ do
+ x <- runResourceT $ mconcat
+ [ CL.fromList [1..5 :: Int]
+ , CL.fromList [6..10]
+ , CL.fromList [11..20]
+ ] C.$$ CL.fold (+) 0
+ x @?= sum [1..20]
+ describe "file access" $ do
+ it "read" $ do
+ bs <- S.readFile "conduit.cabal"
+ bss <- runResourceT $ CB.sourceFile "conduit.cabal" C.$$ CL.consume
+ bs @=? S.concat bss
+ it "write" $ do
+ runResourceT $ CB.sourceFile "conduit.cabal" C.$$ CB.sinkFile "tmp"
+ bs1 <- S.readFile "conduit.cabal"
+ bs2 <- S.readFile "tmp"
+ bs1 @=? bs2

0 comments on commit 1f5d6ed

Please sign in to comment.