Remove raw-strings-qq

This commit is contained in:
Eugen Wissner 2021-09-23 08:23:38 +02:00
parent eedab9e742
commit fbfbb3e73f
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
6 changed files with 156 additions and 113 deletions

View File

@ -102,7 +102,6 @@ test-suite graphql-test
, hspec >= 2.8.2 && < 2.9 , hspec >= 2.8.2 && < 2.9
, hspec-megaparsec >= 2.2.0 && < 2.3 , hspec-megaparsec >= 2.2.0 && < 2.3
, megaparsec , megaparsec
, raw-strings-qq >= 1.1 && < 1.2
, scientific , scientific
, text , text
, unordered-containers , unordered-containers

View File

@ -6,10 +6,10 @@ module Language.GraphQL.AST.EncoderSpec
import qualified Language.GraphQL.AST.Document as Full import qualified Language.GraphQL.AST.Document as Full
import Language.GraphQL.AST.Encoder import Language.GraphQL.AST.Encoder
import Language.GraphQL.TH
import Test.Hspec (Spec, context, describe, it, shouldBe, shouldStartWith, shouldEndWith, shouldNotContain) import Test.Hspec (Spec, context, describe, it, shouldBe, shouldStartWith, shouldEndWith, shouldNotContain)
import Test.QuickCheck (choose, oneof, forAll) import Test.QuickCheck (choose, oneof, forAll)
import Text.RawString.QQ (r) import qualified Data.Text.Lazy as Text.Lazy
import Data.Text.Lazy (cons, toStrict, unpack)
spec :: Spec spec :: Spec
spec = do spec = do
@ -48,23 +48,32 @@ spec = do
it "uses strings for short string values" $ it "uses strings for short string values" $
value pretty (Full.String "Short text") `shouldBe` "\"Short text\"" value pretty (Full.String "Short text") `shouldBe` "\"Short text\""
it "uses block strings for text with new lines, with newline symbol" $ it "uses block strings for text with new lines, with newline symbol" $
value pretty (Full.String "Line 1\nLine 2") let expected = [gql|
`shouldBe` [r|""" """
Line 1 Line 1
Line 2 Line 2
"""|] """
|]
actual = value pretty $ Full.String "Line 1\nLine 2"
in actual `shouldBe` expected
it "uses block strings for text with new lines, with CR symbol" $ it "uses block strings for text with new lines, with CR symbol" $
value pretty (Full.String "Line 1\rLine 2") let expected = [gql|
`shouldBe` [r|""" """
Line 1 Line 1
Line 2 Line 2
"""|] """
|]
actual = value pretty $ Full.String "Line 1\rLine 2"
in actual `shouldBe` expected
it "uses block strings for text with new lines, with CR symbol followed by newline" $ it "uses block strings for text with new lines, with CR symbol followed by newline" $
value pretty (Full.String "Line 1\r\nLine 2") let expected = [gql|
`shouldBe` [r|""" """
Line 1 Line 1
Line 2 Line 2
"""|] """
|]
actual = value pretty $ Full.String "Line 1\r\nLine 2"
in actual `shouldBe` expected
it "encodes as one line string if has escaped symbols" $ do it "encodes as one line string if has escaped symbols" $ do
let let
genNotAllowedSymbol = oneof genNotAllowedSymbol = oneof
@ -76,48 +85,74 @@ spec = do
forAll genNotAllowedSymbol $ \x -> do forAll genNotAllowedSymbol $ \x -> do
let let
rawValue = "Short \n" <> cons x "text" rawValue = "Short \n" <> Text.Lazy.cons x "text"
encoded = value pretty (Full.String $ toStrict rawValue) encoded = value pretty
shouldStartWith (unpack encoded) "\"" $ Full.String $ Text.Lazy.toStrict rawValue
shouldEndWith (unpack encoded) "\"" shouldStartWith (Text.Lazy.unpack encoded) "\""
shouldNotContain (unpack encoded) "\"\"\"" shouldEndWith (Text.Lazy.unpack encoded) "\""
shouldNotContain (Text.Lazy.unpack encoded) "\"\"\""
it "Hello world" $ value pretty (Full.String "Hello,\n World!\n\nYours,\n GraphQL.") it "Hello world" $
`shouldBe` [r|""" let actual = value pretty
Hello, $ Full.String "Hello,\n World!\n\nYours,\n GraphQL."
World! expected = [gql|
"""
Hello,
World!
Yours, Yours,
GraphQL. GraphQL.
"""|] """
|]
in actual `shouldBe` expected
it "has only newlines" $ value pretty (Full.String "\n") `shouldBe` [r|""" it "has only newlines" $
let actual = value pretty $ Full.String "\n"
expected = [gql|
"""
"""|] """
|]
in actual `shouldBe` expected
it "has newlines and one symbol at the begining" $ it "has newlines and one symbol at the begining" $
value pretty (Full.String "a\n\n") `shouldBe` [r|""" let actual = value pretty $ Full.String "a\n\n"
a expected = [gql|
"""
a
"""|] """|]
in actual `shouldBe` expected
it "has newlines and one symbol at the end" $ it "has newlines and one symbol at the end" $
value pretty (Full.String "\n\na") `shouldBe` [r|""" let actual = value pretty $ Full.String "\n\na"
expected = [gql|
"""
a a
"""|] """
|]
in actual `shouldBe` expected
it "has newlines and one symbol in the middle" $ it "has newlines and one symbol in the middle" $
value pretty (Full.String "\na\n") `shouldBe` [r|""" let actual = value pretty $ Full.String "\na\n"
expected = [gql|
"""
a a
"""|] """
it "skip trailing whitespaces" $ value pretty (Full.String " Short\ntext ") |]
`shouldBe` [r|""" in actual `shouldBe` expected
Short it "skip trailing whitespaces" $
text let actual = value pretty $ Full.String " Short\ntext "
"""|] expected = [gql|
"""
Short
text
"""
|]
in actual `shouldBe` expected
describe "definition" $ describe "definition" $
it "indents block strings in arguments" $ it "indents block strings in arguments" $
@ -128,10 +163,13 @@ spec = do
fieldSelection = pure $ Full.FieldSelection field fieldSelection = pure $ Full.FieldSelection field
operation = Full.DefinitionOperation operation = Full.DefinitionOperation
$ Full.SelectionSet fieldSelection location $ Full.SelectionSet fieldSelection location
in definition pretty operation `shouldBe` [r|{ expected = Text.Lazy.snoc [gql|
field(message: """ {
line1 field(message: """
line2 line1
""") line2
} """)
|] }
|] '\n'
actual = definition pretty operation
in actual `shouldBe` expected

View File

@ -8,10 +8,10 @@ import Data.List.NonEmpty (NonEmpty(..))
import Language.GraphQL.AST.Document import Language.GraphQL.AST.Document
import qualified Language.GraphQL.AST.DirectiveLocation as DirLoc import qualified Language.GraphQL.AST.DirectiveLocation as DirLoc
import Language.GraphQL.AST.Parser import Language.GraphQL.AST.Parser
import Language.GraphQL.TH
import Test.Hspec (Spec, describe, it) import Test.Hspec (Spec, describe, it)
import Test.Hspec.Megaparsec (shouldParse, shouldFailOn, shouldSucceedOn) import Test.Hspec.Megaparsec (shouldParse, shouldFailOn, shouldSucceedOn)
import Text.Megaparsec (parse) import Text.Megaparsec (parse)
import Text.RawString.QQ (r)
spec :: Spec spec :: Spec
spec = describe "Parser" $ do spec = describe "Parser" $ do
@ -19,74 +19,74 @@ spec = describe "Parser" $ do
parse document "" `shouldSucceedOn` "\xfeff{foo}" parse document "" `shouldSucceedOn` "\xfeff{foo}"
it "accepts block strings as argument" $ it "accepts block strings as argument" $
parse document "" `shouldSucceedOn` [r|{ parse document "" `shouldSucceedOn` [gql|{
hello(text: """Argument""") hello(text: """Argument""")
}|] }|]
it "accepts strings as argument" $ it "accepts strings as argument" $
parse document "" `shouldSucceedOn` [r|{ parse document "" `shouldSucceedOn` [gql|{
hello(text: "Argument") hello(text: "Argument")
}|] }|]
it "accepts two required arguments" $ it "accepts two required arguments" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
mutation auth($username: String!, $password: String!){ mutation auth($username: String!, $password: String!){
test test
}|] }|]
it "accepts two string arguments" $ it "accepts two string arguments" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
mutation auth{ mutation auth{
test(username: "username", password: "password") test(username: "username", password: "password")
}|] }|]
it "accepts two block string arguments" $ it "accepts two block string arguments" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
mutation auth{ mutation auth{
test(username: """username""", password: """password""") test(username: """username""", password: """password""")
}|] }|]
it "parses minimal schema definition" $ it "parses minimal schema definition" $
parse document "" `shouldSucceedOn` [r|schema { query: Query }|] parse document "" `shouldSucceedOn` [gql|schema { query: Query }|]
it "parses minimal scalar definition" $ it "parses minimal scalar definition" $
parse document "" `shouldSucceedOn` [r|scalar Time|] parse document "" `shouldSucceedOn` [gql|scalar Time|]
it "parses ImplementsInterfaces" $ it "parses ImplementsInterfaces" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
type Person implements NamedEntity & ValuedEntity { type Person implements NamedEntity & ValuedEntity {
name: String name: String
} }
|] |]
it "parses a type without ImplementsInterfaces" $ it "parses a type without ImplementsInterfaces" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
type Person { type Person {
name: String name: String
} }
|] |]
it "parses ArgumentsDefinition in an ObjectDefinition" $ it "parses ArgumentsDefinition in an ObjectDefinition" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
type Person { type Person {
name(first: String, last: String): String name(first: String, last: String): String
} }
|] |]
it "parses minimal union type definition" $ it "parses minimal union type definition" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
union SearchResult = Photo | Person union SearchResult = Photo | Person
|] |]
it "parses minimal interface type definition" $ it "parses minimal interface type definition" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
interface NamedEntity { interface NamedEntity {
name: String name: String
} }
|] |]
it "parses minimal enum type definition" $ it "parses minimal enum type definition" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
enum Direction { enum Direction {
NORTH NORTH
EAST EAST
@ -96,7 +96,7 @@ spec = describe "Parser" $ do
|] |]
it "parses minimal enum type definition" $ it "parses minimal enum type definition" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
enum Direction { enum Direction {
NORTH NORTH
EAST EAST
@ -106,7 +106,7 @@ spec = describe "Parser" $ do
|] |]
it "parses minimal input object type definition" $ it "parses minimal input object type definition" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
input Point2D { input Point2D {
x: Float x: Float
y: Float y: Float
@ -114,7 +114,7 @@ spec = describe "Parser" $ do
|] |]
it "parses minimal input enum definition with an optional pipe" $ it "parses minimal input enum definition with an optional pipe" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
directive @example on directive @example on
| FIELD | FIELD
| FRAGMENT_SPREAD | FRAGMENT_SPREAD
@ -131,15 +131,15 @@ spec = describe "Parser" $ do
example1 = example1 =
directive "example1" directive "example1"
(DirLoc.TypeSystemDirectiveLocation DirLoc.FieldDefinition) (DirLoc.TypeSystemDirectiveLocation DirLoc.FieldDefinition)
(Location {line = 2, column = 17}) (Location {line = 1, column = 1})
example2 = example2 =
directive "example2" directive "example2"
(DirLoc.ExecutableDirectiveLocation DirLoc.Field) (DirLoc.ExecutableDirectiveLocation DirLoc.Field)
(Location {line = 3, column = 17}) (Location {line = 2, column = 1})
testSchemaExtension = example1 :| [ example2 ] testSchemaExtension = example1 :| [ example2 ]
query = [r| query = [gql|
directive @example1 on FIELD_DEFINITION directive @example1 on FIELD_DEFINITION
directive @example2 on FIELD directive @example2 on FIELD
|] |]
in parse document "" query `shouldParse` testSchemaExtension in parse document "" query `shouldParse` testSchemaExtension
@ -167,16 +167,16 @@ spec = describe "Parser" $ do
$ Node (ConstList []) $ Node (ConstList [])
$ Location {line = 1, column = 33})] $ Location {line = 1, column = 33})]
(Location {line = 1, column = 1}) (Location {line = 1, column = 1})
query = [r|directive @test(foo: [String] = []) on FIELD_DEFINITION|] query = [gql|directive @test(foo: [String] = []) on FIELD_DEFINITION|]
in parse document "" query `shouldParse` (defn :| [ ]) in parse document "" query `shouldParse` (defn :| [ ])
it "parses schema extension with a new directive" $ it "parses schema extension with a new directive" $
parse document "" `shouldSucceedOn`[r| parse document "" `shouldSucceedOn`[gql|
extend schema @newDirective extend schema @newDirective
|] |]
it "parses schema extension with an operation type definition" $ it "parses schema extension with an operation type definition" $
parse document "" `shouldSucceedOn` [r|extend schema { query: Query }|] parse document "" `shouldSucceedOn` [gql|extend schema { query: Query }|]
it "parses schema extension with an operation type and directive" $ it "parses schema extension with an operation type and directive" $
let newDirective = Directive "newDirective" [] $ Location 1 15 let newDirective = Directive "newDirective" [] $ Location 1 15
@ -185,25 +185,25 @@ spec = describe "Parser" $ do
$ OperationTypeDefinition Query "Query" :| [] $ OperationTypeDefinition Query "Query" :| []
testSchemaExtension = TypeSystemExtension schemaExtension testSchemaExtension = TypeSystemExtension schemaExtension
$ Location 1 1 $ Location 1 1
query = [r|extend schema @newDirective { query: Query }|] query = [gql|extend schema @newDirective { query: Query }|]
in parse document "" query `shouldParse` (testSchemaExtension :| []) in parse document "" query `shouldParse` (testSchemaExtension :| [])
it "parses an object extension" $ it "parses an object extension" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
extend type Story { extend type Story {
isHiddenLocally: Boolean isHiddenLocally: Boolean
} }
|] |]
it "rejects variables in DefaultValue" $ it "rejects variables in DefaultValue" $
parse document "" `shouldFailOn` [r| parse document "" `shouldFailOn` [gql|
query ($book: String = "Zarathustra", $author: String = $book) { query ($book: String = "Zarathustra", $author: String = $book) {
title title
} }
|] |]
it "parses documents beginning with a comment" $ it "parses documents beginning with a comment" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
""" """
Query Query
""" """
@ -213,7 +213,7 @@ spec = describe "Parser" $ do
|] |]
it "parses subscriptions" $ it "parses subscriptions" $
parse document "" `shouldSucceedOn` [r| parse document "" `shouldSucceedOn` [gql|
subscription NewMessages { subscription NewMessages {
newMessage(roomId: 123) { newMessage(roomId: 123) {
sender sender

View File

@ -12,11 +12,11 @@ import Data.Aeson (object, (.=))
import qualified Data.Aeson as Aeson import qualified Data.Aeson as Aeson
import qualified Data.HashMap.Strict as HashMap import qualified Data.HashMap.Strict as HashMap
import Language.GraphQL import Language.GraphQL
import Language.GraphQL.TH
import Language.GraphQL.Type import Language.GraphQL.Type
import qualified Language.GraphQL.Type.Out as Out import qualified Language.GraphQL.Type.Out as Out
import Test.Hspec (Spec, describe, it) import Test.Hspec (Spec, describe, it)
import Test.Hspec.GraphQL import Test.Hspec.GraphQL
import Text.RawString.QQ (r)
experimentalResolver :: Schema IO experimentalResolver :: Schema IO
experimentalResolver = schema queryType Nothing Nothing mempty experimentalResolver = schema queryType Nothing Nothing mempty
@ -33,7 +33,7 @@ spec :: Spec
spec = spec =
describe "Directive executor" $ do describe "Directive executor" $ do
it "should be able to @skip fields" $ do it "should be able to @skip fields" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
experimentalField @skip(if: true) experimentalField @skip(if: true)
} }
@ -43,7 +43,7 @@ spec =
actual `shouldResolveTo` emptyObject actual `shouldResolveTo` emptyObject
it "should not skip fields if @skip is false" $ do it "should not skip fields if @skip is false" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
experimentalField @skip(if: false) experimentalField @skip(if: false)
} }
@ -56,7 +56,7 @@ spec =
actual `shouldResolveTo` expected actual `shouldResolveTo` expected
it "should skip fields if @include is false" $ do it "should skip fields if @include is false" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
experimentalField @include(if: false) experimentalField @include(if: false)
} }
@ -66,7 +66,7 @@ spec =
actual `shouldResolveTo` emptyObject actual `shouldResolveTo` emptyObject
it "should be able to @skip a fragment spread" $ do it "should be able to @skip a fragment spread" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
...experimentalFragment @skip(if: true) ...experimentalFragment @skip(if: true)
} }
@ -80,7 +80,7 @@ spec =
actual `shouldResolveTo` emptyObject actual `shouldResolveTo` emptyObject
it "should be able to @skip an inline fragment" $ do it "should be able to @skip an inline fragment" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
... on Query @skip(if: true) { ... on Query @skip(if: true) {
experimentalField experimentalField

View File

@ -15,9 +15,9 @@ import Data.Text (Text)
import Language.GraphQL import Language.GraphQL
import Language.GraphQL.Type import Language.GraphQL.Type
import qualified Language.GraphQL.Type.Out as Out import qualified Language.GraphQL.Type.Out as Out
import Language.GraphQL.TH
import Test.Hspec (Spec, describe, it) import Test.Hspec (Spec, describe, it)
import Test.Hspec.GraphQL import Test.Hspec.GraphQL
import Text.RawString.QQ (r)
size :: (Text, Value) size :: (Text, Value)
size = ("size", String "L") size = ("size", String "L")
@ -34,16 +34,18 @@ garment typeName =
) )
inlineQuery :: Text inlineQuery :: Text
inlineQuery = [r|{ inlineQuery = [gql|
garment { {
... on Hat { garment {
circumference ... on Hat {
} circumference
... on Shirt { }
size ... on Shirt {
size
}
} }
} }
}|] |]
shirtType :: Out.ObjectType IO shirtType :: Out.ObjectType IO
shirtType = Out.ObjectType "Shirt" Nothing [] $ HashMap.fromList shirtType = Out.ObjectType "Shirt" Nothing [] $ HashMap.fromList
@ -106,12 +108,14 @@ spec = do
in actual `shouldResolveTo` expected in actual `shouldResolveTo` expected
it "embeds inline fragments without type" $ do it "embeds inline fragments without type" $ do
let sourceQuery = [r|{ let sourceQuery = [gql|
circumference {
... { circumference
size ... {
size
}
} }
}|] |]
actual <- graphql (toSchema "circumference" circumference) sourceQuery actual <- graphql (toSchema "circumference" circumference) sourceQuery
let expected = HashMap.singleton "data" let expected = HashMap.singleton "data"
$ Aeson.object $ Aeson.object
@ -121,16 +125,18 @@ spec = do
in actual `shouldResolveTo` expected in actual `shouldResolveTo` expected
it "evaluates fragments on Query" $ do it "evaluates fragments on Query" $ do
let sourceQuery = [r|{ let sourceQuery = [gql|
... { {
size ... {
size
}
} }
}|] |]
in graphql (toSchema "size" size) `shouldResolve` sourceQuery in graphql (toSchema "size" size) `shouldResolve` sourceQuery
describe "Fragment spread executor" $ do describe "Fragment spread executor" $ do
it "evaluates fragment spreads" $ do it "evaluates fragment spreads" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
...circumferenceFragment ...circumferenceFragment
} }
@ -148,7 +154,7 @@ spec = do
in actual `shouldResolveTo` expected in actual `shouldResolveTo` expected
it "evaluates nested fragments" $ do it "evaluates nested fragments" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
garment { garment {
...circumferenceFragment ...circumferenceFragment
@ -174,7 +180,7 @@ spec = do
in actual `shouldResolveTo` expected in actual `shouldResolveTo` expected
it "considers type condition" $ do it "considers type condition" $ do
let sourceQuery = [r| let sourceQuery = [gql|
{ {
garment { garment {
...circumferenceFragment ...circumferenceFragment

View File

@ -12,7 +12,7 @@ import Data.Aeson ((.=), object)
import qualified Data.HashMap.Strict as HashMap import qualified Data.HashMap.Strict as HashMap
import Language.GraphQL import Language.GraphQL
import Test.Hspec (Spec, describe, it) import Test.Hspec (Spec, describe, it)
import Text.RawString.QQ (r) import Language.GraphQL.TH
import Language.GraphQL.Type import Language.GraphQL.Type
import qualified Language.GraphQL.Type.Out as Out import qualified Language.GraphQL.Type.Out as Out
import Test.Hspec.GraphQL import Test.Hspec.GraphQL
@ -42,7 +42,7 @@ spec :: Spec
spec = spec =
describe "Root operation type" $ do describe "Root operation type" $ do
it "returns objects from the root resolvers" $ do it "returns objects from the root resolvers" $ do
let querySource = [r| let querySource = [gql|
{ {
garment { garment {
circumference circumference
@ -59,7 +59,7 @@ spec =
actual `shouldResolveTo` expected actual `shouldResolveTo` expected
it "chooses Mutation" $ do it "chooses Mutation" $ do
let querySource = [r| let querySource = [gql|
mutation { mutation {
incrementCircumference incrementCircumference
} }