on weeder-instances help sign in
typeable/aeson/tests/ErrorMessages.hs
57 lines 1.6 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE ScopedTypeVariables #-}
4module ErrorMessages
5 (
6 tests
7 ) where
9import Prelude ()
10import Prelude.Compat
12import Data.Aeson (FromJSON(..), eitherDecode)
13import Data.Proxy (Proxy(..))
14import Instances ()
15import Test.Framework (Test)
16import Test.Framework.Providers.HUnit (testCase)
17import Test.HUnit (Assertion, assertFailure, assertEqual)
18import qualified Data.ByteString.Lazy.Char8 as L
19import qualified Data.HashMap.Strict as HM
21tests :: [Test]
22tests =
23 [
24 testCase "Int" int
25 , testCase "String" string
26 , testCase "HashMap" hashMap
27 ]
29int :: Assertion
30int = do
31 let t = test (Proxy :: Proxy Int)
32 t "\"\"" $ expected "Int" "String"
33 t "[]" $ expected "Int" "Array"
34 t "{}" $ expected "Int" "Object"
35 t "null" $ expected "Int" "Null"
37string :: Assertion
38string = do
39 let t = test (Proxy :: Proxy String)
40 t "1" $ expected "String" "Number"
41 t "[]" $ expected "String" "Array"
42 t "{}" $ expected "String" "Object"
43 t "null" $ expected "String" "Null"
45hashMap :: Assertion
46hashMap = do
47 let t = test (Proxy :: Proxy (HM.HashMap String Int))
48 t "\"\"" $ expected "HashMap k v" "String"
49 t "[]" $ expected "HashMap k v" "Array"
51expected :: String -> String -> String
52expected ex enc = "Error in $: expected " ++ ex ++ ", encountered " ++ enc
54test :: forall a proxy . (FromJSON a, Show a) => proxy a -> L.ByteString -> String -> Assertion
55test _ v msg = case eitherDecode v of
56 Left e -> assertEqual "Invalid error message" msg e
57 Right (x :: a) -> assertFailure $ "Expected parsing to fail but it suceeded with: " ++ show x