wrote basic structure of decodeRows
This commit is contained in:
parent
365dc3995b
commit
08c77d4d0f
|
@ -29,6 +29,9 @@ library
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
build-depends:
|
build-depends:
|
||||||
base >=4.7 && <5
|
base >=4.7 && <5
|
||||||
|
, bytestring
|
||||||
|
, conduit >=1.3.4.2 && <1.4
|
||||||
|
, text
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
autogen-modules: Paths_csv_slurp
|
autogen-modules: Paths_csv_slurp
|
||||||
|
|
||||||
|
@ -43,7 +46,10 @@ test-suite csv-slurp-test
|
||||||
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
|
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
|
||||||
build-depends:
|
build-depends:
|
||||||
base >=4.7 && <5
|
base >=4.7 && <5
|
||||||
|
, bytestring
|
||||||
|
, conduit >=1.3.4.2 && <1.4
|
||||||
, csv-slurp
|
, csv-slurp
|
||||||
, hspec >=2.8.5 && <2.9
|
, hspec >=2.8.5 && <2.9
|
||||||
|
, text
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
autogen-modules: Paths_csv_slurp
|
autogen-modules: Paths_csv_slurp
|
||||||
|
|
|
@ -23,6 +23,9 @@ ghc-options:
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
- base >= 4.7 && < 5
|
- base >= 4.7 && < 5
|
||||||
|
- bytestring
|
||||||
|
- conduit >= 1.3.4.2 && < 1.4
|
||||||
|
- text
|
||||||
|
|
||||||
library:
|
library:
|
||||||
source-dirs: src
|
source-dirs: src
|
||||||
|
|
|
@ -23,6 +23,29 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
-}
|
-}
|
||||||
|
|
||||||
module Data.CSV.Slurp where
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
module Data.CSV.Slurp (
|
||||||
|
decodeRows,
|
||||||
|
decodeRawRows,
|
||||||
|
decodeUTF8,
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Conduit (ConduitT, mapC, (.|))
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
|
import Data.Maybe (fromMaybe)
|
||||||
|
import qualified Data.Text as T
|
||||||
|
|
||||||
|
-- | decode the rows from a stream of ByteStrings
|
||||||
|
decodeRows :: Monad m => ConduitT BS.ByteString [T.Text] m ()
|
||||||
|
decodeRows = decodeRawRows .| mapC (map $ fromMaybe "" . decodeUTF8)
|
||||||
|
|
||||||
|
-- | decode the rows returning raw ByteStrings instead of text
|
||||||
|
decodeRawRows :: Monad m => ConduitT BS.ByteString [BS.ByteString] m ()
|
||||||
|
decodeRawRows = return ()
|
||||||
|
|
||||||
|
-- | decode a raw ByteString into Text (if possible)
|
||||||
|
decodeUTF8 :: BS.ByteString -> Maybe T.Text
|
||||||
|
decodeUTF8 = const Nothing
|
||||||
|
|
||||||
--jl
|
--jl
|
||||||
|
|
|
@ -18,11 +18,45 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
-}
|
-}
|
||||||
|
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
module Data.CSV.SlurpSpec (spec) where
|
module Data.CSV.SlurpSpec (spec) where
|
||||||
|
|
||||||
import Test.Hspec (Spec, describe)
|
import Conduit (runConduit, (.|))
|
||||||
|
import Data.Conduit.List (consume, sourceList)
|
||||||
|
import Test.Hspec (Spec, context, describe, shouldBe, xit)
|
||||||
|
|
||||||
|
import Data.CSV.Slurp
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = describe "Data.CSV.Slurp" $ return ()
|
spec = describe "Data.CSV.Slurp"
|
||||||
|
decodeRowsSpec
|
||||||
|
|
||||||
|
decodeRowsSpec :: Spec
|
||||||
|
decodeRowsSpec = describe "decodeRows" $ mapM_
|
||||||
|
( \(label, input, expected) -> context label $ do
|
||||||
|
result <- runConduit $ sourceList input .| decodeRows .| consume
|
||||||
|
let
|
||||||
|
expLen = length expected
|
||||||
|
resLen = length result
|
||||||
|
xit ("should have " ++ show expLen ++ " rows") $
|
||||||
|
resLen `shouldBe` expLen
|
||||||
|
mapM_
|
||||||
|
( \(n, expected', result') -> context ("row " ++ show n) $
|
||||||
|
xit ("should be " ++ show expected') $
|
||||||
|
result' `shouldBe` expected'
|
||||||
|
) $ zip3 [(0::Int)..] expected result
|
||||||
|
)
|
||||||
|
|
||||||
|
-- label, input, expected
|
||||||
|
[ ( "valid", validIn, validRes )
|
||||||
|
, ( "invalid", invalidIn, [] )
|
||||||
|
, ( "empty", [], [] )
|
||||||
|
]
|
||||||
|
|
||||||
|
where
|
||||||
|
validIn = ["foo,bar\r\n", "baz,quuux\r\n"]
|
||||||
|
invalidIn = ["\"a"]
|
||||||
|
validRes = [["foo", "bar"], ["baz", "quux"]]
|
||||||
|
|
||||||
--jl
|
--jl
|
||||||
|
|
|
@ -25,6 +25,6 @@ import Test.Hspec (hspec)
|
||||||
import qualified Data.CSV.SlurpSpec as Slurp
|
import qualified Data.CSV.SlurpSpec as Slurp
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = hspec $ Slurp.spec
|
main = hspec Slurp.spec
|
||||||
|
|
||||||
--jl
|
--jl
|
||||||
|
|
Loading…
Reference in New Issue
Block a user