test[Type]: Sampling is always conforming

This commit is contained in:
vegowotenks 2025-08-22 11:25:33 +02:00
parent 3fd043b9c5
commit 987a849682
2 changed files with 32 additions and 1 deletions

View file

@ -29,6 +29,7 @@ import qualified Data.Text as Text
import qualified Data.Array.Arbitrary as ArbitraryArray import qualified Data.Array.Arbitrary as ArbitraryArray
import qualified Data.Traversable as Traversable import qualified Data.Traversable as Traversable
import qualified Test.QuickCheck.Gen as Gen import qualified Test.QuickCheck.Gen as Gen
import qualified Data.Map as Map
data Type data Type
= Null = Null
@ -73,6 +74,19 @@ instance Monoid Type where
mempty :: Type mempty :: Type
mempty = All mempty = All
instance Arbitrary Type where
arbitrary :: Gen Type
arbitrary = Gen.oneof
[ pure Null
, pure All
, pure Number
, pure String
, pure Boolean
, Array <$> arbitrary
, Union <$> Gen.scale (`div` 2) arbitrary
, Object . Map.mapKeys (Text.pack . getUnicodeString) <$> Gen.scale (`div` 2) arbitrary
]
mergeUnionObjects :: Set Type -> Set Type mergeUnionObjects :: Set Type -> Set Type
mergeUnionObjects set = let mergeUnionObjects set = let
(objects, rest) = Set.partition isObject set (objects, rest) = Set.partition isObject set

View file

@ -1,14 +1,31 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE DerivingVia #-}
module Test.Language.Json.Type (testGroup) where module Test.Language.Json.Type (testGroup) where
import qualified Language.Json.Type as Type import qualified Language.Json.Type as Type
import qualified Language.Json as Json import qualified Language.Json as Json
import Test.QuickCheck (allProperties, Property) import Test.QuickCheck (allProperties, Property, Arbitrary (arbitrary), Gen)
import qualified Test.Tasty.QuickCheck as Tasty.QuickCheck import qualified Test.Tasty.QuickCheck as Tasty.QuickCheck
import Test.Tasty (TestTree) import Test.Tasty (TestTree)
import Language.Json.Type (Type)
prop_inferredTypeConformsSourceValue :: Json.PrintableValue -> Bool prop_inferredTypeConformsSourceValue :: Json.PrintableValue -> Bool
prop_inferredTypeConformsSourceValue (Json.PrintableValue value) = null (Type.conforms (Type.infer value) value) prop_inferredTypeConformsSourceValue (Json.PrintableValue value) = null (Type.conforms (Type.infer value) value)
newtype TypeWithSampledValue = TypeWithSampledValue (Type, Json.Value)
deriving stock Show
instance Arbitrary TypeWithSampledValue where
arbitrary :: Gen TypeWithSampledValue
arbitrary = do
layout <- arbitrary
value <- Type.sample layout
pure . TypeWithSampledValue $ (layout, value)
prop_sampledValueConformsToType :: TypeWithSampledValue -> Bool
prop_sampledValueConformsToType (TypeWithSampledValue (layout, value)) = null (Type.conforms layout value)
return [] -- make ghc 'commit' already defined values return [] -- make ghc 'commit' already defined values
properties :: [(String, Property)] properties :: [(String, Property)]
properties = $allProperties properties = $allProperties