2018-04-02 21:07:09 +03:00
|
|
|
{-# LANGUAGE OverloadedLists, OverloadedStrings #-}
|
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-03-13 20:26:28 +03:00
|
|
|
import Data.Abstract.Value
|
|
|
|
import Data.Map
|
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-03-10 02:01:29 +03:00
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2018-04-02 21:07:09 +03:00
|
|
|
describe "evaluates Python" $ do
|
2018-03-10 02:16:07 +03:00
|
|
|
it "imports" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((_, state), _) <- evaluate "main.py"
|
2018-05-10 17:59:38 +03:00
|
|
|
Env.names (environment state) `shouldContain` [ "a", "b" ]
|
2018-04-02 21:07:09 +03:00
|
|
|
|
2018-05-10 17:58:24 +03:00
|
|
|
(derefQName (heap state) ("a" :| []) (environment state) >>= deNamespace) `shouldBe` Just ("a", ["foo"])
|
|
|
|
(derefQName (heap state) ("b" :| []) (environment state) >>= deNamespace) `shouldBe` Just ("b", ["c"])
|
|
|
|
(derefQName (heap state) ("b" :| ["c"]) (environment state) >>= deNamespace) `shouldBe` Just ("c", ["baz"])
|
2018-03-10 02:01:29 +03:00
|
|
|
|
2018-03-10 02:16:07 +03:00
|
|
|
it "imports with aliases" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
env <- environment . snd . fst <$> evaluate "main1.py"
|
2018-05-10 17:59:38 +03:00
|
|
|
Env.names env `shouldContain` [ "b", "e" ]
|
2018-03-10 02:16:07 +03:00
|
|
|
|
|
|
|
it "imports using 'from' syntax" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
env <- environment . snd . fst <$> evaluate "main2.py"
|
2018-05-10 17:59:38 +03:00
|
|
|
Env.names env `shouldContain` [ "bar", "foo" ]
|
2018-03-10 02:16:07 +03:00
|
|
|
|
2018-03-23 18:01:13 +03:00
|
|
|
it "subclasses" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "subclass.py"
|
|
|
|
res `shouldBe` Right [injValue (String "\"bar\"")]
|
2018-03-23 18:01:13 +03:00
|
|
|
|
|
|
|
it "handles multiple inheritance left-to-right" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "multiple_inheritance.py"
|
|
|
|
res `shouldBe` Right [injValue (String "\"foo!\"")]
|
2018-03-23 18:01:13 +03:00
|
|
|
|
2018-03-10 02:01:29 +03:00
|
|
|
where
|
2018-04-02 21:07:09 +03:00
|
|
|
ns n = Just . Latest . Just . injValue . Namespace n
|
2018-03-10 02:01:29 +03:00
|
|
|
addr = Address . Precise
|
2018-03-10 03:24:23 +03:00
|
|
|
fixtures = "test/fixtures/python/analysis/"
|
2018-04-02 21:07:09 +03:00
|
|
|
evaluate entry = evalPythonProject (fixtures <> entry)
|
2018-05-07 00:30:07 +03:00
|
|
|
evalPythonProject path = testEvaluating <$> evaluateProject pythonParser Language.Python pythonPrelude path
|