Skip to content

Commit

Permalink
Merge pull request #33 from fpco/test-incorrect-size
Browse files Browse the repository at this point in the history
Test behaviour for incorrect size implementations.
  • Loading branch information
mgsloan committed May 29, 2016
2 parents a41042c + 92cadd1 commit 07e62a7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/Data/StoreSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
module Data.StoreSpec where

import Control.Applicative
import Control.Exception (evaluate)
import Control.Monad (unless)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
Expand Down Expand Up @@ -215,6 +216,20 @@ data X = X
instance Monad m => Serial m X
instance Store X


-- Datatypes with faulty instances
newtype BadIdea = BadIdea Int64
instance Store BadIdea where
poke (BadIdea x) = poke x
peek = BadIdea <$> peek
size = ConstSize 1 -- too small

newtype BadIdea2 = BadIdea2 Int64
instance Store BadIdea2 where
poke (BadIdea2 x) = poke x
peek = BadIdea2 <$> peek
size = ConstSize 12 -- too large

spec :: Spec
spec = do
describe "Store on all monomorphic instances"
Expand Down Expand Up @@ -305,3 +320,9 @@ spec = do
mapM_ putStrLn
$(do insts <- getAllInstanceTypes1 ''Store
lift $ map pprint $ filter (not . isMonoType) insts)
it "Faulty implementations of size lead to PokeExceptions" $ do
evaluate (encode (BadIdea 0)) `shouldThrow` isPokeException
evaluate (encode (BadIdea2 0)) `shouldThrow` isPokeException

isPokeException :: Test.Hspec.Selector PokeException
isPokeException = const True

0 comments on commit 07e62a7

Please sign in to comment.