module Unit.Utils where import Data.Aeson (FromJSON, ToJSON, eitherDecode, encode) import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy.Char8 as BSC import Data.Char (isSpace) import Test.HUnit (Assertion, (@=?), assertFailure) eqSerialized :: (ToJSON a) => FilePath -> a -> Assertion eqSerialized x y = do expected <- B.readFile x let actual = encode y filterBS expected @=? filterBS actual where filterBS :: BSC.ByteString -> BSC.ByteString filterBS = BSC.filter (not . isSpace) eqParsed :: (Show a, Eq a, FromJSON a) => a -> FilePath -> Assertion eqParsed x y = do j <- B.readFile y actual <- assertDecode j x @=? actual where assertDecode :: FromJSON a => BSC.ByteString -> IO a assertDecode json = do let d = eitherDecode json assertRight d let Right actual = d return actual where assertRight :: Either String b -> Assertion assertRight e = case e of Left s -> assertFailure s Right _ -> return ()