|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | +-- {-# LANGUAGE ViewPatterns #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +module Class |
| 6 | + ( tests |
| 7 | + ) |
| 8 | +where |
| 9 | + |
| 10 | +import Control.Lens hiding ((<.>)) |
| 11 | +import Control.Monad.IO.Class (MonadIO(liftIO)) |
| 12 | +import qualified Data.ByteString.Lazy as BS |
| 13 | +import qualified Data.Text.Encoding as T |
| 14 | +import Language.Haskell.LSP.Test |
| 15 | +import Language.Haskell.LSP.Types hiding (_title, _command) |
| 16 | +import qualified Language.Haskell.LSP.Types.Lens as J |
| 17 | +import System.FilePath |
| 18 | +import Test.Hls.Util |
| 19 | +import Test.Tasty |
| 20 | +import Test.Tasty.Golden |
| 21 | +import Test.Tasty.HUnit |
| 22 | + |
| 23 | +tests :: TestTree |
| 24 | +tests = testGroup |
| 25 | + "class" |
| 26 | + [ testCase "Produces addMinimalMethodPlaceholders code actions for one instance" $ do |
| 27 | + runSession hlsCommand fullCaps classPath $ do |
| 28 | + doc <- openDoc "T1.hs" "haskell" |
| 29 | + _ <- waitForDiagnosticsFromSource doc "typecheck" |
| 30 | + caResults <- getAllCodeActions doc |
| 31 | + liftIO $ map (^? _CACodeAction . J.title) caResults |
| 32 | + @?= |
| 33 | + [ Just "Add placeholders for '=='" |
| 34 | + , Just "Add placeholders for '/='" |
| 35 | + ] |
| 36 | + , glodenTest "Creates a placeholder for '=='" "T1" "eq" |
| 37 | + $ \(eqAction:_) -> do |
| 38 | + executeCodeAction eqAction |
| 39 | + , glodenTest "Creates a placeholder for '/='" "T1" "ne" |
| 40 | + $ \(_:neAction:_) -> do |
| 41 | + executeCodeAction neAction |
| 42 | + , glodenTest "Creates a placeholder for 'fmap'" "T2" "fmap" |
| 43 | + $ \(_:_:fmapAction:_) -> do |
| 44 | + executeCodeAction fmapAction |
| 45 | + , glodenTest "Creates a placeholder for multiple methods 1" "T3" "1" |
| 46 | + $ \(mmAction:_) -> do |
| 47 | + executeCodeAction mmAction |
| 48 | + , glodenTest "Creates a placeholder for multiple methods 2" "T3" "2" |
| 49 | + $ \(_:mmAction:_) -> do |
| 50 | + executeCodeAction mmAction |
| 51 | + ] |
| 52 | + |
| 53 | +_CACodeAction :: Prism' CAResult CodeAction |
| 54 | +_CACodeAction = prism' CACodeAction $ \case |
| 55 | + CACodeAction action -> Just action |
| 56 | + _ -> Nothing |
| 57 | + |
| 58 | +classPath :: FilePath |
| 59 | +classPath = "test" </> "testdata" </> "class" |
| 60 | + |
| 61 | +glodenTest :: String -> FilePath -> FilePath -> ([CodeAction] -> Session ()) -> TestTree |
| 62 | +glodenTest name fp deco execute |
| 63 | + = goldenVsStringDiff name goldenGitDiff (classPath </> fp <.> deco <.> "expected" <.> "hs") |
| 64 | + $ runSession hlsCommand fullCaps classPath |
| 65 | + $ do |
| 66 | + doc <- openDoc (fp <.> "hs") "haskell" |
| 67 | + _ <- waitForDiagnosticsFromSource doc "typecheck" |
| 68 | + actions <- concatMap (^.. _CACodeAction) <$> getAllCodeActions doc |
| 69 | + execute actions |
| 70 | + BS.fromStrict . T.encodeUtf8 <$> getDocumentEdit doc |
| 71 | + |
| 72 | +goldenGitDiff :: FilePath -> FilePath -> [String] |
| 73 | +goldenGitDiff fRef fNew = ["git", "diff", "--no-index", "--text", "--exit-code", fRef, fNew] |
0 commit comments