1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-13 11:16:48 +03:00
juvix/test/Parsing/Negative.hs
janmasrovira 447f2f1dcf
Keep regular comments in html output (#1766)
- Fixes #1723 
- It refactors parsing/scoping so that the scoper does not need to read
files or parse any module. Instead, the parser takes care of parsing all
the imported modules transitively.
2023-01-27 13:24:28 +01:00

85 lines
2.3 KiB
Haskell

module Parsing.Negative where
import Base
import Juvix.Compiler.Builtins (iniState)
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.PathResolver.Error
import Juvix.Compiler.Pipeline
import Juvix.Parser.Error
root :: Path Abs Dir
root = relToProject $(mkRelDir "tests/negative")
type FailMsg = String
data NegTest = NegTest
{ _name :: String,
_dir :: Path Abs Dir,
_file :: Path Abs File,
_checkErr :: ParserError -> Maybe FailMsg
}
testDescr :: NegTest -> TestDescr
testDescr NegTest {..} =
let tRoot = _dir
in TestDescr
{ _testName = _name,
_testRoot = tRoot,
_testAssertion = Single $ do
let entryPoint = defaultEntryPoint tRoot _file
res <- runIOEither iniState entryPoint upToParsing
case mapLeft fromJuvixError res of
Left (Just parErr) -> whenJust (_checkErr parErr) assertFailure
Left Nothing -> assertFailure "An error ocurred but it was not in the parser."
Right _ -> assertFailure "The parser did not find an error."
}
allTests :: TestTree
allTests =
testGroup
"Parsing negative tests"
( map (mkTest . testDescr) (parserErrorTests <> filesErrorTests)
)
wrongError :: Maybe FailMsg
wrongError = Just "Incorrect error"
negTest :: String -> Path Rel Dir -> Path Rel File -> (ParserError -> Maybe FailMsg) -> NegTest
negTest _name d f _checkErr =
let _dir = root <//> d
in NegTest
{ _file = _dir <//> f,
_dir,
_name,
_checkErr
}
parserErrorTests :: [NegTest]
parserErrorTests =
[ negTest
"Tab character"
$(mkRelDir ".")
$(mkRelFile "Tab.juvix")
$ \case
ErrMegaparsec {} -> Nothing
_ -> wrongError
]
filesErrorTests :: [NegTest]
filesErrorTests =
[ negTest
"Importing a module that conflicts with a module in the stdlib"
$(mkRelDir "StdlibConflict")
$(mkRelFile "Input.juvix")
$ \case
ErrTopModulePath
TopModulePathError {_topModulePathError = ErrDependencyConflict {}} -> Nothing
_ -> wrongError,
negTest
"Incorrect top module path"
$(mkRelDir ".")
$(mkRelFile "WrongModuleName.juvix")
$ \case
ErrWrongTopModuleName {} -> Nothing
_ -> wrongError
]