@@ -687,6 +687,7 @@ codeActionTests = testGroup "code actions"
687
687
, removeRedundantConstraintsTests
688
688
, addTypeAnnotationsToLiteralsTest
689
689
, exportUnusedTests
690
+ , addImplicitParamsConstraintTests
690
691
]
691
692
692
693
codeActionHelperFunctionTests :: TestTree
@@ -2050,59 +2051,95 @@ addFunctionConstraintTests = let
2050
2051
, " return ()"
2051
2052
]
2052
2053
2053
- check :: String -> T. Text -> T. Text -> T. Text -> TestTree
2054
- check testName actionTitle originalCode expectedCode = testSession testName $ do
2055
- doc <- createDoc " Testing.hs" " haskell" originalCode
2056
- _ <- waitForDiagnostics
2057
- actionsOrCommands <- getCodeActions doc (Range (Position 6 0 ) (Position 6 maxBound ))
2058
- chosenAction <- liftIO $ pickActionWithTitle actionTitle actionsOrCommands
2059
- executeCodeAction chosenAction
2060
- modifiedCode <- documentContents doc
2061
- liftIO $ expectedCode @=? modifiedCode
2062
-
2063
2054
in testGroup " add function constraint"
2064
- [ check
2055
+ [ checkCodeAction
2065
2056
" no preexisting constraint"
2066
2057
" Add `Eq a` to the context of the type signature for `eq`"
2067
2058
(missingConstraintSourceCode " " )
2068
2059
(missingConstraintSourceCode " Eq a => " )
2069
- , check
2060
+ , checkCodeAction
2070
2061
" no preexisting constraint, with forall"
2071
2062
" Add `Eq a` to the context of the type signature for `eq`"
2072
2063
(missingConstraintWithForAllSourceCode " " )
2073
2064
(missingConstraintWithForAllSourceCode " Eq a => " )
2074
- , check
2065
+ , checkCodeAction
2075
2066
" preexisting constraint, no parenthesis"
2076
2067
" Add `Eq b` to the context of the type signature for `eq`"
2077
2068
(incompleteConstraintSourceCode " Eq a" )
2078
2069
(incompleteConstraintSourceCode " (Eq a, Eq b)" )
2079
- , check
2070
+ , checkCodeAction
2080
2071
" preexisting constraints in parenthesis"
2081
2072
" Add `Eq c` to the context of the type signature for `eq`"
2082
2073
(incompleteConstraintSourceCode2 " (Eq a, Eq b)" )
2083
2074
(incompleteConstraintSourceCode2 " (Eq a, Eq b, Eq c)" )
2084
- , check
2075
+ , checkCodeAction
2085
2076
" preexisting constraints with forall"
2086
2077
" Add `Eq b` to the context of the type signature for `eq`"
2087
2078
(incompleteConstraintWithForAllSourceCode " Eq a" )
2088
2079
(incompleteConstraintWithForAllSourceCode " (Eq a, Eq b)" )
2089
- , check
2080
+ , checkCodeAction
2090
2081
" preexisting constraint, with extra spaces in context"
2091
2082
" Add `Eq b` to the context of the type signature for `eq`"
2092
2083
(incompleteConstraintSourceCodeWithExtraCharsInContext " Eq a" )
2093
2084
(incompleteConstraintSourceCodeWithExtraCharsInContext " Eq a, Eq b" )
2094
- , check
2085
+ , checkCodeAction
2095
2086
" preexisting constraint, with newlines in type signature"
2096
2087
" Add `Eq b` to the context of the type signature for `eq`"
2097
2088
(incompleteConstraintSourceCodeWithNewlinesInTypeSignature " Eq a" )
2098
2089
(incompleteConstraintSourceCodeWithNewlinesInTypeSignature " Eq a, Eq b" )
2099
- , check
2090
+ , checkCodeAction
2100
2091
" missing Monad constraint"
2101
2092
" Add `Monad m` to the context of the type signature for `f`"
2102
2093
(missingMonadConstraint " " )
2103
2094
(missingMonadConstraint " Monad m => " )
2104
2095
]
2105
2096
2097
+ checkCodeAction :: String -> T. Text -> T. Text -> T. Text -> TestTree
2098
+ checkCodeAction testName actionTitle originalCode expectedCode = testSession testName $ do
2099
+ doc <- createDoc " Testing.hs" " haskell" originalCode
2100
+ _ <- waitForDiagnostics
2101
+ actionsOrCommands <- getCodeActions doc (Range (Position 6 0 ) (Position 6 maxBound ))
2102
+ chosenAction <- liftIO $ pickActionWithTitle actionTitle actionsOrCommands
2103
+ executeCodeAction chosenAction
2104
+ modifiedCode <- documentContents doc
2105
+ liftIO $ expectedCode @=? modifiedCode
2106
+
2107
+ addImplicitParamsConstraintTests :: TestTree
2108
+ addImplicitParamsConstraintTests =
2109
+ testGroup
2110
+ " add missing implicit params constraints"
2111
+ [ testGroup
2112
+ " introduced"
2113
+ [ let ex ctxtA = exampleCode " ?a" ctxtA " "
2114
+ in checkCodeAction " at top level" " Add ?a::() to the context of fBase" (ex " " ) (ex " ?a::()" ),
2115
+ let ex ctxA = exampleCode " x where x = ?a" ctxA " "
2116
+ in checkCodeAction " in nested def" " Add ?a::() to the context of fBase" (ex " " ) (ex " ?a::()" )
2117
+ ],
2118
+ testGroup
2119
+ " inherited"
2120
+ [ let ex = exampleCode " ()" " ?a::()"
2121
+ in checkCodeAction
2122
+ " with preexisting context"
2123
+ " Add `?a::()` to the context of the type signature for `fCaller`"
2124
+ (ex " Eq ()" )
2125
+ (ex " Eq (), ?a::()" ),
2126
+ let ex = exampleCode " ()" " ?a::()"
2127
+ in checkCodeAction " without preexisting context" " Add ?a::() to the context of fCaller" (ex " " ) (ex " ?a::()" )
2128
+ ]
2129
+ ]
2130
+ where
2131
+ mkContext " " = " "
2132
+ mkContext contents = " (" <> contents <> " ) => "
2133
+
2134
+ exampleCode bodyBase contextBase contextCaller =
2135
+ T. unlines
2136
+ [ " {-# LANGUAGE FlexibleContexts, ImplicitParams #-}" ,
2137
+ " module Testing where" ,
2138
+ " fBase :: " <> mkContext contextBase <> " ()" ,
2139
+ " fBase = " <> bodyBase,
2140
+ " fCaller :: " <> mkContext contextCaller <> " ()" ,
2141
+ " fCaller = fBase"
2142
+ ]
2106
2143
removeRedundantConstraintsTests :: TestTree
2107
2144
removeRedundantConstraintsTests = let
2108
2145
header =
0 commit comments