2019-10-30 20:16:17 +03:00
|
|
|
{-# LANGUAGE DataKinds, ImplicitParams, OverloadedStrings, TypeApplications #-}
|
2019-06-12 21:50:18 +03:00
|
|
|
{-# OPTIONS_GHC -O0 #-}
|
2018-03-13 20:26:28 +03:00
|
|
|
module Analysis.Python.Spec (spec) where
|
2018-03-10 02:01:29 +03:00
|
|
|
|
2018-06-26 00:33:38 +03:00
|
|
|
import qualified Data.Abstract.ModuleTable as ModuleTable
|
2019-10-23 17:34:36 +03:00
|
|
|
import Data.Abstract.Value.Concrete
|
2018-04-24 02:47:13 +03:00
|
|
|
import qualified Data.Language as Language
|
2019-10-23 17:34:36 +03:00
|
|
|
import qualified Language.Python.Term as Python
|
|
|
|
import Source.Loc
|
|
|
|
import SpecHelpers
|
2018-03-10 02:01:29 +03:00
|
|
|
|
2018-03-10 03:24:23 +03:00
|
|
|
|
2019-06-12 20:53:59 +03:00
|
|
|
spec :: (?session :: TaskSession) => Spec
|
2019-06-20 00:22:09 +03:00
|
|
|
spec = 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-12-11 21:07:45 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, _))) -> do
|
2019-06-16 18:14:03 +03:00
|
|
|
SpecHelpers.lookupDeclaration "a" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
|
|
|
SpecHelpers.lookupDeclaration "b" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
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
|
2019-06-15 07:53:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, _))) -> do
|
2019-06-16 18:14:03 +03:00
|
|
|
SpecHelpers.lookupDeclaration "b" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
|
|
|
SpecHelpers.lookupDeclaration "e" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
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
|
2019-06-15 07:53:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, _))) -> do
|
2019-06-16 18:14:03 +03:00
|
|
|
SpecHelpers.lookupDeclaration "bar" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
|
|
|
SpecHelpers.lookupDeclaration "foo" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
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
|
2019-06-15 07:53:02 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, _))) -> do
|
2019-06-16 18:14:03 +03:00
|
|
|
SpecHelpers.lookupDeclaration "utils" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
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-12-11 21:07:45 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, value))) -> do
|
2019-06-16 18:14:03 +03:00
|
|
|
SpecHelpers.lookupDeclaration "Foo" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
|
|
|
SpecHelpers.lookupDeclaration "Bar" scopeAndFrame heap scopeGraph `shouldSatisfy` isJust
|
2018-12-04 19:24:01 +03:00
|
|
|
SpecHelpers.lookupMembers "Bar" Superclass scopeAndFrame heap scopeGraph `shouldBe` Just [ "dang" ]
|
2018-12-07 23:23:03 +03:00
|
|
|
value `shouldBe` 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-12-11 21:07:45 +03:00
|
|
|
Right (Just (Module _ (scopeAndFrame, value))) -> do
|
2018-12-04 19:24:01 +03:00
|
|
|
SpecHelpers.lookupMembers "Baz" Superclass scopeAndFrame heap scopeGraph `shouldBe` Just [ "dang" ]
|
2018-12-07 23:23:03 +03:00
|
|
|
value `shouldBe` 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/"
|
2019-10-23 17:34:36 +03:00
|
|
|
evaluate = evaluateProject @'Language.Python @(Python.Term Loc) ?session Proxy . map (fixtures <>)
|