2019-10-31 21:17:45 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, TypeApplications, TypeOperators #-}
|
2019-08-13 21:01:50 +03:00
|
|
|
|
|
|
|
module Main (main) where
|
|
|
|
|
2019-10-30 22:13:34 +03:00
|
|
|
import Analysis.Concrete (Concrete)
|
|
|
|
import qualified Analysis.Concrete as Concrete
|
2019-10-11 19:37:11 +03:00
|
|
|
import Analysis.File
|
2019-10-10 22:07:49 +03:00
|
|
|
import Analysis.ScopeGraph
|
2019-11-09 06:37:17 +03:00
|
|
|
import Control.Algebra
|
|
|
|
import Control.Carrier.Fail.Either
|
|
|
|
import Control.Carrier.Reader
|
2019-08-16 19:34:56 +03:00
|
|
|
import Control.Monad hiding (fail)
|
2019-08-16 20:20:08 +03:00
|
|
|
import Control.Monad.Catch
|
2019-08-13 21:01:50 +03:00
|
|
|
import Control.Monad.IO.Class
|
2019-10-10 22:07:49 +03:00
|
|
|
import Core.Core
|
2019-10-11 19:23:13 +03:00
|
|
|
import qualified Core.Eval as Eval
|
2019-10-10 22:07:49 +03:00
|
|
|
import Core.Name
|
2019-10-30 22:13:34 +03:00
|
|
|
import qualified Core.Parser
|
|
|
|
import Core.Pretty
|
2019-08-16 20:20:08 +03:00
|
|
|
import qualified Data.Aeson as Aeson
|
|
|
|
import qualified Data.Aeson.Encode.Pretty as Aeson
|
2019-08-16 19:34:56 +03:00
|
|
|
import qualified Data.ByteString.Char8 as ByteString
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy
|
2019-08-16 20:20:08 +03:00
|
|
|
import qualified Data.ByteString.Streaming.Char8 as ByteStream
|
2019-08-13 21:01:50 +03:00
|
|
|
import Data.Foldable
|
2019-08-16 20:20:08 +03:00
|
|
|
import Data.Function
|
2019-10-30 22:13:34 +03:00
|
|
|
import qualified Data.IntMap as IntMap
|
2019-08-29 16:32:39 +03:00
|
|
|
import Data.List (sort)
|
2019-10-30 22:13:34 +03:00
|
|
|
import qualified Data.Map as Map
|
2019-08-16 20:20:08 +03:00
|
|
|
import Data.Maybe
|
2019-10-30 22:13:34 +03:00
|
|
|
import Data.Text (Text)
|
2019-08-13 21:01:50 +03:00
|
|
|
import GHC.Stack
|
|
|
|
import qualified Language.Python.Core as Py
|
2019-12-05 23:07:32 +03:00
|
|
|
import Language.Python.Failure
|
2019-08-16 20:20:08 +03:00
|
|
|
import Prelude hiding (fail)
|
2019-10-10 21:18:56 +03:00
|
|
|
import Source.Span
|
2019-10-30 22:13:34 +03:00
|
|
|
import Streaming
|
2019-08-16 20:20:08 +03:00
|
|
|
import qualified Streaming.Process
|
2019-10-10 22:17:22 +03:00
|
|
|
import Syntax.Term
|
2019-08-13 21:01:50 +03:00
|
|
|
import System.Directory
|
2019-08-16 20:20:08 +03:00
|
|
|
import System.Exit
|
2019-10-30 22:13:34 +03:00
|
|
|
import System.Path ((</>))
|
2019-09-18 21:41:58 +03:00
|
|
|
import qualified System.Path as Path
|
|
|
|
import qualified System.Path.Directory as Path
|
2019-10-10 22:17:22 +03:00
|
|
|
import Text.Show.Pretty (ppShow)
|
2019-10-15 20:10:18 +03:00
|
|
|
import qualified Text.Trifecta as Trifecta
|
2019-10-10 22:17:22 +03:00
|
|
|
import qualified TreeSitter.Python as TSP
|
|
|
|
import qualified TreeSitter.Unmarshal as TS
|
2019-08-13 21:01:50 +03:00
|
|
|
|
|
|
|
import qualified Test.Tasty as Tasty
|
|
|
|
import qualified Test.Tasty.HUnit as HUnit
|
|
|
|
|
2019-09-03 20:34:00 +03:00
|
|
|
import qualified Directive
|
|
|
|
import Instances ()
|
2019-08-16 19:34:56 +03:00
|
|
|
|
2019-10-30 20:13:53 +03:00
|
|
|
parsePrelude :: IO (Term (Ann Span :+: Core) Name)
|
|
|
|
parsePrelude = do
|
2019-10-15 20:10:18 +03:00
|
|
|
preludesrc <- ByteString.readFile "semantic-python/src/Prelude.score"
|
|
|
|
let ePrelude = Trifecta.parseByteString (Core.Parser.core <* Trifecta.eof) mempty preludesrc
|
2019-10-30 20:13:53 +03:00
|
|
|
case Trifecta.foldResult (Left . show) Right ePrelude of
|
2019-10-15 20:10:18 +03:00
|
|
|
Right r -> pure r
|
2019-10-30 22:13:34 +03:00
|
|
|
Left s -> HUnit.assertFailure ("Couldn't parse prelude: " <> s)
|
2019-10-15 20:10:18 +03:00
|
|
|
|
2019-10-30 20:13:53 +03:00
|
|
|
assertJQExpressionSucceeds :: Show a => Directive.Directive -> a -> Term (Ann Span :+: Core) Name -> HUnit.Assertion
|
|
|
|
assertJQExpressionSucceeds directive tree core = do
|
|
|
|
prelude <- parsePrelude
|
2019-10-15 20:10:18 +03:00
|
|
|
let allTogether = (named' "__semantic_prelude" :<- prelude) >>>= core
|
|
|
|
|
|
|
|
bod <- case scopeGraph Eval.eval [File (Path.absRel "<interactive>") (Span (Pos 1 1) (Pos 1 1)) allTogether] of
|
2019-10-10 21:18:56 +03:00
|
|
|
(heap, [File _ _ (Right result)]) -> pure $ Aeson.object
|
2019-09-04 19:47:43 +03:00
|
|
|
[ "scope" Aeson..= heap
|
|
|
|
, "heap" Aeson..= result
|
|
|
|
]
|
2019-10-15 20:10:18 +03:00
|
|
|
other -> HUnit.assertFailure ("Couldn't run scope dumping mechanism: " <> showCore (stripAnnotations allTogether) <> "\n" <> show other)
|
2019-08-16 20:20:08 +03:00
|
|
|
|
2019-10-30 20:51:51 +03:00
|
|
|
let ignore = ByteStream.effects . hoist ByteStream.effects
|
|
|
|
sgJSON = ByteStream.fromLazy $ Aeson.encode bod
|
2019-08-16 22:54:17 +03:00
|
|
|
jqPipeline = Streaming.Process.withStreamingProcess (Directive.toProcess directive) sgJSON ignore
|
2019-10-30 20:51:51 +03:00
|
|
|
errorMsg = "jq(1) returned non-zero exit code"
|
|
|
|
dirMsg = "jq expression: " <> show directive
|
|
|
|
jsonMsg = "JSON value: " <> ByteString.Lazy.unpack (Aeson.encodePretty bod)
|
|
|
|
astMsg = "AST (pretty): " <> ppShow tree
|
|
|
|
treeMsg = "Core expr (pretty): " <> showCore (stripAnnotations core)
|
|
|
|
treeMsg' = "Core expr (Show): " <> ppShow (stripAnnotations core)
|
2019-09-11 18:45:21 +03:00
|
|
|
|
2019-08-16 20:20:08 +03:00
|
|
|
|
|
|
|
catch @_ @Streaming.Process.ProcessExitedUnsuccessfully jqPipeline $ \err -> do
|
2019-09-11 18:45:21 +03:00
|
|
|
HUnit.assertFailure (unlines [errorMsg, dirMsg, jsonMsg, astMsg, treeMsg, treeMsg', show err])
|
2019-08-16 20:20:08 +03:00
|
|
|
|
2019-10-30 20:40:36 +03:00
|
|
|
-- handles CHECK-RESULT directives
|
2019-12-20 18:57:33 +03:00
|
|
|
assertEvaluatesTo :: Term (Ann Span :+: Core) Name -> Text -> Concrete (Term (Ann Span :+: Core)) -> HUnit.Assertion
|
2019-10-30 20:13:53 +03:00
|
|
|
assertEvaluatesTo core k val = do
|
|
|
|
prelude <- parsePrelude
|
|
|
|
let allTogether = (named' "__semantic_prelude" :<- prelude) >>>= core
|
|
|
|
let filius = [File (Path.absRel "<interactive>") (Span (Pos 1 1) (Pos 1 1)) allTogether]
|
|
|
|
|
|
|
|
(heap, env) <- case Concrete.concrete Eval.eval filius of
|
2019-10-30 20:40:36 +03:00
|
|
|
(heap, [File _ _ (Right (Concrete.Record env))]) ->
|
|
|
|
pure (heap, env)
|
|
|
|
(_, [File _ _ (Left (_, span, err))]) ->
|
|
|
|
HUnit.assertFailure ("Failed evaluation (" <> show span <> "): " <> err)
|
|
|
|
(_, files) ->
|
|
|
|
HUnit.assertFailure ("Unexpected number of files: " <> show (length files))
|
|
|
|
|
2019-10-30 20:13:53 +03:00
|
|
|
let found = Map.lookup (Name k) env >>= flip IntMap.lookup heap
|
2019-10-30 20:49:54 +03:00
|
|
|
found HUnit.@?= Just val
|
2019-10-30 20:13:53 +03:00
|
|
|
|
2019-10-30 20:40:36 +03:00
|
|
|
-- handles CHECK-TREE directives
|
2019-10-30 20:33:07 +03:00
|
|
|
assertTreeEqual :: Term Core Name -> Term Core Name -> HUnit.Assertion
|
|
|
|
assertTreeEqual t item = HUnit.assertEqual ("got (pretty)" <> showCore item) t item
|
2019-10-30 20:13:53 +03:00
|
|
|
|
2019-12-05 23:07:32 +03:00
|
|
|
|
2019-10-30 20:20:26 +03:00
|
|
|
checkPythonFile :: HasCallStack => Path.RelFile -> Tasty.TestTree
|
|
|
|
checkPythonFile fp = HUnit.testCaseSteps (Path.toString fp) $ \step -> withFrozenCallStack $ do
|
2019-10-30 20:33:07 +03:00
|
|
|
-- Extract the directives and the core associated with the provided file
|
2019-09-24 07:57:22 +03:00
|
|
|
let fullPath = Path.relDir "semantic-python/test/fixtures" </> fp
|
2019-10-30 20:29:18 +03:00
|
|
|
directives <- Directive.readDirectivesFromFile fullPath
|
2019-09-24 05:00:44 +03:00
|
|
|
result <- ByteString.readFile (Path.toString fullPath) >>= TS.parseByteString TSP.tree_sitter_python
|
2019-10-30 20:33:07 +03:00
|
|
|
|
|
|
|
-- Run the compiler
|
2019-11-09 06:37:17 +03:00
|
|
|
let coreResult = Control.Algebra.run
|
2019-09-19 22:35:25 +03:00
|
|
|
. runFail
|
2019-12-05 23:49:01 +03:00
|
|
|
. eliminateFailures
|
2019-12-11 23:47:03 +03:00
|
|
|
. Control.Algebra.run
|
2019-09-23 18:06:10 +03:00
|
|
|
. runReader @Py.Bindings mempty
|
2019-12-05 23:07:32 +03:00
|
|
|
. Py.toplevelCompile @(Failure :+: Ann Span :+: Core) @(Term _)
|
2019-09-19 22:35:25 +03:00
|
|
|
<$> result
|
|
|
|
|
2019-10-30 20:40:36 +03:00
|
|
|
-- Dispatch based on the result-directive pair
|
2019-08-29 16:20:13 +03:00
|
|
|
for_ directives $ \directive -> do
|
|
|
|
step (Directive.describe directive)
|
2019-09-19 01:07:43 +03:00
|
|
|
case (coreResult, directive) of
|
2019-10-30 20:33:07 +03:00
|
|
|
(Right (Left _), Directive.Fails) -> pure ()
|
|
|
|
(Left err, _) -> HUnit.assertFailure ("Parsing failed: " <> err)
|
|
|
|
(Right (Left err), _) -> HUnit.assertFailure ("Compilation failed: " <> err)
|
2019-10-31 21:17:45 +03:00
|
|
|
(Right (Right _), Directive.Fails) -> HUnit.assertFailure "Expected translation to fail"
|
2019-10-30 20:13:53 +03:00
|
|
|
(Right (Right item), Directive.Result k v) -> assertEvaluatesTo item k v
|
2019-10-30 20:33:07 +03:00
|
|
|
(Right (Right item), Directive.JQ _) -> assertJQExpressionSucceeds directive result item
|
|
|
|
(Right (Right item), Directive.Tree t) -> assertTreeEqual (stripAnnotations item) t
|
2019-08-13 21:01:50 +03:00
|
|
|
|
2019-08-29 16:20:13 +03:00
|
|
|
milestoneFixtures :: IO Tasty.TestTree
|
2019-10-30 20:20:26 +03:00
|
|
|
milestoneFixtures = buildTests <$> readFiles
|
|
|
|
where
|
|
|
|
readFiles = liftIO . Path.filesInDir . Path.relDir $ "semantic-python/test/fixtures"
|
|
|
|
buildTests = Tasty.testGroup "Python" . fmap checkPythonFile . sort . filter (Path.hasExtension ".py")
|
2019-08-13 21:01:50 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
2019-08-16 19:34:56 +03:00
|
|
|
main = do
|
|
|
|
jq <- findExecutable "jq"
|
|
|
|
when (isNothing jq) (die "Error: jq(1) not found in $PATH.")
|
2019-08-29 16:20:13 +03:00
|
|
|
milestoneFixtures >>= Tasty.defaultMain
|