Replace tasty and HUnit with Hspec

This commit is contained in:
Eugen Wissner 2019-07-10 05:57:35 +02:00
parent 61879fb124
commit eb40810f25
8 changed files with 293 additions and 303 deletions

View File

@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 20a76d38355648944315f3aa937e5cd72837bbd1b93037f53e2849906de3f2c0
-- hash: 2ad18ce352b6b5324c5e8c4fa3e7c1dc39022b209696d162367d13300c4046fd
name: graphql
version: 0.4.0.0
@ -65,11 +65,12 @@ library
test-suite tasty
type: exitcode-stdio-1.0
main-is: tasty.hs
main-is: Spec.hs
other-modules:
Language.GraphQL.LexerTest
Language.GraphQL.LexerSpec
Test.KitchenSinkSpec
Test.StarWars.Data
Test.StarWars.QueryTests
Test.StarWars.QuerySpec
Test.StarWars.Schema
Paths_graphql
hs-source-dirs:
@ -79,10 +80,10 @@ test-suite tasty
aeson
, base >=4.7 && <5
, graphql
, hspec
, hspec-expectations
, megaparsec
, raw-strings-qq
, tasty
, tasty-hunit
, text
, transformers
default-language: Haskell2010

View File

@ -41,7 +41,7 @@ library:
tests:
tasty:
main: tasty.hs
main: Spec.hs
source-dirs: tests
ghc-options:
- -threaded
@ -49,6 +49,6 @@ tests:
- -with-rtsopts=-N
dependencies:
- graphql
- hspec
- hspec-expectations
- raw-strings-qq
- tasty
- tasty-hunit

View File

@ -0,0 +1,99 @@
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Language.GraphQL.LexerSpec
( spec
) where
import Language.GraphQL.Lexer
import qualified Data.Text as T
import Data.Void (Void)
import Test.Hspec ( Spec
, context
, describe
, it
, shouldBe
)
import Text.Megaparsec ( ParseErrorBundle
, parse
)
import Text.RawString.QQ (r)
spec :: Spec
spec = describe "Lexer" $ do
context "Reference tests" $ do
it "lexes strings" $ do
runParser string [r|"simple"|] `shouldBe` Right "simple"
runParser string [r|" white space "|] `shouldBe` Right " white space "
runParser string [r|"quote \""|] `shouldBe` Right [r|quote "|]
runParser string [r|"escaped \n"|] `shouldBe` Right "escaped \n"
runParser string [r|"slashes \\ \/"|] `shouldBe` Right [r|slashes \ /|]
runParser string [r|"unicode \u1234\u5678\u90AB\uCDEF"|]
`shouldBe` Right "unicode ሴ噸邫췯"
it "lexes block string" $ do
runParser blockString [r|"""simple"""|] `shouldBe` Right "simple"
runParser blockString [r|""" white space """|]
`shouldBe` Right " white space "
runParser blockString [r|"""contains " quote"""|]
`shouldBe` Right [r|contains " quote|]
runParser blockString [r|"""contains \""" triplequote"""|]
`shouldBe` Right [r|contains """ triplequote|]
runParser blockString "\"\"\"multi\nline\"\"\"" `shouldBe` Right "multi\nline"
runParser blockString "\"\"\"multi\rline\r\nnormalized\"\"\""
`shouldBe` Right "multi\nline\nnormalized"
runParser blockString "\"\"\"multi\rline\r\nnormalized\"\"\""
`shouldBe` Right "multi\nline\nnormalized"
runParser blockString [r|"""unescaped \n\r\b\t\f\u1234"""|]
`shouldBe` Right [r|unescaped \n\r\b\t\f\u1234|]
runParser blockString [r|"""slashes \\ \/"""|]
`shouldBe` Right [r|slashes \\ \/|]
runParser blockString [r|"""
spans
multiple
lines
"""|] `shouldBe` Right "spans\n multiple\n lines"
it "lexes numbers" $ do
runParser integer "4" `shouldBe` Right (4 :: Int)
runParser float "4.123" `shouldBe` Right 4.123
runParser integer "-4" `shouldBe` Right (-4 :: Int)
runParser integer "9" `shouldBe` Right (9 :: Int)
runParser integer "0" `shouldBe` Right (0 :: Int)
runParser float "-4.123" `shouldBe` Right (-4.123)
runParser float "0.123" `shouldBe` Right 0.123
runParser float "123e4" `shouldBe` Right 123e4
runParser float "123E4" `shouldBe` Right 123E4
runParser float "123e-4" `shouldBe` Right 123e-4
runParser float "123e+4" `shouldBe` Right 123e+4
runParser float "-1.123e4" `shouldBe` Right (-1.123e4)
runParser float "-1.123E4" `shouldBe` Right (-1.123E4)
runParser float "-1.123e-4" `shouldBe` Right (-1.123e-4)
runParser float "-1.123e+4" `shouldBe` Right (-1.123e+4)
runParser float "-1.123e4567" `shouldBe` Right (-1.123e4567)
it "lexes punctuation" $ do
runParser bang "!" `shouldBe` Right '!'
runParser dollar "$" `shouldBe` Right '$'
runBetween parens "()" `shouldBe` Right ()
runParser spread "..." `shouldBe` Right "..."
runParser colon ":" `shouldBe` Right ":"
runParser equals "=" `shouldBe` Right "="
runParser at "@" `shouldBe` Right '@'
runBetween brackets "[]" `shouldBe` Right ()
runBetween braces "{}" `shouldBe` Right ()
runParser pipe "|" `shouldBe` Right "|"
context "Implementation tests" $ do
it "lexes empty block strings" $
runParser blockString [r|""""""|] `shouldBe` Right ""
it "lexes ampersand" $
runParser amp "&" `shouldBe` Right "&"
runParser :: forall a. Parser a -> T.Text -> Either (ParseErrorBundle T.Text Void) a
runParser = flip parse ""
runBetween :: (Parser () -> Parser ()) -> T.Text -> Either (ParseErrorBundle T.Text Void) ()
runBetween parser = parse (parser $ pure ()) ""

View File

@ -1,102 +0,0 @@
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Language.GraphQL.LexerTest
( implementation
, reference
) where
import Language.GraphQL.Lexer
import qualified Data.Text as T
import Data.Void (Void)
import Test.Tasty ( TestTree
, testGroup
)
import Test.Tasty.HUnit ( testCase
, (@?=)
)
import Text.Megaparsec ( ParseErrorBundle
, parse
)
import Text.RawString.QQ (r)
reference :: TestTree
reference = testGroup "Lexer"
[ testCase "lexes strings" $ do
runParser string [r|"simple"|] @?= Right "simple"
runParser string [r|" white space "|] @?= Right " white space "
runParser string [r|"quote \""|] @?= Right [r|quote "|]
runParser string [r|"escaped \n"|] @?= Right "escaped \n"
runParser string [r|"slashes \\ \/"|] @?= Right [r|slashes \ /|]
runParser string [r|"unicode \u1234\u5678\u90AB\uCDEF"|]
@?= Right "unicode ሴ噸邫췯"
, testCase "lexes block string" $ do
runParser blockString [r|"""simple"""|] @?= Right "simple"
runParser blockString [r|""" white space """|]
@?= Right " white space "
runParser blockString [r|"""contains " quote"""|]
@?= Right [r|contains " quote|]
runParser blockString [r|"""contains \""" triplequote"""|]
@?= Right [r|contains """ triplequote|]
runParser blockString "\"\"\"multi\nline\"\"\"" @?= Right "multi\nline"
runParser blockString "\"\"\"multi\rline\r\nnormalized\"\"\""
@?= Right "multi\nline\nnormalized"
runParser blockString "\"\"\"multi\rline\r\nnormalized\"\"\""
@?= Right "multi\nline\nnormalized"
runParser blockString [r|"""unescaped \n\r\b\t\f\u1234"""|]
@?= Right [r|unescaped \n\r\b\t\f\u1234|]
runParser blockString [r|"""slashes \\ \/"""|]
@?= Right [r|slashes \\ \/|]
runParser blockString [r|"""
spans
multiple
lines
"""|] @?= Right "spans\n multiple\n lines"
, testCase "lexes numbers" $ do
runParser integer "4" @?= Right (4 :: Int)
runParser float "4.123" @?= Right 4.123
runParser integer "-4" @?= Right (-4 :: Int)
runParser integer "9" @?= Right (9 :: Int)
runParser integer "0" @?= Right (0 :: Int)
runParser float "-4.123" @?= Right (-4.123)
runParser float "0.123" @?= Right 0.123
runParser float "123e4" @?= Right 123e4
runParser float "123E4" @?= Right 123E4
runParser float "123e-4" @?= Right 123e-4
runParser float "123e+4" @?= Right 123e+4
runParser float "-1.123e4" @?= Right (-1.123e4)
runParser float "-1.123E4" @?= Right (-1.123E4)
runParser float "-1.123e-4" @?= Right (-1.123e-4)
runParser float "-1.123e+4" @?= Right (-1.123e+4)
runParser float "-1.123e4567" @?= Right (-1.123e4567)
, testCase "lexes punctuation" $ do
runParser bang "!" @?= Right '!'
runParser dollar "$" @?= Right '$'
runBetween parens "()" @?= Right ()
runParser spread "..." @?= Right "..."
runParser colon ":" @?= Right ":"
runParser equals "=" @?= Right "="
runParser at "@" @?= Right '@'
runBetween brackets "[]" @?= Right ()
runBetween braces "{}" @?= Right ()
runParser pipe "|" @?= Right "|"
]
implementation :: TestTree
implementation = testGroup "Lexer"
[ testCase "lexes empty block strings" $
runParser blockString [r|""""""|] @?= Right ""
, testCase "lexes ampersand" $
runParser amp "&" @?= Right "&"
]
runParser :: forall a. Parser a -> T.Text -> Either (ParseErrorBundle T.Text Void) a
runParser = flip parse ""
runBetween :: (Parser () -> Parser ()) -> T.Text -> Either (ParseErrorBundle T.Text Void) ()
runBetween parser = parse (parser $ pure ()) ""

1
tests/Spec.hs Normal file
View File

@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

View File

@ -0,0 +1,29 @@
module Test.KitchenSinkSpec
( spec
) where
import qualified Data.Text.IO as Text.IO
import qualified Language.GraphQL.Encoder as Encoder
import qualified Language.GraphQL.Parser as Parser
import Paths_graphql (getDataFileName)
import Test.Hspec ( Spec
, describe
, it
)
import Test.Hspec.Expectations ( expectationFailure
, shouldBe
)
import Text.Megaparsec ( errorBundlePretty
, parse
)
spec :: Spec
spec = describe "Kitchen Sink" $
it "prints the query" $ do
dataFileName <- getDataFileName "tests/data/kitchen-sink.min.graphql"
expected <- Text.IO.readFile dataFileName
either
(expectationFailure . errorBundlePretty)
(flip shouldBe expected . Encoder.document)
$ parse Parser.document dataFileName expected

View File

@ -1,6 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Test.StarWars.QueryTests (test) where
module Test.StarWars.QuerySpec
( spec
) where
import qualified Data.Aeson as Aeson
import Data.Aeson ( object
@ -10,20 +12,22 @@ import Data.Text (Text)
import Language.GraphQL
import Language.GraphQL.Schema (Subs)
import Text.RawString.QQ (r)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit ( Assertion
, testCase
, (@?=)
import Test.Hspec.Expectations ( Expectation
, shouldBe
)
import Test.Hspec ( Spec
, describe
, it
)
import Test.StarWars.Schema
-- * Test
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsQueryTests.js
test :: TestTree
test = testGroup "Star Wars Query Tests"
[ testGroup "Basic Queries"
[ testCase "R2-D2 hero" . testQuery
spec :: Spec
spec = describe "Star Wars Query Tests" $ do
describe "Basic Queries" $ do
it "R2-D2 hero" $ testQuery
[r| query HeroNameQuery {
hero {
id
@ -31,7 +35,7 @@ test = testGroup "Star Wars Query Tests"
}
|]
$ object [ "data" .= object ["hero" .= object ["id" .= ("2001" :: Text)]]]
, testCase "R2-D2 ID and friends" . testQuery
it "R2-D2 ID and friends" $ testQuery
[r| query HeroNameAndFriendsQuery {
hero {
id
@ -43,19 +47,19 @@ test = testGroup "Star Wars Query Tests"
}
|]
$ object [ "data" .= object [
"hero" .= object [
"id" .= ("2001" :: Text)
"hero" .= object
[ "id" .= ("2001" :: Text)
, r2d2Name
, "friends" .= [
object [lukeName]
, "friends" .=
[ object [lukeName]
, object [hanName]
, object [leiaName]
]
]
]]
]
, testGroup "Nested Queries"
[ testCase "R2-D2 friends" . testQuery
describe "Nested Queries" $ do
it "R2-D2 friends" $ testQuery
[r| query NestedQuery {
hero {
name
@ -105,7 +109,7 @@ test = testGroup "Star Wars Query Tests"
]
]
]]
, testCase "Luke ID" . testQuery
it "Luke ID" $ testQuery
[r| query FetchLukeQuery {
human(id: "1000") {
name
@ -114,12 +118,10 @@ test = testGroup "Star Wars Query Tests"
|]
$ object [ "data" .= object [
"human" .= object [lukeName]
]
]]
, testCase "Luke ID with variable" . testQueryParams
(\v -> if v == "someId"
then Just "1000"
else Nothing)
it "Luke ID with variable" $ testQueryParams
(\v -> if v == "someId" then Just "1000" else Nothing)
[r| query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
@ -129,10 +131,8 @@ test = testGroup "Star Wars Query Tests"
$ object [ "data" .= object [
"human" .= object [lukeName]
]]
, testCase "Han ID with variable" . testQueryParams
(\v -> if v == "someId"
then Just "1002"
else Nothing)
it "Han ID with variable" $ testQueryParams
(\v -> if v == "someId" then Just "1002" else Nothing)
[r| query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
@ -142,17 +142,15 @@ test = testGroup "Star Wars Query Tests"
$ object [ "data" .= object [
"human" .= object [hanName]
]]
, testCase "Invalid ID" . testQueryParams
(\v -> if v == "id"
then Just "Not a valid ID"
else Nothing)
it "Invalid ID" $ testQueryParams
(\v -> if v == "id" then Just "Not a valid ID" else Nothing)
[r| query humanQuery($id: String!) {
human(id: $id) {
name
}
}
|] $ object ["data" .= object ["human" .= Aeson.Null]]
, testCase "Luke aliased" . testQuery
it "Luke aliased" $ testQuery
[r| query FetchLukeAliased {
luke: human(id: "1000") {
name
@ -162,7 +160,7 @@ test = testGroup "Star Wars Query Tests"
$ object [ "data" .= object [
"luke" .= object [lukeName]
]]
, testCase "R2-D2 ID and friends aliased" . testQuery
it "R2-D2 ID and friends aliased" $ testQuery
[r| query HeroNameAndFriendsQuery {
hero {
id
@ -184,7 +182,7 @@ test = testGroup "Star Wars Query Tests"
]
]
]]
, testCase "Luke and Leia aliased" . testQuery
it "Luke and Leia aliased" $ testQuery
[r| query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
name
@ -198,8 +196,9 @@ test = testGroup "Star Wars Query Tests"
"luke" .= object [lukeName]
, "leia" .= object [leiaName]
]]
, testGroup "Fragments for complex queries"
[ testCase "Aliases to query for duplicate content" . testQuery
describe "Fragments for complex queries" $ do
it "Aliases to query for duplicate content" $ testQuery
[r| query DuplicateFields {
luke: human(id: "1000") {
name
@ -215,7 +214,7 @@ test = testGroup "Star Wars Query Tests"
"luke" .= object [lukeName, tatooine]
, "leia" .= object [leiaName, alderaan]
]]
, testCase "Fragment for duplicate content" . testQuery
it "Fragment for duplicate content" $ testQuery
[r| query UseFragment {
luke: human(id: "1000") {
...HumanFragment
@ -233,9 +232,9 @@ test = testGroup "Star Wars Query Tests"
"luke" .= object [lukeName, tatooine]
, "leia" .= object [leiaName, alderaan]
]]
]
, testGroup "__typename"
[ testCase "R2D2 is a Droid" . testQuery
describe "__typename" $ do
it "R2D2 is a Droid" $ testQuery
[r| query CheckTypeOfR2 {
hero {
__typename
@ -246,7 +245,7 @@ test = testGroup "Star Wars Query Tests"
$ object ["data" .= object [
"hero" .= object ["__typename" .= ("Droid" :: Text), r2d2Name]
]]
, testCase "Luke is a human" . testQuery
it "Luke is a human" $ testQuery
[r| query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
@ -257,9 +256,9 @@ test = testGroup "Star Wars Query Tests"
$ object ["data" .= object [
"hero" .= object ["__typename" .= ("Human" :: Text), lukeName]
]]
]
, testGroup "Errors in resolvers"
[ testCase "error on secretBackstory" . testQuery
describe "Errors in resolvers" $ do
it "error on secretBackstory" $ testQuery
[r|
query HeroNameQuery {
hero {
@ -280,7 +279,7 @@ test = testGroup "Star Wars Query Tests"
["message" .= ("secretBackstory is secret." :: Text)]
]
]
, testCase "Error in a list" . testQuery
it "Error in a list" $ testQuery
[r| query HeroNameQuery {
hero {
name
@ -316,7 +315,7 @@ test = testGroup "Star Wars Query Tests"
, object ["message" .= ("secretBackstory is secret." :: Text)]
]
]
, testCase "error on secretBackstory with alias" . testQuery
it "error on secretBackstory with alias" $ testQuery
[r| query HeroNameQuery {
mainHero: hero {
name
@ -335,8 +334,7 @@ test = testGroup "Star Wars Query Tests"
[ object ["message" .= ("secretBackstory is secret." :: Text)]
]
]
]
]
where
lukeName = "name" .= ("Luke Skywalker" :: Text)
leiaName = "name" .= ("Leia Organa" :: Text)
@ -346,8 +344,8 @@ test = testGroup "Star Wars Query Tests"
tatooine = "homePlanet" .= ("Tatooine" :: Text)
alderaan = "homePlanet" .= ("Alderaan" :: Text)
testQuery :: Text -> Aeson.Value -> Assertion
testQuery q expected = graphql schema q >>= (@?= expected)
testQuery :: Text -> Aeson.Value -> Expectation
testQuery q expected = graphql schema q >>= flip shouldBe expected
testQueryParams :: Subs -> Text -> Aeson.Value -> Assertion
testQueryParams f q expected = graphqlSubs schema f q >>= (@?= expected)
testQueryParams :: Subs -> Text -> Aeson.Value -> Expectation
testQueryParams f q expected = graphqlSubs schema f q >>= flip shouldBe expected

View File

@ -1,36 +0,0 @@
module Main where
import qualified Data.Text.IO as T.IO
import qualified Language.GraphQL.Encoder as Encoder
import qualified Language.GraphQL.LexerTest as LexerTest
import qualified Language.GraphQL.Parser as Parser
import Text.Megaparsec ( errorBundlePretty
, parse
)
import Test.Tasty ( TestTree
, defaultMain
, testGroup
)
import Test.Tasty.HUnit ( assertEqual
, assertFailure
, testCase
)
import Paths_graphql (getDataFileName)
import qualified Test.StarWars.QueryTests as SW
main :: IO ()
main = defaultMain $ testGroup "Tests"
[ testGroup "Reference tests" [LexerTest.reference, SW.test]
, testGroup "Implementation tests" [LexerTest.implementation]
, kitchenTest
]
kitchenTest :: TestTree
kitchenTest = testCase "Kitchen Sink" $ do
dataFileName <- getDataFileName "tests/data/kitchen-sink.min.graphql"
expected <- T.IO.readFile dataFileName
either
(assertFailure . errorBundlePretty)
(assertEqual "Encode" expected . Encoder.document)
$ parse Parser.document dataFileName expected