summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-08 04:21:17 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-08 04:21:17 +0400
commitf609302860e5ce655a350c926636a69b9773cf92 (patch)
tree307e8fd3d606a2e72588624db912434f97e8426b
parent4e40597f4024be73f50a392642180d5e36c47143 (diff)
Use hspec instead of test-framework package
-rw-r--r--bencoding.cabal6
-rw-r--r--tests/properties.hs61
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 @@
3module Main (main) where 3module Main (main) where
4 4
5import Control.Applicative 5import Control.Applicative
6import qualified Data.ByteString as B 6import Data.ByteString as BS
7import qualified Data.ByteString.Lazy as L 7import Data.ByteString.Lazy as BL
8import Test.Framework (defaultMain) 8import Data.List as L
9import Test.Framework.Providers.QuickCheck2 (testProperty)
10import Test.QuickCheck
11import GHC.Generics 9import GHC.Generics
10import Test.QuickCheck
11import Test.Hspec
12 12
13import Data.BEncode 13import Data.BEncode
14import qualified Data.BEncode.BDict as BE
14 15
15 16
16instance Arbitrary B.ByteString where 17instance Arbitrary BS.ByteString where
17 arbitrary = fmap B.pack arbitrary 18 arbitrary = BS.pack <$> arbitrary
18
19instance Arbitrary BValue where
20 arbitrary = frequency
21 [ (50, BInteger <$> arbitrary)
22 , (40, BString <$> arbitrary)
23 , (5, BList <$> (arbitrary `suchThat` ((10 >) . length)))
24 ]
25 19
20instance 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
27prop_EncDec :: BValue -> Bool 26instance Arbitrary BValue where
28prop_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
32data List a = Cons a (List a) | Nil 34data 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
43data FileInfo = FileInfo 45data 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
49instance BEncode FileInfo 51instance BEncode FileInfo
@@ -54,16 +56,17 @@ instance Arbitrary FileInfo where
54data T a = T 56data T a = T
55 57
56prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool 58prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool
57prop_bencodable _ x = decode (L.toStrict (encode x)) == Right x 59prop_bencodable _ x = decode (BL.toStrict (encode x)) == Right x
58 60
59-- All tests are (encode >>> decode = id)
60main :: IO () 61main :: IO ()
61main = defaultMain 62main = 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