1
1
mirror of https://github.com/anoma/juvix.git synced 2025-01-07 08:08:44 +03:00
juvix/test/Scope/Positive.hs

213 lines
5.9 KiB
Haskell
Raw Normal View History

2022-02-18 15:01:42 +03:00
module Scope.Positive where
import Base
2022-04-05 20:57:21 +03:00
import Data.HashMap.Strict qualified as HashMap
import Juvix.Compiler.Concrete qualified as Concrete
import Juvix.Compiler.Concrete.Extra
import Juvix.Compiler.Concrete.Pretty qualified as M
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.Scoping qualified as Scoper
import Juvix.Compiler.Concrete.Translation.FromSource qualified as Parser
import Juvix.Compiler.Pipeline
import Juvix.Compiler.Pipeline.Setup
import Juvix.Prelude.Pretty
2022-02-18 15:01:42 +03:00
2022-04-05 20:57:21 +03:00
data PosTest = PosTest
2022-04-07 19:10:53 +03:00
{ _name :: String,
_relDir :: FilePath,
_stdlibMode :: StdlibMode,
2022-04-07 19:10:53 +03:00
_file :: FilePath
2022-02-18 15:01:42 +03:00
}
2022-04-07 19:10:53 +03:00
makeLenses ''PosTest
2022-02-18 15:01:42 +03:00
root :: FilePath
root = "tests/positive"
renderCode :: M.PrettyCode c => c -> Text
renderCode = prettyText . M.ppOutDefault
2022-02-18 15:01:42 +03:00
testDescr :: PosTest -> TestDescr
2022-04-05 20:57:21 +03:00
testDescr PosTest {..} =
2022-04-08 13:46:37 +03:00
let tRoot = root </> _relDir
2022-04-07 19:10:53 +03:00
in TestDescr
{ _testName = _name,
2022-04-08 13:46:37 +03:00
_testRoot = tRoot,
2022-04-07 19:10:53 +03:00
_testAssertion = Steps $ \step -> do
cwd <- getCurrentDirectory
entryFile <- canonicalizePath _file
let noStdlib = _stdlibMode == StdlibExclude
entryPoint =
2022-08-19 17:57:07 +03:00
(defaultEntryPoint entryFile)
{ _entryPointRoot = cwd,
2022-08-19 17:57:07 +03:00
_entryPointNoStdlib = noStdlib
}
stdlibMap :: HashMap FilePath Text
stdlibMap = HashMap.mapKeys (cwd </>) (HashMap.fromList stdlibDir)
unionStdlib :: HashMap FilePath Text -> HashMap FilePath Text
unionStdlib fs
| noStdlib = fs
| otherwise = HashMap.union fs stdlibMap
2022-04-07 19:10:53 +03:00
step "Parsing"
2022-09-14 17:16:15 +03:00
p :: Parser.ParserResult <- runIO' entryPoint upToParsing
2022-04-07 19:10:53 +03:00
let p2 = head (p ^. Parser.resultModules)
step "Scoping"
s :: Scoper.ScoperResult <-
runIO'
2022-09-14 17:16:15 +03:00
entryPoint
( do
2022-09-14 17:16:15 +03:00
void entrySetup
Concrete.fromParsed p
)
2022-04-07 19:10:53 +03:00
let s2 = head (s ^. Scoper.resultModules)
fs :: HashMap FilePath Text
2022-04-07 19:10:53 +03:00
fs =
unionStdlib
( HashMap.fromList
[ (getModuleFileAbsPath cwd m, renderCode m)
| m <- toList (getAllModules s2)
]
)
2022-04-07 19:10:53 +03:00
let scopedPretty = renderCode s2
parsedPretty = renderCode p2
2022-04-07 19:10:53 +03:00
step "Parsing pretty scoped"
let fs2 = unionStdlib (HashMap.singleton entryFile scopedPretty)
p' :: Parser.ParserResult <-
2022-09-14 17:16:15 +03:00
(runM . runErrorIO' @JuvixError . runNameIdGen . runFilesPure fs2 . runReader entryPoint)
upToParsing
2022-04-07 19:10:53 +03:00
step "Parsing pretty parsed"
let fs3 = unionStdlib (HashMap.singleton entryFile parsedPretty)
parsedPretty' :: Parser.ParserResult <-
2022-09-14 17:16:15 +03:00
(runM . runErrorIO' @JuvixError . runNameIdGen . runFilesPure fs3 . runReader entryPoint)
upToParsing
2022-04-07 19:10:53 +03:00
step "Scoping the scoped"
s' :: Scoper.ScoperResult <-
2022-09-14 17:16:15 +03:00
(runM . runErrorIO' @JuvixError . runNameIdGen . runFilesPure fs . runReader entryPoint)
upToScoping
2022-04-07 19:10:53 +03:00
step "Checks"
2022-04-08 13:46:37 +03:00
let smodules = s ^. Scoper.resultModules
smodules' = s' ^. Scoper.resultModules
2022-04-08 13:46:37 +03:00
let pmodules = p ^. Parser.resultModules
pmodules' = p' ^. Parser.resultModules
parsedPrettyModules = parsedPretty' ^. Parser.resultModules
2022-04-08 13:46:37 +03:00
assertEqDiff "check: scope . parse . pretty . scope . parse = scope . parse" smodules smodules'
assertEqDiff "check: parse . pretty . scope . parse = parse" pmodules pmodules'
assertEqDiff "check: parse . pretty . parse = parse" pmodules parsedPrettyModules
2022-04-07 19:10:53 +03:00
}
2022-02-18 15:01:42 +03:00
allTests :: TestTree
2022-04-05 20:57:21 +03:00
allTests =
testGroup
"Scope positive tests"
(map (mkTest . testDescr) tests)
2022-02-18 15:01:42 +03:00
tests :: [PosTest]
2022-04-05 20:57:21 +03:00
tests =
[ PosTest
"Inductive"
"."
StdlibInclude
"Inductive.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Imports and qualified names"
"Imports"
StdlibInclude
"A.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Data.Bool from the stdlib"
"StdlibList"
StdlibExclude
"Data/Bool.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Data.Nat from the stdlib"
"StdlibList"
StdlibExclude
"Data/Nat.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Data.Ord from the stdlib"
"StdlibList"
StdlibExclude
"Data/Ord.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Data.Product from the stdlib"
"StdlibList"
StdlibExclude
"Data/Product.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Data.List and friends from the stdlib"
"StdlibList"
StdlibExclude
"Data/List.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Operators (+)"
"."
StdlibExclude
"Operators.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Literals"
"."
StdlibExclude
"Literals.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Axiom with backends"
"."
StdlibExclude
"Axiom.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Foreign block parsing"
"."
StdlibExclude
"Foreign.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Multiple modules non-ambiguous symbol - same file"
"QualifiedSymbol"
StdlibExclude
"M.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Multiple modules non-ambiguous symbol"
"QualifiedSymbol2"
StdlibExclude
"N.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"Multiple modules constructor non-ambiguous symbol"
"QualifiedConstructor"
StdlibExclude
"M.juvix",
PosTest
"Parsing"
"."
StdlibExclude
"Parsing.juvix",
2022-04-05 20:57:21 +03:00
PosTest
"open overrides open public"
"."
StdlibExclude
"ShadowPublicOpen.juvix",
PosTest
"Import embedded standard library"
"StdlibImport"
StdlibInclude
"StdlibImport.juvix",
PosTest
"Check Valid Symbols"
""
StdlibInclude
"Symbols.juvix",
PosTest
"Builtin bool"
"."
StdlibExclude
"BuiltinsBool.juvix"
2022-04-05 20:57:21 +03:00
]