module SpecCommon where import Data.Aeson import qualified Data.Foldable as F import qualified Data.HashMap.Strict as HashMap import qualified Data.Vector as Vector import Test.Hspec isSubJSON :: Value -> Value -> Bool isSubJSON Null _ = True isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && F.and i where i = HashMap.intersectionWith isSubJSON x y isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.and (Vector.zipWith isSubJSON xs ys) isSubJSON x y = x == y (<~>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec x <~> js = do it "encodes correctly (probably with extra properties)" $ do toJSON x `shouldSatisfy` (js `isSubJSON`) it "decodes correctly" $ do fromJSON js `shouldBe` Success x (<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec x <=> js = do it "encodes correctly" $ do toJSON x `shouldBe` js it "decodes correctly" $ do fromJSON js `shouldBe` Success x