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

76 lines
4.0 KiB
Haskell
Raw Normal View History

2019-10-30 20:16:17 +03:00
{-# LANGUAGE DataKinds, ImplicitParams, OverloadedStrings, TypeApplications #-}
2019-06-12 21:50:18 +03:00
{-# OPTIONS_GHC -O0 #-}
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
import qualified Language.Python.Term as Python
import Source.Loc
import SpecHelpers
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
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
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, _))) -> 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)
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, _))) -> 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
-- 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, _))) -> 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
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
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 = evaluateProject @'Language.Python @(Python.Term Loc) ?session Proxy . map (fixtures <>)