{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Test.Shape where
import Testlib.Prelude
testShapeObjectMatch :: (HasCallStack) => App ()
testShapeObjectMatch :: HasCallStack => App ()
testShapeObjectMatch = do
let v :: Value
v = [Pair] -> Value
object [String
"foo" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
42 :: Int), String
"bar" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (String
"hello" :: String)]
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` [(String, Shape)] -> Shape
SObject [(String
"foo", Shape
SNumber), (String
"bar", Shape
SString)]
testShapeUnexpectedKey :: (HasCallStack) => App ()
testShapeUnexpectedKey :: HasCallStack => App ()
testShapeUnexpectedKey = do
let v :: Value
v = [Pair] -> Value
object [String
"foo" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
1 :: Int), String
"extra" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
2 :: Int)]
(AssertionFailure -> App ()) -> App () -> App ()
forall a.
HasCallStack =>
(AssertionFailure -> App ()) -> App a -> App ()
expectFailure (\AssertionFailure
_ -> () -> App ()
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) do
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` [(String, Shape)] -> Shape
SObject [(String
"foo", Shape
SNumber)]
testShapeMissingKey :: (HasCallStack) => App ()
testShapeMissingKey :: HasCallStack => App ()
testShapeMissingKey = do
let v :: Value
v = [Pair] -> Value
object [String
"foo" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
1 :: Int)]
(AssertionFailure -> App ()) -> App () -> App ()
forall a.
HasCallStack =>
(AssertionFailure -> App ()) -> App a -> App ()
expectFailure (\AssertionFailure
_ -> () -> App ()
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) do
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` [(String, Shape)] -> Shape
SObject [(String
"foo", Shape
SNumber), (String
"bar", Shape
SString)]
testShapeWrongTypeObject :: (HasCallStack) => App ()
testShapeWrongTypeObject :: HasCallStack => App ()
testShapeWrongTypeObject = do
let v :: Value
v = String -> Value
forall a. ToJSON a => a -> Value
toJSON (String
"hello" :: String)
(AssertionFailure -> App ()) -> App () -> App ()
forall a.
HasCallStack =>
(AssertionFailure -> App ()) -> App a -> App ()
expectFailure (\AssertionFailure
_ -> () -> App ()
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) do
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` [(String, Shape)] -> Shape
SObject [(String
"foo", Shape
SNumber)]
testShapeWrongTypeString :: (HasCallStack) => App ()
testShapeWrongTypeString :: HasCallStack => App ()
testShapeWrongTypeString = do
let v :: Value
v = Scientific -> Value
Number Scientific
42
(AssertionFailure -> App ()) -> App () -> App ()
forall a.
HasCallStack =>
(AssertionFailure -> App ()) -> App a -> App ()
expectFailure (\AssertionFailure
_ -> () -> App ()
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) do
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` Shape
SString
testShapeArrayElementMismatch :: (HasCallStack) => App ()
testShapeArrayElementMismatch :: HasCallStack => App ()
testShapeArrayElementMismatch = do
let v :: Value
v = [Value] -> Value
forall a. ToJSON a => a -> Value
toJSON [String -> Value
forall a. ToJSON a => a -> Value
toJSON (String
"a" :: String), String -> Value
forall a. ToJSON a => a -> Value
toJSON (String
"b" :: String), Int -> Value
forall a. ToJSON a => a -> Value
toJSON (Int
3 :: Int)]
(AssertionFailure -> App ()) -> App () -> App ()
forall a.
HasCallStack =>
(AssertionFailure -> App ()) -> App a -> App ()
expectFailure (\AssertionFailure
e -> AssertionFailure
e.msg HasCallStack => String -> String -> App ()
String -> String -> App ()
`shouldContainString` String
"[2]") do
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` Shape -> Shape
SArray Shape
SString
testShapeNestedPathReported :: (HasCallStack) => App ()
testShapeNestedPathReported :: HasCallStack => App ()
testShapeNestedPathReported = do
let v :: Value
v =
[Pair] -> Value
object
[ String
"assets"
String -> [Value] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [ [Pair] -> Value
object
[ String
"key" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
42 :: Int),
String
"size" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (String
"preview" :: String),
String
"type" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (String
"image" :: String)
]
]
]
(AssertionFailure -> App ()) -> App () -> App ()
forall a.
HasCallStack =>
(AssertionFailure -> App ()) -> App a -> App ()
expectFailure (\AssertionFailure
e -> AssertionFailure
e.msg HasCallStack => String -> String -> App ()
String -> String -> App ()
`shouldContainString` String
".assets[0].key") do
Value
v
Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` [(String, Shape)] -> Shape
SObject
[ ( String
"assets",
Shape -> Shape
SArray
( [(String, Shape)] -> Shape
SObject
[ (String
"key", Shape
SString),
(String
"size", Shape
SString),
(String
"type", Shape
SString)
]
)
)
]
testShapeSAny :: (HasCallStack) => App ()
testShapeSAny :: HasCallStack => App ()
testShapeSAny = do
let vals :: [Value]
vals :: [Value]
vals = [Value
Null, Bool -> Value
Bool Bool
True, String -> Value
forall a. ToJSON a => a -> Value
toJSON (String
"x" :: String), Scientific -> Value
Number Scientific
1, [Int] -> Value
forall a. ToJSON a => a -> Value
toJSON ([] :: [Int]), [Pair] -> Value
object []]
(Value -> App ()) -> [Value] -> App ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` Shape
SAny) [Value]
vals
testShapeEmptyArray :: (HasCallStack) => App ()
testShapeEmptyArray :: HasCallStack => App ()
testShapeEmptyArray = do
let v :: Value
v = [Int] -> Value
forall a. ToJSON a => a -> Value
toJSON ([] :: [Int])
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` Shape -> Shape
SArray Shape
SString
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` Shape -> Shape
SArray Shape
SNumber
Value
v Value -> Shape -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Shape -> App ()
`shouldMatchShape` Shape -> Shape
SArray ([(String, Shape)] -> Shape
SObject [])
testValueShape :: (HasCallStack) => App ()
testValueShape :: HasCallStack => App ()
testValueShape = do
let v :: Value
v =
[Pair] -> Value
object
[ String
"name" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (String
"Alice" :: String),
String
"age" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
30 :: Int),
String
"active" String -> Bool -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Bool
True,
String
"scores" String -> [Int] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Int
1 :: Int, Int
2, Int
3],
String
"address" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Pair] -> Value
object [String
"city" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (String
"London" :: String)]
]
shape <- Value -> App Shape
forall a. MakesValue a => a -> App Shape
valueShape Value
v
v `shouldMatchShape` shape