diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-23 03:28:54 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-23 03:28:54 +0400 |
commit | 857988ceb7c9d73926c07bb1522ce86a1669f4c5 (patch) | |
tree | 8f769769ff6fca061249635c4ee6c7f1ae47cc1b | |
parent | 12a07d236274d154846100d27e8d3a33e430ad12 (diff) |
Add spec for Message module
-rw-r--r-- | krpc.cabal | 34 | ||||
-rw-r--r-- | src/Network/KRPC/Message.hs | 1 | ||||
-rw-r--r-- | tests/Network/KRPC/MessageSpec.hs | 71 | ||||
-rw-r--r-- | tests/Spec.hs | 1 |
4 files changed, 97 insertions, 10 deletions
@@ -59,25 +59,39 @@ library | |||
59 | 59 | ||
60 | ghc-options: -Wall | 60 | ghc-options: -Wall |
61 | 61 | ||
62 | 62 | test-suite spec | |
63 | test-suite test-client | ||
64 | type: exitcode-stdio-1.0 | 63 | type: exitcode-stdio-1.0 |
65 | default-language: Haskell2010 | 64 | default-language: Haskell2010 |
66 | hs-source-dirs: tests | 65 | hs-source-dirs: tests |
67 | main-is: Client.hs | 66 | main-is: Spec.hs |
68 | other-modules: Shared | ||
69 | build-depends: base == 4.* | 67 | build-depends: base == 4.* |
70 | , bytestring | 68 | , bytestring |
71 | , process | 69 | |
72 | , filepath | 70 | , hspec |
71 | , QuickCheck | ||
72 | , quickcheck-instances | ||
73 | 73 | ||
74 | , bencoding | 74 | , bencoding |
75 | , krpc | 75 | , krpc |
76 | , network | ||
77 | 76 | ||
78 | , HUnit | 77 | --test-suite test-client |
79 | , test-framework | 78 | -- type: exitcode-stdio-1.0 |
80 | , test-framework-hunit | 79 | -- default-language: Haskell2010 |
80 | -- hs-source-dirs: tests | ||
81 | -- main-is: Client.hs | ||
82 | -- other-modules: Shared | ||
83 | -- build-depends: base == 4.* | ||
84 | -- , bytestring | ||
85 | -- , process | ||
86 | -- , filepath | ||
87 | -- | ||
88 | -- , bencoding | ||
89 | -- , krpc | ||
90 | -- , network | ||
91 | -- | ||
92 | -- , HUnit | ||
93 | -- , test-framework | ||
94 | -- , test-framework-hunit | ||
81 | 95 | ||
82 | 96 | ||
83 | --executable test-server | 97 | --executable test-server |
diff --git a/src/Network/KRPC/Message.hs b/src/Network/KRPC/Message.hs index 0bd34400..1e1dc065 100644 --- a/src/Network/KRPC/Message.hs +++ b/src/Network/KRPC/Message.hs | |||
@@ -227,6 +227,7 @@ data KMessage | |||
227 | = Q KQuery | 227 | = Q KQuery |
228 | | R KResponse | 228 | | R KResponse |
229 | | E KError | 229 | | E KError |
230 | deriving (Show, Eq) | ||
230 | 231 | ||
231 | instance BEncode KMessage where | 232 | instance BEncode KMessage where |
232 | toBEncode (Q q) = toBEncode q | 233 | toBEncode (Q q) = toBEncode q |
diff --git a/tests/Network/KRPC/MessageSpec.hs b/tests/Network/KRPC/MessageSpec.hs new file mode 100644 index 00000000..7aca4489 --- /dev/null +++ b/tests/Network/KRPC/MessageSpec.hs | |||
@@ -0,0 +1,71 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | {-# OPTIONS_GHC -fno-warn-orphans #-} | ||
3 | module Network.KRPC.MessageSpec (spec) where | ||
4 | import Control.Applicative | ||
5 | import Data.ByteString.Lazy as BL | ||
6 | import Test.Hspec | ||
7 | import Test.QuickCheck | ||
8 | import Test.QuickCheck.Instances () | ||
9 | |||
10 | import Data.BEncode as BE | ||
11 | import Network.KRPC.Message | ||
12 | |||
13 | instance Arbitrary ErrorCode where | ||
14 | arbitrary = arbitraryBoundedEnum | ||
15 | |||
16 | instance Arbitrary KError where | ||
17 | arbitrary = KError <$> arbitrary <*> arbitrary <*> arbitrary | ||
18 | |||
19 | instance Arbitrary KQuery where | ||
20 | arbitrary = KQuery <$> pure (BInteger 0) <*> arbitrary <*> arbitrary | ||
21 | |||
22 | instance Arbitrary KResponse where | ||
23 | arbitrary = KResponse <$> pure (BList []) <*> arbitrary | ||
24 | |||
25 | instance Arbitrary KMessage where | ||
26 | arbitrary = frequency | ||
27 | [ (1, Q <$> arbitrary) | ||
28 | , (1, R <$> arbitrary) | ||
29 | , (1, E <$> arbitrary) | ||
30 | ] | ||
31 | |||
32 | spec :: Spec | ||
33 | spec = do | ||
34 | describe "error message" $ do | ||
35 | it "properly bencoded (iso)" $ property $ \ ke -> | ||
36 | BE.decode (BL.toStrict (BE.encode ke)) `shouldBe` Right (ke :: KError) | ||
37 | |||
38 | it "properly bencoded" $ do | ||
39 | BE.decode "d1:eli201e23:A Generic Error Ocurrede1:t2:aa1:y1:ee" | ||
40 | `shouldBe` Right (KError GenericError "A Generic Error Ocurred" "aa") | ||
41 | |||
42 | BE.decode "d1:eli202e22:A Server Error Ocurrede1:t2:bb1:y1:ee" | ||
43 | `shouldBe` Right (KError ServerError "A Server Error Ocurred" "bb") | ||
44 | |||
45 | BE.decode "d1:eli203e24:A Protocol Error Ocurrede1:t2:cc1:y1:ee" | ||
46 | `shouldBe` Right (KError ProtocolError "A Protocol Error Ocurred" "cc") | ||
47 | |||
48 | BE.decode "d1:eli204e30:Attempt to call unknown methode1:t2:dd1:y1:ee" | ||
49 | `shouldBe` Right | ||
50 | (KError MethodUnknown "Attempt to call unknown method" "dd") | ||
51 | |||
52 | describe "query message" $ do | ||
53 | it "properly bencoded (iso)" $ property $ \ kq -> | ||
54 | BE.decode (BL.toStrict (BE.encode kq)) `shouldBe` Right (kq :: KQuery) | ||
55 | |||
56 | it "properly bencoded" $ do | ||
57 | BE.decode "d1:ale1:q4:ping1:t2:aa1:y1:qe" `shouldBe` | ||
58 | Right (KQuery (BList []) "ping" "aa") | ||
59 | |||
60 | |||
61 | describe "response message" $ do | ||
62 | it "properly bencoded (iso)" $ property $ \ kr -> | ||
63 | BE.decode (BL.toStrict (BE.encode kr)) `shouldBe` Right (kr :: KResponse) | ||
64 | |||
65 | it "properly bencoded" $ do | ||
66 | BE.decode "d1:rle1:t2:aa1:y1:re" `shouldBe` | ||
67 | Right (KResponse (BList []) "aa") | ||
68 | |||
69 | describe "generic message" $ do | ||
70 | it "properly bencoded (iso)" $ property $ \ km -> | ||
71 | BE.decode (BL.toStrict (BE.encode km)) `shouldBe` Right (km :: KMessage) \ No newline at end of file | ||
diff --git a/tests/Spec.hs b/tests/Spec.hs new file mode 100644 index 00000000..52ef578f --- /dev/null +++ b/tests/Spec.hs | |||
@@ -0,0 +1 @@ | |||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-} \ No newline at end of file | |||