@@ -11,6 +11,7 @@ import Test.Hspec.QuickCheck
11import Test.QuickCheck (forAll, choose, Property, property, listOf, vectorOf, counterexample, Gen)
11import Test.QuickCheck (forAll, choose, Property, property, listOf, vectorOf, counterexample, Gen)
13import Test.QuickCheck.Monadic (monadicIO, PropertyM, assert, monitor)
13import Test.QuickCheck.Monadic (monadicIO, PropertyM, assert, monitor)
14+import Control.Exception (SomeException(..), displayException)
14import Control.Monad.IO.Class (MonadIO(..))
15import Control.Monad.IO.Class (MonadIO(..))
15import Control.Monad (when)
16import Control.Monad (when)
16import Data.List ((\\))
17import Data.List ((\\))
@@ -75,7 +76,7 @@ all =
75 Left _ -> False
76 Left _ -> False
77yield :: Property
78yield :: Property
79 forAll (chooseInt (min_value, max_value)) $ \x ->
80 forAll (chooseInt (min_value, max_value)) $ \x ->
80 case S.parse (P.yield x) (S.fromList [1 :: Int]) of
81 case S.parse (P.yield x) (S.fromList [1 :: Int]) of
81 Right r -> r == x
82 Right r -> r == x
@@ -102,10 +103,19 @@ dieM =
102 Right _ -> False
103 Right _ -> False
103 Left _ -> True
104 Left _ -> True
106+parserFail :: Property
109+ case S.parse (fail err) (S.fromList [0 :: Int]) of
111+ Left (SomeException e) -> err == displayException e
113+ err = "Testing MonadFail.fail."
105-- Element Parser Tests
115-- Element Parser Tests
107peekPass :: Property
117peekPass :: Property
108−peekPass =
118+peekPass =
109 forAll (chooseInt (1, max_length)) $ \list_length ->
119 forAll (chooseInt (1, max_length)) $ \list_length ->
110 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
120 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
111 case S.parse P.peek (S.fromList ls) of
121 case S.parse P.peek (S.fromList ls) of
@@ -121,13 +131,13 @@ peekFail =
121 Left _ -> True)
131 Left _ -> True)
123eofPass :: Property
133eofPass :: Property
124−eofPass =
134+eofPass =
125 property (case S.parse P.eof (S.fromList []) of
135 property (case S.parse P.eof (S.fromList []) of
126 Right _ -> True
136 Right _ -> True
127 Left _ -> False)
137 Left _ -> False)
129eofFail :: Property
139eofFail :: Property
130−eofFail =
140+eofFail =
131 forAll (chooseInt (1, max_length)) $ \list_length ->
141 forAll (chooseInt (1, max_length)) $ \list_length ->
132 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
142 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
133 case S.parse P.eof (S.fromList ls) of
143 case S.parse P.eof (S.fromList ls) of
@@ -135,7 +145,7 @@ eofFail =
135 Left _ -> True
145 Left _ -> True
137satisfyPass :: Property
147satisfyPass :: Property
138−satisfyPass =
148+satisfyPass =
139 forAll (chooseInt (mid_value, max_value)) $ \first_element ->
149 forAll (chooseInt (mid_value, max_value)) $ \first_element ->
140 forAll (listOf (chooseInt (min_value, max_value))) $ \ls_tail ->
150 forAll (listOf (chooseInt (min_value, max_value))) $ \ls_tail ->
@@ -147,7 +157,7 @@ satisfyPass =
147 Left _ -> False
157 Left _ -> False
149satisfy :: Property
159satisfy :: Property
150−satisfy =
160+satisfy =
151 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
161 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
152 case S.parse (P.satisfy predicate) (S.fromList ls) of
162 case S.parse (P.satisfy predicate) (S.fromList ls) of
153 Right r -> case ls of
163 Right r -> case ls of
@@ -162,7 +172,7 @@ satisfy =
162-- Sequence Parsers Tests
172-- Sequence Parsers Tests
164take :: Property
174take :: Property
166 forAll (chooseInt (min_value, max_value)) $ \n ->
176 forAll (chooseInt (min_value, max_value)) $ \n ->
167 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
177 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
168 case S.parse (P.take n FL.toList) (S.fromList ls) of
178 case S.parse (P.take n FL.toList) (S.fromList ls) of
@@ -170,7 +180,7 @@ take =
170 Left _ -> property False
180 Left _ -> property False
172takeEQPass :: Property
182takeEQPass :: Property
173−takeEQPass =
183+takeEQPass =
174 forAll (chooseInt (min_value, max_value)) $ \n ->
184 forAll (chooseInt (min_value, max_value)) $ \n ->
175 forAll (chooseInt (n, max_value)) $ \list_length ->
185 forAll (chooseInt (n, max_value)) $ \list_length ->
176 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
186 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
@@ -182,11 +192,11 @@ takeEQ :: Property
183 forAll (chooseInt (min_value, max_value)) $ \n ->
193 forAll (chooseInt (min_value, max_value)) $ \n ->
184 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
194 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
186 list_length = Prelude.length ls
196 list_length = Prelude.length ls
188 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
198 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
189− Right parsed_list ->
199+ Right parsed_list ->
190 if (n <= list_length) then
200 if (n <= list_length) then
191 checkListEqual parsed_list (Prelude.take n ls)
201 checkListEqual parsed_list (Prelude.take n ls)
@@ -194,7 +204,7 @@ takeEQ =
194 Left _ -> property (n > list_length)
204 Left _ -> property (n > list_length)
196takeGEPass :: Property
206takeGEPass :: Property
197−takeGEPass =
207+takeGEPass =
198 forAll (chooseInt (min_value, max_value)) $ \n ->
208 forAll (chooseInt (min_value, max_value)) $ \n ->
199 forAll (chooseInt (n, max_value)) $ \list_length ->
209 forAll (chooseInt (n, max_value)) $ \list_length ->
200 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
210 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
@@ -206,11 +216,11 @@ takeGE :: Property
207 forAll (chooseInt (min_value, max_value)) $ \n ->
217 forAll (chooseInt (min_value, max_value)) $ \n ->
208 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
218 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
210 list_length = Prelude.length ls
220 list_length = Prelude.length ls
212 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
222 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
213− Right parsed_list ->
223+ Right parsed_list ->
214 if (n <= list_length) then
224 if (n <= list_length) then
215 checkListEqual parsed_list ls
225 checkListEqual parsed_list ls
@@ -219,7 +229,7 @@ takeGE =
220-- lookAheadPass :: Property
230-- lookAheadPass :: Property
221-- lookAheadPass =
231-- lookAheadPass =
222−-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
232+-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
224-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
234-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
225-- parseTwice = do
235-- parseTwice = do
@@ -235,7 +245,7 @@ takeGE =
236-- lookAheadFail :: Property
246-- lookAheadFail :: Property
237-- lookAheadFail =
247-- lookAheadFail =
238−-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
248+-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
240-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
250-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
241-- parseTwice = do
251-- parseTwice = do
@@ -251,7 +261,7 @@ takeGE =
252-- lookAhead :: Property
262-- lookAhead :: Property
253-- lookAhead =
263-- lookAhead =
254−-- forAll (chooseInt (min_value, max_value)) $ \n ->
264+-- forAll (chooseInt (min_value, max_value)) $ \n ->
256-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
266-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
257-- parseTwice = do
267-- parseTwice = do
@@ -281,7 +291,7 @@ takeWhile1 =
281 case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
291 case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
282 Right parsed_list -> case ls of
292 Right parsed_list -> case ls of
283 [] -> property False
293 [] -> property False
284− (x : _) ->
294+ (x : _) ->
285 if predicate x then
295 if predicate x then
286 checkListEqual parsed_list (Prelude.takeWhile predicate ls)
296 checkListEqual parsed_list (Prelude.takeWhile predicate ls)
@@ -291,7 +301,7 @@ takeWhile1 =
291 (x : _) -> property (not $ predicate x)
301 (x : _) -> property (not $ predicate x)
293 predicate = (== 0)
303 predicate = (== 0)
295sliceSepBy :: Property
305sliceSepBy :: Property
296sliceSepBy =
306sliceSepBy =
297 forAll (listOf (chooseInt (0, 1))) $ \ls ->
307 forAll (listOf (chooseInt (0, 1))) $ \ls ->
@@ -302,7 +312,7 @@ sliceSepBy =
302 predicate = (== 1)
312 predicate = (== 1)
304-- sliceSepByMax :: Property
314-- sliceSepByMax :: Property
305−-- sliceSepByMax =
315+-- sliceSepByMax =
306-- forAll (chooseInt (min_value, max_value)) $ \n ->
316-- forAll (chooseInt (min_value, max_value)) $ \n ->
307-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
317-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
308-- case S.parse (P.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
318-- case S.parse (P.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
@@ -341,7 +351,7 @@ sliceSepBy =
341-- Left _ -> True)
351-- Left _ -> True)
343-- teeWithPass :: Property
353-- teeWithPass :: Property
344−-- teeWithPass =
354+-- teeWithPass =
345-- forAll (chooseInt (0, 10000)) $ \n ->
355-- forAll (chooseInt (0, 10000)) $ \n ->
346-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
356-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
@@ -352,25 +362,25 @@ sliceSepBy =
352-- Left _ -> property False
362-- Left _ -> property False
354-- teeWithFailLeft :: Property
364-- teeWithFailLeft :: Property
355−-- teeWithFailLeft =
365+-- teeWithFailLeft =
356-- property (case S.parse (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
366-- property (case S.parse (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
357-- Right _ -> False
367-- Right _ -> False
358-- Left _ -> True)
368-- Left _ -> True)
360-- teeWithFailRight :: Property
370-- teeWithFailRight :: Property
361−-- teeWithFailRight =
371+-- teeWithFailRight =
362-- property (case S.parse (P.teeWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
372-- property (case S.parse (P.teeWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
363-- Right _ -> False
373-- Right _ -> False
364-- Left _ -> True)
374-- Left _ -> True)
366-- teeWithFailBoth :: Property
376-- teeWithFailBoth :: Property
367−-- teeWithFailBoth =
377+-- teeWithFailBoth =
368-- property (case S.parse (P.teeWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
378-- property (case S.parse (P.teeWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
369-- Right _ -> False
379-- Right _ -> False
370-- Left _ -> True)
380-- Left _ -> True)
372-- deintercalate :: Property
382-- deintercalate :: Property
373−-- deintercalate =
383+-- deintercalate =
374-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
384-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
375-- case S.parse (P.deintercalate concatFold prsr_1 concatFold prsr_2) (S.fromList ls) of
385-- case S.parse (P.deintercalate concatFold prsr_1 concatFold prsr_2) (S.fromList ls) of
376-- Right parsed_list_tuple -> parsed_list_tuple == (partition (== 0) ls)
386-- Right parsed_list_tuple -> parsed_list_tuple == (partition (== 0) ls)
@@ -419,9 +429,10 @@ main = hspec $ do
419 prop "P.all = Prelude.all" Main.all
429 prop "P.all = Prelude.all" Main.all
420 prop "yield value provided" yield
430 prop "yield value provided" yield
421 prop "yield monadic value provided" yieldM
431 prop "yield monadic value provided" yieldM
432+ prop "fail err = Left (SomeException (ParseError err))" parserFail
422 prop "always fail" die
433 prop "always fail" die
423 prop "always fail but monadic" dieM
434 prop "always fail but monadic" dieM
425 describe "test for element parser" $ do
436 describe "test for element parser" $ do
426 prop "peek = head with list length > 0" peekPass
437 prop "peek = head with list length > 0" peekPass
427 prop "peek fail on []" peekFail
438 prop "peek fail on []" peekFail
@@ -455,4 +466,4 @@ main = hspec $ do
455 -- prop "pass test for shortest function" shortestPass
466 -- prop "pass test for shortest function" shortestPass
456 -- prop "left fail test for shortest function" shortestFailLeft
467 -- prop "left fail test for shortest function" shortestFailLeft
457 -- prop "right fail test for shortest function" shortestFailRight
468 -- prop "right fail test for shortest function" shortestFailRight
458− -- prop "both fail test for shortest function" shortestFailBoth
469+ -- prop "both fail test for shortest function" shortestFailBoth