Skip to content

Commit aef7e17

Browse files
author
Ondrej Sebek
committed
Eval plugin: mark exceptions
1 parent fd34887 commit aef7e17

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

Diff for: plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs

+17-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Control.Applicative (Alternative ((<|>)))
2727
import Control.Arrow (second, (>>>))
2828
import Control.Exception (try)
2929
import qualified Control.Exception as E
30-
import Control.Lens (_1, _3, (%~), (<&>), (^.))
30+
import Control.Lens (_1, _3, ix, (%~), (<&>), (^.))
3131
import Control.Monad (guard, join, void, when)
3232
import Control.Monad.IO.Class (MonadIO (liftIO))
3333
import Control.Monad.Trans (lift)
@@ -344,7 +344,7 @@ runTests :: Bool -> TEnv -> [(Section, Test)] -> Ghc [TextEdit]
344344
runTests diff e@(_st, _) tests = do
345345
df <- getInteractiveDynFlags
346346
evalSetup
347-
when (hasQuickCheck df && needsQuickCheck tests) $ void $ evals e df propSetup
347+
when (hasQuickCheck df && needsQuickCheck tests) $ void $ evals True e df propSetup
348348

349349
mapM (processTest e df) tests
350350
where
@@ -368,7 +368,7 @@ runTests diff e@(_st, _) tests = do
368368
return $
369369
singleLine
370370
"Add QuickCheck to your cabal dependencies to run this test."
371-
runTest e df test = evals e df (asStatements test)
371+
runTest e df test = evals (isProperty test) e df (asStatements test)
372372

373373
asEdit :: Format -> Test -> [Text] -> TextEdit
374374
asEdit (MultiLine commRange) test resultLines
@@ -440,8 +440,8 @@ Or for a value that does not have a Show instance and can therefore not be displ
440440
>>> V
441441
No instance for (Show V)
442442
-}
443-
evals :: TEnv -> DynFlags -> [Statement] -> Ghc [Text]
444-
evals (st, fp) df stmts = do
443+
evals :: Bool -> TEnv -> DynFlags -> [Statement] -> Ghc [Text]
444+
evals property (st, fp) df stmts = do
445445
er <- gStrictTry $ mapM eval stmts
446446
return $ case er of
447447
Left err -> errorLines err
@@ -488,9 +488,9 @@ evals (st, fp) df stmts = do
488488
do
489489
dbg "{STMT " stmt
490490
res <- exec stmt l
491-
r <- case res of
492-
Left err -> return . Just . errorLines $ err
493-
Right x -> return $ singleLine <$> x
491+
let r = case res of
492+
Left err -> Just . (if property then errorLines else exceptionLines) $ err
493+
Right x -> singleLine <$> x
494494
dbg "STMT} -> " r
495495
return r
496496
| -- An import
@@ -556,6 +556,15 @@ errorLines =
556556
. T.lines
557557
. T.pack
558558

559+
{- |
560+
Convert exception messages to a list of text lines
561+
Remove unnecessary information and mark it as exception.
562+
We use '*** Exception:' to make it identical to doctest
563+
output, see #2353.
564+
-}
565+
exceptionLines :: String -> [Text]
566+
exceptionLines = (ix 0 %~ ("*** Exception: " <>)) . errorLines
567+
559568
{- |
560569
>>> map (pad_ (T.pack "--")) (map T.pack ["2+2",""])
561570
["--2+2","--<BLANKLINE>"]

Diff for: plugins/hls-eval-plugin/test/Main.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ tests =
7676
| ghcVersion == GHC90 -> "-- No instance for (Num String) arising from a use of ‘+’"
7777
| otherwise -> "-- No instance for (Num [Char]) arising from a use of ‘+’"
7878
evalInFile "T8.hs" "-- >>> \"" "-- lexical error in string/character literal at end of input"
79-
evalInFile "T8.hs" "-- >>> 3 `div` 0" "-- divide by zero"
79+
evalInFile "T8.hs" "-- >>> 3 `div` 0" "-- *** Exception: divide by zero"
80+
, goldenWithEval "Evaluates to exception" "TException" "hs"
8081
, goldenWithEval "Applies file LANGUAGE extensions" "T9" "hs"
8182
, goldenWithEval' "Evaluate a type with :kind!" "T10" "hs" (if ghcVersion == GHC92 then "ghc92.expected" else "expected")
8283
, goldenWithEval' "Reports an error for an incorrect type with :kind!" "T11" "hs" (if ghcVersion == GHC92 then "ghc92.expected" else "expected")
@@ -133,6 +134,7 @@ tests =
133134
, goldenWithEval "The default language extensions for the eval plugin are the same as those for ghci" "TSameDefaultLanguageExtensionsAsGhci" "hs"
134135
, goldenWithEval "IO expressions are supported, stdout/stderr output is ignored" "TIO" "hs"
135136
, goldenWithEval "Property checking" "TProperty" "hs"
137+
, goldenWithEval "Property checking with exception" "TPropertyError" "hs"
136138
, goldenWithEval "Prelude has no special treatment, it is imported as stated in the module" "TPrelude" "hs"
137139
, goldenWithEval "Don't panic on {-# UNPACK #-} pragma" "TUNPACK" "hs"
138140
, goldenWithEval "Can handle eval inside nested comment properly" "TNested" "hs"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module TException where
2+
3+
-- >>> exceptionalCode
4+
-- *** Exception: I am exceptional!
5+
exceptionalCode :: Int
6+
exceptionalCode = error "I am exceptional!"

Diff for: plugins/hls-eval-plugin/test/testdata/TException.hs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module TException where
2+
3+
-- >>> exceptionalCode
4+
exceptionalCode :: Int
5+
exceptionalCode = error "I am exceptional!"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Support for property checking
2+
module TProperty where
3+
4+
-- prop> \(l::[Bool]) -> head l
5+
-- *** Failed! Exception: 'Prelude.head: empty list' (after 1 test):
6+
-- []
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Support for property checking
2+
module TProperty where
3+
4+
-- prop> \(l::[Bool]) -> head l

0 commit comments

Comments
 (0)