File: RunnerSpec.hs

package info (click to toggle)
haskell-doctest-parallel 0.3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 652 kB
  • sloc: haskell: 3,241; makefile: 6; ansic: 4
file content (93 lines) | stat: -rw-r--r-- 2,869 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE CPP, OverloadedStrings, ImplicitParams #-}
module RunnerSpec (main, spec) where

import Test.Hspec

import Control.Concurrent
import Control.Monad.Trans.State
import System.IO
import System.IO.Silently (hCapture)
import Test.DocTest.Internal.Logging
import Test.DocTest.Internal.Runner
import Text.Printf (printf)

main :: IO ()
main = hspec spec

capture :: Report a -> IO String
capture = fmap fst . hCapture [stderr] . (`execStateT` ReportState 0 True mempty)

-- like capture, but with interactivity set to False
capture_ :: Report a -> IO String
capture_ = fmap fst . hCapture [stderr] . (`execStateT` ReportState 0 False mempty)

spec :: Spec
spec = do
  threadId <- runIO myThreadId
  let ?threadId = threadId
  let ?verbosity = Info

  describe "report" $ do

    context "when mode is interactive" $ do

      it "writes to stderr" $ do
        capture $ do
          report Info "foobar"
        `shouldReturn` printf "[INFO   ] [%s] foobar\n" (show threadId)

      it "overwrites any intermediate output" $ do
        capture $ do
          report_ Info "foo"
          report Info  "bar"
        `shouldReturn` printf "foo\r[INFO   ] [%s] bar\n" (show threadId)

      it "blank out intermediate output if necessary" $ do
        capture $ do
          report_ Info "foobarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"
          report Info  "baz"
        `shouldReturn` printf "foobarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\r[INFO   ] [%s] baz             \n" (show threadId)

    context "when mode is non-interactive" $ do
      it "writes to stderr" $ do
        capture_ $ do
          report Info "foobar"
        `shouldReturn` printf "[INFO   ] [%s] foobar\n" (show threadId)

  describe "report_ Info" $ do

    context "when mode is interactive" $ do
      it "writes intermediate output to stderr" $ do
        capture $ do
          report_ Info "foobar"
        `shouldReturn` "foobar"

      it "overwrites any intermediate output" $ do
        capture $ do
          report_ Info "foo"
          report_ Info "bar"
        `shouldReturn` "foo\rbar"

      it "blank out intermediate output if necessary" $ do
        capture $ do
          report_ Info "foobar"
          report_ Info  "baz"
        `shouldReturn` "foobar\rbaz   "

    context "when mode is non-interactive" $ do
      it "is ignored" $ do
        capture_ $ do
          report_ Info "foobar"
        `shouldReturn` ""

      it "does not influence a subsequent call to `report Info`" $ do
        capture_ $ do
          report_ Info "foo"
          report Info  "bar"
        `shouldReturn` printf "[INFO   ] [%s] bar\n" (show threadId)

      it "does not require `report Info` to blank out any intermediate output" $ do
        capture_ $ do
          report_ Info "foobar"
          report Info  "baz"
        `shouldReturn` printf "[INFO   ] [%s] baz\n" (show threadId)