Add instances for text

This commit is contained in:
Taylor Fausak 2021-04-18 15:08:47 +00:00 committed by GitHub
parent be71496828
commit 3bae76975e
3 changed files with 67 additions and 0 deletions

View File

@ -22,6 +22,8 @@ import qualified Data.Map as Map
import qualified Data.Ratio as Ratio
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import qualified Data.Word as Word
import qualified Numeric.Natural as Natural
import qualified Witch.Cast as Cast
@ -763,6 +765,28 @@ instance Cast.Cast ShortByteString.ShortByteString [Word.Word8] where
instance Cast.Cast ShortByteString.ShortByteString ByteString.ByteString where
cast = ShortByteString.fromShort
-- Text
instance Cast.Cast String Text.Text where
cast = Text.pack
instance Cast.Cast Text.Text String where
cast = Text.unpack
instance Cast.Cast Text.Text LazyText.Text where
cast = LazyText.fromStrict
-- LazyText
instance Cast.Cast String LazyText.Text where
cast = LazyText.pack
instance Cast.Cast LazyText.Text String where
cast = LazyText.unpack
instance Cast.Cast LazyText.Text Text.Text where
cast = LazyText.toStrict
fromNonNegativeIntegral :: (Integral s, Num t) => s -> Maybe t
fromNonNegativeIntegral x = if x < 0 then Nothing else Just $ fromIntegral x

View File

@ -19,6 +19,8 @@ import qualified Data.Map as Map
import qualified Data.Ratio as Ratio
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import qualified Data.Word as Word
import qualified Numeric.Natural as Natural
import qualified Test.Hspec as Hspec
@ -1567,6 +1569,46 @@ main = Hspec.hspec . Hspec.describe "Witch" $ do
test $ f (ShortByteString.pack [0x00]) `Hspec.shouldBe` ByteString.pack [0x00]
test $ f (ShortByteString.pack [0x0f, 0xf0]) `Hspec.shouldBe` ByteString.pack [0x0f, 0xf0]
-- Text
Hspec.describe "Cast String Text" $ do
let f = Witch.cast @String @Text.Text
test $ f "" `Hspec.shouldBe` Text.pack ""
test $ f "a" `Hspec.shouldBe` Text.pack "a"
test $ f "ab" `Hspec.shouldBe` Text.pack "ab"
Hspec.describe "Cast Text String" $ do
let f = Witch.cast @Text.Text @String
test $ f (Text.pack "") `Hspec.shouldBe` ""
test $ f (Text.pack "a") `Hspec.shouldBe` "a"
test $ f (Text.pack "ab") `Hspec.shouldBe` "ab"
Hspec.describe "Cast Text LazyText" $ do
let f = Witch.cast @Text.Text @LazyText.Text
test $ f (Text.pack "") `Hspec.shouldBe` LazyText.pack ""
test $ f (Text.pack "a") `Hspec.shouldBe` LazyText.pack "a"
test $ f (Text.pack "ab") `Hspec.shouldBe` LazyText.pack "ab"
-- LazyText
Hspec.describe "Cast String LazyText" $ do
let f = Witch.cast @String @LazyText.Text
test $ f "" `Hspec.shouldBe` LazyText.pack ""
test $ f "a" `Hspec.shouldBe` LazyText.pack "a"
test $ f "ab" `Hspec.shouldBe` LazyText.pack "ab"
Hspec.describe "Cast LazyText String" $ do
let f = Witch.cast @LazyText.Text @String
test $ f (LazyText.pack "") `Hspec.shouldBe` ""
test $ f (LazyText.pack "a") `Hspec.shouldBe` "a"
test $ f (LazyText.pack "ab") `Hspec.shouldBe` "ab"
Hspec.describe "Cast LazyText Text" $ do
let f = Witch.cast @LazyText.Text @Text.Text
test $ f (LazyText.pack "") `Hspec.shouldBe` Text.pack ""
test $ f (LazyText.pack "a") `Hspec.shouldBe` Text.pack "a"
test $ f (LazyText.pack "ab") `Hspec.shouldBe` Text.pack "ab"
test :: Hspec.Example a => a -> Hspec.SpecWith (Hspec.Arg a)
test = Hspec.it ""

View File

@ -21,6 +21,7 @@ common basics
, base >= 4.13.0 && < 4.16
, bytestring >= 0.10.12 && < 0.11
, containers >= 0.6.2 && < 0.7
, text >= 1.2.4 && < 1.3
default-language: Haskell2010
ghc-options:
-Weverything