]> git.rkrishnan.org Git - functorrent.git/blob - test/BencodeSpec.hs
beginning of the filesystem module
[functorrent.git] / test / BencodeSpec.hs
1 {-
2  - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
3  -
4  - This file is part of FuncTorrent.
5  -
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.
10  -
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.
15  -
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/>
18  -}
19
20 {-# LANGUAGE OverloadedStrings #-}
21
22 module BencodeSpec where
23
24 import Test.Hspec
25
26 import Data.Map.Strict (fromList)
27 import FuncTorrent.Bencode (encode, decode, BVal(..))
28
29 spec :: Spec
30 spec =
31   describe "Bencode" $ do
32     it "encode-strings" $ do
33       encode (Bstr "") `shouldBe` "0:"
34       encode (Bstr "spam") `shouldBe` "4:spam"
35     it "encode-ints" $ do
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"
41     it "encode-dict" $ do
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 "")
46     it "decode-ints" $ do
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")]))