2018-03-13 20:26:28 +03:00
|
|
|
module Analysis.Python.Spec (spec) where
|
2018-03-10 02:01:29 +03:00
|
|
|
|
2018-05-10 16:59:54 +03:00
|
|
|
import Data.Abstract.Environment as Env
|
2018-05-07 00:37:44 +03:00
|
|
|
import Data.Abstract.Evaluatable (EvalError(..))
|
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-06-26 22:14:28 +03:00
|
|
|
(_, (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-06-26 22:14:28 +03:00
|
|
|
Right (Just (Module _ (env, addr) :| [])) -> do
|
2018-06-26 00:33:38 +03:00
|
|
|
Env.names env `shouldContain` [ "a", "b" ]
|
|
|
|
|
|
|
|
(derefQName heap ("a" :| []) env >>= deNamespace) `shouldBe` Just ("a", ["foo"])
|
|
|
|
(derefQName heap ("b" :| []) env >>= deNamespace) `shouldBe` Just ("b", ["c"])
|
|
|
|
(derefQName heap ("b" :| ["c"]) env >>= deNamespace) `shouldBe` Just ("c", ["baz"])
|
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "imports with aliases" $ do
|
2018-06-26 22:14:28 +03:00
|
|
|
(_, (_, 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-06-26 22:14:28 +03:00
|
|
|
Right (Just (Module _ (env, addr) :| [])) -> Env.names env `shouldContain` [ "b", "e" ]
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "imports using 'from' syntax" $ do
|
2018-06-26 22:14:28 +03:00
|
|
|
(_, (_, 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-06-26 22:14:28 +03:00
|
|
|
Right (Just (Module _ (env, addr) :| [])) -> Env.names env `shouldContain` [ "bar", "foo" ]
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "imports with relative syntax" $ do
|
2018-06-26 22:14:28 +03:00
|
|
|
(_, (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-06-26 22:14:28 +03:00
|
|
|
Right (Just (Module _ (env, addr) :| [])) -> do
|
2018-06-26 00:33:38 +03:00
|
|
|
Env.names env `shouldContain` [ "utils" ]
|
|
|
|
(derefQName heap ("utils" :| []) env >>= deNamespace) `shouldBe` Just ("utils", ["to_s"])
|
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "subclasses" $ do
|
2018-06-26 22:14:28 +03:00
|
|
|
(_, (heap, res)) <- evaluate ["subclass.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "subclass.py" <$> res of
|
2018-06-26 22:14:28 +03:00
|
|
|
Right (Just (Module _ (env, addr) :| [])) -> heapLookupAll addr heap `shouldBe` Just [String "\"bar\""]
|
2018-06-26 00:33:38 +03:00
|
|
|
other -> expectationFailure (show other)
|
|
|
|
|
|
|
|
it "handles multiple inheritance left-to-right" $ do
|
2018-06-26 22:14:28 +03:00
|
|
|
(_, (heap, res)) <- evaluate ["multiple_inheritance.py"]
|
2018-06-26 00:33:38 +03:00
|
|
|
case ModuleTable.lookup "multiple_inheritance.py" <$> res of
|
2018-06-26 22:14:28 +03:00
|
|
|
Right (Just (Module _ (env, addr) :| [])) -> heapLookupAll addr heap `shouldBe` Just [String "\"foo!\""]
|
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-05-28 15:54:33 +03:00
|
|
|
ns n = Just . Latest . Last . Just . Namespace n
|
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-07-10 21:09:22 +03:00
|
|
|
evalPythonProject = testEvaluating <=< evaluateProject' config (Proxy :: Proxy 'Language.Python) pythonParser Language.Python
|