2018-03-13 20:26:28 +03:00
|
|
|
module Analysis.Python.Spec (spec) where
|
2018-03-10 02:01:29 +03:00
|
|
|
|
2018-12-04 01:58:48 +03:00
|
|
|
import Data.Abstract.Evaluatable (EvalError(..), ValueRef(..))
|
2018-06-26 00:33:38 +03:00
|
|
|
import qualified Data.Abstract.ModuleTable as ModuleTable
|
2018-07-03 21:22:26 +03:00
|
|
|
import Data.Abstract.Value.Concrete
|
2018-04-21 17:22:09 +03:00
|
|
|
import qualified Language.Python.Assignment as Python
|
2018-04-24 02:47:13 +03:00
|
|
|
import qualified Data.Language as Language
|
2018-03-13 20:59:06 +03:00
|
|
|
|
2018-03-10 02:01:29 +03:00
|
|
|
import SpecHelpers
|
|
|
|
|
2018-03-10 03:24:23 +03:00
|
|
|
|
2018-07-10 21:09:22 +03:00
|
|
|
spec :: TaskConfig -> Spec
|
|
|
|
spec config = parallel $ do
|
2018-06-25 18:51:17 +03:00
|
|
|
describe "Python" $ do
|
2018-06-26 00:33:38 +03:00
|
|
|
it "imports" $ do
|
2018-11-29 04:44:02 +03:00
|
|
|
(scopeGraph, (heap, res)) <- evaluate ["main.py", "a.py", "b/__init__.py", "b/c.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "main.py" <$> res of
|
2018-11-29 04:44:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, _) :| [])) -> do
|
|
|
|
const () <$> SpecHelpers.lookupDeclaration "a" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
|
|
|
const () <$> SpecHelpers.lookupDeclaration "b" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
2018-06-26 00:33:38 +03:00
|
|
|
|
2018-12-04 19:24:01 +03:00
|
|
|
fromJust (SpecHelpers.lookupMembers "a" Import scopeAndFrame heap scopeGraph) `shouldContain` [ "foo" ]
|
|
|
|
fromJust (SpecHelpers.lookupMembers "b" Import scopeAndFrame heap scopeGraph) `shouldContain` ["c"]
|
2018-11-08 02:27:56 +03:00
|
|
|
-- (derefQName heap ("b" :| ["c"]) env >>= deNamespace heap) `shouldBe` Just ("c", ["baz"])
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "imports with aliases" $ do
|
2018-11-29 04:44:02 +03:00
|
|
|
(scopeGraph, (heap, res)) <- evaluate ["main1.py", "a.py", "b/__init__.py", "b/c.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "main1.py" <$> res of
|
2018-11-29 04:44:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, valueRef) :| [])) -> do
|
|
|
|
const () <$> SpecHelpers.lookupDeclaration "b" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
|
|
|
const () <$> SpecHelpers.lookupDeclaration "e" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
2018-11-27 22:16:27 +03:00
|
|
|
it "imports using from syntax" $ do
|
2018-11-29 04:44:02 +03:00
|
|
|
(scopeGraph, (heap, res)) <- evaluate ["main2.py", "a.py", "b/__init__.py", "b/c.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "main2.py" <$> res of
|
2018-11-29 04:44:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, valueRef) :| [])) -> do
|
|
|
|
const () <$> SpecHelpers.lookupDeclaration "bar" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
|
|
|
const () <$> SpecHelpers.lookupDeclaration "foo" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
2018-11-27 22:16:27 +03:00
|
|
|
|
|
|
|
-- TODO: Enable when we constrain edge paths with path predicates
|
|
|
|
-- () <$ SpecHelpers.lookupDeclaration "baz" heap scopeGraph `shouldBe` Nothing
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "imports with relative syntax" $ do
|
2018-11-29 04:44:02 +03:00
|
|
|
(scopeGraph, (heap, res)) <- evaluate ["main3.py", "c/__init__.py", "c/utils.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "main3.py" <$> res of
|
2018-11-29 04:44:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, valueRef) :| [])) -> do
|
|
|
|
const () <$> SpecHelpers.lookupDeclaration "utils" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
2018-11-09 02:22:35 +03:00
|
|
|
-- (lookupDeclaration "utils" heap >>= deNamespace heap) `shouldBe` Just ("utils", ["to_s"])
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "subclasses" $ do
|
2018-11-29 04:44:02 +03:00
|
|
|
(scopeGraph, (heap, res)) <- evaluate ["subclass.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "subclass.py" <$> res of
|
2018-11-29 04:44:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, valueRef) :| [])) -> do
|
2018-12-04 01:58:48 +03:00
|
|
|
() <$ SpecHelpers.lookupDeclaration "Foo" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
|
|
|
() <$ SpecHelpers.lookupDeclaration "Bar" scopeAndFrame heap scopeGraph `shouldBe` Just ()
|
2018-12-04 19:24:01 +03:00
|
|
|
SpecHelpers.lookupMembers "Bar" Superclass scopeAndFrame heap scopeGraph `shouldBe` Just [ "dang" ]
|
2018-12-04 01:58:48 +03:00
|
|
|
valueRef `shouldBe` Rval (String "\"bar\"")
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "handles multiple inheritance left-to-right" $ do
|
2018-11-29 04:44:02 +03:00
|
|
|
(scopeGraph, (heap, res)) <- evaluate ["multiple_inheritance.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "multiple_inheritance.py" <$> res of
|
2018-11-29 04:44:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, valueRef) :| [])) -> do
|
2018-12-04 19:24:01 +03:00
|
|
|
SpecHelpers.lookupMembers "Baz" Superclass scopeAndFrame heap scopeGraph `shouldBe` Just [ "dang" ]
|
2018-12-04 18:36:23 +03:00
|
|
|
valueRef `shouldBe` Rval (String "\"bar!\"")
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
2018-03-23 18:01:13 +03:00
|
|
|
|
2018-03-10 02:01:29 +03:00
|
|
|
where
|
2018-03-10 03:24:23 +03:00
|
|
|
fixtures = "test/fixtures/python/analysis/"
|
2018-06-22 22:45:42 +03:00
|
|
|
evaluate = evalPythonProject . map (fixtures <>)
|
2018-08-02 17:24:55 +03:00
|
|
|
evalPythonProject = testEvaluating <=< evaluateProject' config (Proxy :: Proxy 'Language.Python) pythonParser
|