Skip to content

Commit 94a93da

Browse files
committed
eval: avoid usage of term "test"; instead use "evalExpr"
1 parent da2d822 commit 94a93da

5 files changed

Lines changed: 172 additions & 167 deletions

File tree

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
-- | Expression execution
66
module Ide.Plugin.Eval.Code (
77
Statement
8-
, testRanges
8+
, evalExprRanges
99
, resultRange
1010
, propSetup
11-
, testCheck
11+
, evalExprCheck
1212
, asStatements
1313
, execStmtCaptureResult
1414
) where
@@ -27,21 +27,21 @@ import GHC (ExecOptions, ExecResult (..),
2727
import Ide.Logger (Recorder, WithPriority, logWith)
2828
import qualified Ide.Logger as Log
2929

30-
import Ide.Plugin.Eval.Types (Language (Plain), Loc,
31-
Located (..), Log (..),
32-
Section (sectionLanguage),
33-
Test (..), Txt, locate, locate0)
30+
import Ide.Plugin.Eval.Types (EvalExpr (..), Language (Plain),
31+
Loc, Located (..), Log (..),
32+
Section (sectionLanguage), Txt,
33+
locate, locate0)
3434
import Ide.Plugin.Eval.Util (gStrictTry)
3535
import qualified Language.LSP.Protocol.Lens as L
3636
import Language.LSP.Protocol.Types (Position (Position),
3737
Range (Range))
3838
import System.IO.Extra (newTempFile, readFile')
3939

40-
-- | Return the ranges of the expression and result parts of the given test
41-
testRanges :: Test -> (Range, Range)
42-
testRanges tst =
43-
let startLine = testRange tst ^. L.start . L.line
44-
(fromIntegral -> exprLines, fromIntegral -> resultLines) = testLengths tst
40+
-- | Return the ranges of the expression and result parts of the given 'EvalExpr'.
41+
evalExprRanges :: EvalExpr -> (Range, Range)
42+
evalExprRanges tst =
43+
let startLine = evalExprRange tst ^. L.start . L.line
44+
(fromIntegral -> exprLines, fromIntegral -> resultLines) = evalExprLengths tst
4545
resLine = startLine + exprLines
4646
in ( Range
4747
(Position startLine 0)
@@ -50,14 +50,9 @@ testRanges tst =
5050
, Range (Position resLine 0) (Position (resLine + resultLines) 0)
5151
)
5252

53-
{- |The document range where a test is defined
54-
testRange :: Loc Test -> Range
55-
testRange = fst . testRanges
56-
-}
57-
58-
-- |The document range where the result of the test is defined
59-
resultRange :: Test -> Range
60-
resultRange = snd . testRanges
53+
-- | The document range where the result of the 'EvalExpr' is defined.
54+
resultRange :: EvalExpr -> Range
55+
resultRange = snd . evalExprRanges
6156

6257
-- TODO: handle BLANKLINE
6358
{-
@@ -72,30 +67,31 @@ showDiff (First w) = "WAS " <> w
7267
showDiff (Second w) = "NOW " <> w
7368
showDiff (Both w _) = w
7469

75-
-- | Compare the expected output recorded in the test with the actual output
76-
-- @out@. When diffing is enabled and there is a recorded output, return a
77-
-- line-by-line diff (see 'showDiffs'); otherwise return @out@ unchanged.
78-
testCheck :: Bool -> (Section, Test) -> [T.Text] -> [T.Text]
79-
testCheck diff (section, test) out
80-
| not diff || null (testOutput test) || sectionLanguage section == Plain = out
81-
| otherwise = showDiffs $ getDiff (map T.pack $ testOutput test) out
70+
-- | Compare the expected output recorded in the 'EvalExpr' with the actual
71+
-- output @out@. When diffing is enabled and there is a recorded output, return
72+
-- a line-by-line diff (see 'showDiffs'); otherwise return @out@ unchanged.
73+
evalExprCheck :: Bool -> (Section, EvalExpr) -> [T.Text] -> [T.Text]
74+
evalExprCheck diff (section, evalExpr) out
75+
| not diff || null (evalExprOutput evalExpr) || sectionLanguage section == Plain = out
76+
| otherwise = showDiffs $ getDiff (map T.pack $ evalExprOutput evalExpr) out
8277

83-
-- | The number of (expression lines, result lines) a test occupies.
84-
testLengths :: Test -> (Int, Int)
85-
testLengths (Example e r _) = (NE.length e, length r)
86-
testLengths (Property _ r _) = (1, length r)
78+
-- | The number of (expression lines, result lines) an 'EvalExpr' occupies.
79+
evalExprLengths :: EvalExpr -> (Int, Int)
80+
evalExprLengths (Example e r _) = (NE.length e, length r)
81+
evalExprLengths (Property _ r _) = (1, length r)
8782

8883
-- |A one-line Haskell statement
8984
type Statement = Loc String
9085

91-
-- | The Haskell statements to feed to GHCi for a test, each tagged with its
92-
-- source line so evaluation errors can be located.
93-
asStatements :: Test -> [Statement]
94-
asStatements lt = locate $ Located (fromIntegral $ testRange lt ^. L.start . L.line) (asStmts lt)
86+
-- | The Haskell statements to feed to GHCi for an 'EvalExpr', each tagged with
87+
-- its source line so evaluation errors can be located.
88+
asStatements :: EvalExpr -> [Statement]
89+
asStatements lt =
90+
locate $ Located (fromIntegral $ evalExprRange lt ^. L.start . L.line) (asStmts lt)
9591

96-
-- | The raw statement lines of a test. A 'Property' is wrapped so its result
97-
-- is evaluated through 'propEvaluation' (see 'propSetup').
98-
asStmts :: Test -> [Txt]
92+
-- | The raw statement lines of an 'EvalExpr'. A 'Property' is wrapped so its
93+
-- result is evaluated through 'propEvaluation' (see 'propSetup').
94+
asStmts :: EvalExpr -> [Txt]
9995
asStmts (Example e _ _) = NE.toList e
10096
asStmts (Property t _ _) =
10197
["prop11 = " ++ t, "(propEvaluation prop11 :: IO String)"]
@@ -210,15 +206,15 @@ captureTeardown = unwords
210206
, " }"
211207
]
212208

213-
{- |GHC declarations required to execute test properties
209+
{- | GHC declarations required to evaluate property 'EvalExprs'
214210
215211
Example:
216212
217213
prop> \(l::[Bool]) -> reverse (reverse l) == l
218-
+++ OK, passed 100 tests.
214+
+++ OK, passed 100 evalExprs.
219215
220216
prop> \(l::[Bool]) -> reverse l == l
221-
*** Failed! Falsified (after 6 tests and 2 shrinks):
217+
*** Failed! Falsified (after 6 evalExprs and 2 shrinks):
222218
[True,False]
223219
-}
224220
propSetup :: [Loc [Char]]

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Handlers.hs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ import Ide.Plugin.Eval.Code (Statement,
102102
execStmtCaptureResult,
103103
propSetup,
104104
resultRange,
105-
testCheck,
106-
testRanges)
105+
evalExprCheck,
106+
evalExprRanges)
107107
import Ide.Plugin.Eval.Config (EvalConfig (..),
108108
getEvalConfig)
109109
import Ide.Plugin.Eval.GHC (addImport,
@@ -134,8 +134,8 @@ codeAction recorder st plId CodeActionParams{_textDocument,_range} = do
134134
pure
135135
$ InL
136136
[ InL command
137-
| (testRange, command) <- rangeCommands
138-
, _range `isSubrangeOf` testRange
137+
| (evalExprRange, command) <- rangeCommands
138+
, _range `isSubrangeOf` evalExprRange
139139
]
140140

141141
{- | Code Lens provider
@@ -150,7 +150,7 @@ codeLens recorder st plId CodeLensParams{_textDocument} = do
150150
| (range, command) <- rangeCommands
151151
]
152152

153-
-- | Find every test in the document and pair its source range with the
153+
-- | Find every eval-expr in the document and pair its source range with the
154154
-- 'Command' that evaluates it. Shared by the code action and code lens
155155
-- providers.
156156
mkRangeCommands :: Recorder (WithPriority Log) -> IdeState -> PluginId -> TextDocumentIdentifier -> ExceptT PluginError (HandlerM Config) [(Range, Command)]
@@ -168,14 +168,14 @@ mkRangeCommands recorder st plId textDocument =
168168
runActionE "eval.GetParsedModuleWithComments" st $ useWithStaleE GetEvalComments nfp
169169
dbg $ LogCodeLensComments comments
170170

171-
-- Extract tests from source code
171+
-- Extract 'EvalExpr's from source code
172172
let Sections{..} = commentsToSections isLHS comments
173-
tests = testsBySection nonSetupSections
173+
evalExprs = evalExprsBySection nonSetupSections
174174
cmd = mkLspCommand plId evalCommandName "Evaluate=..." (Just [])
175175
let rangeCommands =
176-
[ (testRange, cmd')
177-
| (section, ident, test) <- tests
178-
, let (testRange, resultRange) = testRanges test
176+
[ (evalExprRange, cmd')
177+
| (section, ident, evalExpr) <- evalExprs
178+
, let (evalExprRange, resultRange) = evalExprRanges evalExpr
179179
args = EvalParams (setupSections ++ [section]) textDocument ident
180180
cmd' =
181181
(cmd :: Command)
@@ -187,9 +187,9 @@ mkRangeCommands recorder st plId textDocument =
187187
}
188188
]
189189

190-
perf "tests" $
191-
dbg $ LogTests
192-
(length tests)
190+
perf "evalExprs" $
191+
dbg $ LogEvalExprs
192+
(length evalExprs)
193193
(length nonSetupSections)
194194
(length setupSections)
195195
(length rangeCommands)
@@ -212,7 +212,7 @@ runEvalCmd recorder plId st mtoken EvalParams{..} =
212212
perf = timed (\lbl duration -> dbg $ LogExecutionTime lbl duration)
213213
cmd :: ExceptT PluginError (HandlerM Config) WorkspaceEdit
214214
cmd = do
215-
let tests = map (\(a,_,b) -> (a,b)) $ testsBySection sections
215+
let evalExprs = map (\(a,_,b) -> (a,b)) $ evalExprsBySection sections
216216

217217
let TextDocumentIdentifier{_uri} = module_
218218
fp <- uriToFilePathE _uri
@@ -229,7 +229,7 @@ runEvalCmd recorder plId st mtoken EvalParams{..} =
229229
unqueueForEvaluation st nfp
230230
return [toKey IsEvaluating nfp]
231231
)
232-
(initialiseSessionForEval (needsQuickCheck tests) st nfp)
232+
(initialiseSessionForEval (needsQuickCheck evalExprs) st nfp)
233233

234234
evalCfg <- liftIO $ runAction "eval: config" st $ getEvalConfig plId
235235

@@ -238,7 +238,7 @@ runEvalCmd recorder plId st mtoken EvalParams{..} =
238238
perf "edits" $
239239
liftIO $
240240
evalGhcEnv final_hscEnv $ do
241-
runTests recorder evalCfg fp tests
241+
runEvalExprs recorder evalCfg fp evalExprs
242242

243243
let workspaceEditsMap = Map.singleton _uri (addFinalReturn mdlText edits)
244244
let workspaceEdits = WorkspaceEdit (Just workspaceEditsMap) Nothing Nothing
@@ -359,13 +359,13 @@ moduleText state uri = do
359359
toNormalizedUri uri
360360
pure $ Rope.toText contents
361361

362-
-- | Flatten sections into their individual tests, tagging each with the index
362+
-- | Flatten sections into their individual 'EvalExpr's, tagging each with the index
363363
-- ('EvalId') of its containing section.
364-
testsBySection :: [Section] -> [(Section, EvalId, Test)]
365-
testsBySection sections =
366-
[(section, ident, test)
364+
evalExprsBySection :: [Section] -> [(Section, EvalId, EvalExpr)]
365+
evalExprsBySection sections =
366+
[(section, ident, evalExpr)
367367
| (ident, section) <- zip [0..] sections
368-
, test <- sectionTests section
368+
, evalExpr <- sectionEvalExprs section
369369
]
370370

371371
type TEnv = String
@@ -382,59 +382,59 @@ evalSetup = do
382382
context <- getContext
383383
setContext (IIDecl preludeAsP : IIDecl systemIO : IIDecl ghcIOHandle : context)
384384

385-
-- | Evaluate every test and produce the 'TextEdit's that write the results
385+
-- | Evaluate every 'EvalExpr' and produce the 'TextEdit's that write the results
386386
-- back into the document, prefixing/padding each result line as the section's
387387
-- format requires.
388-
runTests :: Recorder (WithPriority Log) -> EvalConfig -> TEnv -> [(Section, Test)] -> Ghc [TextEdit]
389-
runTests recorder EvalConfig{..} e tests = do
388+
runEvalExprs :: Recorder (WithPriority Log) -> EvalConfig -> TEnv -> [(Section, EvalExpr)] -> Ghc [TextEdit]
389+
runEvalExprs recorder EvalConfig{..} e evalExprs = do
390390
df <- getInteractiveDynFlags
391391
evalSetup
392-
when (hasQuickCheck df && needsQuickCheck tests) $ void $ evals recorder True e df propSetup
392+
when (hasQuickCheck df && needsQuickCheck evalExprs) $ void $ evals recorder True e df propSetup
393393

394-
mapM (processTest e df) tests
394+
mapM (processEvalExpr e df) evalExprs
395395
where
396-
processTest :: TEnv -> DynFlags -> (Section, Test) -> Ghc TextEdit
397-
processTest fp df (section, test) = do
396+
processEvalExpr :: TEnv -> DynFlags -> (Section, EvalExpr) -> Ghc TextEdit
397+
processEvalExpr fp df (section, evalExpr) = do
398398
let dbg = logWith recorder Debug
399399
let pad = pad_ $ (if isLiterate fp then ("> " `T.append`) else id) $ padPrefix (sectionFormat section)
400-
rs <- runTest e df test
401-
dbg $ LogRunTestResults rs
400+
rs <- runEvalExpr e df evalExpr
401+
dbg $ LogRunEvalExprResults rs
402402

403-
let checkedResult = testCheck eval_cfg_diff (section, test) rs
403+
let checkedResult = evalExprCheck eval_cfg_diff (section, evalExpr) rs
404404
let resultLines = concatMap T.lines checkedResult
405405

406-
let edit = asEdit (sectionFormat section) test (map pad resultLines)
407-
dbg $ LogRunTestEdits edit
406+
let edit = asEdit (sectionFormat section) evalExpr (map pad resultLines)
407+
dbg $ LogRunEvalExprEdits edit
408408
return edit
409409

410-
-- runTest :: String -> DynFlags -> Loc Test -> Ghc [Text]
411-
runTest _ df test
412-
| not (hasQuickCheck df) && isProperty test =
410+
-- runEvalExpr :: String -> DynFlags -> Loc EvalExpr -> Ghc [Text]
411+
runEvalExpr _ df evalExpr
412+
| not (hasQuickCheck df) && isProperty evalExpr =
413413
return $
414414
singleLine
415-
"Add QuickCheck to your cabal dependencies to run this test."
416-
runTest e df test = evals recorder (eval_cfg_exception && not (isProperty test)) e df (asStatements test)
417-
418-
-- | Build the edit that replaces a test's old result with @resultLines@. For a
419-
-- test that sits on the closing @-}@ line of a block comment, the result is
420-
-- inserted before @-}@ on fresh lines; otherwise it simply overwrites the
421-
-- existing result range.
422-
asEdit :: Format -> Test -> [Text] -> TextEdit
423-
asEdit (MultiLine commRange) test resultLines
424-
-- A test in a block comment, ending with @-\}@ without newline in-between.
425-
| testRange test ^. L.end . L.line == commRange ^. L.end . L.line
415+
"Add QuickCheck to your cabal dependencies to run this property."
416+
runEvalExpr e df evalExpr = evals recorder (eval_cfg_exception && not (isProperty evalExpr)) e df (asStatements evalExpr)
417+
418+
-- | Build the edit that replaces the old result of an 'EvalExpr' with
419+
-- @resultLines@. For an 'EvalExpr' that sits on the closing @-}@ line of a
420+
-- block comment, the result is inserted before @-}@ on fresh lines; otherwise
421+
-- it simply overwrites the existing result range.
422+
asEdit :: Format -> EvalExpr -> [Text] -> TextEdit
423+
asEdit (MultiLine commRange) evalExpr resultLines
424+
-- An 'EvalExpr' in a block comment, ending with @-\}@ without newline in-between.
425+
| evalExprRange evalExpr ^. L.end . L.line == commRange ^. L.end . L.line
426426
=
427427
TextEdit
428428
(Range
429-
(testRange test ^. L.end)
430-
(resultRange test ^. L.end)
429+
(evalExprRange evalExpr ^. L.end)
430+
(resultRange evalExpr ^. L.end)
431431
)
432432
("\n" <> T.unlines (resultLines <> ["-}"]))
433-
asEdit _ test resultLines =
434-
TextEdit (resultRange test) (T.unlines resultLines)
433+
asEdit _ evalExpr resultLines =
434+
TextEdit (resultRange evalExpr) (T.unlines resultLines)
435435

436436
{- |
437-
The result of evaluating a test line can be:
437+
The result of evaluating an eval-expr line can be:
438438
* a value
439439
* nothing
440440
* a (possibly multiline) error message
@@ -576,7 +576,7 @@ evals recorder mark_exception fp df stmts = do
576576
let opts = execOptions{execSourceFile = fp, execLineNumber = l}
577577
in execStmtCaptureResult recorder stmt opts
578578

579-
needsQuickCheck :: [(Section, Test)] -> Bool
579+
needsQuickCheck :: [(Section, EvalExpr)] -> Bool
580580
needsQuickCheck = any (isProperty . snd)
581581

582582
hasQuickCheck :: DynFlags -> Bool

0 commit comments

Comments
 (0)