@@ -17,6 +17,7 @@ import Control.Lens ((^.))
1717import Control.Monad.Catch (MonadMask , bracket )
1818import Control.Monad.IO.Class
1919import Data.Algorithm.Diff (Diff , PolyDiff (.. ), getDiff )
20+ import Data.List (dropWhileEnd )
2021import qualified Data.List.NonEmpty as NE
2122import Data.Maybe (listToMaybe )
2223import Data.String (IsString )
@@ -45,7 +46,6 @@ evalExprRanges tst =
4546 resLine = startLine + exprLines
4647 in ( Range
4748 (Position startLine 0 )
48- -- (Position (startLine + exprLines + resultLines) 0),
4949 (Position resLine 0 )
5050 , Range (Position resLine 0 ) (Position (resLine + resultLines) 0 )
5151 )
@@ -54,16 +54,15 @@ evalExprRanges tst =
5454resultRange :: EvalExpr -> Range
5555resultRange = snd . evalExprRanges
5656
57- -- TODO: handle BLANKLINE
58- {-
59- >>> showDiffs $ getDiff ["abc","def","ghi","end"] ["abc","def","Z","ZZ","end"]
57+ {- |
58+ >>> showDiffs $ getDiff ["abc" :: String,"def","ghi","end"] ["abc","def","Z","ZZ","end"]
6059["abc","def","WAS ghi","NOW Z","NOW ZZ","end"]
6160-}
6261showDiffs :: (Semigroup a , IsString a ) => [Diff a ] -> [a ]
6362showDiffs = map showDiff
6463
6564showDiff :: (Semigroup a , IsString a ) => Diff a -> a
66- showDiff (First w) = " WAS " <> w
65+ showDiff (First w) = " WAS " <> w
6766showDiff (Second w) = " NOW " <> w
6867showDiff (Both w _) = w
6968
@@ -78,8 +77,13 @@ showDiff (Both w _) = w
7877-- this, identical multi-line results would be reported as entirely changed.
7978evalExprCheck :: Bool -> (Section , EvalExpr ) -> [T. Text ] -> [T. Text ]
8079evalExprCheck diff (section, evalExpr) out
81- | not diff || null (evalExprOutput evalExpr) || sectionLanguage section == Plain = outLines
82- | otherwise = showDiffs $ getDiff (map T. pack $ evalExprOutput evalExpr) outLines
80+ | not diff
81+ || null (evalExprOutput evalExpr)
82+ || sectionLanguage section == Plain =
83+ outLines
84+ | otherwise =
85+ showDiffs $
86+ getDiff (map T. pack $ evalExprOutput evalExpr) outLines
8387 where
8488 outLines = concatMap T. lines out
8589
@@ -88,7 +92,7 @@ evalExprLengths :: EvalExpr -> (Int, Int)
8892evalExprLengths (Example e r _) = (NE. length e, length r)
8993evalExprLengths (Property _ r _) = (1 , length r)
9094
91- -- | A one-line Haskell statement
95+ -- | A one-line Haskell statement
9296type Statement = Loc String
9397
9498-- | The Haskell statements to feed to GHCi for an 'EvalExpr', each tagged with
@@ -121,14 +125,27 @@ execStmtCaptureResult recorder stmt opts = do
121125 Right (ExecComplete (Left err) _) ->
122126 pure $ Left $ show err
123127 Right (ExecComplete (Right _) _) -> do
124- pure $ Right $ toMaybe (output <> result)
128+ pure $ Right $ toMaybe (combine output result)
125129 Right ExecBreak {} ->
126130 pure $ Right $ Just " breakpoints are not supported"
127131 where
128132 toMaybe :: String -> Maybe String
129133 toMaybe x | null x = Nothing
130134 | otherwise = Just x
131135
136+ -- Join the captured stdout/stderr output with the result value. GHC
137+ -- diagnostics (e.g. warnings) written to stderr end with a trailing blank
138+ -- line; drop trailing newlines so output and result are separated by a
139+ -- single newline rather than a spurious blank line (which would otherwise
140+ -- surface unexpectedly in the rendered result).
141+ combine :: String -> String -> String
142+ combine output result
143+ | null trimmed = result
144+ | null result = trimmed
145+ | otherwise = trimmed <> " \n " <> result
146+ where
147+ trimmed = dropWhileEnd (== ' \n ' ) output
148+
132149-- 'System.IO.Extra.withTempFile' is specialized to 'IO'.
133150withTempFile :: (MonadIO m , MonadMask m ) => (FilePath -> m b ) -> m (String , b )
134151withTempFile k = do
@@ -219,10 +236,10 @@ captureTeardown = unwords
219236Example:
220237
221238prop> \(l::[Bool]) -> reverse (reverse l) == l
222- +++ OK, passed 100 evalExprs .
239+ +++ OK, passed 100 tests .
223240
224241prop> \(l::[Bool]) -> reverse l == l
225- *** Failed! Falsified (after 6 evalExprs and 2 shrinks ):
242+ *** Failed! Falsified (after 4 tests and 1 shrink ):
226243[True,False]
227244-}
228245propSetup :: [Loc [Char ]]
0 commit comments