diff options
-rw-r--r-- | bencoding.cabal | 6 | ||||
-rw-r--r-- | tests/properties.hs | 61 |
2 files changed, 35 insertions, 32 deletions
diff --git a/bencoding.cabal b/bencoding.cabal index 9b913a8..eaa4597 100644 --- a/bencoding.cabal +++ b/bencoding.cabal | |||
@@ -63,10 +63,10 @@ test-suite properties | |||
63 | , bytestring >= 0.10.0.2 | 63 | , bytestring >= 0.10.0.2 |
64 | , attoparsec >= 0.10 | 64 | , attoparsec >= 0.10 |
65 | 65 | ||
66 | , test-framework | ||
67 | , test-framework-quickcheck2 | ||
68 | , QuickCheck | ||
69 | , bencoding | 66 | , bencoding |
67 | , hspec | ||
68 | , QuickCheck | ||
69 | |||
70 | ghc-options: -Wall -fno-warn-orphans | 70 | ghc-options: -Wall -fno-warn-orphans |
71 | 71 | ||
72 | 72 | ||
diff --git a/tests/properties.hs b/tests/properties.hs index 3d08eed..2e5345f 100644 --- a/tests/properties.hs +++ b/tests/properties.hs | |||
@@ -3,31 +3,33 @@ | |||
3 | module Main (main) where | 3 | module Main (main) where |
4 | 4 | ||
5 | import Control.Applicative | 5 | import Control.Applicative |
6 | import qualified Data.ByteString as B | 6 | import Data.ByteString as BS |
7 | import qualified Data.ByteString.Lazy as L | 7 | import Data.ByteString.Lazy as BL |
8 | import Test.Framework (defaultMain) | 8 | import Data.List as L |
9 | import Test.Framework.Providers.QuickCheck2 (testProperty) | ||
10 | import Test.QuickCheck | ||
11 | import GHC.Generics | 9 | import GHC.Generics |
10 | import Test.QuickCheck | ||
11 | import Test.Hspec | ||
12 | 12 | ||
13 | import Data.BEncode | 13 | import Data.BEncode |
14 | import qualified Data.BEncode.BDict as BE | ||
14 | 15 | ||
15 | 16 | ||
16 | instance Arbitrary B.ByteString where | 17 | instance Arbitrary BS.ByteString where |
17 | arbitrary = fmap B.pack arbitrary | 18 | arbitrary = BS.pack <$> arbitrary |
18 | |||
19 | instance Arbitrary BValue where | ||
20 | arbitrary = frequency | ||
21 | [ (50, BInteger <$> arbitrary) | ||
22 | , (40, BString <$> arbitrary) | ||
23 | , (5, BList <$> (arbitrary `suchThat` ((10 >) . length))) | ||
24 | ] | ||
25 | 19 | ||
20 | instance Arbitrary a => Arbitrary (BE.BDictMap a) where | ||
21 | arbitrary = frequency | ||
22 | [ (90, pure BE.Nil) | ||
23 | , (10, BE.Cons <$> arbitrary <*> arbitrary <*> arbitrary) | ||
24 | ] | ||
26 | 25 | ||
27 | prop_EncDec :: BValue -> Bool | 26 | instance Arbitrary BValue where |
28 | prop_EncDec x = case decode (L.toStrict (encode x)) of | 27 | arbitrary = frequency |
29 | Left _ -> False | 28 | [ (30, BInteger <$> arbitrary) |
30 | Right x' -> x == x' | 29 | , (30, BString <$> arbitrary) |
30 | , (20, BList <$> (arbitrary `suchThat` ((10 >) . L.length))) | ||
31 | , (20, BDict <$> arbitrary) | ||
32 | ] | ||
31 | 33 | ||
32 | data List a = Cons a (List a) | Nil | 34 | data List a = Cons a (List a) | Nil |
33 | deriving (Show, Eq, Generic) | 35 | deriving (Show, Eq, Generic) |
@@ -42,8 +44,8 @@ instance Arbitrary a => Arbitrary (List a) where | |||
42 | 44 | ||
43 | data FileInfo = FileInfo | 45 | data FileInfo = FileInfo |
44 | { fiLength :: !Integer | 46 | { fiLength :: !Integer |
45 | , fiPath :: [B.ByteString] | 47 | , fiPath :: [BS.ByteString] |
46 | , fiMD5Sum :: B.ByteString | 48 | , fiMD5Sum :: BS.ByteString |
47 | } deriving (Show, Eq, Generic) | 49 | } deriving (Show, Eq, Generic) |
48 | 50 | ||
49 | instance BEncode FileInfo | 51 | instance BEncode FileInfo |
@@ -54,16 +56,17 @@ instance Arbitrary FileInfo where | |||
54 | data T a = T | 56 | data T a = T |
55 | 57 | ||
56 | prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool | 58 | prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool |
57 | prop_bencodable _ x = decode (L.toStrict (encode x)) == Right x | 59 | prop_bencodable _ x = decode (BL.toStrict (encode x)) == Right x |
58 | 60 | ||
59 | -- All tests are (encode >>> decode = id) | ||
60 | main :: IO () | 61 | main :: IO () |
61 | main = defaultMain | 62 | main = hspec $ do |
62 | [ testProperty "BEncode" prop_EncDec | 63 | describe "BValue" $ do |
64 | it "properly encoded" $ property $ | ||
65 | prop_bencodable (T :: T BValue) | ||
63 | 66 | ||
64 | , testProperty "generic recordless" $ | 67 | describe "BEncode" $ do |
65 | prop_bencodable (T :: T (List Int)) | 68 | it "generic recordless" $ property $ |
69 | prop_bencodable (T :: T (List Int)) | ||
66 | 70 | ||
67 | , testProperty "generic records" $ | 71 | it "generic records" $ property $ |
68 | prop_bencodable (T :: T FileInfo) | 72 | prop_bencodable (T :: T FileInfo) |
69 | ] \ No newline at end of file | ||