forked from facebookincubator/hsthrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumTest.hs
68 lines (56 loc) · 2.14 KB
/
EnumTest.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- Copyright (c) Facebook, Inc. and its affiliates.
{-# LANGUAGE TypeApplications #-}
module EnumTest where
import Thrift.Binary.Parser
import qualified Data.ByteString as ByteString
import Data.Proxy
import Test.HUnit
import TestRunner
import Thrift.Protocol
import Thrift.Protocol.Binary
import Enum.Types
enumListTest :: Test
enumListTest = TestLabel "enum list" $ TestCase $
assertEqual "allThriftEnumValues"
[UnsortedEnum_A, UnsortedEnum_B, UnsortedEnum_C, UnsortedEnum_D,
UnsortedEnum_E, UnsortedEnum_G]
(allThriftEnumValues :: [UnsortedEnum])
ordTest :: Test
ordTest = TestLabel "Ord" $ TestCase $
assertBool "Ord" $ UnsortedEnum_A < UnsortedEnum_E
perfectEnumTest :: Test
perfectEnumTest = TestLabel "perfect" $ TestCase $
assertEqual "perfect enum"
(map fromThriftEnum (allThriftEnumValues :: [PerfectEnum])) [0..3]
enumParseNoErrorTest :: Test
enumParseNoErrorTest = TestLabel "Parse No Error" $ TestCase $ do
let
rawI32 = ByteString.pack [0,0,0,8]
result = parse (parseEnum @Binary @UnsortedEnum Proxy "UnsortedEnum") rawI32
assertEqual "Parse No Error" (Right $ UnsortedEnum__UNKNOWN 8) result
-- Enum with hs.nounknown annotation will throw when enum value is unknown
enumParseErrorTest :: Test
enumParseErrorTest = TestLabel "Parse Error" $ TestCase $ do
let
rawI32 = ByteString.pack [0,0,0,8]
result = parse
(parseEnumNoUnknown @Binary @EnumWithNounknown Proxy "EnumWithNounknown")
rawI32
errorMsg = "ParseError \"parseEnum: not a valid identifier for thrift enum "
++ "'EnumWithNounknown': 8. Failed reading at byte position 4\""
assertEqual "Parse Error" (Left errorMsg) result
toThriftEnumEitherTest :: Test
toThriftEnumEitherTest = TestLabel "toThriftEnumEither" $ TestCase $ do
assertEqual "Right PerfectEnum"
(toThriftEnumEither 0 :: Either String PerfectEnum) (Right PerfectEnum_W)
assertEqual "Left String"
(toThriftEnumEither 4 :: Either String PerfectEnum)
(Left "toThriftEnumEither: not a valid identifier for enum PerfectEnum: 4")
main :: IO ()
main = testRunner $ TestList
[ enumListTest
, ordTest
, perfectEnumTest
, enumParseErrorTest
, toThriftEnumEitherTest
]