potoki-core 2.3 → 2.3.0.1
raw patch · 4 files changed
+40/−18 lines, 4 files
Files
- library/Potoki/Core/Transform/ByteString.hs +17/−14
- potoki-core.cabal +1/−1
- test/Main.hs +18/−3
- test/Transform.hs +4/−0
library/Potoki/Core/Transform/ByteString.hs view
@@ -13,6 +13,7 @@ import qualified Data.ByteString.Builder as E import qualified Data.ByteString.Lazy as F import qualified Acquire.Acquire as M+import qualified Control.Monad.Par as Par {-# INLINE builderChunks #-}@@ -29,17 +30,10 @@ lineList >>> list where lineList =- Transform $ \ (A.Fetch fetchIO) -> M.Acquire $ do+ Transform $ \ (Fetch fetchIO) -> liftIO $ do stateRef <- newIORef Nothing- return $ (, return ()) $ A.Fetch $ fetchIO >>= \case- Nothing -> (do- state <- readIORef stateRef- case state of- Just poking -> do- writeIORef stateRef Nothing- return (Just [D.poking poking])- Nothing -> return Nothing)- Just chunk -> (+ return $ Fetch $ fetchIO >>= \case+ Just chunk -> case B.split 10 chunk of firstInput : tailVal -> do state <- readIORef stateRef@@ -56,16 +50,23 @@ do writeIORef stateRef (Just newPoking) return (Just [])- _ -> return (Just []))+ _ -> return (Just [])+ Nothing -> do+ state <- readIORef stateRef+ case state of+ Just poking -> do+ writeIORef stateRef Nothing+ return (Just [D.poking poking])+ Nothing -> return Nothing extractLinesWithoutTrail :: Transform ByteString ByteString extractLinesWithoutTrail = lineList >>> list where lineList =- Transform $ \ (A.Fetch fetchIO) -> M.Acquire $ do+ Transform $ \ (Fetch fetchIO) -> liftIO $ do pokingRef <- newIORef mempty- return $ (, return ()) $ A.Fetch $ do+ return $ Fetch $ do fetchResult <- fetchIO case fetchResult of Just chunk -> case B.split 10 chunk of@@ -88,4 +89,6 @@ then return Nothing else let !bytes = D.poking poking- in return (Just (bytes : []))+ in do+ writeIORef pokingRef mempty+ return (Just [bytes])
potoki-core.cabal view
@@ -1,5 +1,5 @@ name: potoki-core-version: 2.3+version: 2.3.0.1 synopsis: Low-level components of "potoki" description: Provides everything required for building custom instances of
test/Main.hs view
@@ -1,6 +1,6 @@ module Main where -import Prelude hiding (first, second)+import Prelude hiding (first, second, choose) import Control.Arrow import Test.QuickCheck.Instances import Test.QuickCheck.Monadic as M@@ -15,6 +15,7 @@ import qualified Potoki.Core.Fetch as Fe import qualified Data.Attoparsec.ByteString.Char8 as B import qualified Data.ByteString as F+import qualified Data.ByteString.Char8 as I import qualified Data.Vector as G import qualified System.Random as H import qualified Acquire.Acquire as Ac@@ -25,9 +26,23 @@ defaultMain $ testGroup "All tests" $ [+ testGroup "lines extraction props" $ let+ chunksGen = listOf $ do+ byteList <- listOf (frequency [(1, pure 10), (20, choose (97, 122))])+ return (F.pack byteList)+ in [+ testProperty "extractLines" $ forAll chunksGen $ \ chunks ->+ F.split 10 (F.concat chunks) ===+ unsafePerformIO (C.produceAndConsume (E.transform A.extractLines (E.list chunks)) D.list)+ ,+ testProperty "extractLinesWithoutTrail" $ forAll chunksGen $ \ chunks ->+ I.lines (F.concat chunks) ===+ unsafePerformIO (C.produceAndConsume (E.transform A.extractLinesWithoutTrail (E.list chunks)) D.list)+ ]+ , testCase "extractLinesWithoutTrail" $ do- assertEqual "" ["ab", "", "cd"] =<<- C.produceAndConsume (E.transform A.extractLinesWithoutTrail (E.list ["a", "b\n", "\nc", "d\n"])) D.list+ assertEqual "" ["ab", "", "c", "d"] =<<+ C.produceAndConsume (E.transform A.extractLinesWithoutTrail (E.list ["a", "b\n\nc", "\nd\n"])) D.list , testCase "extractLines" $ do assertEqual "" ["ab", "", "cd", ""] =<<
test/Transform.hs view
@@ -21,6 +21,10 @@ transform = testGroup "Transform" $ [+ testProperty "list" $ \ (list :: [[Int]]) -> + concat list ===+ unsafePerformIO (C.produceAndTransformAndConsume (E.list list) A.list D.list)+ , testProperty "Applying chunksOf to list has the same effect as the \"batch\" transform" $ let gen = do list <- listOf (choose (0, 1000 :: Int))