2023-02-14 18:27:11 +03:00
|
|
|
module Format where
|
|
|
|
|
|
|
|
import Base
|
|
|
|
import Juvix.Compiler.Concrete qualified as Concrete
|
|
|
|
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.Scoping qualified as Scoper
|
|
|
|
import Juvix.Compiler.Concrete.Translation.FromSource qualified as Parser
|
|
|
|
import Juvix.Compiler.Pipeline.Setup
|
2023-11-03 14:51:45 +03:00
|
|
|
import Juvix.Formatter
|
2023-02-14 18:27:11 +03:00
|
|
|
|
|
|
|
data PosTest = PosTest
|
|
|
|
{ _name :: String,
|
|
|
|
_dir :: Path Abs Dir,
|
2023-11-03 14:51:45 +03:00
|
|
|
_file :: Path Abs File,
|
|
|
|
_expectedFile :: Maybe (Path Abs File),
|
|
|
|
_force :: Bool
|
2023-02-14 18:27:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''PosTest
|
|
|
|
|
|
|
|
root :: Path Abs Dir
|
|
|
|
root = relToProject $(mkRelDir "tests/positive")
|
|
|
|
|
2023-11-03 14:51:45 +03:00
|
|
|
posTest :: String -> Path Rel Dir -> Path Rel File -> Maybe (Path Rel File) -> Bool -> PosTest
|
|
|
|
posTest _name rdir rfile efile _force =
|
2023-02-14 18:27:11 +03:00
|
|
|
let _dir = root <//> rdir
|
|
|
|
_file = _dir <//> rfile
|
2023-11-03 14:51:45 +03:00
|
|
|
_expectedFile = (_dir <//>) <$> efile
|
2023-02-14 18:27:11 +03:00
|
|
|
in PosTest {..}
|
|
|
|
|
|
|
|
testDescr :: PosTest -> TestDescr
|
|
|
|
testDescr PosTest {..} =
|
|
|
|
TestDescr
|
|
|
|
{ _testName = _name,
|
|
|
|
_testRoot = _dir,
|
|
|
|
_testAssertion = Steps $ \step -> do
|
2023-04-13 12:27:39 +03:00
|
|
|
entryPoint <- defaultEntryPointCwdIO _file
|
2023-04-27 18:33:08 +03:00
|
|
|
let maybeFile = entryPoint ^? entryPointModulePaths . _head
|
|
|
|
f <- fromMaybeM (assertFailure "Not a module") (return maybeFile)
|
2023-11-03 14:51:45 +03:00
|
|
|
|
2023-04-27 18:33:08 +03:00
|
|
|
original :: Text <- readFile (toFilePath f)
|
2023-11-03 14:51:45 +03:00
|
|
|
|
2023-02-14 18:27:11 +03:00
|
|
|
step "Parsing"
|
2023-03-30 14:39:27 +03:00
|
|
|
p :: Parser.ParserResult <- snd <$> runIO' entryPoint upToParsing
|
2023-02-14 18:27:11 +03:00
|
|
|
|
|
|
|
step "Scoping"
|
|
|
|
s :: Scoper.ScoperResult <-
|
|
|
|
snd
|
|
|
|
<$> runIO'
|
|
|
|
entryPoint
|
|
|
|
( do
|
2023-10-03 19:09:13 +03:00
|
|
|
void (entrySetup defaultDependenciesConfig)
|
2023-02-14 18:27:11 +03:00
|
|
|
Concrete.fromParsed p
|
|
|
|
)
|
|
|
|
|
2023-11-03 14:51:45 +03:00
|
|
|
let formatted = formatScoperResult' _force original s
|
|
|
|
case _expectedFile of
|
|
|
|
Nothing -> do
|
|
|
|
step "Format"
|
|
|
|
assertEqDiffText "check: pretty . scope . parse = id" original formatted
|
|
|
|
Just eFile -> do
|
|
|
|
step "Checking against expected output file"
|
|
|
|
expFile :: Text <- readFile (toFilePath eFile)
|
|
|
|
assertEqDiffText "Compare to expected output" formatted expFile
|
2023-02-14 18:27:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
allTests :: TestTree
|
|
|
|
allTests =
|
|
|
|
testGroup
|
|
|
|
"Format positive tests"
|
|
|
|
(map (mkTest . testDescr) tests)
|
|
|
|
|
|
|
|
tests :: [PosTest]
|
|
|
|
tests =
|
|
|
|
[ posTest
|
|
|
|
"Format"
|
|
|
|
$(mkRelDir ".")
|
2023-11-03 14:51:45 +03:00
|
|
|
$(mkRelFile "Format.juvix")
|
|
|
|
Nothing
|
|
|
|
False,
|
2023-09-07 17:20:14 +03:00
|
|
|
posTest
|
2023-11-03 14:51:45 +03:00
|
|
|
"TrailingWhitespace"
|
2023-09-07 17:20:14 +03:00
|
|
|
$(mkRelDir ".")
|
2023-11-03 14:51:45 +03:00
|
|
|
$(mkRelFile "LocalModWithAxiom.juvix")
|
|
|
|
(Just $(mkRelFile "LocalModWithAxiom.juvix.formatted"))
|
|
|
|
True
|
2023-02-14 18:27:11 +03:00
|
|
|
]
|