@@ -2,6 +2,8 @@
22-- SPDX-License-Identifier: Apache-2.0
33
44{-# LANGUAGE DuplicateRecordFields #-}
5+{-# LANGUAGE CPP #-}
6+#include "ghc-api-version.h"
57
68module Main (main) where
79
@@ -232,6 +234,7 @@ codeActionTests = testGroup "code actions"
232234 , typeWildCardActionTests
233235 , removeImportTests
234236 , importRenameActionTests
237+ , fillTypedHoleTests
235238 ]
236239
237240renameActionTests :: TestTree
@@ -453,6 +456,75 @@ importRenameActionTests = testGroup "import rename actions"
453456 ]
454457 liftIO $ expectedContentAfterAction @=? contentAfterAction
455458
459+fillTypedHoleTests :: TestTree
460+fillTypedHoleTests = let
461+
462+ sourceCode :: T.Text -> T.Text -> T.Text -> T.Text
463+ sourceCode a b c = T.unlines
464+ [ "module Testing where"
465+ , ""
466+ , "globalConvert :: Int -> String"
467+ , "globalConvert = undefined"
468+ , ""
469+ , "globalInt :: Int"
470+ , "globalInt = 3"
471+ , ""
472+ , "bar :: Int -> Int -> String"
473+ , "bar n parameterInt = " <> a <> " (n + " <> b <> " + " <> c <> ") where"
474+ , " localConvert = (flip replicate) 'x'"
475+
476+ ]
477+
478+ check :: T.Text -> T.Text -> T.Text -> T.Text -> T.Text -> T.Text -> T.Text -> TestTree
479+ check actionTitle
480+ oldA oldB oldC
481+ newA newB newC = testSession (T.unpack actionTitle) $ do
482+ let originalCode = sourceCode oldA oldB oldC
483+ let expectedCode = sourceCode newA newB newC
484+ doc <- openDoc' "Testing.hs" "haskell" originalCode
485+ _ <- waitForDiagnostics
486+ actionsOrCommands <- getCodeActions doc (Range (Position 9 0) (Position 9 maxBound))
487+ let chosenAction = pickActionWithTitle actionTitle actionsOrCommands
488+ executeCodeAction chosenAction
489+ modifiedCode <- documentContents doc
490+ liftIO $ expectedCode @=? modifiedCode
491+
492+ pickActionWithTitle title actions = head
493+ [ action
494+ | CACodeAction action@CodeAction{ _title = actionTitle } <- actions
495+ , title == actionTitle ]
496+
497+ in
498+ testGroup "fill typed holes"
499+ [ check "replace hole `_` with show"
500+ "_" "n" "n"
501+ "show" "n" "n"
502+
503+ , check "replace hole `_` with globalConvert"
504+ "_" "n" "n"
505+ "globalConvert" "n" "n"
506+
507+#if MIN_GHC_API_VERSION(8,6,0)
508+ , check "replace hole `_convertme` with localConvert"
509+ "_convertme" "n" "n"
510+ "localConvert" "n" "n"
511+#endif
512+
513+ , check "replace hole `_b` with globalInt"
514+ "_a" "_b" "_c"
515+ "_a" "globalInt" "_c"
516+
517+ , check "replace hole `_c` with globalInt"
518+ "_a" "_b" "_c"
519+ "_a" "_b" "globalInt"
520+
521+#if MIN_GHC_API_VERSION(8,6,0)
522+ , check "replace hole `_c` with parameterInt"
523+ "_a" "_b" "_c"
524+ "_a" "_b" "parameterInt"
525+#endif
526+ ]
527+
456528----------------------------------------------------------------------
457529-- Utils
458530