2022-02-02 18:52:46 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
module Language.GraphQL.AST.Arbitrary
|
|
|
|
( AnyArgument(..)
|
|
|
|
, AnyLocation(..)
|
|
|
|
, AnyName(..)
|
|
|
|
, AnyNode(..)
|
|
|
|
, AnyObjectField(..)
|
|
|
|
, AnyValue(..)
|
|
|
|
, printArgument
|
|
|
|
) where
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
import qualified Language.GraphQL.AST.Document as Doc
|
|
|
|
import Test.QuickCheck.Arbitrary (Arbitrary (arbitrary))
|
|
|
|
import Test.QuickCheck (oneof, elements, listOf, resize, NonEmptyList (..))
|
|
|
|
import Test.QuickCheck.Gen (Gen (..))
|
2024-10-17 18:08:30 +02:00
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as Text
|
2024-04-02 21:07:31 +02:00
|
|
|
import Data.Functor ((<&>))
|
2022-02-02 18:52:46 +01:00
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyPrintableChar = AnyPrintableChar
|
|
|
|
{ getAnyPrintableChar :: Char
|
|
|
|
} deriving (Eq, Show)
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
alpha :: String
|
|
|
|
alpha = ['a'..'z'] <> ['A'..'Z']
|
|
|
|
|
|
|
|
num :: String
|
|
|
|
num = ['0'..'9']
|
|
|
|
|
|
|
|
instance Arbitrary AnyPrintableChar where
|
|
|
|
arbitrary = AnyPrintableChar <$> elements chars
|
|
|
|
where
|
2024-10-17 18:08:30 +02:00
|
|
|
chars = alpha <> num <> ['_']
|
2022-02-02 18:52:46 +01:00
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyPrintableText = AnyPrintableText
|
|
|
|
{ getAnyPrintableText :: Text
|
|
|
|
} deriving (Eq, Show)
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
instance Arbitrary AnyPrintableText where
|
|
|
|
arbitrary = do
|
|
|
|
nonEmptyStr <- getNonEmpty <$> (arbitrary :: Gen (NonEmptyList AnyPrintableChar))
|
2024-10-17 18:08:30 +02:00
|
|
|
pure $ AnyPrintableText
|
|
|
|
$ Text.pack
|
|
|
|
$ map getAnyPrintableChar nonEmptyStr
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
-- https://spec.graphql.org/June2018/#Name
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyName = AnyName
|
|
|
|
{ getAnyName :: Text
|
|
|
|
} deriving (Eq, Show)
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
instance Arbitrary AnyName where
|
|
|
|
arbitrary = do
|
|
|
|
firstChar <- elements $ alpha <> ['_']
|
|
|
|
rest <- (arbitrary :: Gen [AnyPrintableChar])
|
2024-10-17 18:08:30 +02:00
|
|
|
pure $ AnyName
|
|
|
|
$ Text.pack
|
|
|
|
$ firstChar : map getAnyPrintableChar rest
|
2022-02-02 18:52:46 +01:00
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyLocation = AnyLocation
|
|
|
|
{ getAnyLocation :: Doc.Location
|
|
|
|
} deriving (Eq, Show)
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
instance Arbitrary AnyLocation where
|
|
|
|
arbitrary = AnyLocation <$> (Doc.Location <$> arbitrary <*> arbitrary)
|
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyNode a = AnyNode
|
|
|
|
{ getAnyNode :: Doc.Node a
|
|
|
|
} deriving (Eq, Show)
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
instance Arbitrary a => Arbitrary (AnyNode a) where
|
|
|
|
arbitrary = do
|
|
|
|
(AnyLocation location') <- arbitrary
|
|
|
|
node' <- flip Doc.Node location' <$> arbitrary
|
|
|
|
pure $ AnyNode node'
|
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyObjectField a = AnyObjectField
|
|
|
|
{ getAnyObjectField :: Doc.ObjectField a
|
|
|
|
} deriving (Eq, Show)
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
instance Arbitrary a => Arbitrary (AnyObjectField a) where
|
|
|
|
arbitrary = do
|
|
|
|
name' <- getAnyName <$> arbitrary
|
|
|
|
value' <- getAnyNode <$> arbitrary
|
|
|
|
location' <- getAnyLocation <$> arbitrary
|
|
|
|
pure $ AnyObjectField $ Doc.ObjectField name' value' location'
|
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyValue = AnyValue
|
|
|
|
{ getAnyValue :: Doc.Value
|
|
|
|
} deriving (Eq, Show)
|
2024-04-02 21:07:31 +02:00
|
|
|
|
|
|
|
instance Arbitrary AnyValue
|
|
|
|
where
|
|
|
|
arbitrary =
|
|
|
|
let variableGen :: Gen Doc.Value
|
|
|
|
variableGen = Doc.Variable . getAnyName <$> arbitrary
|
|
|
|
listGen :: Gen [Doc.Node Doc.Value]
|
|
|
|
listGen = (resize 5 . listOf) nodeGen
|
|
|
|
nodeGen :: Gen (Doc.Node Doc.Value)
|
|
|
|
nodeGen = fmap getAnyNode arbitrary <&> fmap getAnyValue
|
|
|
|
objectGen :: Gen [Doc.ObjectField Doc.Value]
|
|
|
|
objectGen = resize 1
|
|
|
|
$ fmap getNonEmpty arbitrary
|
|
|
|
<&> map (fmap getAnyValue . getAnyObjectField)
|
|
|
|
in AnyValue <$> oneof
|
|
|
|
[ variableGen
|
|
|
|
, Doc.Int <$> arbitrary
|
|
|
|
, Doc.Float <$> arbitrary
|
|
|
|
, Doc.String . getAnyPrintableText <$> arbitrary
|
|
|
|
, Doc.Boolean <$> arbitrary
|
|
|
|
, MkGen $ \_ _ -> Doc.Null
|
|
|
|
, Doc.Enum . getAnyName <$> arbitrary
|
|
|
|
, Doc.List <$> listGen
|
|
|
|
, Doc.Object <$> objectGen
|
|
|
|
]
|
|
|
|
|
2024-10-17 18:08:30 +02:00
|
|
|
newtype AnyArgument a = AnyArgument
|
|
|
|
{ getAnyArgument :: Doc.Argument
|
|
|
|
} deriving (Eq, Show)
|
2022-02-02 18:52:46 +01:00
|
|
|
|
|
|
|
instance Arbitrary a => Arbitrary (AnyArgument a) where
|
|
|
|
arbitrary = do
|
|
|
|
name' <- getAnyName <$> arbitrary
|
|
|
|
(AnyValue value') <- arbitrary
|
|
|
|
(AnyLocation location') <- arbitrary
|
|
|
|
pure $ AnyArgument $ Doc.Argument name' (Doc.Node value' location') location'
|
|
|
|
|
|
|
|
printArgument :: AnyArgument AnyValue -> Text
|
2024-10-17 18:08:30 +02:00
|
|
|
printArgument (AnyArgument (Doc.Argument name' (Doc.Node value' _) _)) =
|
|
|
|
name' <> ": " <> (Text.pack . show) value'
|