freec-unit-tests

Safe HaskellSafe
LanguageHaskell2010

FreeC.Test.Parser

Contents

Description

This module contains utility functions for tests that have to parse the intermediate representation.

There is one general function that works with any AST node that has a Parseable instance as well as a specialized function for each AST node with a Parseable instance. The specialized functions are mainly to be used for documentation purposes and to avoid expression type signatures in places where Haskell cannot infer the type of the node to parse.

There are two versions of the parsing functions. The functions with parseTest prefix parse the input string and report errors to an arbitrary MonadReporter. The functions with expectParseTest prefix, on the other hand, set the expectation that parsing is successful. If there is a parse error, an assertion failure will cause the test to fail immediately. The latter kind of functions is supposed to be used for setup actions. The example below illustrates how to use these functions.

it "..." $ do
  expr <- expectParseTestExpr "..."
  foo expr `shouldReturn` something

This pattern is especially important when testing for failures. In the following example, we cannot distinguish whether foo failed or we have a typo in our test expression.

it "..." $ shouldFail $ do
  expr <- parseTestExpr "..."
  foo expr

Thus, we should rewrite such tests as follows (if possible).

it "..." $ do
  expr <- expectParseTestExpr "..."
  shouldFail (foo expr)
Synopsis

Within Reporters

parseTestIR :: (MonadReporter r, Parseable a) => String -> r a Source #

Parses an IR node of type a for testing purposes.

parseTestName :: MonadReporter r => String -> r Name Source #

Parses an IR name for testing purposes.

parseTestQName :: MonadReporter r => String -> r QName Source #

Parses a qualifiable IR name for testing purposes.

parseTestType :: MonadReporter r => String -> r Type Source #

Parses an IR type expression for testing purposes.

parseTestTypeScheme :: MonadReporter r => String -> r TypeScheme Source #

Parses an IR type scheme for testing purposes.

parseTestTypeDecl :: MonadReporter r => String -> r TypeDecl Source #

Parses an IR type declaration for testing purposes.

parseTestTypeSig :: MonadReporter r => String -> r TypeSig Source #

Parses an IR type declaration for testing purposes.

parseTestExpr :: MonadReporter r => String -> r Expr Source #

Parses an IR expression for testing purposes.

parseTestFuncDecl :: MonadReporter r => String -> r FuncDecl Source #

Parses an IR function declaration for testing purposes.

parseTestImportDecl :: MonadReporter r => String -> r ImportDecl Source #

Parses an IR import declaration for testing purposes.

parseTestModule :: MonadReporter r => [String] -> r Module Source #

Parses an IR module for testing purposes.

Within Expectations

expectParseTestIR :: (MonadIO m, Parseable a) => String -> String -> m a Source #

Parses an IR node of type a for testing purposes and sets the expectation that parsing is successful.

The first argument is a textual description of the type of node to parse.

expectParseTestName :: MonadIO m => String -> m Name Source #

Parses an IR name for testing purposes and sets the expectation that parsing is successful.

expectParseTestQName :: MonadIO m => String -> m QName Source #

Parses a qualifiable IR name for testing purposes and sets the expectation that parsing is successful.

expectParseTestType :: MonadIO m => String -> m Type Source #

Parses an IR type expression for testing purposes and sets the expectation that parsing is successful.

expectParseTestTypeScheme :: MonadIO m => String -> m TypeScheme Source #

Parses an IR type scheme for testing purposes and sets the expectation that parsing is successful.

expectParseTestTypeDecl :: MonadIO m => String -> m TypeDecl Source #

Parses an IR type declaration for testing purposes and sets the expectation that parsing is successful.

expectParseTestTypeSig :: MonadIO m => String -> m TypeSig Source #

Parses an IR type declaration for testing purposes and sets the expectation that parsing is successful.

expectParseTestExpr :: MonadIO m => String -> m Expr Source #

Parses an IR expression for testing purposes and sets the expectation that parsing is successful.

expectParseTestFuncDecl :: MonadIO m => String -> m FuncDecl Source #

Parses an IR function declaration for testing purposes and sets the expectation that parsing is successful.

expectParseTestImportDecl :: MonadIO m => String -> m ImportDecl Source #

Parses an IR import declaration for testing purposes and sets the expectation that parsing is successful.

expectParseTestModule :: MonadIO m => [String] -> m Module Source #

Parses an IR module for testing purposes and sets the expectation that parsing is successful.

Parsing Dependency Components

parseTestComponent :: (MonadFail r, MonadReporter r, Parseable decl) => DependencyComponent String -> r (DependencyComponent decl) Source #

Parses the declarations in the given dependency component.

expectParseTestComponent :: (MonadFail m, MonadIO m, Parseable decl) => DependencyComponent String -> m (DependencyComponent decl) Source #

Parses the declarations in the given dependency component and sets the expectation that parsing is successful.