git.iko.soy/typeable/openapi3
on bb1c64f help sign in
typeable/openapi3/test/SpecCommon.hs
41 lines 1.5 kB history raw
1module SpecCommon where
3import Data.Aeson
4import Data.ByteString.Builder (toLazyByteString)
5import qualified Data.Foldable as F
6import qualified Data.HashMap.Strict as HashMap
7import Data.Proxy
8import Data.Typeable
9import qualified Data.Vector as Vector
11import Test.Hspec
12import Test.QuickCheck (Arbitrary, property)
14isSubJSON :: Value -> Value -> Bool
15isSubJSON Null _ = True
16isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && F.and i
17 where
18 i = HashMap.intersectionWith isSubJSON x y
19isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.and (Vector.zipWith isSubJSON xs ys)
20isSubJSON x y = x == y
22(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a, HasCallStack) => a -> Value -> Spec
23x <=> js = do
24 it "encodes correctly" $ do
25 toJSON x `shouldBe` js
26 it "decodes correctly" $ do
27 fromJSON js `shouldBe` Success x
28 it "roundtrips: eitherDecode . encode" $ do
29 eitherDecode (encode x) `shouldBe` Right x
30 it "roundtrips with toJSON" $ do
31 eitherDecode (encode $ toJSON x) `shouldBe` Right x
32 it "roundtrips with toEncoding" $ do
33 eitherDecode (toLazyByteString $ fromEncoding $ toEncoding x) `shouldBe` Right x
35testJsonIdempotency :: (Eq a, Show a, Arbitrary a, FromJSON a, ToJSON a, Typeable a, HasCallStack) =>
36 Proxy a -> Spec
37testJsonIdempotency px = describe (show ty) $ do
38 it "roundtrips: decode . encode" $ property $ \x ->
39 decode (encode x) == Just (x `asProxyTypeOf` px)
40 where
41 ty = typeOf $ undefined `asProxyTypeOf` px