-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
97 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Test.Toml.Codec.Combinator.Common | ||
( codecRoundtrip | ||
|
||
-- * Double helpers | ||
, Batman (..) | ||
, batmanDoubleCodec | ||
, batmanFloatCodec | ||
) where | ||
|
||
import Hedgehog (Gen, forAll, tripping) | ||
import Test.Hspec (Arg, Expectation, SpecWith, it) | ||
import Test.Hspec.Hedgehog (hedgehog) | ||
|
||
import Toml.Codec.Code (decode, encode) | ||
import Toml.Codec.Types (TomlCodec) | ||
import Toml.Type.Key (Key) | ||
|
||
|
||
import qualified Toml.Codec as Toml | ||
|
||
|
||
codecRoundtrip | ||
:: forall a | ||
. (Eq a, Show a) | ||
=> String | ||
-> (Key -> TomlCodec a) | ||
-> Gen a | ||
-> SpecWith (Arg Expectation) | ||
codecRoundtrip typeName mkCodec genA = it label $ hedgehog $ do | ||
a <- forAll genA | ||
let codec = mkCodec "a" | ||
tripping a (encode codec) (decode codec) | ||
where | ||
label :: String | ||
label = typeName ++ ": decode . encode ≡ id" | ||
|
||
-- | Wrapper over 'Double' and 'Float' to be equal on @NaN@ values. | ||
newtype Batman a = Batman | ||
{ unBatman :: a | ||
} deriving stock (Show) | ||
|
||
instance Toml.HasCodec a => Toml.HasCodec (Batman a) where | ||
hasCodec = Toml.diwrap . Toml.hasCodec @a | ||
|
||
instance RealFloat a => Eq (Batman a) where | ||
Batman a == Batman b = | ||
if isNaN a | ||
then isNaN b | ||
else a == b | ||
|
||
batmanDoubleCodec :: Key -> TomlCodec (Batman Double) | ||
batmanDoubleCodec = Toml.diwrap . Toml.double | ||
|
||
batmanFloatCodec :: Key -> TomlCodec (Batman Float) | ||
batmanFloatCodec = Toml.diwrap . Toml.float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Test.Toml.Codec.Combinator.Primitive | ||
( primitiveSpec | ||
) where | ||
|
||
import Test.Hspec (Spec, describe) | ||
|
||
import Test.Toml.Codec.Combinator.Common (Batman (..), batmanDoubleCodec, batmanFloatCodec, | ||
codecRoundtrip) | ||
|
||
import qualified Test.Toml.Gen as Gen | ||
import qualified Toml.Codec.Combinator.Primitive as Toml | ||
|
||
|
||
primitiveSpec :: Spec | ||
primitiveSpec = describe "Combinator.Primitive: Roundtrip tests" $ do | ||
codecRoundtrip "Bool " Toml.bool Gen.genBool | ||
codecRoundtrip "Integer " Toml.integer Gen.genInteger | ||
codecRoundtrip "Int " Toml.int Gen.genInt | ||
codecRoundtrip "Natural " Toml.natural Gen.genNatural | ||
codecRoundtrip "Word " Toml.word Gen.genWord | ||
codecRoundtrip "Word8 " Toml.word8 Gen.genWord8 | ||
codecRoundtrip "Double " batmanDoubleCodec (Batman <$> Gen.genDouble) | ||
codecRoundtrip "Float " batmanFloatCodec (Batman <$> Gen.genFloat) | ||
codecRoundtrip "String " Toml.string Gen.genString | ||
codecRoundtrip "Text " Toml.text Gen.genText | ||
codecRoundtrip "LText " Toml.lazyText Gen.genLText | ||
codecRoundtrip "ByteString " Toml.byteString Gen.genByteString | ||
codecRoundtrip "LByteString " Toml.lazyByteString Gen.genLByteString | ||
codecRoundtrip "ByteString Array " Toml.byteStringArray Gen.genByteString | ||
codecRoundtrip "LByteString Array" Toml.lazyByteStringArray Gen.genLByteString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters