Merge pull request #34 from tfausak/gh33-strings

Add more instances for string types
This commit is contained in:
Taylor Fausak 2021-05-29 08:35:49 -04:00 committed by GitHub
commit f48ea4c7b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 236 additions and 37 deletions

View File

@ -1015,6 +1015,14 @@ instance From.From ByteString.ByteString ShortByteString.ShortByteString where
instance TryFrom.TryFrom ByteString.ByteString Text.Text where
tryFrom = Utility.eitherTryFrom Text.decodeUtf8'
-- | Converts via 'Text.Text'.
instance TryFrom.TryFrom ByteString.ByteString LazyText.Text where
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @LazyText.Text) . Utility.tryInto @Text.Text
-- | Converts via 'Text.Text'.
instance TryFrom.TryFrom ByteString.ByteString String where
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @Text.Text
-- LazyByteString
-- | Uses 'LazyByteString.pack'.
@ -1033,6 +1041,14 @@ instance From.From LazyByteString.ByteString ByteString.ByteString where
instance TryFrom.TryFrom LazyByteString.ByteString LazyText.Text where
tryFrom = Utility.eitherTryFrom LazyText.decodeUtf8'
-- | Converts via 'LazyText.Text'.
instance TryFrom.TryFrom LazyByteString.ByteString Text.Text where
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @Text.Text) . Utility.tryInto @LazyText.Text
-- | Converts via 'LazyText.Text'.
instance TryFrom.TryFrom LazyByteString.ByteString String where
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @LazyText.Text
-- ShortByteString
-- | Uses 'ShortByteString.pack'.
@ -1049,15 +1065,6 @@ instance From.From ShortByteString.ShortByteString ByteString.ByteString where
-- Text
-- | Uses 'Text.pack'. Some 'Char' values cannot be represented in 'Text.Text'
-- and will be replaced with @'\\xFFFD'@.
instance From.From String Text.Text where
from = Text.pack
-- | Uses 'Text.unpack'.
instance From.From Text.Text String where
from = Text.unpack
-- | Uses 'LazyText.fromStrict'.
instance From.From Text.Text LazyText.Text where
from = LazyText.fromStrict
@ -1066,8 +1073,35 @@ instance From.From Text.Text LazyText.Text where
instance From.From Text.Text ByteString.ByteString where
from = Text.encodeUtf8
-- | Converts via 'ByteString.ByteString'.
instance From.From Text.Text LazyByteString.ByteString where
from = Utility.via @ByteString.ByteString
-- LazyText
-- | Uses 'LazyText.toStrict'.
instance From.From LazyText.Text Text.Text where
from = LazyText.toStrict
-- | Uses 'LazyText.encodeUtf8'.
instance From.From LazyText.Text LazyByteString.ByteString where
from = LazyText.encodeUtf8
-- | Converts via 'LazyByteString.ByteString'.
instance From.From LazyText.Text ByteString.ByteString where
from = Utility.via @LazyByteString.ByteString
-- String
-- | Uses 'Text.pack'. Some 'Char' values cannot be represented in 'Text.Text'
-- and will be replaced with @'\\xFFFD'@.
instance From.From String Text.Text where
from = Text.pack
-- | Uses 'Text.unpack'.
instance From.From Text.Text String where
from = Text.unpack
-- | Uses 'LazyText.pack'. Some 'Char' values cannot be represented in
-- 'LazyText.Text' and will be replaced with @'\\xFFFD'@.
instance From.From String LazyText.Text where
@ -1077,13 +1111,13 @@ instance From.From String LazyText.Text where
instance From.From LazyText.Text String where
from = LazyText.unpack
-- | Uses 'LazyText.toStrict'.
instance From.From LazyText.Text Text.Text where
from = LazyText.toStrict
-- | Converts via 'Text.Text'.
instance From.From String ByteString.ByteString where
from = Utility.via @Text.Text
-- | Uses 'LazyText.encodeUtf8'.
instance From.From LazyText.Text LazyByteString.ByteString where
from = LazyText.encodeUtf8
-- | Converts via 'LazyText.Text'.
instance From.From String LazyByteString.ByteString where
from = Utility.via @LazyText.Text
-- TryFromException

View File

@ -92,7 +92,14 @@ main = runTestTTAndExit $ "Witch" ~:
]
]
, "Instances" ~:
[ "From Int8 Int16" ~:
[ "From a a" ~:
let f = Witch.from @Int @Int in
[ f 0 ~?= 0
]
-- Int8
, "From Int8 Int16" ~:
let f = Witch.from @Int.Int8 @Int.Int16 in
[ f 0 ~?= 0
, f 127 ~?= 127
@ -170,6 +177,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f 127 ~?= 127
, f (-128) ~?= (-128)
]
-- Int16
, "TryFrom Int16 Int8" ~:
let f = hush . Witch.tryFrom @Int.Int16 @Int.Int8 in
[ f 0 ~?= Just 0
@ -251,7 +261,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f 32767 ~?= 32767
, f (-32768) ~?= (-32768)
]
]
-- Int32
, "TryFrom Int32 Int8" ~:
let f = hush . Witch.tryFrom @Int.Int32 @Int.Int8 in
[ f 0 ~?= Just 0
@ -338,6 +350,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f 2147483647 ~?= 2147483647
, f (-2147483648) ~?= (-2147483648)
]
-- Int64
, "TryFrom Int64 Int8" ~:
let f = hush . Witch.tryFrom @Int.Int64 @Int.Int8 in
[ f 0 ~?= Just 0
@ -434,6 +449,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (-9007199254740991) ~?= Just (-9007199254740991)
, f (-9007199254740992) ~?= Nothing
]
-- Int
, "TryFrom Int Int8" ~:
let f = hush . Witch.tryFrom @Int @Int.Int8 in
[ f 0 ~?= Just 0
@ -541,6 +559,9 @@ main = runTestTTAndExit $ "Witch" ~:
then f (-9007199254740992) ~?= Nothing
else f x ~?= Just (fromIntegral x)
]
-- Integer
, "TryFrom Integer Int8" ~:
let f = hush . Witch.tryFrom @Integer @Int.Int8 in
[ f 0 ~?= Just 0
@ -638,6 +659,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (-9007199254740991) ~?= Just (-9007199254740991)
, f (-9007199254740992) ~?= Nothing
]
-- Word8
, "From Word8 Word16" ~:
let f = Witch.from @Word.Word8 @Word.Word16 in
[ f 0 ~?= 0
@ -704,6 +728,9 @@ main = runTestTTAndExit $ "Witch" ~:
[ f 0 ~?= 0
, f 255 ~?= 255
]
-- Word16
, "TryFrom Word16 Word8" ~:
let f = hush . Witch.tryFrom @Word.Word16 @Word.Word8 in
[ f 0 ~?= Just 0
@ -772,6 +799,9 @@ main = runTestTTAndExit $ "Witch" ~:
[ f 0 ~?= 0
, f 65535 ~?= 65535
]
-- Word32
, "TryFrom Word32 Word8" ~:
let f = hush . Witch.tryFrom @Word.Word32 @Word.Word8 in
[ f 0 ~?= Just 0
@ -845,6 +875,9 @@ main = runTestTTAndExit $ "Witch" ~:
[ f 0 ~?= 0
, f 4294967295 ~?= 4294967295
]
-- Word64
, "TryFrom Word64 Word8" ~:
let f = hush . Witch.tryFrom @Word.Word64 @Word.Word8 in
[ f 0 ~?= Just 0
@ -922,6 +955,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f 9007199254740991 ~?= Just 9007199254740991
, f 9007199254740992 ~?= Nothing
]
-- Word
, "TryFrom Word Word8" ~:
let f = hush . Witch.tryFrom @Word @Word.Word8 in
[ f 0 ~?= Just 0
@ -1007,6 +1043,9 @@ main = runTestTTAndExit $ "Witch" ~:
then f 9007199254740992 ~?= Nothing
else f x ~?= Just (fromIntegral x)
]
-- Natural
, "TryFrom Natural Word8" ~:
let f = hush . Witch.tryFrom @Natural.Natural @Word.Word8 in
[ f 0 ~?= Just 0
@ -1084,6 +1123,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f 9007199254740991 ~?= Just 9007199254740991
, f 9007199254740992 ~?= Nothing
]
-- Float
, "TryFrom Float Int8" ~:
let f = hush . Witch.tryFrom @Float @Int.Int8 in
[ f 0 ~?= Just 0
@ -1227,6 +1269,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (1 / 0) ~?= (1 / 0)
, f (-1 / 0) ~?= (-1 / 0)
]
-- Double
, "TryFrom Double Int8" ~:
let f = hush . Witch.tryFrom @Double @Int.Int8 in
[ f 0 ~?= Just 0
@ -1376,6 +1421,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (1 / 0) ~?= (1 / 0)
, f (-1 / 0) ~?= (-1 / 0)
]
-- Ratio
, "From a (Ratio a)" ~:
let f = Witch.from @Int @(Ratio.Ratio Int) in
[ Witch.from @Integer @Rational 0 ~?= 0
@ -1400,6 +1448,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f 0.5 ~?= 0.5
, f (-0.5) ~?= (-0.5)
]
-- Fixed
, "From Integer (Fixed a)" ~:
let f = Witch.from @Integer @Fixed.Deci in
[ Witch.from @Integer @Fixed.Uni 1 ~?= 1
@ -1410,6 +1461,9 @@ main = runTestTTAndExit $ "Witch" ~:
[ Witch.from @Fixed.Uni @Integer 1 ~?= 1
, f 1 ~?= 10
]
-- Complex
, "From a (Complex a)" ~:
let f = Witch.from @Float @(Complex.Complex Float) in
[ Witch.from @Double @(Complex.Complex Double) 1 ~?= 1
@ -1422,6 +1476,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f 1 ~?= Just 1
, f (0 Complex.:+ 1) ~?= Nothing
]
-- NonEmpty
, "TryFrom [a] (NonEmpty a)" ~:
let f = hush . Witch.tryFrom @[Int] @(NonEmpty.NonEmpty Int) in
[ f [] ~?= Nothing
@ -1433,6 +1490,9 @@ main = runTestTTAndExit $ "Witch" ~:
[ f (1 NonEmpty.:| []) ~?= [1]
, f (1 NonEmpty.:| [2]) ~?= [1, 2]
]
-- Set
, "From [a] (Set a)" ~:
let f = Witch.from @[Char] @(Set.Set Char) in
[ f [] ~?= Set.fromList []
@ -1446,6 +1506,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (Set.fromList ['a']) ~?= ['a']
, f (Set.fromList ['a', 'b']) ~?= ['a', 'b']
]
-- IntSet
, "From [Int] IntSet" ~:
let f = Witch.from @[Int] @IntSet.IntSet in
[ f [] ~?= IntSet.fromList []
@ -1458,6 +1521,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (IntSet.fromList [1]) ~?= [1]
, f (IntSet.fromList [1, 2]) ~?= [1, 2]
]
-- Map
, "From [(k, v)] (Map k v)" ~:
let f = Witch.from @[(Char, Int)] @(Map.Map Char Int) in
[ f [] ~?= Map.empty
@ -1471,6 +1537,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (Map.fromList [('a', 1)]) ~?= [('a', 1)]
, f (Map.fromList [('a', 1), ('b', 2)]) ~?= [('a', 1), ('b', 2)]
]
-- IntMap
, "From [(Int, v)] (IntMap v)" ~:
let f = Witch.from @[(Int, Char)] @(IntMap.IntMap Char) in
[ f [] ~?= IntMap.fromList []
@ -1484,6 +1553,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (IntMap.fromList [(1, 'a')]) ~?= [(1, 'a')]
, f (IntMap.fromList [(1, 'a'), (2, 'b')]) ~?= [(1, 'a'), (2, 'b')]
]
-- Seq
, "From [a] (Seq a)" ~:
let f = Witch.from @[Int] @(Seq.Seq Int) in
[ f [] ~?= Seq.fromList []
@ -1496,6 +1568,9 @@ main = runTestTTAndExit $ "Witch" ~:
, f (Seq.fromList [1]) ~?= [1]
, f (Seq.fromList [1, 2]) ~?= [1, 2]
]
-- ByteString
, "From [Word8] ByteString" ~:
let f = Witch.from @[Word.Word8] @ByteString.ByteString in
[ f [] ~?= ByteString.pack []
@ -1526,6 +1601,21 @@ main = runTestTTAndExit $ "Witch" ~:
, f (ByteString.pack [0x61]) ~?= Just (Text.pack "a")
, f (ByteString.pack [0xff]) ~?= Nothing
]
, "TryFrom ByteString LazyText" ~:
let f = hush . Witch.tryFrom @ByteString.ByteString @LazyText.Text in
[ f (ByteString.pack []) ~?= Just (LazyText.pack "")
, f (ByteString.pack [0x61]) ~?= Just (LazyText.pack "a")
, f (ByteString.pack [0xff]) ~?= Nothing
]
, "TryFrom ByteString String" ~:
let f = hush . Witch.tryFrom @ByteString.ByteString @String in
[ f (ByteString.pack []) ~?= Just ""
, f (ByteString.pack [0x61]) ~?= Just "a"
, f (ByteString.pack [0xff]) ~?= Nothing
]
-- LazyByteString
, "From [Word8] LazyByteString" ~:
let f = Witch.from @[Word.Word8] @LazyByteString.ByteString in
[ f [] ~?= LazyByteString.pack []
@ -1550,6 +1640,21 @@ main = runTestTTAndExit $ "Witch" ~:
, f (LazyByteString.pack [0x61]) ~?= Just (LazyText.pack "a")
, f (LazyByteString.pack [0xff]) ~?= Nothing
]
, "TryFrom LazyByteString Text" ~:
let f = hush . Witch.tryFrom @LazyByteString.ByteString @Text.Text in
[ f (LazyByteString.pack []) ~?= Just (Text.pack "")
, f (LazyByteString.pack [0x61]) ~?= Just (Text.pack "a")
, f (LazyByteString.pack [0xff]) ~?= Nothing
]
, "TryFrom LazyByteString String" ~:
let f = hush . Witch.tryFrom @LazyByteString.ByteString @String in
[ f (LazyByteString.pack []) ~?= Just ""
, f (LazyByteString.pack [0x61]) ~?= Just "a"
, f (LazyByteString.pack [0xff]) ~?= Nothing
]
-- ShortByteString
, "From [Word8] ShortByteString" ~:
let f = Witch.from @[Word.Word8] @ShortByteString.ShortByteString in
[ f [] ~?= ShortByteString.pack []
@ -1568,6 +1673,47 @@ main = runTestTTAndExit $ "Witch" ~:
, f (ShortByteString.pack [0x00]) ~?= ByteString.pack [0x00]
, f (ShortByteString.pack [0x0f, 0xf0]) ~?= ByteString.pack [0x0f, 0xf0]
]
-- Text
, "From Text LazyText" ~:
let f = Witch.from @Text.Text @LazyText.Text in
[ f (Text.pack "") ~?= LazyText.pack ""
, f (Text.pack "a") ~?= LazyText.pack "a"
, f (Text.pack "ab") ~?= LazyText.pack "ab"
]
, "From Text ByteString" ~:
let f = Witch.from @Text.Text @ByteString.ByteString in
[ f (Text.pack "") ~?= ByteString.pack []
, f (Text.pack "a") ~?= ByteString.pack [0x61]
]
, "From Text LazyByteString" ~:
let f = Witch.from @Text.Text @LazyByteString.ByteString in
[ f (Text.pack "") ~?= LazyByteString.pack []
, f (Text.pack "a") ~?= LazyByteString.pack [0x61]
]
-- LazyText
, "From LazyText Text" ~:
let f = Witch.from @LazyText.Text @Text.Text in
[ f (LazyText.pack "") ~?= Text.pack ""
, f (LazyText.pack "a") ~?= Text.pack "a"
, f (LazyText.pack "ab") ~?= Text.pack "ab"
]
, "From LazyText LazyByteString" ~:
let f = Witch.from @LazyText.Text @LazyByteString.ByteString in
[ f (LazyText.pack "") ~?= LazyByteString.pack []
, f (LazyText.pack "a") ~?= LazyByteString.pack [0x61]
]
, "From LazyText ByteString" ~:
let f = Witch.from @LazyText.Text @ByteString.ByteString in
[ f (LazyText.pack "") ~?= ByteString.pack []
, f (LazyText.pack "a") ~?= ByteString.pack [0x61]
]
-- String
, "From String Text" ~:
let f = Witch.from @String @Text.Text in
[ f "" ~?= Text.pack ""
@ -1580,17 +1726,6 @@ main = runTestTTAndExit $ "Witch" ~:
, f (Text.pack "a") ~?= "a"
, f (Text.pack "ab") ~?= "ab"
]
, "From Text LazyText" ~:
let f = Witch.from @Text.Text @LazyText.Text in
[ f (Text.pack "") ~?= LazyText.pack ""
, f (Text.pack "a") ~?= LazyText.pack "a"
, f (Text.pack "ab") ~?= LazyText.pack "ab"
]
, "From Text ByteString" ~:
let f = Witch.from @Text.Text @ByteString.ByteString in
[ f (Text.pack "") ~?= ByteString.pack []
, f (Text.pack "a") ~?= ByteString.pack [0x61]
]
, "From String LazyText" ~:
let f = Witch.from @String @LazyText.Text in
[ f "" ~?= LazyText.pack ""
@ -1603,17 +1738,19 @@ main = runTestTTAndExit $ "Witch" ~:
, f (LazyText.pack "a") ~?= "a"
, f (LazyText.pack "ab") ~?= "ab"
]
, "From LazyText Text" ~:
let f = Witch.from @LazyText.Text @Text.Text in
[ f (LazyText.pack "") ~?= Text.pack ""
, f (LazyText.pack "a") ~?= Text.pack "a"
, f (LazyText.pack "ab") ~?= Text.pack "ab"
, "From String ByteString" ~:
let f = Witch.from @String @ByteString.ByteString in
[ f "" ~?= ByteString.pack []
, f "a" ~?= ByteString.pack [0x61]
]
, "From LazyText LazyByteString" ~:
let f = Witch.from @LazyText.Text @LazyByteString.ByteString in
[ f (LazyText.pack "") ~?= LazyByteString.pack []
, f (LazyText.pack "a") ~?= LazyByteString.pack [0x61]
, "From String LazyByteString" ~:
let f = Witch.from @String @LazyByteString.ByteString in
[ f "" ~?= LazyByteString.pack []
, f "a" ~?= LazyByteString.pack [0x61]
]
-- Day
, "From Integer Day" ~:
let f = Witch.from @Integer @Time.Day in
[ f 0 ~?= Time.ModifiedJulianDay 0
@ -1622,10 +1759,16 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.Day @Integer in
[ f (Time.ModifiedJulianDay 0) ~?= 0
]
-- DayOfWeek
, "From Day DayOfWeek" ~:
let f = Witch.from @Time.Day @Time.DayOfWeek in
[ f (Time.ModifiedJulianDay 0) ~?= Time.Wednesday
]
-- UniversalTime
, "From Rational UniversalTime" ~:
let f = Witch.from @Rational @Time.UniversalTime in
[ f 0 ~?= Time.ModJulianDate 0
@ -1634,6 +1777,9 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.UniversalTime @Rational in
[ f (Time.ModJulianDate 0) ~?= 0
]
-- DiffTime
, "From Pico DiffTime" ~:
let f = Witch.from @Fixed.Pico @Time.DiffTime in
[ f 0 ~?= 0
@ -1642,6 +1788,9 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.DiffTime @Fixed.Pico in
[ f 0 ~?= 0
]
-- NominalDiffTime
, "From Pico NominalDiffTime" ~:
let f = Witch.from @Fixed.Pico @Time.NominalDiffTime in
[ f 0 ~?= 0
@ -1650,6 +1799,9 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.NominalDiffTime @Fixed.Pico in
[ f 0 ~?= 0
]
-- POSIXTime
, "From SystemTime POSIXTime" ~:
let f = Witch.from @Time.SystemTime @Time.POSIXTime in
[ f (Time.MkSystemTime 0 0) ~?= 0
@ -1662,6 +1814,9 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.POSIXTime @Time.UTCTime in
[ f 0 ~?= unixEpoch
]
-- SystemTime
, "From UTCTime SystemTime" ~:
let f = Witch.from @Time.UTCTime @Time.SystemTime in
[ f unixEpoch ~?= Time.MkSystemTime 0 0
@ -1674,6 +1829,9 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.SystemTime @Time.UTCTime in
[ f (Time.MkSystemTime 0 0) ~?= unixEpoch
]
-- TimeOfDay
, "From DiffTime TimeOfDay" ~:
let f = Witch.from @Time.DiffTime @Time.TimeOfDay in
[ f 0 ~?= Time.TimeOfDay 0 0 0
@ -1690,6 +1848,9 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.TimeOfDay @Rational in
[ f (Time.TimeOfDay 0 0 0) ~?= 0
]
-- CalendarDiffTime
, "From CalendarDiffDays CalendarDiffTime" ~:
let f = Witch.from @Time.CalendarDiffDays @Time.CalendarDiffTime in
[ f (Time.CalendarDiffDays 0 0) ~?= Time.CalendarDiffTime 0 0
@ -1698,11 +1859,15 @@ main = runTestTTAndExit $ "Witch" ~:
let f = Witch.from @Time.NominalDiffTime @Time.CalendarDiffTime in
[ f 0 ~?= Time.CalendarDiffTime 0 0
]
-- ZonedTime
, "From ZonedTime UTCTime" ~:
let f = Witch.from @Time.ZonedTime @Time.UTCTime in
[ f (Time.ZonedTime (Time.LocalTime (Time.ModifiedJulianDay 0) (Time.TimeOfDay 0 0 0)) Time.utc) ~?= Time.UTCTime (Time.ModifiedJulianDay 0) 0
]
]
]
unixEpoch :: Time.UTCTime
unixEpoch = Time.UTCTime (Time.ModifiedJulianDay 40587) 0