2018-03-14 02:19:26 +03:00
|
|
|
module Analysis.Ruby.Spec (spec) where
|
|
|
|
|
2018-05-10 16:08:12 +03:00
|
|
|
import Data.Abstract.Environment as Env
|
2018-04-22 17:47:59 +03:00
|
|
|
import Data.Abstract.Evaluatable
|
2018-04-19 10:09:30 +03:00
|
|
|
import Data.Abstract.Value as Value
|
|
|
|
import Data.Abstract.Number as Number
|
2018-05-28 16:55:01 +03:00
|
|
|
import Data.AST
|
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-05-07 23:56:40 +03:00
|
|
|
import Data.Sum
|
2018-04-21 17:22:09 +03:00
|
|
|
import qualified Language.Ruby.Assignment as Ruby
|
2018-04-24 02:47:13 +03:00
|
|
|
import qualified Data.Language as Language
|
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-05-31 00:19:05 +03:00
|
|
|
((Right [(res, env)], state), _) <- evaluate "main.rb"
|
|
|
|
res `shouldBe` Value.Integer (Number.Integer 1)
|
|
|
|
Env.names env `shouldContain` ["foo"]
|
2018-03-14 02:19:26 +03:00
|
|
|
|
2018-03-28 01:18:38 +03:00
|
|
|
it "evaluates load" $ do
|
2018-05-31 00:19:05 +03:00
|
|
|
((Right [(_, env)], _), _) <- evaluate "load.rb"
|
2018-05-10 16:08:12 +03:00
|
|
|
Env.names env `shouldContain` ["foo"]
|
2018-03-16 01:09:07 +03:00
|
|
|
|
2018-03-28 01:18:38 +03:00
|
|
|
it "evaluates load with wrapper" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, state), _) <- evaluate "load-wrap.rb"
|
2018-05-28 22:30:19 +03:00
|
|
|
res `shouldBe` Left (SomeExc (inject @(EnvironmentError Precise) (FreeVariable "foo")))
|
2018-03-16 01:09:07 +03:00
|
|
|
|
2018-03-28 01:18:38 +03:00
|
|
|
it "evaluates subclass" $ do
|
2018-05-31 00:19:05 +03:00
|
|
|
((Right [(res, env)], state), _) <- evaluate "subclass.rb"
|
|
|
|
res `shouldBe` String "\"<bar>\""
|
|
|
|
Env.names env `shouldContain` [ "Bar", "Foo" ]
|
2018-03-27 01:29:39 +03:00
|
|
|
|
2018-05-31 00:19:05 +03:00
|
|
|
(derefQName (heap state) ("Bar" :| []) env >>= deNamespace) `shouldBe` Just ("Bar", ["baz", "foo", "inspect"])
|
2018-03-22 19:31:53 +03:00
|
|
|
|
2018-03-26 22:50:06 +03:00
|
|
|
it "evaluates modules" $ do
|
2018-05-31 00:19:05 +03:00
|
|
|
((Right [(res, env)], state), _) <- evaluate "modules.rb"
|
|
|
|
res `shouldBe` String "\"<hello>\""
|
|
|
|
Env.names env `shouldContain` [ "Bar" ]
|
2018-03-26 22:50:06 +03:00
|
|
|
|
2018-04-23 20:14:52 +03:00
|
|
|
it "handles break correctly" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "break.rb"
|
2018-05-31 00:19:05 +03:00
|
|
|
fmap fst <$> res `shouldBe` Right [Value.Integer (Number.Integer 3)]
|
2018-04-23 20:14:52 +03:00
|
|
|
|
|
|
|
it "handles break correctly" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "next.rb"
|
2018-05-31 00:19:05 +03:00
|
|
|
fmap fst <$> res `shouldBe` Right [Value.Integer (Number.Integer 8)]
|
2018-04-23 20:14:52 +03:00
|
|
|
|
2018-04-30 21:50:16 +03:00
|
|
|
it "calls functions with arguments" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "call.rb"
|
2018-05-31 00:19:05 +03:00
|
|
|
fmap fst <$> res `shouldBe` Right [Value.Integer (Number.Integer 579)]
|
2018-04-30 21:50:16 +03:00
|
|
|
|
2018-04-19 10:09:30 +03:00
|
|
|
it "evaluates early return statements" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "early-return.rb"
|
2018-05-31 00:19:05 +03:00
|
|
|
fmap fst <$> res `shouldBe` Right [Value.Integer (Number.Integer 123)]
|
2018-04-19 10:09:30 +03:00
|
|
|
|
2018-03-22 20:02:39 +03:00
|
|
|
it "has prelude" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "preluded.rb"
|
2018-05-31 00:19:05 +03:00
|
|
|
fmap fst <$> res `shouldBe` Right [String "\"<foo>\""]
|
2018-03-22 19:31:53 +03:00
|
|
|
|
2018-05-09 22:23:52 +03:00
|
|
|
it "evaluates __LINE__" $ do
|
2018-05-10 17:58:24 +03:00
|
|
|
((res, _), _) <- evaluate "line.rb"
|
2018-05-31 00:19:05 +03:00
|
|
|
fmap fst <$> res `shouldBe` Right [Value.Integer (Number.Integer 4)]
|
2018-05-09 22:23:52 +03:00
|
|
|
|
2018-05-10 18:03:42 +03:00
|
|
|
it "resolves builtins used in the prelude" $ do
|
|
|
|
((res, _), traces) <- evaluate "puts.rb"
|
2018-05-31 00:19:05 +03:00
|
|
|
fmap fst <$> res `shouldBe` Right [Unit]
|
2018-05-10 18:03:42 +03:00
|
|
|
traces `shouldContain` [ "\"hello\"" ]
|
|
|
|
|
2018-03-14 02:19:26 +03:00
|
|
|
where
|
2018-05-28 15:54:33 +03:00
|
|
|
ns n = Just . Latest . Last . Just . Namespace n
|
2018-03-14 02:19:26 +03:00
|
|
|
fixtures = "test/fixtures/ruby/analysis/"
|
2018-04-02 21:31:26 +03:00
|
|
|
evaluate entry = evalRubyProject (fixtures <> entry)
|
2018-06-18 18:10:44 +03:00
|
|
|
evalRubyProject path = testEvaluating <$> evaluateProject (Proxy :: Proxy 'Language.Ruby) rubyParser Language.Ruby path
|