Skip to content

Commit 5c67e11

Browse files
committed
Started moving tests into defaultMain
1 parent fe9df29 commit 5c67e11

File tree

1 file changed

+43
-63
lines changed

1 file changed

+43
-63
lines changed

tests/TestSuite.hs

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,46 @@ import Test.Framework.Providers.HUnit
2323

2424
main :: IO ()
2525
main = defaultMain [ testCase "encode RPC error" testEncodeRpcError
26+
2627
, testCase "encode error with data" testEncodeErrorWithData
27-
, testCase "invalid JSON" testInvalidJson
28-
, testCase "invalid JSON RPC" testInvalidJsonRpc
29-
, testCase "empty batch call" testEmptyBatchCall
28+
29+
, testCase "invalid JSON" $
30+
assertSubtractResponse ("5" :: String) (errResponse idNull (-32700))
31+
32+
, testCase "invalid JSON RPC" $
33+
assertSubtractResponse (object ["id" .= (10 :: Int)]) (errResponse idNull (-32600))
34+
35+
, testCase "empty batch call" $
36+
assertSubtractResponse emptyArray (errResponse idNull (-32600))
37+
3038
, testCase "invalid batch element" testInvalidBatchElement
39+
3140
, testCase "wrong version in request" testWrongVersion
32-
, testCase "method not found" testMethodNotFound
33-
, testCase "wrong method name capitalization" testWrongMethodNameCapitalization
34-
, testCase "missing required named argument" testMissingRequiredNamedArg
35-
, testCase "missing required unnamed argument" testMissingRequiredUnnamedArg
36-
, testCase "wrong argument type" testWrongArgType
37-
, testCase "disallow extra unnamed arguments" testDisallowExtraUnnamedArg
41+
42+
, let req = TestRequest "add" (Just defaultArgs) (Just nonNullId)
43+
rsp = errResponse nonNullId (-32601)
44+
in testCase "method not found" $ assertSubtractResponse req rsp
45+
46+
, let req = TestRequest "Subtract" (Just defaultArgs) (Just nonNullId)
47+
rsp = errResponse nonNullId (-32601)
48+
in testCase "wrong method name capitalization" $ assertSubtractResponse req rsp
49+
50+
, let req = subtractRequestNamed [("X", Number 1), ("y", Number 20)] nonNullId
51+
rsp = errResponse nonNullId (-32602)
52+
in testCase "missing required named argument" $ assertSubtractResponse req rsp
53+
54+
, let req = TestRequest "subtract 2" (Just [Number 0]) (Just nonNullId)
55+
rsp = errResponse nonNullId (-32602)
56+
in testCase "missing required unnamed argument" $ assertSubtractResponse req rsp
57+
58+
, let req = subtractRequestNamed [("x", Number 1), ("y", String "2")] nonNullId
59+
rsp = errResponse nonNullId (-32602)
60+
in testCase "wrong argument type" $ assertSubtractResponse req rsp
61+
62+
, let req = subtractRequestUnnamed (map Number [1, 2, 3]) nonNullId
63+
rsp = errResponse nonNullId (-32602)
64+
in testCase "disallow extra unnamed arguments" $ assertSubtractResponse req rsp
65+
3866
, testCase "invalid notification" testNoResponseToInvalidNotification
3967
, testCase "batch request" testBatch
4068
, testCase "batch notifications" testBatchNotifications
@@ -59,17 +87,9 @@ testEncodeErrorWithData = fromByteString (encode err) @?= Just testError
5987
testError = TestRpcError 1 "my message" $ Just $ toJSON errorData
6088
errorData = ('\x03BB', [True], ())
6189

62-
testInvalidJson :: Assertion
63-
testInvalidJson = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32700))
64-
where rsp = callSubtractMethods ("5" :: String)
65-
66-
testInvalidJsonRpc :: Assertion
67-
testInvalidJsonRpc = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32600))
68-
where rsp = callSubtractMethods $ object ["id" .= (10 :: Int)]
69-
70-
testEmptyBatchCall :: Assertion
71-
testEmptyBatchCall = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32600))
72-
where rsp = callSubtractMethods emptyArray
90+
assertSubtractResponse :: ToJSON a => a -> TestResponse -> Assertion
91+
assertSubtractResponse request expectedRsp = removeErrMsg <$> rsp @?= Just expectedRsp
92+
where rsp = callSubtractMethods request
7393

7494
testInvalidBatchElement :: Assertion
7595
testInvalidBatchElement = map removeErrMsg <$> rsp @?= Just [errResponse idNull (-32600)]
@@ -78,35 +98,7 @@ testInvalidBatchElement = map removeErrMsg <$> rsp @?= Just [errResponse idNull
7898
testWrongVersion :: Assertion
7999
testWrongVersion = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32600))
80100
where rsp = callSubtractMethods $ Object $ H.insert versionKey (String "1") hm
81-
Object hm = toJSON $ subtractRequestNamed [("x", Number 4)] defaultId
82-
83-
testMethodNotFound :: Assertion
84-
testMethodNotFound = removeErrMsg <$> rsp @?= Just (errResponse defaultId (-32601))
85-
where rsp = callSubtractMethods $ TestRequest "ad" (Just defaultArgs) (Just defaultId)
86-
87-
testWrongMethodNameCapitalization :: Assertion
88-
testWrongMethodNameCapitalization = removeErrMsg <$> rsp @?= Just (errResponse defaultId (-32601))
89-
where rsp = callSubtractMethods $ TestRequest "Add" (Just defaultArgs) (Just defaultId)
90-
91-
testMissingRequiredNamedArg :: Assertion
92-
testMissingRequiredNamedArg = removeErrMsg <$> rsp @?= Just (errResponse defaultId (-32602))
93-
where rsp = callSubtractMethods $ subtractRequestNamed args defaultId
94-
args = [("X", Number 1), ("y", Number 20)]
95-
96-
testMissingRequiredUnnamedArg :: Assertion
97-
testMissingRequiredUnnamedArg = checkResponseWithSubtract (encode request) i (-32602)
98-
where request = TestRequest "subtract 2" (Just [Number 0]) (Just i)
99-
i = idString ""
100-
101-
testWrongArgType :: Assertion
102-
testWrongArgType = checkResponseWithSubtract (encode request) i (-32602)
103-
where request = subtractRequestNamed [("x", Number 1), ("y", Bool True)] i
104-
i = idString "ABC"
105-
106-
testDisallowExtraUnnamedArg :: Assertion
107-
testDisallowExtraUnnamedArg = checkResponseWithSubtract (encode request) i (-32602)
108-
where request = subtractRequestUnnamed (map Number [1, 2, 3]) i
109-
i = idString "i"
101+
Object hm = toJSON $ subtractRequestNamed [("x", Number 4)] nonNullId
110102

111103
testNoResponseToInvalidNotification :: Assertion
112104
testNoResponseToInvalidNotification = runIdentity response @?= Nothing
@@ -194,23 +186,11 @@ callSubtractMethods req = let methods :: Methods Identity
194186
rsp = call methods $ encode req
195187
in fromByteString =<< runIdentity rsp
196188

197-
checkResponseWithSubtract :: B.ByteString -> TestId -> Int -> Assertion
198-
checkResponseWithSubtract input expectedId expectedCode = do
199-
rspId <$> res2 @?= Just expectedId
200-
(getErrorCode =<< res2) @?= Just expectedCode
201-
where res1 :: Identity (Maybe B.ByteString)
202-
res1 = call (toMethods [subtractMethod, flippedSubtractMethod]) input
203-
res2 = fromByteString =<< runIdentity res1
204-
205189
fromByteString :: FromJSON a => B.ByteString -> Maybe a
206190
fromByteString str = case fromJSON <$> decode str of
207191
Just (Success x) -> Just x
208192
_ -> Nothing
209193

210-
getErrorCode :: TestResponse -> Maybe Int
211-
getErrorCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
212-
getErrorCode _ = Nothing
213-
214194
subtractMethod :: Method Identity
215195
subtractMethod = toMethod "subtract 1" sub (Required "x" :+: Optional "y" 0 :+: ())
216196
where sub :: Int -> Int -> RpcResult Identity Int
@@ -237,8 +217,8 @@ removeErrMsg rsp = rsp
237217
errResponse :: TestId -> Int -> TestResponse
238218
errResponse i code = TestResponse i (Left (TestRpcError code "" Nothing))
239219

240-
defaultId :: TestId
241-
defaultId = idNumber 3
220+
nonNullId :: TestId
221+
nonNullId = idNumber 3
242222

243223
defaultArgs :: [Int]
244224
defaultArgs = [1, 2]

0 commit comments

Comments
 (0)