1
1
mirror of https://github.com/github/semantic.git synced 2024-12-28 09:21:35 +03:00
semantic/test/Analysis/Python/Spec.hs

76 lines
4.2 KiB
Haskell
Raw Normal View History

module Analysis.Python.Spec (spec) where
import Data.Abstract.Evaluatable (EvalError(..), ValueRef(..))
2018-06-26 00:33:38 +03:00
import qualified Data.Abstract.ModuleTable as ModuleTable
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
import SpecHelpers
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
fromJust (SpecHelpers.lookupMembers "a" Import scopeAndFrame heap scopeGraph) `shouldContain` [ "foo" ]
fromJust (SpecHelpers.lookupMembers "b" Import scopeAndFrame heap scopeGraph) `shouldContain` ["c"]
-- (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)
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 ()
-- 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
() <$ SpecHelpers.lookupDeclaration "Foo" scopeAndFrame heap scopeGraph `shouldBe` Just ()
() <$ SpecHelpers.lookupDeclaration "Bar" scopeAndFrame heap scopeGraph `shouldBe` Just ()
SpecHelpers.lookupMembers "Bar" Superclass scopeAndFrame heap scopeGraph `shouldBe` Just [ "dang" ]
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
SpecHelpers.lookupMembers "Baz" Superclass scopeAndFrame heap scopeGraph `shouldBe` Just [ "dang" ]
valueRef `shouldBe` Rval (String "\"bar!\"")
2018-06-26 00:33:38 +03:00
other -> expectationFailure (show other)
where
fixtures = "test/fixtures/python/analysis/"
evaluate = evalPythonProject . map (fixtures <>)
2018-08-02 17:24:55 +03:00
evalPythonProject = testEvaluating <=< evaluateProject' config (Proxy :: Proxy 'Language.Python) pythonParser