serial-test-generators (empty) → 0.1.0
raw patch · 5 files changed
+167/−0 lines, 5 filesdep +QuickCheckdep +aesondep +basesetup-changed
Dependencies added: QuickCheck, aeson, base, binary, bytestring, cereal, hspec, system-fileio, transformers
Files
- LICENSE +24/−0
- Setup.hs +2/−0
- serial-test-generators.cabal +69/−0
- src/Test/Serial.hs +71/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,24 @@+Copyright (c) 2014, Scott++The MIT License (MIT)++Copyright (c) <year> <copyright holders>++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ serial-test-generators.cabal view
@@ -0,0 +1,69 @@+Name: serial-test-generators+Version: 0.1.0+Author: Scott <scottmurphy09@gmail.com>+Maintainer: Scott <scottmurphy09@gmail.com>+License: MIT+License-File: LICENSE+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+ @+ 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. +++Cabal-Version: >= 1.10+Build-Type: Simple++Library+ Default-Language: Haskell2010+ HS-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules: Test.Serial+ Other-Modules: + Build-Depends: base >= 4 && < 5+ , aeson+ , binary+ , cereal+ , bytestring >= 0.9.0+ ++Test-Suite spec+ Type: exitcode-stdio-1.0+ Default-Language: Haskell2010+ Hs-Source-Dirs: src+ , test+ Ghc-Options: -Wall+ Main-Is: Spec.hs+ Build-Depends: base+ , hspec+ , QuickCheck + , aeson+ , binary+ , cereal+ , bytestring+ , transformers >= 0.3.0+ , system-fileio+++Source-Repository head+ Type: git+ Location: https://github.com/smurphy8/serial-test-generators.git
+ src/Test/Serial.hs view
@@ -0,0 +1,71 @@+++{- |+Module : Test.Serial+Description : Test.Serial run serialization tests against static files+Copyright : (c) Plow Technologies+License : MIT License+Maintainer : Scott Murphy+Stability : unstable +Portability : non-portable (System.Posix)+++-}+++{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE DeriveGeneric #-}++module Test.Serial (runAesonSerializationTest, TestError (..) ) where++import Data.Aeson (ToJSON+ , FromJSON+ , toJSON+ , encode+ , eitherDecode)+++import qualified Data.ByteString.Lazy as BLazy+import GHC.Generics+import System.IO (withFile, IOMode(..),openFile,hIsEOF)+--------------------------------------------------++data TestError = NoFileFound | -- NoFileFound could simply mean it is the first time the test was ran+ AesonError String+ deriving (Generic,Read,Show,Eq,Ord)++instance ToJSON TestError where++newtype MockInference a = MockInference a+ deriving (Generic)++instance ToJSON a => ToJSON (MockInference a) where +++makeMockInference :: (ToJSON a, FromJSON a) => a -> MockInference a+makeMockInference testVal = MockInference testVal++runAesonSerializationTest :: (ToJSON a, FromJSON a) => a -> FilePath -> IO (Either TestError a)+runAesonSerializationTest dataUnderTest file = withFile file ReadWriteMode createAesonSerializeTest+ where+ createAesonSerializeTest h = do+ aNewFile <- hIsEOF h+ if aNewFile+ then writeOutputAndExit h+ else createAesonSerializeTest' h+ + createAesonSerializeTest' h = do+ aesonByteString <- BLazy.hGetContents h+ case eitherDecode aesonByteString of+ (Left s) -> return . Left . AesonError $ s+ (Right a) + |(toJSON . makeMockInference $ a) == (toJSON.makeMockInference $ 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+ return . Left $ NoFileFound++
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}