Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property tests and Char8 un/pack fix #6

Merged
merged 2 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
dist-*
TAGS
*.swp
12 changes: 6 additions & 6 deletions Data/String/Conv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module Data.String.Conv
) where

------------------------------------------------------------------------------
import Data.ByteString.Char8 as B
import Data.ByteString.Lazy.Char8 as LB
import Data.ByteString as B
import Data.ByteString.Lazy as LB
import Data.Text as T
import Data.Text.Encoding as T
import Data.Text.Encoding.Error as T
Expand Down Expand Up @@ -61,18 +61,18 @@ toSL = strConv Lenient


instance StringConv String String where strConv _ = id
instance StringConv String B.ByteString where strConv _ = B.pack
instance StringConv String LB.ByteString where strConv _ = LB.pack
instance StringConv String B.ByteString where strConv _ = T.encodeUtf8 . T.pack
instance StringConv String LB.ByteString where strConv _ = LT.encodeUtf8 . LT.pack
instance StringConv String T.Text where strConv _ = T.pack
instance StringConv String LT.Text where strConv _ = LT.pack

instance StringConv B.ByteString String where strConv _ = B.unpack
instance StringConv B.ByteString String where strConv l = T.unpack . decodeUtf8T l
instance StringConv B.ByteString B.ByteString where strConv _ = id
instance StringConv B.ByteString LB.ByteString where strConv _ = LB.fromChunks . return
instance StringConv B.ByteString T.Text where strConv = decodeUtf8T
instance StringConv B.ByteString LT.Text where strConv l = strConv l . LB.fromChunks . return

instance StringConv LB.ByteString String where strConv _ = LB.unpack
instance StringConv LB.ByteString String where strConv l = LT.unpack . decodeUtf8LT l
instance StringConv LB.ByteString B.ByteString where strConv _ = B.concat . LB.toChunks
instance StringConv LB.ByteString LB.ByteString where strConv _ = id
instance StringConv LB.ByteString T.Text where strConv l = decodeUtf8T l . strConv l
Expand Down
13 changes: 13 additions & 0 deletions string-conv.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ library
exposed-modules: Data.String.Conv
build-depends: base >= 4.4 && < 5, bytestring, text
ghc-options: -Wall

test-suite tests
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends: base >= 4.4 && < 5
, bytestring
, quickcheck-instances
, string-conv
, tasty
, tasty-quickcheck
, text
88 changes: 88 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{-# LANGUAGE OverloadedStrings #-}

import Data.ByteString as B
import Data.ByteString.Lazy as LB
import Data.String.Conv
import Data.Text as T
import Data.Text.Lazy as LT
import Test.QuickCheck.Instances.ByteString ()
import Test.QuickCheck.Instances.Text ()
import Test.Tasty
import Test.Tasty.QuickCheck


main :: IO ()
main = defaultMain $ testGroup " tests"
[ strictDecoding
, lenientDecoding
]


strictDecoding :: TestTree
strictDecoding = testGroup "strict decoding (toS method)"
[ testProperty "converting String to String" $ do
\s -> s == (toS (toS (s :: String) :: String))
, testProperty "converting String to strict ByteString" $ do
\s -> s == (toS (toS (s :: String) :: B.ByteString))
, testProperty "converting String to lazy ByteString" $ do
\s -> s == (toS (toS (s :: String) :: LB.ByteString))
, testProperty "converting String to strict Text" $ do
\s -> s == (toS (toS (s :: String) :: T.Text))
, testProperty "converting String to lazy Text" $ do
\s -> s == (toS (toS (s :: String) :: LT.Text))
, testProperty "converting strict ByteString to strict ByteString" $ do
\s -> s == (toS (toS (s :: B.ByteString) :: B.ByteString))
, testProperty "converting strict ByteString to lazy ByteString" $ do
\s -> s == (toS (toS (s :: B.ByteString) :: LB.ByteString))
, testProperty "converting lazy ByteString to lazy ByteString" $ do
\s -> s == (toS (toS (s :: LB.ByteString) :: LB.ByteString))
, testProperty "converting strict Text to lazy ByteString" $ do
\s -> s == (toS (toS (s :: T.Text) :: LB.ByteString))
, testProperty "converting strict Text to strict ByteString" $ do
\s -> s == (toS (toS (s :: LB.ByteString) :: B.ByteString))
, testProperty "converting strict Text to strict Text" $ do
\s -> s == (toS (toS (s :: T.Text) :: T.Text))
, testProperty "converting strict Text to lazy Text" $ do
\s -> s == (toS (toS (s :: T.Text) :: LT.Text))
, testProperty "converting lazy Text to strict ByteString" $ do
\s -> s == (toS (toS (s :: LT.Text) :: B.ByteString))
, testProperty "converting lazy Text to lazy ByteString" $ do
\s -> s == (toS (toS (s :: LT.Text) :: LB.ByteString))
, testProperty "converting lazy Text to lazy Text" $ do
\s -> s == (toS (toS (s :: LT.Text) :: LT.Text))
]


lenientDecoding :: TestTree
lenientDecoding = testGroup "lenient decoding (toSL method)"
[ testProperty "converting String to String" $ do
\s -> s == (toSL (toSL (s :: String) :: String))
, testProperty "converting String to strict ByteString" $ do
\s -> s == (toSL (toSL (s :: String) :: B.ByteString))
, testProperty "converting String to lazy ByteString" $ do
\s -> s == (toSL (toSL (s :: String) :: LB.ByteString))
, testProperty "converting String to strict Text" $ do
\s -> s == (toSL (toSL (s :: String) :: T.Text))
, testProperty "converting String to lazy Text" $ do
\s -> s == (toSL (toSL (s :: String) :: LT.Text))
, testProperty "converting strict ByteString to strict ByteString" $ do
\s -> s == (toSL (toSL (s :: B.ByteString) :: B.ByteString))
, testProperty "converting strict ByteString to lazy ByteString" $ do
\s -> s == (toSL (toSL (s :: B.ByteString) :: LB.ByteString))
, testProperty "converting lazy ByteString to lazy ByteString" $ do
\s -> s == (toSL (toSL (s :: LB.ByteString) :: LB.ByteString))
, testProperty "converting strict Text to lazy ByteString" $ do
\s -> s == (toSL (toSL (s :: T.Text) :: LB.ByteString))
, testProperty "converting strict Text to strict ByteString" $ do
\s -> s == (toSL (toSL (s :: LB.ByteString) :: B.ByteString))
, testProperty "converting strict Text to strict Text" $ do
\s -> s == (toSL (toSL (s :: T.Text) :: T.Text))
, testProperty "converting strict Text to lazy Text" $ do
\s -> s == (toSL (toSL (s :: T.Text) :: LT.Text))
, testProperty "converting lazy Text to strict ByteString" $ do
\s -> s == (toSL (toSL (s :: LT.Text) :: B.ByteString))
, testProperty "converting lazy Text to lazy ByteString" $ do
\s -> s == (toSL (toSL (s :: LT.Text) :: LB.ByteString))
, testProperty "converting lazy Text to lazy Text" $ do
\s -> s == (toSL (toSL (s :: LT.Text) :: LT.Text))
]