elna/tests/Language/Elna/ParserSpec.hs

59 lines
2.2 KiB
Haskell

module Language.Elna.ParserSpec
( spec
) where
import Test.Hspec (Spec, describe, it, pendingWith, xit)
import Test.Hspec.Megaparsec (shouldParse, shouldSucceedOn, parseSatisfies)
import Language.Elna.Parser (programP)
import Text.Megaparsec (parse)
import Language.Elna.AST
( Declaration(..)
, Parameter(..)
, Program(..)
, TypeExpression(..)
)
spec :: Spec
spec =
describe "programP" $ do
it "parses an empty main function" $
parse programP "" `shouldSucceedOn` "proc main() {}"
it "parses type definition for a type starting like array" $
let expected = Program [TypeDefinition "t" $ NamedType "arr"]
actual = parse programP "" "type t = arr"
in actual `shouldParse` expected
it "parses array type definition" $
let expected = Program [TypeDefinition "t" $ ArrayType (NamedType "integer") 10]
actual = parse programP "" "type t = array[10] of integer"
in actual `shouldParse` expected
it "parses parameters" $
let given = "proc main(x: integer) {}"
parameters = [Parameter "x" (NamedType "integer") False]
expected = Program [ProcedureDefinition "main" parameters [] []]
actual = parse programP "" given
in actual `shouldParse` expected
it "parses ref parameters" $
let given = "proc main(x: integer, ref y: boolean) {}"
parameters =
[ Parameter "x" (NamedType "integer") False
, Parameter "y" (NamedType "boolean") True
]
expected = Program [ProcedureDefinition "main" parameters [] []]
actual = parse programP "" given
in actual `shouldParse` expected
it "parses variable declaration" $
let given = "proc main() { var x: integer; }"
expected (Program [ProcedureDefinition _ _ variables _]) =
not $ null variables
expected _ = False
actual = parse programP "" given
in actual `parseSatisfies` expected
it "parses procedure body statements" $
pendingWith "Not implemented"