|
| 1 | +{-# LANGUAGE NamedFieldPuns #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 4 | + |
| 5 | +module Eval (tests) where |
| 6 | + |
| 7 | +import Control.Applicative.Combinators (skipManyTill) |
| 8 | +import Control.Monad.IO.Class (MonadIO (liftIO)) |
| 9 | +import qualified Data.Text.IO as T |
| 10 | +import Language.Haskell.LSP.Test |
| 11 | +import Language.Haskell.LSP.Types (ApplyWorkspaceEditRequest, |
| 12 | + CodeLens (CodeLens, _command, _range), |
| 13 | + Command (_title), |
| 14 | + Position (..), Range (..)) |
| 15 | +import System.FilePath |
| 16 | +import Test.Hls.Util |
| 17 | +import Test.Tasty |
| 18 | +import Test.Tasty.HUnit |
| 19 | + |
| 20 | +tests :: TestTree |
| 21 | +tests = |
| 22 | + testGroup |
| 23 | + "eval" |
| 24 | + [ testCase "Produces Evaluate code lenses" $ do |
| 25 | + runSession hieCommand fullCaps evalPath $ do |
| 26 | + doc <- openDoc "T1.hs" "haskell" |
| 27 | + lenses <- getCodeLenses doc |
| 28 | + liftIO $ map (fmap _title . _command) lenses @?= [Just "Evaluate..."], |
| 29 | + testCase "Produces Refresh code lenses" $ do |
| 30 | + runSession hieCommand fullCaps evalPath $ do |
| 31 | + doc <- openDoc "T2.hs" "haskell" |
| 32 | + lenses <- getCodeLenses doc |
| 33 | + liftIO $ map (fmap _title . _command) lenses @?= [Just "Refresh..."], |
| 34 | + testCase "Code lenses have ranges" $ do |
| 35 | + runSession hieCommand fullCaps evalPath $ do |
| 36 | + doc <- openDoc "T1.hs" "haskell" |
| 37 | + lenses <- getCodeLenses doc |
| 38 | + liftIO $ map _range lenses @?= [Range (Position 4 0) (Position 4 15)], |
| 39 | + testCase "Multi-line expressions have a multi-line range" $ do |
| 40 | + runSession hieCommand fullCaps evalPath $ do |
| 41 | + doc <- openDoc "T3.hs" "haskell" |
| 42 | + lenses <- getCodeLenses doc |
| 43 | + liftIO $ map _range lenses @?= [Range (Position 3 0) (Position 4 15)], |
| 44 | + testCase "Executed expressions range covers only the expression" $ do |
| 45 | + runSession hieCommand fullCaps evalPath $ do |
| 46 | + doc <- openDoc "T2.hs" "haskell" |
| 47 | + lenses <- getCodeLenses doc |
| 48 | + liftIO $ map _range lenses @?= [Range (Position 4 0) (Position 4 15)], |
| 49 | + testCase "Evaluation of expressions" $ goldenTest "T1.hs", |
| 50 | + testCase "Reevaluation of expressions" $ goldenTest "T2.hs", |
| 51 | + testCase "Evaluation of expressions w/ imports" $ goldenTest "T3.hs", |
| 52 | + testCase "Evaluation of expressions w/ lets" $ goldenTest "T4.hs" |
| 53 | + ] |
| 54 | + |
| 55 | +goldenTest :: FilePath -> IO () |
| 56 | +goldenTest input = runSession hieCommand fullCaps evalPath $ do |
| 57 | + doc <- openDoc input "haskell" |
| 58 | + [CodeLens {_command = Just c}] <- getCodeLenses doc |
| 59 | + executeCommand c |
| 60 | + _resp :: ApplyWorkspaceEditRequest <- skipManyTill anyMessage message |
| 61 | + edited <- documentContents doc |
| 62 | + expected <- liftIO $ T.readFile $ evalPath </> input <.> "expected" |
| 63 | + liftIO $ edited @?= expected |
| 64 | + |
| 65 | +evalPath :: FilePath |
| 66 | +evalPath = "test/testdata/eval" |
0 commit comments