From 609f6f75db38ca12db453346e7649ae52145fe89 Mon Sep 17 00:00:00 2001 From: Sam T Date: Mon, 26 Aug 2013 03:46:45 +0400 Subject: + Added test for generics. --- tests/properties.hs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/properties.hs b/tests/properties.hs index 41950e7..23371ba 100644 --- a/tests/properties.hs +++ b/tests/properties.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DeriveGeneric #-} module Main (main) where import Control.Applicative @@ -6,9 +7,11 @@ import qualified Data.ByteString.Lazy as L import Test.Framework (defaultMain) import Test.Framework.Providers.QuickCheck2 (testProperty) import Test.QuickCheck +import GHC.Generics import Data.BEncode + instance Arbitrary B.ByteString where arbitrary = fmap B.pack arbitrary @@ -19,12 +22,31 @@ instance Arbitrary BEncode where , (5, BList <$> (arbitrary `suchThat` ((10 >) . length))) ] + prop_EncDec :: BEncode -> Bool prop_EncDec x = case decode (L.toStrict (encode x)) of Left _ -> False Right x' -> x == x' +data List a = Cons a (List a) | Nil + deriving (Show, Eq, Generic) + +instance BEncodable a => BEncodable (List a) + +instance Arbitrary a => Arbitrary (List a) where + arbitrary = frequency + [ (90, pure Nil) + , (10, Cons <$> arbitrary <*> arbitrary) + ] + +data T a = T + +prop_bencodable :: Eq a => BEncodable a => T a -> a -> Bool +prop_bencodable _ x = decoded (L.toStrict (encoded x)) == Right x + main :: IO () main = defaultMain - [ testProperty "encode <-> decode" prop_EncDec + [ testProperty "encode >>> decode = id" prop_EncDec + , testProperty "generic encode >>> decode = id" $ + prop_bencodable (T :: T (List Int)) ] \ No newline at end of file -- cgit v1.2.3