-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPropMime.hs
51 lines (40 loc) · 1.46 KB
/
PropMime.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
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ConstraintKinds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
module PropMime where
import Data.Aeson
import Data.Aeson.Types (parseEither)
import Data.Monoid ((<>))
import Data.Typeable (Proxy(..), typeOf, Typeable)
import qualified Data.ByteString.Lazy.Char8 as BL8
import Test.Hspec
import Test.QuickCheck
import Test.QuickCheck.Property
import Test.Hspec.QuickCheck (prop)
import Kubernetes.OpenAPI.MimeTypes
import ApproxEq
-- * Type Aliases
type ArbitraryMime mime a = ArbitraryRoundtrip (MimeUnrender mime) (MimeRender mime) a
type ArbitraryRoundtrip from to a = (from a, to a, Arbitrary' a)
type Arbitrary' a = (Arbitrary a, Show a, Typeable a)
-- * Mime
propMime
:: forall a b mime.
(ArbitraryMime mime a, Testable b)
=> String -> (a -> a -> b) -> mime -> Proxy a -> Spec
propMime eqDescr eq m _ =
prop
(show (typeOf (undefined :: a)) <> " " <> show (typeOf (undefined :: mime)) <> " roundtrip " <> eqDescr) $
\(x :: a) ->
let rendered = mimeRender' m x
actual = mimeUnrender' m rendered
expected = Right x
failMsg =
"ACTUAL: " <> show actual <> "\nRENDERED: " <> BL8.unpack rendered
in counterexample failMsg $
either reject property (eq <$> actual <*> expected)
where
reject = property . const rejected
propMimeEq :: (ArbitraryMime mime a, Eq a) => mime -> Proxy a -> Spec
propMimeEq = propMime "(EQ)" (==)