2022-04-18 16:29:28 -04:00
|
|
|
{-
|
|
|
|
|
|
|
|
csv-slurp
|
|
|
|
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or (at
|
|
|
|
your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2022-04-19 16:03:14 -04:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2022-04-18 16:29:28 -04:00
|
|
|
module Data.CSV.SlurpSpec (spec) where
|
|
|
|
|
2022-04-19 16:03:14 -04:00
|
|
|
import Conduit (runConduit, (.|))
|
|
|
|
import Data.Conduit.List (consume, sourceList)
|
2022-04-19 16:31:04 -04:00
|
|
|
import Test.Hspec (Spec, context, describe, it, shouldBe, xit)
|
2022-04-19 16:03:14 -04:00
|
|
|
|
|
|
|
import Data.CSV.Slurp
|
2022-04-18 16:29:28 -04:00
|
|
|
|
|
|
|
spec :: Spec
|
2022-04-19 16:31:04 -04:00
|
|
|
spec = describe "Data.CSV.Slurp" $ do
|
2022-04-19 16:03:14 -04:00
|
|
|
decodeRowsSpec
|
2022-04-19 16:31:04 -04:00
|
|
|
decodeUTF8Spec
|
2022-04-19 16:03:14 -04:00
|
|
|
|
|
|
|
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"]]
|
2022-04-18 16:29:28 -04:00
|
|
|
|
2022-04-19 16:31:04 -04:00
|
|
|
decodeUTF8Spec :: Spec
|
|
|
|
decodeUTF8Spec = describe "decodeUTF8" $ mapM_
|
|
|
|
( \(label, input, expected) -> context label $
|
|
|
|
it ("should be " ++ show expected) $
|
|
|
|
decodeUTF8 input `shouldBe` expected
|
|
|
|
)
|
|
|
|
|
|
|
|
-- label, input, expected
|
|
|
|
[ ( "plain ASCII", "hello", Just "hello" )
|
|
|
|
, ( "valid UTF8", "\xc3\xa9", Just "é" )
|
|
|
|
, ( "invalid UTF8", "\xff", Nothing )
|
|
|
|
, ( "blank", "", Just "" )
|
|
|
|
]
|
|
|
|
|
2022-04-18 16:29:28 -04:00
|
|
|
--jl
|