sync up all the usage test types

This commit is contained in:
Tom Sydney Kerckhove 2021-11-05 13:47:37 +01:00
parent c5c8df20d6
commit a067824491
6 changed files with 33 additions and 7 deletions

View File

@ -83,7 +83,7 @@ validateAccordingTo val schema = (`evalState` M.empty) $ go val schema
JSON.Object hm ->
let goKey :: Text -> JSON.Value -> State (Map Text JSONSchema) Bool
goKey key value' = case lookup key kss of
Nothing -> pure False
Nothing -> pure True -- Keys not mentioned in the schema are fine.
Just (_, ks, _) -> go value' ks
goKeySchema :: Text -> (KeyRequirement, JSONSchema, Maybe Text) -> State (Map Text JSONSchema) Bool
goKeySchema key (kr, ks, _) = case HM.lookup key hm of

View File

@ -40,6 +40,7 @@ spec = do
jsonSchemaSpec @LT.Text "lazy-text"
jsonSchemaSpec @String "string"
jsonSchemaSpec @Scientific "scientific"
jsonSchemaSpec @JSON.Object "object"
jsonSchemaSpec @JSON.Value "value"
jsonSchemaSpec @Int "int"
jsonSchemaSpec @Int8 "int8"
@ -131,4 +132,24 @@ jsonSchemaSpec filePath =
(JSON.toJSON (jsonSchemaViaCodec @a))
it "validates all encoded values" $
forAllValid $ \(a :: a) ->
validateAccordingTo (toJSONViaCodec a) (jsonSchemaViaCodec @a) `shouldBe` True
let schema = jsonSchemaViaCodec @a
encoded = toJSONViaCodec a
in if validateAccordingTo encoded schema
then pure ()
else
expectationFailure $
unlines
[ "Generated value did not pass the JSON Schema validation, but it should have",
unwords
[ "value",
ppShow a
],
unwords
[ "encoded",
ppShow encoded
],
unwords
[ "schema",
ppShow schema
]
]

View File

@ -30,12 +30,12 @@ spec = do
aesonCodecSpec @NullUnit
aesonCodecSpec @Bool
aesonCodecSpec @Ordering
-- Does not hold
-- aesonCodecSpec @Char
-- aesonCodecSpec @String
xdescribe "does not hold" $ aesonCodecSpec @Char
aesonCodecSpec @Text
aesonCodecSpec @LT.Text
xdescribe "does not hold" $ aesonCodecSpec @String
aesonCodecSpec @Scientific
aesonCodecSpec @JSON.Object
aesonCodecSpec @JSON.Value
aesonCodecSpec @Int
aesonCodecSpec @Int8
@ -48,8 +48,8 @@ spec = do
aesonCodecSpec @Word32
aesonCodecSpec @Word64
aesonCodecSpec @(Maybe Text)
aesonCodecSpec @(Either Text Bool)
aesonCodecSpec @(Either (Either Text Scientific) Bool)
aesonCodecSpec @(Either Bool Text)
aesonCodecSpec @(Either (Either Bool Scientific) Text)
aesonCodecSpec @[Text]
aesonCodecSpec @Fruit
aesonCodecSpec @Example

View File

@ -31,6 +31,7 @@ spec = do
showCodecSpec @LT.Text "lazy-text"
showCodecSpec @String "string"
showCodecSpec @Scientific "scientific"
showCodecSpec @JSON.Object "object"
showCodecSpec @JSON.Value "value"
showCodecSpec @Int "int"
showCodecSpec @Int8 "int8"

View File

@ -0,0 +1,3 @@
{
"type": "object"
}

View File

@ -0,0 +1 @@
ObjectCodec