1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00
semantic/test/Analysis/Python/Spec.hs

74 lines
4.0 KiB
Haskell
Raw Normal View History

module Analysis.Python.Spec (spec) where
2018-06-26 00:33:38 +03:00
import qualified Data.Abstract.ModuleTable as ModuleTable
import Data.Abstract.Value.Concrete
2018-04-24 02:47:13 +03:00
import qualified Data.Language as Language
2018-03-13 20:59:06 +03:00
import SpecHelpers
2019-02-02 02:04:23 +03:00
spec :: TaskSession -> Spec
spec session = 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
Right (Just (Module _ (scopeAndFrame, _))) -> do
2018-11-29 04:44:02 +03:00
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
Right (Just (Module _ (scopeAndFrame, value))) -> do
2018-11-29 04:44:02 +03:00
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
Right (Just (Module _ (scopeAndFrame, value))) -> do
2018-11-29 04:44:02 +03:00
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
Right (Just (Module _ (scopeAndFrame, value))) -> do
2018-11-29 04:44:02 +03:00
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
Right (Just (Module _ (scopeAndFrame, value))) -> 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" ]
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
Right (Just (Module _ (scopeAndFrame, value))) -> do
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)
where
fixtures = "test/fixtures/python/analysis/"
evaluate = evalPythonProject . map (fixtures <>)
2019-02-02 02:04:23 +03:00
evalPythonProject = testEvaluating <=< evaluateProject' session (Proxy :: Proxy 'Language.Python) pythonParser