2018-03-16 23:58:15 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
|
2018-03-14 02:19:26 +03:00
|
|
|
module Analysis.Ruby.Spec (spec) where
|
|
|
|
|
2018-03-28 19:58:12 +03:00
|
|
|
import Data.Abstract.Evaluatable (EvalError(..))
|
2018-03-14 02:19:26 +03:00
|
|
|
import Data.Abstract.Value
|
2018-03-28 19:58:12 +03:00
|
|
|
import Control.Monad.Effect (SomeExc(..))
|
|
|
|
import Data.List.NonEmpty (NonEmpty(..))
|
2018-03-14 02:19:26 +03:00
|
|
|
import Data.Map
|
2018-03-27 01:29:39 +03:00
|
|
|
import Data.Map.Monoidal as Map
|
2018-03-14 02:19:26 +03:00
|
|
|
|
|
|
|
import SpecHelpers
|
|
|
|
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2018-03-26 21:09:22 +03:00
|
|
|
describe "Ruby" $ do
|
|
|
|
it "evaluates require_relative" $ do
|
2018-03-23 20:11:29 +03:00
|
|
|
env <- findEnv <$> evaluate "main.rb"
|
2018-03-26 22:53:49 +03:00
|
|
|
env `shouldBe` [ (name "Object", addr 0)
|
|
|
|
, (name "foo", addr 3) ]
|
2018-03-14 02:19:26 +03:00
|
|
|
|
2018-03-28 01:18:38 +03:00
|
|
|
it "evaluates load" $ do
|
2018-03-23 20:11:29 +03:00
|
|
|
env <- findEnv <$> evaluate "load.rb"
|
2018-03-26 22:53:49 +03:00
|
|
|
env `shouldBe` [ (name "Object", addr 0)
|
|
|
|
, (name "foo", addr 3) ]
|
2018-03-16 01:09:07 +03:00
|
|
|
|
2018-03-28 01:18:38 +03:00
|
|
|
it "evaluates load with wrapper" $ do
|
2018-03-23 20:11:29 +03:00
|
|
|
res <- evaluate "load-wrap.rb"
|
2018-03-28 19:58:12 +03:00
|
|
|
findValue res `shouldBe` Right (Right (Right (Right (Left (SomeExc (FreeVariableError ("foo" :| [])))))))
|
2018-03-26 22:53:49 +03:00
|
|
|
findEnv res `shouldBe` [ (name "Object", addr 0) ]
|
2018-03-16 01:09:07 +03:00
|
|
|
|
2018-03-28 01:18:38 +03:00
|
|
|
it "evaluates subclass" $ do
|
2018-03-27 01:29:39 +03:00
|
|
|
res <- evaluate "subclass.rb"
|
2018-03-28 19:58:12 +03:00
|
|
|
findValue res `shouldBe` Right (Right (Right (Right (Right (injValue (String "\"<bar>\""))))))
|
2018-03-27 01:29:39 +03:00
|
|
|
findEnv res `shouldBe` [ (name "Bar", addr 6)
|
|
|
|
, (name "Foo", addr 3)
|
|
|
|
, (name "Object", addr 0) ]
|
|
|
|
|
|
|
|
let heap = findHeap res
|
|
|
|
Map.lookup (Precise 6) heap `shouldBe` ns "Bar" [ (name "baz", addr 8)
|
|
|
|
, (name "foo", addr 5)
|
|
|
|
, (name "inspect", addr 7) ]
|
2018-03-22 19:31:53 +03:00
|
|
|
|
2018-03-26 22:50:06 +03:00
|
|
|
it "evaluates modules" $ do
|
2018-03-26 22:53:58 +03:00
|
|
|
res <- evaluate "modules.rb"
|
2018-03-28 19:58:12 +03:00
|
|
|
findValue res `shouldBe` Right (Right (Right (Right (Right (injValue (String "\"<hello>\""))))))
|
2018-03-26 22:53:58 +03:00
|
|
|
findEnv res `shouldBe` [ (name "Object", addr 0)
|
|
|
|
, (name "Bar", addr 3) ]
|
2018-03-26 22:50:06 +03:00
|
|
|
|
2018-03-22 20:02:39 +03:00
|
|
|
it "has prelude" $ do
|
2018-03-24 00:56:26 +03:00
|
|
|
res <- findValue <$> evaluate "preluded.rb"
|
2018-03-28 19:58:12 +03:00
|
|
|
res `shouldBe` Right (Right (Right (Right (Right (injValue (String "\"<foo>\""))))))
|
2018-03-22 19:31:53 +03:00
|
|
|
|
2018-03-14 02:19:26 +03:00
|
|
|
where
|
2018-03-27 01:29:39 +03:00
|
|
|
ns n = Just . Latest . Just . injValue . Namespace (name n)
|
2018-03-14 02:19:26 +03:00
|
|
|
addr = Address . Precise
|
|
|
|
fixtures = "test/fixtures/ruby/analysis/"
|
2018-03-24 00:56:26 +03:00
|
|
|
evaluate entry = evaluateFilesWithPrelude rubyParser
|
2018-03-14 02:19:26 +03:00
|
|
|
[ fixtures <> entry
|
|
|
|
, fixtures <> "foo.rb"
|
|
|
|
]
|