doctest 0.12.0 → 0.13.0
raw patch · 4 files changed
+61/−35 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- doctest.cabal +1/−1
- src/Options.hs +30/−10
- src/Run.hs +6/−6
- src/Runner.hs +24/−18
doctest.cabal view
@@ -1,5 +1,5 @@ name: doctest-version: 0.12.0+version: 0.13.0 synopsis: Test interactive Haskell examples description: The doctest program checks examples in source code comments. It is modeled after doctest for Python
src/Options.hs view
@@ -3,6 +3,9 @@ module Options ( Result(..) , Run(..)+, defaultMagic+, defaultFastMode+, defaultPreserveIt , parseOptions #ifdef TEST , usage@@ -25,16 +28,17 @@ usage :: String usage = unlines [ "Usage:"- , " doctest [ --fast | --no-magic | GHC OPTION | MODULE ]..."+ , " doctest [ --fast | --preserve-it | --no-magic | GHC OPTION | MODULE ]..." , " doctest --help" , " doctest --version" , " doctest --info" , "" , "Options:"- , " --fast disable :reload between example groups"- , " --help display this help and exit"- , " --version output version information and exit"- , " --info output machine-readable version information and exit"+ , " --fast disable :reload between example groups"+ , " --preserve-it preserve the `it` variable between examples"+ , " --help display this help and exit"+ , " --version output version information and exit"+ , " --info output machine-readable version information and exit" ] version :: String@@ -67,22 +71,38 @@ , runOptions :: [String] , runMagicMode :: Bool , runFastMode :: Bool+, runPreserveIt :: Bool } deriving (Eq, Show) +defaultMagic :: Bool+defaultMagic = True++defaultFastMode :: Bool+defaultFastMode = False++defaultPreserveIt :: Bool+defaultPreserveIt = False+ parseOptions :: [String] -> Result Run parseOptions args | "--help" `elem` args = Output usage | "--info" `elem` args = Output info | "--version" `elem` args = Output versionInfo- | otherwise = case fmap stripOptGhc . stripFast <$> stripNoMagic args of- (magicMode, (fastMode, (warning, xs))) ->- Result (Run (maybeToList warning) xs magicMode fastMode)+ | otherwise = case fmap (fmap stripOptGhc)+ . fmap stripPreserveIt+ . stripFast+ <$> stripNoMagic args of+ (magicMode, (fastMode, (preserveIt, (warning, xs)))) ->+ Result (Run (maybeToList warning) xs magicMode fastMode preserveIt) stripNoMagic :: [String] -> (Bool, [String])-stripNoMagic = stripFlag False "--no-magic"+stripNoMagic = stripFlag (not defaultMagic) "--no-magic" stripFast :: [String] -> (Bool, [String])-stripFast = stripFlag True "--fast"+stripFast = stripFlag (not defaultFastMode) "--fast"++stripPreserveIt :: [String] -> (Bool, [String])+stripPreserveIt = stripFlag (not defaultPreserveIt) "--preserve-it" stripFlag :: Bool -> String -> [String] -> (Bool, [String]) stripFlag enableIt flag args = ((flag `elem` args) == enableIt, filter (/= flag) args)
src/Run.hs view
@@ -2,7 +2,7 @@ module Run ( doctest #ifdef TEST-, doctestWithFastMode+, doctestWithOptions , Summary , expandDirs #endif@@ -43,7 +43,7 @@ doctest :: [String] -> IO () doctest args0 = case parseOptions args0 of Output s -> putStr s- Result (Run warnings args_ magicMode fastMode) -> do+ Result (Run warnings args_ magicMode fastMode preserveIt) -> do mapM_ (hPutStrLn stderr) warnings hFlush stderr @@ -60,7 +60,7 @@ addDistArgs <- getAddDistArgs return (addDistArgs $ packageDBArgs ++ expandedArgs) - r <- doctestWithFastMode fastMode args `E.catch` \e -> do+ r <- doctestWithOptions fastMode preserveIt args `E.catch` \e -> do case fromException e of Just (UsageError err) -> do hPutStrLn stderr ("doctest: " ++ err)@@ -123,11 +123,11 @@ isSuccess :: Summary -> Bool isSuccess s = sErrors s == 0 && sFailures s == 0 -doctestWithFastMode :: Bool -> [String] -> IO Summary-doctestWithFastMode fastMode args = do+doctestWithOptions :: Bool -> Bool -> [String] -> IO Summary+doctestWithOptions fastMode preserveIt args = do -- get examples from Haddock comments modules <- getDocTests args Interpreter.withInterpreter args $ \repl -> withCP65001 $ do- runModules fastMode repl modules+ runModules fastMode preserveIt repl modules
src/Runner.hs view
@@ -52,11 +52,11 @@ (Summary x1 x2 x3 x4) `mappend` (Summary y1 y2 y3 y4) = Summary (x1 + y1) (x2 + y2) (x3 + y3) (x4 + y4) -- | Run all examples from a list of modules.-runModules :: Bool -> Interpreter -> [Module [Located DocTest]] -> IO Summary-runModules fastMode repl modules = do+runModules :: Bool -> Bool -> Interpreter -> [Module [Located DocTest]] -> IO Summary+runModules fastMode preserveIt repl modules = do isInteractive <- hIsTerminalDevice stderr ReportState _ _ s <- (`execStateT` ReportState 0 isInteractive mempty {sExamples = c}) $ do- forM_ modules $ runModule fastMode repl+ forM_ modules $ runModule fastMode preserveIt repl -- report final summary gets (show . reportStateSummary) >>= report@@ -107,20 +107,20 @@ liftIO (hPutStr stderr str) -- | Run all examples from given module.-runModule :: Bool -> Interpreter -> Module [Located DocTest] -> Report ()-runModule fastMode repl (Module module_ setup examples) = do+runModule :: Bool -> Bool -> Interpreter -> Module [Located DocTest] -> Report ()+runModule fastMode preserveIt repl (Module module_ setup examples) = do Summary _ _ e0 f0 <- gets reportStateSummary forM_ setup $- runTestGroup repl reload+ runTestGroup preserveIt repl reload Summary _ _ e1 f1 <- gets reportStateSummary -- only run tests, if setup does not produce any errors/failures when (e0 == e1 && f0 == f1) $ forM_ examples $- runTestGroup repl setup_+ runTestGroup preserveIt repl setup_ where reload :: IO () reload = do@@ -131,17 +131,18 @@ void $ Interpreter.safeEval repl ":reload" void $ Interpreter.safeEval repl $ ":m *" ++ module_ - -- Evaluate a dumb expression to populate the 'it' variable NOTE: This is- -- one reason why we cannot have safeEval = safeEvalIt: 'it' isn't set in- -- a fresh GHCi session.- void $ Interpreter.safeEval repl $ "()"+ when preserveIt $+ -- Evaluate a dumb expression to populate the 'it' variable NOTE: This is+ -- one reason why we cannot have safeEval = safeEvalIt: 'it' isn't set in+ -- a fresh GHCi session.+ void $ Interpreter.safeEval repl $ "()" setup_ :: IO () setup_ = do reload forM_ setup $ \l -> forM_ l $ \(Located _ x) -> case x of Property _ -> return ()- Example e _ -> void $ Interpreter.safeEvalIt repl e+ Example e _ -> void $ safeEvalWith preserveIt repl e reportFailure :: Location -> Expression -> Report () reportFailure loc expression = do@@ -167,14 +168,14 @@ -- -- The interpreter state is zeroed with @:reload@ first. This means that you -- can reuse the same 'Interpreter' for several test groups.-runTestGroup :: Interpreter -> IO () -> [Located DocTest] -> Report ()-runTestGroup repl setup tests = do+runTestGroup :: Bool -> Interpreter -> IO () -> [Located DocTest] -> Report ()+runTestGroup preserveIt repl setup tests = do -- report intermediate summary gets (show . reportStateSummary) >>= report_ liftIO setup- runExampleGroup repl examples+ runExampleGroup preserveIt repl examples forM_ properties $ \(loc, expression) -> do r <- liftIO $ do@@ -197,11 +198,11 @@ -- | -- Execute all expressions from given example in given 'Interpreter' and verify -- the output.-runExampleGroup :: Interpreter -> [Located Interaction] -> Report ()-runExampleGroup repl = go+runExampleGroup :: Bool -> Interpreter -> [Located Interaction] -> Report ()+runExampleGroup preserveIt repl = go where go ((Located loc (expression, expected)) : xs) = do- r <- fmap lines <$> liftIO (Interpreter.safeEvalIt repl expression)+ r <- fmap lines <$> liftIO (safeEvalWith preserveIt repl expression) case r of Left err -> do reportError loc expression err@@ -213,3 +214,8 @@ reportSuccess go xs go [] = return ()++safeEvalWith :: Bool -> Interpreter -> String -> IO (Either String String)+safeEvalWith preserveIt+ | preserveIt = Interpreter.safeEvalIt+ | otherwise = Interpreter.safeEval