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-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-04-02 21:07:09 +03:00
|
|
|
res <- snd <$> evaluate "main.py"
|
2018-04-04 01:50:18 +03:00
|
|
|
environment res `shouldBe` [ ("print", addr 0)
|
|
|
|
, ("a", addr 1)
|
|
|
|
, ("b", addr 3)
|
2018-04-02 21:07:09 +03:00
|
|
|
]
|
|
|
|
|
2018-04-04 01:50:18 +03:00
|
|
|
heapLookup (Address (Precise 1)) (heap res) `shouldBe` ns "a" [ ("foo", addr 2) ]
|
|
|
|
heapLookup (Address (Precise 3)) (heap res) `shouldBe` ns "b" [ ("c", addr 4) ]
|
|
|
|
heapLookup (Address (Precise 4)) (heap res) `shouldBe` ns "c" [ ("baz", addr 5) ]
|
2018-03-10 02:01:29 +03:00
|
|
|
|
2018-03-10 02:16:07 +03:00
|
|
|
it "imports with aliases" $ do
|
2018-03-27 22:23:26 +03:00
|
|
|
env <- environment . snd <$> evaluate "main1.py"
|
2018-04-04 01:50:18 +03:00
|
|
|
env `shouldBe` [ ("print", addr 0)
|
|
|
|
, ("b", addr 1)
|
|
|
|
, ("e", addr 3)
|
2018-03-23 20:11:29 +03:00
|
|
|
]
|
2018-03-10 02:16:07 +03:00
|
|
|
|
|
|
|
it "imports using 'from' syntax" $ do
|
2018-03-27 22:23:26 +03:00
|
|
|
env <- environment . snd <$> evaluate "main2.py"
|
2018-04-04 01:50:18 +03:00
|
|
|
env `shouldBe` [ ("print", addr 0)
|
|
|
|
, ("foo", addr 1)
|
|
|
|
, ("bar", addr 2)
|
2018-03-23 20:11:29 +03:00
|
|
|
]
|
2018-03-10 02:16:07 +03:00
|
|
|
|
2018-03-23 18:01:13 +03:00
|
|
|
it "subclasses" $ do
|
2018-03-27 22:23:26 +03:00
|
|
|
v <- fst <$> evaluate "subclass.py"
|
2018-04-27 17:00:07 +03:00
|
|
|
v `shouldBe` Right (Right (Right (Right (Right (Right (Right (pure (injValue (String "\"bar\"")))))))))
|
2018-03-23 18:01:13 +03:00
|
|
|
|
|
|
|
it "handles multiple inheritance left-to-right" $ do
|
2018-03-27 22:23:26 +03:00
|
|
|
v <- fst <$> evaluate "multiple_inheritance.py"
|
2018-04-27 17:00:07 +03:00
|
|
|
v `shouldBe` Right (Right (Right (Right (Right (Right (Right (pure (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
|