From 4da4b46f95b7b897ced5829d94bf257695accb00 Mon Sep 17 00:00:00 2001 From: Sam T Date: Thu, 4 Jul 2013 03:47:20 +0400 Subject: + Add tests for ScrapeInfo encoding. --- tests/Main.hs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/Main.hs b/tests/Main.hs index 44ea3393..0288cad9 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -36,6 +36,7 @@ import Test.Framework as Framework (Test, defaultMain) import Test.Framework.Providers.QuickCheck2 (testProperty) import Test.Framework.Providers.HUnit (testCase) +import Data.Aeson as JSON import Data.BEncode as BE import Data.Bitfield as BF import Data.Torrent @@ -51,6 +52,17 @@ import qualified System.IO.MMap.Fixed as Fixed data T a = T + +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 + +prop_properJSON :: (FromJSON a, ToJSON a) => Eq a => T a -> a -> Bool +prop_properJSON _ expected = actual == Just expected + where + actual = JSON.decode $ JSON.encode expected + instance Arbitrary URI where arbitrary = pure $ fromJust $ parseURI "http://exsample.com:80/123365_asd" @@ -93,12 +105,6 @@ 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 @@ -151,6 +157,10 @@ prop_cerealEncoding _ msgs = S.decode (S.encode msgs) == Right msgs Tracker/Scrape -----------------------------------------------------------------------} +instance Arbitrary ScrapeInfo where + arbitrary = ScrapeInfo <$> arbitrary <*> arbitrary + <*> arbitrary <*> arbitrary + -- | Note that in 6 esample we intensionally do not agree with -- specification, because taking in account '/' in query parameter -- seems to be meaningless. (And thats because other clients do not @@ -294,6 +304,12 @@ allTests = , testCase "single" mmapSingle , testCase "coalesce" coalesceTest ] ++ test_scrape_url + ++ + [ testProperty "scrape bencode" $ + prop_properBEncode (T :: T ScrapeInfo) + , testProperty "scrape json" $ + prop_properJSON (T :: T ScrapeInfo) + ] main :: IO () main = do -- cgit v1.2.3