File: RunnerSpec.hs

package info (click to toggle)
haskell-doctest 0.22.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: haskell: 3,148; makefile: 5; ansic: 3
file content (89 lines) | stat: -rw-r--r-- 2,347 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
{-# LANGUAGE CPP, OverloadedStrings #-}
module RunnerSpec (main, spec) where

import           Imports

import           Test.Hspec

import           System.IO
import           System.IO.Silently (hCapture)
import           Control.Monad.Trans.State
import           Runner

main :: IO ()
main = hspec spec

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

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

spec :: Spec
spec = do

  describe "report" $ do

    context "when mode is interactive" $ do

      it "writes to stderr" $ do
        capture $ do
          report "foobar"
        `shouldReturn` "foobar\n"

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

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

    context "when mode is non-interactive" $ do
      it "writes to stderr" $ do
        capture_ $ do
          report "foobar"
        `shouldReturn` "foobar\n"

  describe "report_" $ do

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

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

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

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

      it "does not influence a subsequent call to `report`" $ do
        capture_ $ do
          report_ "foo"
          report  "bar"
        `shouldReturn` "bar\n"

      it "does not require `report` to blank out any intermediate output" $ do
        capture_ $ do
          report_ "foobar"
          report  "baz"
        `shouldReturn` "baz\n"