Skip to content

Commit

Permalink
task: add test cases for decoding escaped input
Browse files Browse the repository at this point in the history
Test case `escapedMalformed0` is failing, throwing uncaught `error`
  • Loading branch information
gzh authored and yitz-zoomin committed Jan 3, 2024
1 parent 206d4a5 commit 0822a68
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
import qualified Data.Vector as V
import qualified Data.Foldable as F
import Data.List (isPrefixOf)
import Data.Word
import Numeric.Natural
import GHC.Generics (Generic)
Expand Down Expand Up @@ -56,6 +57,14 @@ assertResult input expected res = case res of
" input: " ++ show (BL8.unpack input) ++ "\n" ++
"parse error: " ++ err

decodeFailsWith :: BL.ByteString -> String -> Assertion
decodeFailsWith input expected = case decode NoHeader input of
Right r -> assertFailure $ "input: " ++ show (BL8.unpack input) ++ "\n" ++
"retuned: " ++ show (r :: (V.Vector (V.Vector B.ByteString))) ++ "\n" ++
"whereas should have failed with " <> expected
Left err -> assertBool ("got " <> err <> "\ninstead of " <> expected)
$ ("parse error ("++expected++")") `isPrefixOf` err

encodesAs :: [[B.ByteString]] -> BL.ByteString -> Assertion
encodesAs input expected =
encode (map V.fromList input) @?= expected
Expand Down Expand Up @@ -166,6 +175,7 @@ positionalTests =
[ testGroup "decode" $ map streamingDecodeTest decodeTests
, testGroup "decodeWith" $ map streamingDecodeWithTest decodeWithTests
]
, testGroup "escaped" escapedTests
]
where
rfc4180Input = BL8.pack $
Expand Down Expand Up @@ -210,6 +220,15 @@ positionalTests =
defEncAllEnq = defaultEncodeOptions { encQuoting = QuoteAll }
defDec = defaultDecodeOptions

escapedTests = [
testCase "escaped" $
"\"x,y\",z\nbaz,\"bar\nfoo,\"" `decodesAs` [["x,y", "z"], ["baz", "bar\nfoo,"]],
testCase "escapedMalformed1" $
"\"x,\"y" `decodeFailsWith` "endOfInput",
testCase "escapedMalformed0" $
"baz,\"" `decodeFailsWith` "endOfInput"
]

nameBasedTests :: [TF.Test]
nameBasedTests =
[ testGroup "encode" $ map encodeTest
Expand Down

0 comments on commit 0822a68

Please sign in to comment.