serial-test-generators 0.1.0 → 0.1.1
raw patch · 2 files changed
+65/−45 lines, 2 filesdep ~aesondep ~binarydep ~cerealPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: aeson, binary, cereal
API changes (from Hackage documentation)
- Test.Serial: instance Constructor C1_0MockInference
- Test.Serial: instance Datatype D1MockInference
- Test.Serial: instance Generic (MockInference a)
- Test.Serial: instance ToJSON a => ToJSON (MockInference a)
+ Test.Serial: BinaryError :: String -> TestError
+ Test.Serial: instance Binary a => Binary (MockBinaryInference a)
+ Test.Serial: instance Constructor C1_0MockAesonInference
+ Test.Serial: instance Constructor C1_0MockBinaryInference
+ Test.Serial: instance Constructor C1_2TestError
+ Test.Serial: instance Datatype D1MockAesonInference
+ Test.Serial: instance Datatype D1MockBinaryInference
+ Test.Serial: instance Generic (MockAesonInference a)
+ Test.Serial: instance Generic (MockBinaryInference a)
+ Test.Serial: instance ToJSON a => ToJSON (MockAesonInference a)
+ Test.Serial: runBinarySerializationTest :: Binary a => a -> FilePath -> IO (Either TestError a)
Files
- serial-test-generators.cabal +7/−25
- src/Test/Serial.hs +58/−20
serial-test-generators.cabal view
@@ -1,5 +1,5 @@ Name: serial-test-generators-Version: 0.1.0+Version: 0.1.1 Author: Scott <scottmurphy09@gmail.com> Maintainer: Scott <scottmurphy09@gmail.com> License: MIT@@ -7,28 +7,10 @@ Category: Test Synopsis: Test your 'Aeson' 'Serialize' and 'Binary' instances for stability over time Description: - When I am programming haskell I write a lot of- @- instance ToJSON ... where- instance FromJSON ... where- instance Binary ... where- @+ When I am programming haskell I write a lot of getter setter serialization tests+ for aeson, binary, cereal These libraries are often associated with state.-- So, I end up writing a lot of tests of the form ...-- >>> expect (encode someTestAeson) `toBe` "{\"someSerializedThing\":\"expected encoding\"}-- so I have to write all these pieces down... but what I would really like is---- >>> runAesonSerializeTest someTestAeson outputfile.txt--- That is what these libraries do for Serialize, Binary and Aeson- They make very little assumption about what version of the library you are using. -+ This library standardizes those tests Cabal-Version: >= 1.10 Build-Type: Simple@@ -40,9 +22,9 @@ Exposed-Modules: Test.Serial Other-Modules: Build-Depends: base >= 4 && < 5- , aeson- , binary- , cereal+ , aeson >= 0.6.2.0+ , binary >= 0.6.3.0+ , cereal >= 0.3.5.0 , bytestring >= 0.9.0
src/Test/Serial.hs view
@@ -1,5 +1,3 @@-- {- | Module : Test.Serial Description : Test.Serial run serialization tests against static files@@ -16,36 +14,39 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE DeriveGeneric #-} -module Test.Serial (runAesonSerializationTest, TestError (..) ) where--import Data.Aeson (ToJSON- , FromJSON- , toJSON- , encode- , eitherDecode)+module Test.Serial (runAesonSerializationTest+ , runBinarySerializationTest+ , TestError (..) ) where +import qualified Data.Aeson as A +import qualified Data.Binary as B import qualified Data.ByteString.Lazy as BLazy import GHC.Generics-import System.IO (withFile, IOMode(..),openFile,hIsEOF)+import System.IO (withFile, IOMode(..),hIsEOF) -------------------------------------------------- data TestError = NoFileFound | -- NoFileFound could simply mean it is the first time the test was ran- AesonError String+ AesonError String |+ BinaryError String deriving (Generic,Read,Show,Eq,Ord) -instance ToJSON TestError where+instance A.ToJSON TestError where -newtype MockInference a = MockInference a+++-- | AESON SERIALIZER +-- | 'MockAesonInference' just to force two inferred types to be the same +newtype MockAesonInference a = MockAesonInference a deriving (Generic) -instance ToJSON a => ToJSON (MockInference a) where +instance A.ToJSON a => A.ToJSON (MockAesonInference a) where -makeMockInference :: (ToJSON a, FromJSON a) => a -> MockInference a-makeMockInference testVal = MockInference testVal+makeMockAesonInference :: (A.ToJSON a, A.FromJSON a ) => a -> MockAesonInference a+makeMockAesonInference testVal = MockAesonInference testVal -runAesonSerializationTest :: (ToJSON a, FromJSON a) => a -> FilePath -> IO (Either TestError a)+runAesonSerializationTest :: (A.ToJSON a, A.FromJSON a) => a -> FilePath -> IO (Either TestError a) runAesonSerializationTest dataUnderTest file = withFile file ReadWriteMode createAesonSerializeTest where createAesonSerializeTest h = do@@ -56,16 +57,53 @@ createAesonSerializeTest' h = do aesonByteString <- BLazy.hGetContents h- case eitherDecode aesonByteString of+ case A.eitherDecode aesonByteString of (Left s) -> return . Left . AesonError $ s (Right a) - |(toJSON . makeMockInference $ a) == (toJSON.makeMockInference $ dataUnderTest)+ |(A.toJSON . makeMockAesonInference $ a) == (A.toJSON.makeMockAesonInference $ dataUnderTest) -> return . Right $ a |otherwise -> return . Left . AesonError $ "Serializations do not match" writeOutputAndExit h = do putStrLn "file not found, writing given serialization to disk, rerun tests"- BLazy.hPut h $ encode dataUnderTest+ BLazy.hPut h $ A.encode dataUnderTest return . Left $ NoFileFound +++++-- | 'MockBinaryInference' just to force two inferred types to be the same +newtype MockBinaryInference a = MockBinaryInference a+ deriving (Generic)++instance B.Binary a => B.Binary (MockBinaryInference a) where +++makeMockBinaryInference :: (B.Binary a) => a -> MockBinaryInference a+makeMockBinaryInference testVal = MockBinaryInference testVal++runBinarySerializationTest :: (B.Binary a) => a -> FilePath -> IO (Either TestError a)+runBinarySerializationTest dataUnderTest file = withFile file ReadWriteMode createBinarySerializeTest+ where+ createBinarySerializeTest h = do+ aNewFile <- hIsEOF h+ if aNewFile+ then writeOutputAndExit h+ else createBinarySerializeTest' h+ + createBinarySerializeTest' h = do+ binaryByteString <- BLazy.hGetContents h+ case B.decodeOrFail binaryByteString of+ (Left s) -> return . Left . BinaryError $ " whee"+ (Right (_,_,a) )+ |(B.encode . makeMockBinaryInference $ a) == (B.encode.makeMockBinaryInference $ dataUnderTest)+ -> return . Right $ a+ |otherwise -> return . Left . BinaryError $ "Serializations do not match" + writeOutputAndExit h = do+ putStrLn "file not found, writing given serialization to disk, rerun tests"+ BLazy.hPut h $ B.encode dataUnderTest+ return . Left $ NoFileFound++