]> git.rkrishnan.org Git - functorrent.git/blobdiff - test/BencodeSpec.hs
hspec tests for single/multi torrent, bencode etc
[functorrent.git] / test / BencodeSpec.hs
diff --git a/test/BencodeSpec.hs b/test/BencodeSpec.hs
new file mode 100644 (file)
index 0000000..d6d2cb5
--- /dev/null
@@ -0,0 +1,53 @@
+{-
+ - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
+ -
+ - This file is part of FuncTorrent.
+ -
+ - FuncTorrent is free software; you can redistribute it and/or modify
+ - it under the terms of the GNU General Public License as published by
+ - the Free Software Foundation; either version 3 of the License, or
+ - (at your option) any later version.
+ -
+ - FuncTorrent is distributed in the hope that it will be useful,
+ - but WITHOUT ANY WARRANTY; without even the implied warranty of
+ - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ - GNU General Public License for more details.
+ -
+ - You should have received a copy of the GNU General Public License
+ - along with FuncTorrent; if not,  see <http://www.gnu.org/licenses/>
+ -}
+
+{-# LANGUAGE OverloadedStrings #-}
+
+module BencodeSpec where
+
+import Test.Hspec
+
+import Data.Map.Strict (fromList)
+import FuncTorrent.Bencode (encode, decode, BVal(..))
+
+spec :: Spec
+spec =
+  describe "Bencode" $ do
+    it "encode-strings" $ do
+      encode (Bstr "") `shouldBe` "0:"
+      encode (Bstr "spam") `shouldBe` "4:spam"
+    it "encode-ints" $ do
+      encode (Bint 0) `shouldBe` "i0e"
+      encode (Bint 42) `shouldBe` "i42e"
+    it "encode-lists" $ do
+      encode (Blist [(Bstr "spam"), (Bstr "eggs")]) `shouldBe` "l4:spam4:eggse"
+      encode (Blist []) `shouldBe` "le"
+    it "encode-dict" $ do
+      encode (Bdict (fromList [("spam", Bstr "eggs")])) `shouldBe` "d4:spam4:eggse"
+    it "decode-string" $ do
+      decode "4:spam" `shouldBe` Right (Bstr "spam")
+      decode "0:" `shouldBe` Right (Bstr "")
+    it "decode-ints" $ do
+      decode "i0e" `shouldBe` Right (Bint 0)
+      decode "i32e" `shouldBe` Right (Bint 32)
+    it "decode-lists" $ do
+      decode "l4:spam4:eggse" `shouldBe` Right (Blist [(Bstr "spam"), (Bstr "eggs")])
+      decode "le" `shouldBe` Right (Blist [])
+    it "decode-dicts" $ do
+      decode "d4:spam4:eggse" `shouldBe` Right (Bdict (fromList [("spam", Bstr "eggs")]))