git.iko.soy/typeable/swagger2
on v2.1.2 help sign in
typeable/swagger2/test/SpecCommon.hs
30 lines 1.1 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 qualified Data.Vector as Vector
9import Test.Hspec
11isSubJSON :: Value -> Value -> Bool
12isSubJSON Null _ = True
13isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && F.and i
14 where
15 i = HashMap.intersectionWith isSubJSON x y
16isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.and (Vector.zipWith isSubJSON xs ys)
17isSubJSON x y = x == y
19(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
20x <=> js = do
21 it "encodes correctly" $ do
22 toJSON x `shouldBe` js
23 it "decodes correctly" $ do
24 fromJSON js `shouldBe` Success x
25 it "roundtrips: eitherDecode . encode" $ do
26 eitherDecode (encode x) `shouldBe` Right x
27 it "roundtrips with toJSON" $ do
28 eitherDecode (encode $ toJSON x) `shouldBe` Right x
29 it "roundtrips with toEncoding" $ do
30 eitherDecode (toLazyByteString $ fromEncoding $ toEncoding x) `shouldBe` Right x