@@ -0,0 +1,39 @@
1+module SpecCommon where
2+
3+import Data.Aeson
4+import Data.Aeson.QQ
5+import qualified Data.Foldable as F
6+import Data.HashMap.Strict (HashMap)
7+import qualified Data.HashMap.Strict as HashMap
8+import Data.Maybe
9+import Data.Monoid
10+import qualified Data.Vector as Vector
11+import Data.Text (Text)
12+
13+import Data.Swagger
14+
15+import Test.Hspec
16+
17+isSubJSON :: Value -> Value -> Bool
18+isSubJSON Null _ = True
19+isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && F.and i
20+ where
21+ i = HashMap.intersectionWith isSubJSON x y
22+isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.and (Vector.zipWith isSubJSON xs ys)
23+isSubJSON x y = x == y
24+
25+(<~>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
26+x <~> json = do
27+ it "encodes correctly (probably with extra properties)" $ do
28+ toJSON x `shouldSatisfy` (json `isSubJSON`)
29+ it "decodes correctly" $ do
30+ fromJSON json `shouldBe` Success x
31+
32+(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
33+x <=> json = do
34+ it "encodes correctly" $ do
35+ toJSON x `shouldBe` json
36+ it "decodes correctly" $ do
37+ fromJSON json `shouldBe` Success x
38+
39+