2 - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
4 - This file is part of FuncTorrent.
6 - FuncTorrent is free software; you can redistribute it and/or modify
7 - it under the terms of the GNU General Public License as published by
8 - the Free Software Foundation; either version 3 of the License, or
9 - (at your option) any later version.
11 - FuncTorrent is distributed in the hope that it will be useful,
12 - but WITHOUT ANY WARRANTY; without even the implied warranty of
13 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 - GNU General Public License for more details.
16 - You should have received a copy of the GNU General Public License
17 - along with FuncTorrent; if not, see <http://www.gnu.org/licenses/>
20 {-# LANGUAGE OverloadedStrings #-}
22 module BencodeSpec where
26 import Data.Map.Strict (fromList)
27 import FuncTorrent.Bencode (encode, decode, BVal(..))
31 describe "Bencode" $ do
32 it "encode-strings" $ do
33 encode (Bstr "") `shouldBe` "0:"
34 encode (Bstr "spam") `shouldBe` "4:spam"
36 encode (Bint 0) `shouldBe` "i0e"
37 encode (Bint 42) `shouldBe` "i42e"
38 it "encode-lists" $ do
39 encode (Blist [(Bstr "spam"), (Bstr "eggs")]) `shouldBe` "l4:spam4:eggse"
40 encode (Blist []) `shouldBe` "le"
42 encode (Bdict (fromList [("spam", Bstr "eggs")])) `shouldBe` "d4:spam4:eggse"
43 it "decode-string" $ do
44 decode "4:spam" `shouldBe` Right (Bstr "spam")
45 decode "0:" `shouldBe` Right (Bstr "")
47 decode "i0e" `shouldBe` Right (Bint 0)
48 decode "i32e" `shouldBe` Right (Bint 32)
49 it "decode-lists" $ do
50 decode "l4:spam4:eggse" `shouldBe` Right (Blist [(Bstr "spam"), (Bstr "eggs")])
51 decode "le" `shouldBe` Right (Blist [])
52 it "decode-dicts" $ do
53 decode "d4:spam4:eggse" `shouldBe` Right (Bdict (fromList [("spam", Bstr "eggs")]))