|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +module ContextSpec where |
| 3 | + |
| 4 | + |
| 5 | +import Test.Hspec |
| 6 | + |
| 7 | +import GHC ( tm_parsed_module ) |
| 8 | +import System.Directory |
| 9 | + |
| 10 | +import Haskell.Ide.Engine.PluginApi |
| 11 | +import Haskell.Ide.Engine.PluginsIdeMonads |
| 12 | +import Haskell.Ide.Engine.PluginUtils |
| 13 | +import Haskell.Ide.Engine.ModuleCache |
| 14 | +import Haskell.Ide.Engine.Context |
| 15 | + |
| 16 | +import TestUtils |
| 17 | + |
| 18 | +spec :: Spec |
| 19 | +spec = describe "Context of different cursor positions" $ do |
| 20 | + it "can set the module as type checked" |
| 21 | + $ withCurrentDirectory "./test/testdata/context" |
| 22 | + $ do |
| 23 | + fp <- makeAbsolute "./ExampleContext.hs" |
| 24 | + let arg = filePathToUri fp |
| 25 | + let res = IdeResultOk (Nothing :: Maybe Context) |
| 26 | + actual <- runSingle (IdePlugins mempty) $ do |
| 27 | + _ <- setTypecheckedModule arg |
| 28 | + return $ IdeResultOk Nothing |
| 29 | + |
| 30 | + actual `shouldBe` res |
| 31 | + |
| 32 | + it "module header context" |
| 33 | + $ withCurrentDirectory "./test/testdata/context" |
| 34 | + $ do |
| 35 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 36 | + let res = IdeResultOk (Just (ModuleContext "ExampleContext")) |
| 37 | + |
| 38 | + actual <- getContextAt fp_ (toPos (1, 10)) |
| 39 | + |
| 40 | + actual `shouldBe` res |
| 41 | + |
| 42 | + |
| 43 | + it "module export list context" |
| 44 | + $ withCurrentDirectory "./test/testdata/context" |
| 45 | + $ do |
| 46 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 47 | + let res = IdeResultOk (Just ExportContext) |
| 48 | + actual <- getContextAt fp_ (toPos (1, 24)) |
| 49 | + |
| 50 | + actual `shouldBe` res |
| 51 | + |
| 52 | + it "value context" $ withCurrentDirectory "./test/testdata/context" $ do |
| 53 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 54 | + let res = IdeResultOk (Just ValueContext) |
| 55 | + actual <- getContextAt fp_ (toPos (7, 6)) |
| 56 | + |
| 57 | + actual `shouldBe` res |
| 58 | + |
| 59 | + it "value context" $ withCurrentDirectory "./test/testdata/context" $ do |
| 60 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 61 | + let res = IdeResultOk (Just ValueContext) |
| 62 | + actual <- getContextAt fp_ (toPos (7, 12)) |
| 63 | + |
| 64 | + actual `shouldBe` res |
| 65 | + |
| 66 | + it "import context" $ withCurrentDirectory "./test/testdata/context" $ do |
| 67 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 68 | + let res = IdeResultOk (Just (ImportContext "Data.List")) |
| 69 | + actual <- getContextAt fp_ (toPos (3, 8)) |
| 70 | + |
| 71 | + actual `shouldBe` res |
| 72 | + |
| 73 | + it "function declaration context" |
| 74 | + $ withCurrentDirectory "./test/testdata/context" |
| 75 | + $ do |
| 76 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 77 | + let res = IdeResultOk (Just TypeContext) |
| 78 | + actual <- getContextAt fp_ (toPos (6, 1)) |
| 79 | + |
| 80 | + actual `shouldBe` res |
| 81 | + |
| 82 | + |
| 83 | + it "function definition context" |
| 84 | + $ withCurrentDirectory "./test/testdata/context" |
| 85 | + $ do |
| 86 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 87 | + let res = IdeResultOk (Just ValueContext) |
| 88 | + actual <- getContextAt fp_ (toPos (7, 1)) |
| 89 | + actual `shouldBe` res |
| 90 | + |
| 91 | + it "function signature context" |
| 92 | + $ withCurrentDirectory "./test/testdata/context" |
| 93 | + $ do |
| 94 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 95 | + let res = IdeResultOk (Just TypeContext) |
| 96 | + actual <- getContextAt fp_ (toPos (6, 8)) |
| 97 | + actual `shouldBe` res |
| 98 | + |
| 99 | + it "nothing" $ withCurrentDirectory "./test/testdata/context" $ do |
| 100 | + fp_ <- makeAbsolute "./ExampleContext.hs" |
| 101 | + let res = IdeResultOk Nothing |
| 102 | + actual <- getContextAt fp_ (toPos (2, 1)) |
| 103 | + actual `shouldBe` res |
| 104 | + |
| 105 | +getContextAt :: [Char] -> Position -> IO (IdeResult (Maybe Context)) |
| 106 | +getContextAt fp_ pos = do |
| 107 | + let arg = filePathToUri fp_ |
| 108 | + runSingle (IdePlugins mempty) $ do |
| 109 | + _ <- setTypecheckedModule arg |
| 110 | + pluginGetFile "getContext: " arg $ \fp -> |
| 111 | + ifCachedModuleAndData fp (IdeResultOk Nothing) $ \tm _ () -> |
| 112 | + return $ IdeResultOk $ getContext pos (tm_parsed_module tm) |
0 commit comments