From 095f22bba763aba8303322b104ae39e2ff2807c2 Mon Sep 17 00:00:00 2001 From: Sam T Date: Fri, 7 Jun 2013 03:37:11 +0400 Subject: ~ Fix bug in torrent bencode instance. Also add encoding tests for torrent module. --- tests/Main.hs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'tests/Main.hs') diff --git a/tests/Main.hs b/tests/Main.hs index 0aa6423f..ff571b6b 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -2,20 +2,35 @@ module Main (main) where import Control.Applicative +import qualified Data.ByteString.Lazy as Lazy import Data.IntervalSet import Data.List as L import Data.Ord +import Data.Maybe import Data.Word +import Data.Text as T +import Network.URI import Test.Framework (defaultMain) import Test.Framework.Providers.QuickCheck2 (testProperty) import Test.QuickCheck +import Data.BEncode import Data.Bitfield as BF +import Data.Torrent import Network.BitTorrent as BT +import Debug.Trace import Encoding + +instance Arbitrary URI where + arbitrary = pure $ fromJust + $ parseURI "http://exsample.com:80/123365_asd" + +instance Arbitrary Text where + arbitrary = T.pack <$> arbitrary + {----------------------------------------------------------------------- Bitfield -----------------------------------------------------------------------} @@ -51,8 +66,29 @@ prop_differenceDeMorgan a b c = Torrent -----------------------------------------------------------------------} +prop_properBEncode :: Show a => BEncodable a => Eq a => T a -> a -> Bool +prop_properBEncode _ expected = actual == Right expected + where + actual = decoded $ Lazy.toStrict $ encoded expected + + -- TODO tests for torrent: encoding <-> decoding +instance Arbitrary FileInfo where + arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary +instance Arbitrary ContentInfo where + arbitrary = oneof + [ SingleFile <$> arbitrary <*> arbitrary <*> arbitrary + <*> arbitrary <*> arbitrary <*> arbitrary + , MultiFile <$> arbitrary <*> arbitrary <*> arbitrary + <*> arbitrary <*> arbitrary + ] + +instance Arbitrary Torrent where + arbitrary = torrent <$> arbitrary + <*> arbitrary <*> arbitrary <*> arbitrary + <*> arbitrary <*> arbitrary <*> arbitrary + <*> arbitrary <*> pure Nothing <*> arbitrary main :: IO () main = defaultMain @@ -60,4 +96,11 @@ main = defaultMain , testProperty "rarest in range" prop_rarestInRange , testProperty "min less that max" prop_minMax , testProperty "difference de morgan" prop_differenceDeMorgan + + , testProperty "file info encoding" $ + prop_properBEncode (T :: T FileInfo) + , testProperty "content info encoding" $ + prop_properBEncode (T :: T ContentInfo) + , testProperty "torrent encoding" $ + prop_properBEncode (T :: T Torrent) ] -- cgit v1.2.3