2022-09-07 14:59:41 +03:00
|
|
|
module Parsing.Negative where
|
|
|
|
|
|
|
|
import Base
|
2022-11-07 16:47:56 +03:00
|
|
|
import Juvix.Compiler.Builtins (iniState)
|
2023-01-27 15:24:28 +03:00
|
|
|
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.PathResolver.Error
|
2022-09-07 14:59:41 +03:00
|
|
|
import Juvix.Compiler.Pipeline
|
|
|
|
import Juvix.Parser.Error
|
|
|
|
|
2022-12-20 15:05:40 +03:00
|
|
|
root :: Path Abs Dir
|
|
|
|
root = relToProject $(mkRelDir "tests/negative")
|
2022-09-07 14:59:41 +03:00
|
|
|
|
2023-01-27 15:24:28 +03:00
|
|
|
type FailMsg = String
|
|
|
|
|
2022-09-07 14:59:41 +03:00
|
|
|
data NegTest = NegTest
|
|
|
|
{ _name :: String,
|
2023-01-27 15:24:28 +03:00
|
|
|
_dir :: Path Abs Dir,
|
|
|
|
_file :: Path Abs File,
|
|
|
|
_checkErr :: ParserError -> Maybe FailMsg
|
2022-09-07 14:59:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
testDescr :: NegTest -> TestDescr
|
|
|
|
testDescr NegTest {..} =
|
2023-01-27 15:24:28 +03:00
|
|
|
let tRoot = _dir
|
2022-09-07 14:59:41 +03:00
|
|
|
in TestDescr
|
|
|
|
{ _testName = _name,
|
|
|
|
_testRoot = tRoot,
|
|
|
|
_testAssertion = Single $ do
|
2023-01-27 15:24:28 +03:00
|
|
|
let entryPoint = defaultEntryPoint tRoot _file
|
2022-11-07 16:47:56 +03:00
|
|
|
res <- runIOEither iniState entryPoint upToParsing
|
2022-09-07 14:59:41 +03:00
|
|
|
case mapLeft fromJuvixError res of
|
2023-01-27 15:24:28 +03:00
|
|
|
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."
|
2022-09-07 14:59:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
allTests :: TestTree
|
|
|
|
allTests =
|
|
|
|
testGroup
|
|
|
|
"Parsing negative tests"
|
2023-01-27 15:24:28 +03:00
|
|
|
( map (mkTest . testDescr) (parserErrorTests <> filesErrorTests)
|
2022-09-07 14:59:41 +03:00
|
|
|
)
|
|
|
|
|
2023-01-27 15:24:28 +03:00
|
|
|
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
|
2022-09-07 14:59:41 +03:00
|
|
|
"Tab character"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir ".")
|
|
|
|
$(mkRelFile "Tab.juvix")
|
2023-01-27 15:24:28 +03:00
|
|
|
$ \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
|
2022-09-07 14:59:41 +03:00
|
|
|
]
|