graphql/tests/Language/GraphQL/AST/LexerSpec.hs

100 lines
4.5 KiB
Haskell
Raw Normal View History

2019-07-10 05:57:35 +02:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Language.GraphQL.AST.LexerSpec
2019-07-10 05:57:35 +02:00
( spec
) where
2019-07-22 05:50:00 +02:00
import Data.Text (Text)
2019-07-10 05:57:35 +02:00
import Data.Void (Void)
import Language.GraphQL.AST.Lexer
2021-09-22 08:50:20 +02:00
import Language.GraphQL.TH
2019-09-27 10:50:38 +02:00
import Test.Hspec (Spec, context, describe, it)
2020-01-25 16:37:17 +01:00
import Test.Hspec.Megaparsec (shouldParse, shouldFailOn, shouldSucceedOn)
2019-09-27 10:50:38 +02:00
import Text.Megaparsec (ParseErrorBundle, parse)
2019-07-10 05:57:35 +02:00
spec :: Spec
spec = describe "Lexer" $ do
context "Reference tests" $ do
2019-07-22 05:50:00 +02:00
it "accepts BOM header" $
2019-09-27 10:50:38 +02:00
parse unicodeBOM "" `shouldSucceedOn` "\xfeff"
2019-07-22 05:50:00 +02:00
2019-07-10 05:57:35 +02:00
it "lexes strings" $ do
2021-09-22 08:50:20 +02:00
parse string "" [gql|"simple"|] `shouldParse` "simple"
parse string "" [gql|" white space "|] `shouldParse` " white space "
parse string "" [gql|"quote \""|] `shouldParse` [gql|quote "|]
parse string "" [gql|"escaped \n"|] `shouldParse` "escaped \n"
parse string "" [gql|"slashes \\ \/"|] `shouldParse` [gql|slashes \ /|]
parse string "" [gql|"unicode \u1234\u5678\u90AB\uCDEF"|]
2019-09-27 10:50:38 +02:00
`shouldParse` "unicode ሴ噸邫췯"
2019-07-10 05:57:35 +02:00
it "lexes block string" $ do
2021-09-22 08:50:20 +02:00
parse blockString "" [gql|"""simple"""|] `shouldParse` "simple"
parse blockString "" [gql|""" white space """|]
2019-09-27 10:50:38 +02:00
`shouldParse` " white space "
2021-09-22 08:50:20 +02:00
parse blockString "" [gql|"""contains " quote"""|]
`shouldParse` [gql|contains " quote|]
parse blockString "" [gql|"""contains \""" triplequote"""|]
`shouldParse` [gql|contains """ triplequote|]
2019-09-27 10:50:38 +02:00
parse blockString "" "\"\"\"multi\nline\"\"\"" `shouldParse` "multi\nline"
parse blockString "" "\"\"\"multi\rline\r\nnormalized\"\"\""
`shouldParse` "multi\nline\nnormalized"
parse blockString "" "\"\"\"multi\rline\r\nnormalized\"\"\""
`shouldParse` "multi\nline\nnormalized"
2021-09-22 08:50:20 +02:00
parse blockString "" [gql|"""unescaped \n\r\b\t\f\u1234"""|]
`shouldParse` [gql|unescaped \n\r\b\t\f\u1234|]
parse blockString "" [gql|"""slashes \\ \/"""|]
`shouldParse` [gql|slashes \\ \/|]
parse blockString "" [gql|"""
2019-07-10 05:57:35 +02:00
spans
multiple
lines
2019-09-27 10:50:38 +02:00
"""|] `shouldParse` "spans\n multiple\n lines"
2019-07-10 05:57:35 +02:00
it "lexes numbers" $ do
2019-09-27 10:50:38 +02:00
parse integer "" "4" `shouldParse` (4 :: Int)
parse float "" "4.123" `shouldParse` 4.123
parse integer "" "-4" `shouldParse` (-4 :: Int)
parse integer "" "9" `shouldParse` (9 :: Int)
parse integer "" "0" `shouldParse` (0 :: Int)
parse float "" "-4.123" `shouldParse` (-4.123)
parse float "" "0.123" `shouldParse` 0.123
parse float "" "123e4" `shouldParse` 123e4
parse float "" "123E4" `shouldParse` 123E4
parse float "" "123e-4" `shouldParse` 123e-4
parse float "" "123e+4" `shouldParse` 123e+4
parse float "" "-1.123e4" `shouldParse` (-1.123e4)
parse float "" "-1.123E4" `shouldParse` (-1.123E4)
parse float "" "-1.123e-4" `shouldParse` (-1.123e-4)
parse float "" "-1.123e+4" `shouldParse` (-1.123e+4)
parse float "" "-1.123e4567" `shouldParse` (-1.123e4567)
2019-07-10 05:57:35 +02:00
it "lexes punctuation" $ do
parse bang "" "!" `shouldParse` "!"
parse dollar "" "$" `shouldParse` "$"
2019-09-27 10:50:38 +02:00
runBetween parens `shouldSucceedOn` "()"
parse spread "" "..." `shouldParse` "..."
parse colon "" `shouldSucceedOn` ":"
2019-09-27 10:50:38 +02:00
parse equals "" "=" `shouldParse` "="
parse at "" `shouldSucceedOn` "@"
2019-09-27 10:50:38 +02:00
runBetween brackets `shouldSucceedOn` "[]"
runBetween braces `shouldSucceedOn` "{}"
parse pipe "" "|" `shouldParse` "|"
2019-07-10 05:57:35 +02:00
context "Implementation tests" $ do
it "lexes empty block strings" $
2021-09-22 08:50:20 +02:00
parse blockString "" [gql|""""""|] `shouldParse` ""
2019-07-10 05:57:35 +02:00
it "lexes ampersand" $
2019-09-27 10:50:38 +02:00
parse amp "" "&" `shouldParse` "&"
it "lexes schema extensions" $
2020-01-28 11:08:28 +01:00
parseExtend "schema" `shouldSucceedOn` "extend schema"
2020-01-25 16:37:17 +01:00
it "fails if the given token doesn't match" $
2020-01-28 11:08:28 +01:00
parseExtend "schema" `shouldFailOn` "extend shema"
parseExtend :: Text -> (Text -> Either (ParseErrorBundle Text Void) ())
parseExtend extension = parse (extend extension "" $ pure $ pure ()) ""
2019-07-10 05:57:35 +02:00
2019-07-22 05:50:00 +02:00
runBetween :: (Parser () -> Parser ()) -> Text -> Either (ParseErrorBundle Text Void) ()
2019-07-10 05:57:35 +02:00
runBetween parser = parse (parser $ pure ()) ""