File: ParseSpec.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 (203 lines) | stat: -rw-r--r-- 5,928 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{-# LANGUAGE OverloadedStrings #-}
module ParseSpec (main, spec) where

import           Test.Hspec
import           Data.String
import           Data.String.Builder (Builder, build)
import           Control.Monad.Trans.Writer

import           Test.DocTest.Internal.Parse
import           Test.DocTest.Internal.Location

main :: IO ()
main = hspec spec

group :: Writer [DocTest] () -> Writer [[DocTest]] ()
group g = tell [execWriter g]

ghci :: Expression -> Builder -> Writer [DocTest] ()
ghci expressions expected = tell [Example expressions $ (map fromString . lines . build) expected]

prop_ :: Expression -> Writer [DocTest] ()
prop_ e = tell [Property e]

module_ :: String -> Writer [[DocTest]] () -> Writer [Module [DocTest]] ()
module_ name gs = tell [Module name Nothing (execWriter gs) []]

shouldGive :: IO [Module [Located DocTest]] -> Writer [Module [DocTest]] () -> Expectation
shouldGive action expected = map (fmap $ map unLoc) `fmap` action `shouldReturn` execWriter expected

spec :: Spec
spec = do
  describe "getDocTests" $ do
    it "extracts properties from a module" $ do
      getDocTests ["test/parse/property/Fib.hs"] `shouldGive` do
        module_ "Fib" $ do
          group $ do
            prop_ "foo"
            prop_ "bar"
            prop_ "baz"

    it "extracts examples from a module" $ do
      getDocTests ["test/parse/simple/Fib.hs"] `shouldGive` do
        module_ "Fib" $ do
          group $ do
            ghci "putStrLn \"foo\""
              "foo"
            ghci "putStr \"bar\""
              "bar"
            ghci "putStrLn \"baz\""
              "baz"

    it "extracts examples from documentation for non-exported names" $ do
      getDocTests ["test/parse/non-exported/Fib.hs"] `shouldGive` do
        module_ "Fib" $ do
          group $ do
            ghci "putStrLn \"foo\""
              "foo"
            ghci "putStr \"bar\""
              "bar"
            ghci "putStrLn \"baz\""
              "baz"

    it "extracts multiple examples from a module" $ do
      getDocTests ["test/parse/multiple-examples/Foo.hs"] `shouldGive` do
        module_ "Foo" $ do
          group $ do
            ghci "foo"
              "23"
          group $ do
            ghci "bar"
              "42"

    it "returns an empty list, if documentation contains no examples" $ do
      getDocTests ["test/parse/no-examples/Fib.hs"] >>= (`shouldBe` [])

    it "sets setup code to Nothing, if it does not contain any tests" $ do
      getDocTests ["test/parse/setup-empty/Foo.hs"] `shouldGive` do
        module_ "Foo" $ do
          group $ do
            ghci "foo"
              "23"

    it "keeps modules that only contain setup code" $ do
      getDocTests ["test/parse/setup-only/Foo.hs"] `shouldGive` do
        tell [Module "Foo" (Just [Example "foo" ["23"]]) [] []]

  describe "parseInteractions (an internal function)" $ do

    let parse_ = map unLoc . parseInteractions . noLocation . build

    it "parses an interaction" $ do
      parse_ $ do
        ">>> foo"
        "23"
      `shouldBe` [("foo", ["23"])]

    it "drops whitespace as appropriate" $ do
      parse_ $ do
        "    >>> foo   "
        "    23"
      `shouldBe` [("foo", ["23"])]

    it "parses an interaction without a result" $ do
      parse_ $ do
        ">>> foo"
      `shouldBe` [("foo", [])]

    it "works with a complex example" $ do
      parse_ $ do
        "test"
        "foobar"
        ""
        ">>> foo"
        "23"
        ""
        ">>> baz"
        ""
        ">>> bar"
        "23"
        ""
        "baz"
      `shouldBe` [("foo", ["23"]), ("baz", []), ("bar", ["23"])]

    it "attaches location information to parsed interactions" $ do
      let loc = Located . Location "Foo.hs"
      r <- return . parseInteractions . loc 23 . build  $ do
        "1"
        "2"
        ""
        ">>> 4"
        "5"
        ""
        ">>> 7"
        ""
        ">>> 9"
        "10"
        ""
        "11"
      r `shouldBe` [loc 26 $ ("4", ["5"]), loc 29 $ ("7", []), loc 31 $ ("9", ["10"])]

    it "basic multiline" $ do
      parse_ $ do
        ">>> :{ first"
        " next"
        "some"
        ":}"
        "output"
      `shouldBe` [(":{ first\n next\nsome\n:}", ["output"])]

    it "multiline align output" $ do
      parse_ $ do
        ">>> :{ first"
        "  :}"
        "  output"
      `shouldBe` [(":{ first\n:}", ["output"])]

    it "multiline align output with >>>" $ do
      parse_ $ do
        " >>> :{ first"
        " >>> :}"
        " output"
      `shouldBe` [(":{ first\n:}", ["output"])]

    it "parses wild cards lines" $ do
      parse_ $ do
        " >>> action"
        " foo"
        " ..."
        " bar"
      `shouldBe` [("action", ["foo", WildCardLine, "bar"])]

    it "parses wild card chunks" $ do
      parse_ $ do
        " >>> action"
        " foo ... bar"
      `shouldBe` [("action", [ExpectedLine ["foo ", WildCardChunk, " bar"]])]

  describe " parseProperties (an internal function)" $ do
    let parse_ = map unLoc . parseProperties . noLocation . build

    it "parses a property" $ do
      parse_ $ do
        "prop> foo"
      `shouldBe` ["foo"]

  describe "mkLineChunks (an internal function)" $ do

    it "replaces ellipsis with WildCardChunks" $ do
      mkLineChunks "foo ... bar ... baz" `shouldBe`
        ["foo ", WildCardChunk, " bar ", WildCardChunk, " baz"]

    it "doesn't replace fewer than 3 consecutive dots" $ do
      mkLineChunks "foo .. bar .. baz" `shouldBe`
        ["foo .. bar .. baz"]

    it "handles leading and trailing dots" $ do
      mkLineChunks ".. foo bar .." `shouldBe` [".. foo bar .."]

    it "handles leading and trailing ellipsis" $ do
      mkLineChunks "... foo bar ..." `shouldBe` [ WildCardChunk
                                                , " foo bar "
                                                , WildCardChunk
                                                ]