45 lines
1.4 KiB
Haskell
45 lines
1.4 KiB
Haskell
-- | Shared test utilities.
|
|
module FWL.Util where
|
|
|
|
import Test.Tasty.HUnit
|
|
import Text.Parsec.String (Parser)
|
|
import Text.Parsec (parse)
|
|
|
|
import FWL.Parser (parseProgram)
|
|
import FWL.AST
|
|
|
|
-- | Assert a parser succeeds and return the result.
|
|
shouldParse :: (Show a) => Parser a -> String -> IO a
|
|
shouldParse p input =
|
|
case parse p "<test>" input of
|
|
Left err -> assertFailure ("Unexpected parse error:\n" ++ show err)
|
|
>> undefined
|
|
Right v -> return v
|
|
|
|
-- | Assert a parser fails.
|
|
shouldFailParse :: (Show a) => Parser a -> String -> IO ()
|
|
shouldFailParse p input =
|
|
case parse p "<test>" input of
|
|
Left _ -> return ()
|
|
Right v -> assertFailure ("Expected parse failure but got: " ++ show v)
|
|
|
|
-- | Parse a full program, asserting success.
|
|
parseOk :: String -> IO Program
|
|
parseOk src =
|
|
case parseProgram "<test>" src of
|
|
Left err -> assertFailure ("Parse error:\n" ++ show err) >> undefined
|
|
Right p -> return p
|
|
|
|
-- | Parse a full program, asserting failure.
|
|
parseFail :: String -> IO ()
|
|
parseFail src =
|
|
case parseProgram "<test>" src of
|
|
Left _ -> return ()
|
|
Right p -> assertFailure ("Expected parse failure, got:\n" ++ show p)
|
|
|
|
-- | Extract the single declaration from a one-decl program.
|
|
singleDecl :: Program -> IO Decl
|
|
singleDecl (Program _ [d]) = return d
|
|
singleDecl (Program _ ds) =
|
|
assertFailure ("Expected 1 decl, got " ++ show (length ds)) >> undefined
|