2019-11-03 10:42:10 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Language.GraphQL.AST.EncoderSpec
|
|
|
|
( spec
|
|
|
|
) where
|
|
|
|
|
2022-12-25 16:38:00 +01:00
|
|
|
import Data.List.NonEmpty (NonEmpty(..))
|
2020-09-30 05:14:52 +02:00
|
|
|
import qualified Language.GraphQL.AST.Document as Full
|
2019-11-03 10:42:10 +01:00
|
|
|
import Language.GraphQL.AST.Encoder
|
2020-03-29 15:56:07 +02:00
|
|
|
import Test.Hspec (Spec, context, describe, it, shouldBe, shouldStartWith, shouldEndWith, shouldNotContain)
|
|
|
|
import Test.QuickCheck (choose, oneof, forAll)
|
2021-09-23 08:23:38 +02:00
|
|
|
import qualified Data.Text.Lazy as Text.Lazy
|
2024-08-28 20:00:44 +02:00
|
|
|
import qualified Language.GraphQL.AST.DirectiveLocation as DirectiveLocation
|
2019-11-03 10:42:10 +01:00
|
|
|
|
|
|
|
spec :: Spec
|
2019-12-21 09:16:41 +01:00
|
|
|
spec = do
|
|
|
|
describe "value" $ do
|
|
|
|
context "minified" $ do
|
2024-10-17 18:08:30 +02:00
|
|
|
it "encodes null" $
|
|
|
|
value minified Full.Null `shouldBe` "null"
|
2019-12-21 09:16:41 +01:00
|
|
|
it "escapes \\" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value minified (Full.String "\\") `shouldBe` "\"\\\\\""
|
2020-03-29 15:56:07 +02:00
|
|
|
it "escapes double quotes" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value minified (Full.String "\"") `shouldBe` "\"\\\"\""
|
2020-03-29 15:56:07 +02:00
|
|
|
it "escapes \\f" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value minified (Full.String "\f") `shouldBe` "\"\\f\""
|
2020-03-29 15:56:07 +02:00
|
|
|
it "escapes \\n" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value minified (Full.String "\n") `shouldBe` "\"\\n\""
|
2020-03-29 15:56:07 +02:00
|
|
|
it "escapes \\r" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value minified (Full.String "\r") `shouldBe` "\"\\r\""
|
2020-03-29 15:56:07 +02:00
|
|
|
it "escapes \\t" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value minified (Full.String "\t") `shouldBe` "\"\\t\""
|
2019-12-21 09:16:41 +01:00
|
|
|
it "escapes backspace" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value minified (Full.String "a\bc") `shouldBe` "\"a\\bc\""
|
2020-03-29 15:56:07 +02:00
|
|
|
context "escapes Unicode for chars less than 0010" $ do
|
2020-09-30 05:14:52 +02:00
|
|
|
it "Null" $ value minified (Full.String "\x0000") `shouldBe` "\"\\u0000\""
|
|
|
|
it "bell" $ value minified (Full.String "\x0007") `shouldBe` "\"\\u0007\""
|
2020-03-29 15:56:07 +02:00
|
|
|
context "escapes Unicode for char less than 0020" $ do
|
2020-09-30 05:14:52 +02:00
|
|
|
it "DLE" $ value minified (Full.String "\x0010") `shouldBe` "\"\\u0010\""
|
|
|
|
it "EM" $ value minified (Full.String "\x0019") `shouldBe` "\"\\u0019\""
|
2020-03-29 15:56:07 +02:00
|
|
|
context "encodes without escape" $ do
|
2020-09-30 05:14:52 +02:00
|
|
|
it "space" $ value minified (Full.String "\x0020") `shouldBe` "\" \""
|
|
|
|
it "~" $ value minified (Full.String "\x007E") `shouldBe` "\"~\""
|
2019-12-21 09:16:41 +01:00
|
|
|
|
|
|
|
context "pretty" $ do
|
2024-10-17 18:08:30 +02:00
|
|
|
it "encodes null" $
|
|
|
|
value pretty Full.Null `shouldBe` "null"
|
|
|
|
|
2019-12-21 09:16:41 +01:00
|
|
|
it "uses strings for short string values" $
|
2020-09-30 05:14:52 +02:00
|
|
|
value pretty (Full.String "Short text") `shouldBe` "\"Short text\""
|
2020-03-29 15:56:07 +02:00
|
|
|
it "uses block strings for text with new lines, with newline symbol" $
|
2024-10-14 20:50:34 +02:00
|
|
|
let expected = "\"\"\"\n\
|
|
|
|
\ Line 1\n\
|
|
|
|
\ Line 2\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
actual = value pretty $ Full.String "Line 1\nLine 2"
|
|
|
|
in actual `shouldBe` expected
|
2020-03-29 15:56:07 +02:00
|
|
|
it "uses block strings for text with new lines, with CR symbol" $
|
2024-10-14 20:50:34 +02:00
|
|
|
let expected = "\"\"\"\n\
|
|
|
|
\ Line 1\n\
|
|
|
|
\ Line 2\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
actual = value pretty $ Full.String "Line 1\rLine 2"
|
|
|
|
in actual `shouldBe` expected
|
2020-03-29 15:56:07 +02:00
|
|
|
it "uses block strings for text with new lines, with CR symbol followed by newline" $
|
2024-10-14 20:50:34 +02:00
|
|
|
let expected = "\"\"\"\n\
|
|
|
|
\ Line 1\n\
|
|
|
|
\ Line 2\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
actual = value pretty $ Full.String "Line 1\r\nLine 2"
|
|
|
|
in actual `shouldBe` expected
|
2020-03-29 15:56:07 +02:00
|
|
|
it "encodes as one line string if has escaped symbols" $ do
|
2024-10-14 20:50:34 +02:00
|
|
|
let genNotAllowedSymbol = oneof
|
|
|
|
[ choose ('\x0000', '\x0008')
|
|
|
|
, choose ('\x000B', '\x000C')
|
|
|
|
, choose ('\x000E', '\x001F')
|
|
|
|
, pure '\x007F'
|
|
|
|
]
|
2020-03-29 15:56:07 +02:00
|
|
|
forAll genNotAllowedSymbol $ \x -> do
|
2024-10-14 20:50:34 +02:00
|
|
|
let rawValue = "Short \n" <> Text.Lazy.cons x "text"
|
|
|
|
encoded = Text.Lazy.unpack
|
|
|
|
$ value pretty
|
|
|
|
$ Full.String
|
|
|
|
$ Text.Lazy.toStrict rawValue
|
|
|
|
shouldStartWith encoded "\""
|
|
|
|
shouldEndWith encoded "\""
|
|
|
|
shouldNotContain encoded "\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
|
|
|
|
it "Hello world" $
|
|
|
|
let actual = value pretty
|
|
|
|
$ Full.String "Hello,\n World!\n\nYours,\n GraphQL."
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "\"\"\"\n\
|
|
|
|
\ Hello,\n\
|
|
|
|
\ World!\n\
|
|
|
|
\\n\
|
|
|
|
\ Yours,\n\
|
|
|
|
\ GraphQL.\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
in actual `shouldBe` expected
|
|
|
|
|
|
|
|
it "has only newlines" $
|
|
|
|
let actual = value pretty $ Full.String "\n"
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "\"\"\"\n\n\n\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
in actual `shouldBe` expected
|
2020-03-29 15:56:07 +02:00
|
|
|
it "has newlines and one symbol at the begining" $
|
2021-09-23 08:23:38 +02:00
|
|
|
let actual = value pretty $ Full.String "a\n\n"
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "\"\"\"\n\
|
|
|
|
\ a\n\
|
|
|
|
\\n\
|
|
|
|
\\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
in actual `shouldBe` expected
|
2020-03-29 15:56:07 +02:00
|
|
|
it "has newlines and one symbol at the end" $
|
2021-09-23 08:23:38 +02:00
|
|
|
let actual = value pretty $ Full.String "\n\na"
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "\"\"\"\n\
|
|
|
|
\\n\
|
|
|
|
\\n\
|
|
|
|
\ a\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
in actual `shouldBe` expected
|
2020-03-29 15:56:07 +02:00
|
|
|
it "has newlines and one symbol in the middle" $
|
2021-09-23 08:23:38 +02:00
|
|
|
let actual = value pretty $ Full.String "\na\n"
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "\"\"\"\n\
|
|
|
|
\\n\
|
|
|
|
\ a\n\
|
|
|
|
\\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
in actual `shouldBe` expected
|
|
|
|
it "skip trailing whitespaces" $
|
|
|
|
let actual = value pretty $ Full.String " Short\ntext "
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "\"\"\"\n\
|
|
|
|
\ Short\n\
|
|
|
|
\ text\n\
|
|
|
|
\\"\"\""
|
2021-09-23 08:23:38 +02:00
|
|
|
in actual `shouldBe` expected
|
2019-12-21 09:16:41 +01:00
|
|
|
|
|
|
|
describe "definition" $
|
|
|
|
it "indents block strings in arguments" $
|
2020-09-30 05:14:52 +02:00
|
|
|
let location = Full.Location 0 0
|
|
|
|
argumentValue = Full.Node (Full.String "line1\nline2") location
|
|
|
|
arguments = [Full.Argument "message" argumentValue location]
|
|
|
|
field = Full.Field Nothing "field" arguments [] [] location
|
|
|
|
fieldSelection = pure $ Full.FieldSelection field
|
|
|
|
operation = Full.DefinitionOperation
|
|
|
|
$ Full.SelectionSet fieldSelection location
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "{\n\
|
|
|
|
\ field(message: \"\"\"\n\
|
|
|
|
\ line1\n\
|
|
|
|
\ line2\n\
|
|
|
|
\ \"\"\")\n\
|
|
|
|
\}\n"
|
2021-09-23 08:23:38 +02:00
|
|
|
actual = definition pretty operation
|
|
|
|
in actual `shouldBe` expected
|
2022-10-02 11:38:53 +02:00
|
|
|
|
|
|
|
describe "operationType" $
|
|
|
|
it "produces lowercase mutation operation type" $
|
|
|
|
let actual = operationType pretty Full.Mutation
|
|
|
|
in actual `shouldBe` "mutation"
|
2022-12-25 16:38:00 +01:00
|
|
|
|
2022-12-27 10:37:34 +01:00
|
|
|
describe "typeSystemDefinition" $ do
|
2022-12-25 16:38:00 +01:00
|
|
|
it "produces a schema with an indented operation type definition" $
|
|
|
|
let queryType = Full.OperationTypeDefinition Full.Query "QueryRootType"
|
|
|
|
mutationType = Full.OperationTypeDefinition Full.Mutation "MutationType"
|
|
|
|
operations = queryType :| pure mutationType
|
|
|
|
definition' = Full.SchemaDefinition [] operations
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "schema {\n\
|
|
|
|
\ query: QueryRootType\n\
|
|
|
|
\ mutation: MutationType\n\
|
|
|
|
\}\n"
|
2022-12-25 16:38:00 +01:00
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|
2022-12-27 10:37:34 +01:00
|
|
|
|
|
|
|
it "encodes a scalar type definition" $
|
|
|
|
let uuidType = Full.ScalarTypeDefinition mempty "UUID" mempty
|
|
|
|
definition' = Full.TypeDefinition uuidType
|
|
|
|
expected = "scalar UUID"
|
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|
2023-01-02 10:30:37 +01:00
|
|
|
|
|
|
|
it "encodes an interface definition" $
|
|
|
|
let someType = Full.TypeNamed "String"
|
|
|
|
argument = Full.InputValueDefinition mempty "arg" someType Nothing mempty
|
|
|
|
arguments = Full.ArgumentsDefinition [argument]
|
|
|
|
definition' = Full.TypeDefinition
|
|
|
|
$ Full.InterfaceTypeDefinition mempty "UUID" mempty
|
|
|
|
$ pure
|
|
|
|
$ Full.FieldDefinition mempty "value" arguments someType mempty
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "interface UUID {\n\
|
|
|
|
\ value(arg: String): String\n\
|
|
|
|
\}"
|
2023-01-02 10:30:37 +01:00
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|
2023-01-08 17:33:25 +01:00
|
|
|
|
2023-01-09 20:56:21 +01:00
|
|
|
it "encodes an union definition" $
|
2023-01-08 17:33:25 +01:00
|
|
|
let definition' = Full.TypeDefinition
|
|
|
|
$ Full.UnionTypeDefinition mempty "SearchResult" mempty
|
|
|
|
$ Full.UnionMemberTypes ["Photo", "Person"]
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "union SearchResult =\n\
|
|
|
|
\ | Photo\n\
|
|
|
|
\ | Person"
|
2023-01-08 17:33:25 +01:00
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|
2023-01-09 20:56:21 +01:00
|
|
|
|
|
|
|
it "encodes an enum definition" $
|
|
|
|
let values =
|
|
|
|
[ Full.EnumValueDefinition mempty "NORTH" mempty
|
|
|
|
, Full.EnumValueDefinition mempty "EAST" mempty
|
|
|
|
, Full.EnumValueDefinition mempty "SOUTH" mempty
|
|
|
|
, Full.EnumValueDefinition mempty "WEST" mempty
|
|
|
|
]
|
|
|
|
definition' = Full.TypeDefinition
|
|
|
|
$ Full.EnumTypeDefinition mempty "Direction" mempty values
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "enum Direction {\n\
|
|
|
|
\ NORTH\n\
|
|
|
|
\ EAST\n\
|
|
|
|
\ SOUTH\n\
|
|
|
|
\ WEST\n\
|
|
|
|
\}"
|
2023-01-09 20:56:21 +01:00
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|
2023-01-10 09:53:18 +01:00
|
|
|
|
|
|
|
it "encodes an input type" $
|
|
|
|
let intType = Full.TypeNonNull $ Full.NonNullTypeNamed "Int"
|
|
|
|
stringType = Full.TypeNamed "String"
|
|
|
|
fields =
|
|
|
|
[ Full.InputValueDefinition mempty "a" stringType Nothing mempty
|
|
|
|
, Full.InputValueDefinition mempty "b" intType Nothing mempty
|
|
|
|
]
|
|
|
|
definition' = Full.TypeDefinition
|
|
|
|
$ Full.InputObjectTypeDefinition mempty "ExampleInputObject" mempty fields
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "input ExampleInputObject {\n\
|
|
|
|
\ a: String\n\
|
|
|
|
\ b: Int!\n\
|
|
|
|
\}"
|
2023-01-10 09:53:18 +01:00
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|
2024-08-28 20:00:44 +02:00
|
|
|
|
|
|
|
context "directive definition" $ do
|
|
|
|
it "encodes a directive definition" $ do
|
|
|
|
let definition' = Full.DirectiveDefinition mempty "example" mempty False
|
|
|
|
$ pure
|
|
|
|
$ DirectiveLocation.ExecutableDirectiveLocation DirectiveLocation.Field
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "@example() on\n\
|
|
|
|
\ | FIELD"
|
2024-08-28 20:00:44 +02:00
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|
|
|
|
|
|
|
|
it "encodes a repeatable directive definition" $ do
|
|
|
|
let definition' = Full.DirectiveDefinition mempty "example" mempty True
|
|
|
|
$ pure
|
|
|
|
$ DirectiveLocation.ExecutableDirectiveLocation DirectiveLocation.Field
|
2024-10-14 20:50:34 +02:00
|
|
|
expected = "@example() repeatable on\n\
|
|
|
|
\ | FIELD"
|
2024-08-28 20:00:44 +02:00
|
|
|
actual = typeSystemDefinition pretty definition'
|
|
|
|
in actual `shouldBe` expected
|