serial-test-generators 0.1.1 → 0.1.2
raw patch · 2 files changed
+45/−4 lines, 2 files
Files
- serial-test-generators.cabal +1/−1
- src/Test/Serial.hs +44/−3
serial-test-generators.cabal view
@@ -1,5 +1,5 @@ Name: serial-test-generators-Version: 0.1.1+Version: 0.1.2 Author: Scott <scottmurphy09@gmail.com> Maintainer: Scott <scottmurphy09@gmail.com> License: MIT
src/Test/Serial.hs view
@@ -16,11 +16,13 @@ module Test.Serial (runAesonSerializationTest , runBinarySerializationTest+ , runCerealSerializationTest , TestError (..) ) where import qualified Data.Aeson as A- import qualified Data.Binary as B+import qualified Data.Serialize as C+import qualified Data.ByteString as BStrict import qualified Data.ByteString.Lazy as BLazy import GHC.Generics import System.IO (withFile, IOMode(..),hIsEOF)@@ -28,7 +30,8 @@ data TestError = NoFileFound | -- NoFileFound could simply mean it is the first time the test was ran AesonError String |- BinaryError String+ BinaryError String |+ CerealError String deriving (Generic,Read,Show,Eq,Ord) instance A.ToJSON TestError where@@ -95,7 +98,7 @@ createBinarySerializeTest' h = do binaryByteString <- BLazy.hGetContents h case B.decodeOrFail binaryByteString of- (Left s) -> return . Left . BinaryError $ " whee"+ (Left s) -> return . Left . BinaryError . show $ s (Right (_,_,a) ) |(B.encode . makeMockBinaryInference $ a) == (B.encode.makeMockBinaryInference $ dataUnderTest) -> return . Right $ a@@ -104,6 +107,44 @@ writeOutputAndExit h = do putStrLn "file not found, writing given serialization to disk, rerun tests" BLazy.hPut h $ B.encode dataUnderTest+ return . Left $ NoFileFound+++++++-- | 'MockCerealInference' just to force two inferred types to be the same +newtype MockCerealInference a = MockCerealInference a+ deriving (Generic)++instance C.Serialize a => C.Serialize (MockCerealInference a) where +++makeMockCerealInference :: (C.Serialize a) => a -> MockCerealInference a+makeMockCerealInference testVal = MockCerealInference testVal++runCerealSerializationTest :: (C.Serialize a) => a -> FilePath -> IO (Either TestError a)+runCerealSerializationTest dataUnderTest file = withFile file ReadWriteMode createCerealSerializeTest+ where+ createCerealSerializeTest h = do+ aNewFile <- hIsEOF h+ if aNewFile+ then writeOutputAndExit h+ else createCerealSerializeTest' h+ + createCerealSerializeTest' h = do+ cerealByteString <- BStrict.hGetContents h+ case C.decode cerealByteString of+ (Left s) -> return . Left . CerealError $ s+ (Right a)+ |(C.encode . makeMockCerealInference $ a) == (C.encode.makeMockCerealInference $ dataUnderTest)+ -> return . Right $ a+ |otherwise -> return . Left . CerealError $ "Serializations do not match"+ + writeOutputAndExit h = do+ putStrLn "file not found, writing given serialization to disk, rerun tests"+ BStrict.hPut h $ C.encode dataUnderTest return . Left $ NoFileFound