2018-07-16 18:07:40 +03:00
|
|
|
{-# LANGUAGE PackageImports #-}
|
|
|
|
|
|
|
|
module Graphing.Calls.Spec ( spec ) where
|
|
|
|
|
|
|
|
import Prelude hiding (readFile)
|
|
|
|
import Prologue
|
|
|
|
import SpecHelpers hiding (readFile)
|
|
|
|
|
|
|
|
import Algebra.Graph
|
|
|
|
import Data.List (uncons)
|
|
|
|
|
2018-07-16 18:12:17 +03:00
|
|
|
import "semantic" Data.Graph (Graph (..), topologicalSort)
|
|
|
|
import Data.Graph.Vertex
|
2018-07-16 18:07:40 +03:00
|
|
|
import qualified Data.Language as Language
|
2018-07-16 18:12:17 +03:00
|
|
|
import Semantic.Config (defaultOptions)
|
|
|
|
import Semantic.Graph
|
|
|
|
import Semantic.IO
|
2018-07-16 18:07:40 +03:00
|
|
|
|
|
|
|
callGraphPythonProject paths = runTaskWithOptions defaultOptions $ do
|
|
|
|
let proxy = Proxy @'Language.Python
|
|
|
|
let lang = Language.Python
|
|
|
|
blobs <- catMaybes <$> traverse readFile (flip File lang <$> paths)
|
|
|
|
package <- parsePackage pythonParser (Project (takeDirectory (maybe "/" fst (uncons paths))) blobs lang [])
|
|
|
|
modules <- topologicalSort <$> runImportGraph proxy package
|
|
|
|
runCallGraph proxy False modules package
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = describe "call graphing" $ do
|
|
|
|
|
2018-07-16 20:13:05 +03:00
|
|
|
let needs r n = unGraph r `shouldSatisfy` hasVertex (Variable n)
|
|
|
|
|
2018-07-16 18:07:40 +03:00
|
|
|
it "should work for a simple example" $ do
|
|
|
|
res <- callGraphPythonProject ["test/fixtures/python/graphing/simple/simple.py"]
|
2018-07-16 20:13:05 +03:00
|
|
|
res `needs` "magnus"
|
2018-07-16 18:12:17 +03:00
|
|
|
|
|
|
|
it "should evaluate both sides of an if-statement" $ do
|
|
|
|
res <- callGraphPythonProject ["test/fixtures/python/graphing/conditional/conditional.py"]
|
2018-07-16 20:13:05 +03:00
|
|
|
res `needs` "merle"
|
|
|
|
res `needs` "taako"
|
2018-07-16 18:36:17 +03:00
|
|
|
|
|
|
|
it "should continue even when a type error is encountered" $ do
|
|
|
|
res <- callGraphPythonProject ["test/fixtures/python/graphing/typeerror/typeerror.py"]
|
2018-07-16 20:13:05 +03:00
|
|
|
res `needs` "lup"
|
2018-07-16 18:36:17 +03:00
|
|
|
|
|
|
|
it "should continue when an unbound variable is encountered" $ do
|
|
|
|
res <- callGraphPythonProject ["test/fixtures/python/graphing/unbound/unbound.py"]
|
2018-07-16 20:13:05 +03:00
|
|
|
res `needs` "lucretia"
|