1
1
mirror of https://github.com/github/semantic.git synced 2024-12-18 12:21:57 +03:00
semantic/test/SpecHelpers.hs

205 lines
7.9 KiB
Haskell
Raw Normal View History

2019-10-30 20:16:17 +03:00
{-# LANGUAGE DataKinds, FlexibleContexts #-}
2018-06-29 23:17:27 +03:00
{-# OPTIONS_GHC -fno-warn-orphans #-}
module SpecHelpers
( module X
2018-05-14 17:27:45 +03:00
, runBuilder
, diffFilePaths
, parseFilePath
2019-05-22 05:28:32 +03:00
, readFilePathPair
2019-02-02 02:04:23 +03:00
, runTaskOrDie
2019-10-01 00:59:23 +03:00
, runParseWithConfig
2019-02-02 02:04:23 +03:00
, TaskSession(..)
, testEvaluating
, toList
, Config
, LogQueue
, StatQueue
, lookupDeclaration
, lookupMembers
, EdgeLabel(..)
, TestEvaluatingResult
, TestEvaluatingState
2019-10-22 23:16:47 +03:00
, evaluateProject
) where
2019-06-14 16:46:06 +03:00
import Control.Abstract
import Control.Carrier.Fresh.Strict
2019-10-01 00:59:23 +03:00
import Control.Carrier.Parse.Simple
2019-11-09 06:48:26 +03:00
import Control.Carrier.Reader as X
import qualified Control.Carrier.Trace.Ignoring as Trace.Ignoring
import Control.Carrier.Resumable.Either
import Control.Carrier.Lift
import Control.Carrier.State.Strict
2019-10-22 23:00:35 +03:00
import Control.Exception (displayException)
import Control.Monad as X
2018-08-10 20:46:24 +03:00
import Data.Abstract.Address.Precise as X
2019-06-14 16:46:06 +03:00
import Data.Abstract.Evaluatable
2018-05-18 20:06:49 +03:00
import Data.Abstract.FreeVariables as X
2019-10-22 23:00:35 +03:00
import qualified Data.Abstract.Heap as Heap
import Data.Abstract.Module as X
2018-03-31 02:53:23 +03:00
import Data.Abstract.ModuleTable as X hiding (lookup)
2018-05-18 20:06:35 +03:00
import Data.Abstract.Name as X
2019-10-22 23:00:35 +03:00
import qualified Data.Abstract.ScopeGraph as ScopeGraph
import Data.Abstract.Value.Concrete (Value(..), ValueError, runValueError)
2018-03-13 20:59:20 +03:00
import Data.Blob as X
2019-05-22 05:28:32 +03:00
import Data.Blob.IO as X
2019-10-22 23:00:35 +03:00
import Data.ByteString as X (ByteString)
import Data.ByteString.Builder (Builder, toLazyByteString)
2018-05-14 17:18:52 +03:00
import Data.ByteString.Lazy (toStrict)
2019-10-22 23:00:35 +03:00
import Data.Edit as X
import Data.Foldable (toList)
import Data.Functor.Listable as X
import Data.Language as X hiding (Precise)
import Data.List.NonEmpty as X (NonEmpty(..))
2019-10-22 23:00:35 +03:00
import Data.Maybe as X
import Data.Monoid as X (Monoid(..), First(..), Last(..))
import Data.Project as X
import Data.Proxy as X
import Data.Semigroup as X (Semigroup(..))
2018-06-15 18:41:38 +03:00
import Data.Semilattice.Lower as X
2018-06-29 23:17:27 +03:00
import Data.String
2019-10-23 17:01:27 +03:00
import Data.Sum as Sum
import Data.Term as X
2019-10-22 23:00:35 +03:00
import Data.Traversable as X (for)
import Debug.Trace as X (traceShowM, traceM)
import Parsing.Parser as X
2019-10-22 23:00:35 +03:00
import Semantic.Api hiding (File, Blob, BlobPair)
import Semantic.Config (Config(..), optionsLogLevel)
2019-10-22 23:16:47 +03:00
import Semantic.Graph (analysisParsers, runHeap, runScopeGraph)
2019-06-14 16:46:06 +03:00
import Semantic.Task as X
2019-10-22 23:00:35 +03:00
import Semantic.Telemetry (LogQueue, StatQueue)
import Semantic.Util as X
import Source.Range as X hiding (start, end, point)
2019-10-22 23:00:35 +03:00
import Source.Source as X (Source)
import Source.Span as X hiding (HasSpan(..), start, end, point)
2019-10-22 23:00:35 +03:00
import System.Exit (die)
import qualified System.Path as Path
2018-03-13 21:10:50 +03:00
import Test.Hspec as X (Spec, SpecWith, context, describe, it, xit, parallel, pendingWith, around, runIO)
import Test.Hspec.Expectations as X
2018-03-13 20:59:20 +03:00
import Test.Hspec.LeanCheck as X
import Test.LeanCheck as X
2019-10-22 23:16:47 +03:00
import Unsafe.Coerce (unsafeCoerce)
2018-03-13 20:59:20 +03:00
runBuilder :: Builder -> ByteString
2018-05-14 17:27:45 +03:00
runBuilder = toStrict . toLazyByteString
2018-05-14 17:31:40 +03:00
2018-06-29 23:17:27 +03:00
-- | This orphan instance is so we don't have to insert @name@ calls
-- in dozens and dozens of environment specs.
instance IsString Name where
fromString = X.name . fromString
2018-06-29 23:17:27 +03:00
-- | Returns an s-expression formatted diff for the specified FilePath pair.
2019-10-18 17:45:59 +03:00
diffFilePaths :: TaskSession -> Path.RelFile -> Path.RelFile -> IO ByteString
diffFilePaths session p1 p2 = do
blobs <- readFilePathPair p1 p2
2019-10-18 17:29:43 +03:00
builder <- runTask session (runParse (configTreeSitterParseTimeout (config session)) (parseDiffBuilder DiffSExpression [ blobs ]))
either (die . displayException) (pure . runBuilder) builder
2019-09-20 18:52:39 +03:00
-- | Returns an s-expression parse tree for the specified path.
parseFilePath :: TaskSession -> Path.RelFile -> IO (Either SomeException ByteString)
parseFilePath session path = do
2019-10-12 02:24:53 +03:00
blob <- readBlobFromFile (fileForTypedPath path)
res <- runTask session . runParse (configTreeSitterParseTimeout (config session)) . runReader defaultLanguageModes $ parseTermBuilder TermSExpression (toList blob)
pure (runBuilder <$> res)
runParseWithConfig :: Has (Reader Config) sig m => ParseC m a -> m a
2019-10-01 00:59:23 +03:00
runParseWithConfig task = asks configTreeSitterParseTimeout >>= \ timeout -> runParse timeout task
2017-12-11 19:27:13 +03:00
-- | Read two files to a BlobPair.
2019-10-18 17:45:59 +03:00
readFilePathPair :: Path.RelFile -> Path.RelFile -> IO BlobPair
readFilePathPair p1 p2 = readFilePair (fileForTypedPath p1) (fileForTypedPath p2)
2017-05-11 16:28:52 +03:00
2019-02-16 01:55:15 +03:00
-- Run a Task and call `die` if it returns an Exception.
2019-10-01 00:59:23 +03:00
runTaskOrDie :: ParseC TaskC a -> IO a
runTaskOrDie task = runTaskWithOptions defaultOptions { optionsLogLevel = Nothing } (runParseWithConfig task) >>= either (die . displayException) pure
2019-02-02 02:04:23 +03:00
2018-10-24 17:01:49 +03:00
type TestEvaluatingC term
2019-03-06 18:12:10 +03:00
= ResumableC (BaseError (AddressError Precise (Val term)))
( ResumableC (BaseError (ValueError term Precise))
2019-03-06 18:12:10 +03:00
( ResumableC (BaseError ResolutionError)
( ResumableC (BaseError (EvalError term Precise (Val term)))
( ResumableC (BaseError (HeapError Precise))
( ResumableC (BaseError (ScopeError Precise))
( ResumableC (BaseError (UnspecializedError Precise (Val term)))
( ResumableC (BaseError (LoadError Precise (Val term)))
( StateC (Heap Precise Precise (Val term))
( StateC (ScopeGraph Precise)
( FreshC
( Trace.Ignoring.TraceC
2019-03-06 18:12:10 +03:00
( LiftC IO))))))))))))
type TestEvaluatingErrors term
= '[ BaseError (AddressError Precise (Val term))
, BaseError (ValueError term Precise)
, BaseError ResolutionError
2018-12-12 00:51:21 +03:00
, BaseError (EvalError term Precise (Val term))
, BaseError (HeapError Precise)
, BaseError (ScopeError Precise)
, BaseError (UnspecializedError Precise (Val term))
, BaseError (LoadError Precise (Val term))
]
type TestEvaluatingState term a
= ( ScopeGraph Precise
, ( Heap Precise Precise (Val term)
, Either (SomeError (Sum.Sum (TestEvaluatingErrors term))) a
)
)
type TestEvaluatingResult term = ModuleTable (Module (ModuleResult Precise (Val term)))
testEvaluating :: Evaluator term Precise (Val term) (TestEvaluatingC term) a
-> IO (TestEvaluatingState term a)
testEvaluating
= runM
. Trace.Ignoring.runTrace
. fmap snd
. runFresh 0
2018-10-24 17:05:22 +03:00
. runEvaluator
. runScopeGraph
. runHeap
2018-06-06 16:45:40 +03:00
. fmap reassociate
2018-10-24 16:59:57 +03:00
. runLoadError
. runUnspecialized
. runScopeError
. runHeapError
2018-10-24 16:59:57 +03:00
. runEvalError
. runResolutionError
. runValueError
2018-10-24 16:59:57 +03:00
. runAddressError
type Val term = Value term Precise
2018-05-07 00:36:33 +03:00
evaluateProject :: (HasPrelude lang, SLanguage lang) => TaskSession -> Proxy lang -> [FilePath] -> IO (TestEvaluatingState term (TestEvaluatingResult term))
evaluateProject session proxy = case parserForLanguage analysisParsers lang of
Just (SomeParser parser) -> unsafeCoerce . testEvaluating <=< evaluateProject' session proxy parser
2019-10-22 23:16:47 +03:00
_ -> error $ "analysis not supported for " <> show lang
where lang = reflect proxy
members :: EdgeLabel
-> Heap Precise Precise (Value term Precise)
-> ScopeGraph Precise
-> Value term Precise
-> Maybe [Name]
members edgeLabel heap scopeGraph (Data.Abstract.Value.Concrete.Object frame) = frameNames [ edgeLabel ] heap scopeGraph frame
members edgeLabel heap scopeGraph (Class _ _ frame) = frameNames [ edgeLabel ] heap scopeGraph frame
members _ _ _ _ = Nothing
frameNames :: [ EdgeLabel ]
-> Heap Precise Precise (Value term Precise)
-> ScopeGraph Precise
-> Precise
-> Maybe [ Name ]
frameNames edge heap scopeGraph frame = do
scopeAddress <- Heap.scopeLookup frame heap
scope <- ScopeGraph.lookupScope scopeAddress scopeGraph
2018-12-05 20:44:52 +03:00
pure (unDeclaration <$> toList (ScopeGraph.declarationNames edge scope scopeGraph))
lookupMembers :: Name -> EdgeLabel -> (Precise, Precise) -> Heap Precise Precise (Value term Precise) -> ScopeGraph Precise -> Maybe [ Name ]
lookupMembers name edgeLabel scopeAndFrame heap scopeGraph =
(lookupDeclaration name scopeAndFrame heap scopeGraph >>= members edgeLabel heap scopeGraph . Prelude.head)
2018-11-29 01:27:42 +03:00
lookupDeclaration :: Name -> (Precise, Precise) -> Heap Precise Precise (Value term Precise) -> ScopeGraph Precise -> Maybe [ Value term Precise ]
lookupDeclaration name (currentScope, currentFrame) heap scopeGraph = do
path <- ScopeGraph.lookupScopePath name currentScope scopeGraph
frameAddress <- Heap.lookupFrameAddress path currentFrame heap
toList <$> Heap.getSlotValue (Slot frameAddress (Heap.pathPosition path)) heap