summaryrefslogtreecommitdiff
path: root/tests/Language/GraphQL/AST/EncoderSpec.hs
blob: 862fa349c7c785ac7a5b6800f25453cf9cd4f127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Language.GraphQL.AST.EncoderSpec
    ( spec
    ) where

import Data.List.NonEmpty (NonEmpty(..))
import qualified Language.GraphQL.AST.Document as Full
import Language.GraphQL.AST.Encoder
import Language.GraphQL.TH
import Test.Hspec (Spec, context, describe, it, shouldBe, shouldStartWith, shouldEndWith, shouldNotContain)
import Test.QuickCheck (choose, oneof, forAll)
import qualified Data.Text.Lazy as Text.Lazy

spec :: Spec
spec = do
    describe "value" $ do
        context "null value" $ do
            let testNull formatter = value formatter Full.Null `shouldBe` "null"
            it "minified" $ testNull minified
            it "pretty" $ testNull pretty

        context "minified" $ do
            it "escapes \\" $
                value minified (Full.String "\\") `shouldBe` "\"\\\\\""
            it "escapes double quotes" $
                value minified (Full.String "\"") `shouldBe` "\"\\\"\""
            it "escapes \\f" $
                value minified (Full.String "\f") `shouldBe` "\"\\f\""
            it "escapes \\n" $
                value minified (Full.String "\n") `shouldBe` "\"\\n\""
            it "escapes \\r" $
                value minified (Full.String "\r") `shouldBe` "\"\\r\""
            it "escapes \\t" $
                value minified (Full.String "\t") `shouldBe` "\"\\t\""
            it "escapes backspace" $
                value minified (Full.String "a\bc") `shouldBe` "\"a\\bc\""
            context "escapes Unicode for chars less than 0010" $ do
                it "Null" $ value minified (Full.String "\x0000") `shouldBe` "\"\\u0000\""
                it "bell" $ value minified (Full.String "\x0007") `shouldBe` "\"\\u0007\""
            context "escapes Unicode for char less than 0020" $ do
                it "DLE" $ value minified (Full.String "\x0010") `shouldBe` "\"\\u0010\""
                it "EM" $ value minified (Full.String "\x0019") `shouldBe` "\"\\u0019\""
            context "encodes without escape" $ do
                it "space" $ value minified (Full.String "\x0020") `shouldBe` "\" \""
                it "~" $ value minified (Full.String "\x007E") `shouldBe` "\"~\""

        context "pretty" $ do
            it "uses strings for short string values" $
                value pretty (Full.String "Short text") `shouldBe` "\"Short text\""
            it "uses block strings for text with new lines, with newline symbol" $
                let expected = [gql|
                  """
                    Line 1
                    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" $
                let expected = [gql|
                  """
                    Line 1
                    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" $
                let expected = [gql|
                  """
                    Line 1
                    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
                let
                  genNotAllowedSymbol = oneof
                    [ choose ('\x0000', '\x0008')
                    , choose ('\x000B', '\x000C')
                    , choose ('\x000E', '\x001F')
                    , pure '\x007F'
                    ]

                forAll genNotAllowedSymbol $ \x -> do
                    let
                      rawValue = "Short \n" <> Text.Lazy.cons x "text"
                      encoded = value pretty
                          $ Full.String $ Text.Lazy.toStrict rawValue
                    shouldStartWith (Text.Lazy.unpack encoded) "\""
                    shouldEndWith (Text.Lazy.unpack encoded) "\""
                    shouldNotContain (Text.Lazy.unpack encoded) "\"\"\""

            it "Hello world" $
                let actual = value pretty
                        $ Full.String "Hello,\n  World!\n\nYours,\n  GraphQL."
                    expected = [gql|
                      """
                        Hello,
                          World!

                        Yours,
                          GraphQL.
                      """
                    |]
                  in actual `shouldBe` expected

            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" $
                let actual = value pretty $ Full.String "a\n\n"
                    expected = [gql|
                      """
                        a


                      """|]
                 in actual `shouldBe` expected
            it "has newlines and one symbol at the end" $
                let actual = value pretty $ Full.String "\n\na"
                    expected = [gql|
                      """


                        a
                      """
                    |]
                 in actual `shouldBe` expected
            it "has newlines and one symbol in the middle" $
                let actual = value pretty $ Full.String "\na\n"
                    expected = [gql|
                      """

                        a

                      """
                    |]
                 in actual `shouldBe` expected
            it "skip trailing whitespaces" $
                let actual = value pretty $ Full.String "  Short\ntext    "
                    expected = [gql|
                      """
                        Short
                        text
                      """
                    |]
                 in actual `shouldBe` expected

    describe "definition" $
        it "indents block strings in arguments" $
            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
                expected = Text.Lazy.snoc [gql|
                  {
                    field(message: """
                      line1
                      line2
                    """)
                  }
                |] '\n'
                actual = definition pretty operation
             in actual `shouldBe` expected

    describe "operationType" $
        it "produces lowercase mutation operation type" $
            let actual = operationType pretty Full.Mutation
             in actual `shouldBe` "mutation"

    describe "typeSystemDefinition" $ do
        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
                expected = Text.Lazy.snoc [gql|
                  schema {
                    query: QueryRootType
                    mutation: MutationType
                  }
                |] '\n'
                actual = typeSystemDefinition pretty definition'
             in actual `shouldBe` expected

        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

        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
                expected = [gql|
                  interface UUID {
                    value(arg: String): String
                  }
                |]
                actual = typeSystemDefinition pretty definition'
             in actual `shouldBe` expected