1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00
semantic/test/Analysis/Ruby/Spec.hs

76 lines
3.1 KiB
Haskell
Raw Normal View History

2018-03-16 23:58:15 +03:00
{-# LANGUAGE OverloadedLists #-}
2018-03-14 02:19:26 +03:00
module Analysis.Ruby.Spec (spec) where
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-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
import Data.Map.Monoidal as Map
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
describe "Ruby" $ do
it "evaluates require_relative" $ do
env <- environment . snd <$> evaluate "main.rb"
env `shouldBe` [ ("Object", addr 0)
, ("foo", addr 3) ]
2018-03-14 02:19:26 +03:00
2018-03-28 01:18:38 +03:00
it "evaluates load" $ do
env <- environment . snd <$> evaluate "load.rb"
env `shouldBe` [ ("Object", addr 0)
, ("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-04-26 00:12:10 +03:00
fst res `shouldBe` Right (Right (Right (Right (Right (Right (Right (Left (SomeExc (FreeVariableError "foo")))))))))
environment (snd res) `shouldBe` [ ("Object", addr 0) ]
2018-03-16 01:09:07 +03:00
2018-03-28 01:18:38 +03:00
it "evaluates subclass" $ do
res <- evaluate "subclass.rb"
2018-04-23 20:09:32 +03:00
fst res `shouldBe` Right (Right (Right (Right (Right (Right (Right (Right (Right (pure $ injValue (String "\"<bar>\""))))))))))
environment (snd res) `shouldBe` [ ("Bar", addr 6)
, ("Foo", addr 3)
, ("Object", addr 0) ]
heapLookup (Address (Precise 6)) (heap (snd res))
`shouldBe` ns "Bar" [ ("baz", addr 8)
, ("foo", addr 5)
, ("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-04-23 20:09:32 +03:00
fst res `shouldBe` Right (Right (Right (Right (Right (Right (Right (Right (Right (pure $ injValue (String "\"<hello>\""))))))))))
environment (snd res) `shouldBe` [ ("Object", addr 0)
, ("Bar", addr 3) ]
2018-03-26 22:50:06 +03:00
2018-04-23 20:14:52 +03:00
it "handles break correctly" $ do
res <- evaluate "break.rb"
fst res `shouldBe` Right (Right (Right (Right (Right (Right (Right (Right (Right (pure $ injValue (Value.Integer (Number.Integer 3)))))))))))
it "handles break correctly" $ do
res <- evaluate "next.rb"
fst res `shouldBe` Right (Right (Right (Right (Right (Right (Right (Right (Right (pure $ injValue (Value.Integer (Number.Integer 8)))))))))))
2018-04-19 10:09:30 +03:00
it "evaluates early return statements" $ do
res <- evaluate "early-return.rb"
2018-04-23 20:09:32 +03:00
fst res `shouldBe` Right (Right (Right (Right (Right (Right (Right (Right (Right (pure $ injValue (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
res <- fst <$> evaluate "preluded.rb"
2018-04-23 20:09:32 +03:00
res `shouldBe` Right (Right (Right (Right (Right (Right (Right (Right (Right (pure $ injValue (String "\"<foo>\""))))))))))
2018-03-22 19:31:53 +03:00
2018-03-14 02:19:26 +03:00
where
ns n = Just . Latest . Just . injValue . Namespace n
2018-03-14 02:19:26 +03:00
addr = Address . Precise
fixtures = "test/fixtures/ruby/analysis/"
evaluate entry = evalRubyProject (fixtures <> entry)
2018-04-25 19:10:33 +03:00
evalRubyProject path = runAnalysis @(TestEvaluating Ruby.Term) <$> evaluateProject rubyParser Language.Ruby rubyPrelude path