diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-24 23:50:23 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-24 23:50:23 +0400 |
commit | 46b6ba10202b73ba413d18bd21a284e3897c12b0 (patch) | |
tree | 6fee6300db0f0f0df55780946bc2a541caa9d421 /tests/Network | |
parent | 10829a428735d034f927e45561dcf94703cd376a (diff) |
Update tests
Diffstat (limited to 'tests/Network')
-rw-r--r-- | tests/Network/KRPC/MethodSpec.hs | 52 | ||||
-rw-r--r-- | tests/Network/KRPCSpec.hs | 33 |
2 files changed, 85 insertions, 0 deletions
diff --git a/tests/Network/KRPC/MethodSpec.hs b/tests/Network/KRPC/MethodSpec.hs new file mode 100644 index 00000000..c1c58282 --- /dev/null +++ b/tests/Network/KRPC/MethodSpec.hs | |||
@@ -0,0 +1,52 @@ | |||
1 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
2 | {-# LANGUAGE FlexibleInstances #-} | ||
3 | {-# LANGUAGE OverloadedStrings #-} | ||
4 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
5 | {-# LANGUAGE DeriveDataTypeable #-} | ||
6 | {-# OPTIONS_GHC -fno-warn-orphans #-} | ||
7 | module Network.KRPC.MethodSpec where | ||
8 | import Control.Applicative | ||
9 | import Data.BEncode | ||
10 | import Data.ByteString as BS | ||
11 | import Data.Typeable | ||
12 | import Network.KRPC | ||
13 | import Test.Hspec | ||
14 | |||
15 | |||
16 | data Ping = Ping | ||
17 | deriving (Show, Eq, Typeable) | ||
18 | |||
19 | instance BEncode Ping where | ||
20 | toBEncode Ping = toBEncode () | ||
21 | fromBEncode b = Ping <$ (fromBEncode b :: Result ()) | ||
22 | |||
23 | instance KRPC Ping Ping | ||
24 | |||
25 | ping :: Monad h => Handler h | ||
26 | ping = handler $ \ _ Ping -> return Ping | ||
27 | |||
28 | newtype Echo a = Echo a | ||
29 | deriving (Show, Eq, BEncode, Typeable) | ||
30 | |||
31 | echo :: Monad h => Handler h | ||
32 | echo = handler $ \ _ (Echo a) -> return (Echo (a :: ByteString)) | ||
33 | |||
34 | instance (Typeable a, BEncode a) => KRPC (Echo a) (Echo a) | ||
35 | |||
36 | spec :: Spec | ||
37 | spec = do | ||
38 | describe "ping method" $ do | ||
39 | it "name is ping" $ do | ||
40 | (method :: Method Ping Ping) `shouldBe` "ping" | ||
41 | |||
42 | it "has pretty Show instance" $ do | ||
43 | show (method :: Method Ping Ping) `shouldBe` "ping :: Ping -> Ping" | ||
44 | |||
45 | describe "echo method" $ do | ||
46 | it "is overloadable" $ do | ||
47 | (method :: Method (Echo Int ) (Echo Int )) `shouldBe` "echo int" | ||
48 | (method :: Method (Echo Bool) (Echo Bool)) `shouldBe` "echo bool" | ||
49 | |||
50 | it "has pretty Show instance" $ do | ||
51 | show (method :: Method (Echo Int) (Echo Int)) | ||
52 | `shouldBe` "echo int :: Echo Int -> Echo Int" \ No newline at end of file | ||
diff --git a/tests/Network/KRPCSpec.hs b/tests/Network/KRPCSpec.hs new file mode 100644 index 00000000..27148682 --- /dev/null +++ b/tests/Network/KRPCSpec.hs | |||
@@ -0,0 +1,33 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | module Network.KRPCSpec (spec) where | ||
3 | import Control.Monad.Reader | ||
4 | import Network.Socket (SockAddr (..)) | ||
5 | import Network.KRPC | ||
6 | import Network.KRPC.MethodSpec hiding (spec) | ||
7 | import Test.Hspec | ||
8 | |||
9 | servAddr :: SockAddr | ||
10 | servAddr = SockAddrInet 6000 (256 * 256 * 256 + 127) | ||
11 | |||
12 | handlers :: [Handler IO] | ||
13 | handlers = | ||
14 | [ handler $ \ _ Ping -> return Ping | ||
15 | , handler $ \ _ (Echo a) -> return (Echo (a :: Bool)) | ||
16 | , handler $ \ _ (Echo a) -> return (Echo (a :: Int)) | ||
17 | ] | ||
18 | |||
19 | spec :: Spec | ||
20 | spec = do | ||
21 | describe "query" $ do | ||
22 | it "run handlers" $ do | ||
23 | let int = 0xabcd :: Int | ||
24 | (withManager servAddr handlers $ runReaderT $ do | ||
25 | listen | ||
26 | query servAddr (Echo int)) | ||
27 | `shouldReturn` Echo int | ||
28 | |||
29 | it "throw timeout exception" $ do | ||
30 | (withManager servAddr handlers $ runReaderT $ do | ||
31 | query servAddr (Echo (0xabcd :: Int)) | ||
32 | ) | ||
33 | `shouldThrow` (== KError GenericError "timeout expired" "0") | ||