@@ -3,8 +3,8 @@ module Main (main) where
33import Control.Applicative ((<|>))
44import Control.Exception (SomeException(..), try)
55import Data.Word (Word8, Word32, Word64)
6−import Streamly.Test.Common (listEquals, checkListEqual, chooseInt, equals)
7−import Test.Hspec (Spec, hspec, describe, Expectation, it, shouldReturn)
6+import Streamly.Test.Common (listEquals, checkListEqual, chooseInt)
7+import Test.Hspec (Spec, hspec, describe)
88import Test.Hspec.QuickCheck
99import Test.QuickCheck
1010 (arbitrary, forAll, elements, Property,
@@ -28,7 +28,7 @@ import Prelude hiding (sequence)
2828#if MIN_VERSION_QuickCheck(2,14,0)
2929
3030import Test.QuickCheck (chooseAny)
31−
31+import Control.Monad.Identity (runIdentity, Identity (Identity))
3232
3333#else
3434
@@ -65,7 +65,7 @@ fromFold :: Property
6565fromFold =
6666 forAll (listOf $ chooseInt (min_value, max_value))
6767 $ \ls ->
68− case (==) <$> (S.parseD (P.fromFold FL.sum) (S.fromList ls))
68+ case (==) <$> runIdentity (S.parseD (P.fromFold FL.sum) (S.fromList ls))
6969 <*> (S.fold FL.sum (S.fromList ls)) of
7070 Right is_equal -> is_equal
7171 Left _ -> False
@@ -73,28 +73,28 @@ fromFold =
7373fromPure :: Property
7474fromPure =
7575 forAll (chooseInt (min_value, max_value)) $ \x ->
76− case S.parseD (P.fromPure x) (S.fromList [1 :: Int]) of
76+ case runIdentity $ S.parseD (P.fromPure x) (S.fromList [1 :: Int]) of
7777 Right r -> r == x
7878 Left _ -> False
7979
8080fromEffect :: Property
8181fromEffect =
8282 forAll (chooseInt (min_value, max_value)) $ \x ->
83− case S.parseD (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
83+ case runIdentity $ S.parseD (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
8484 Right r -> r == x
8585 Left _ -> False
8686
8787die :: Property
8888die =
8989 property $
90− case S.parseD (P.die "die test") (S.fromList [0 :: Int]) of
90+ case runIdentity (S.parseD (P.die "die test") (S.fromList [0 :: Int])) of
9191 Right _ -> False
9292 Left _ -> True
9393
9494dieM :: Property
9595dieM =
9696 property $
97− case S.parseD (P.dieM (Right "die test")) (S.fromList [0 :: Int]) of
97+ case runIdentity (S.parseD (P.dieM (Identity "die test")) (S.fromList [0 :: Int])) of
9898 Right _ -> False
9999 Left _ -> True
100100
@@ -104,7 +104,7 @@ peekPass :: Property
104104peekPass =
105105 forAll (chooseInt (1, max_length)) $ \list_length ->
106106 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
107− case S.parseD P.peek (S.fromList ls) of
107+ case runIdentity $ S.parseD P.peek (S.fromList ls) of
108108 Right head_value -> case ls of
109109 head_ls : _ -> head_value == head_ls
110110 _ -> False
@@ -112,7 +112,7 @@ peekPass =
112112
113113peekFail :: Property
114114peekFail =
115− property (case S.parseD P.peek (S.fromList []) of
115+ property (case runIdentity $ S.parseD P.peek (S.fromList []) of
116116 Right _ -> False
117117 Left _ -> True)
118118
@@ -126,7 +126,7 @@ eofFail :: Property
126126eofFail =
127127 forAll (chooseInt (1, max_length)) $ \list_length ->
128128 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
129− case S.parseD P.eof (S.fromList ls) of
129+ case runIdentity $S.parseD P.eof (S.fromList ls) of
130130 Right _ -> False
131131 Left _ -> True
132132
@@ -138,14 +138,14 @@ satisfyPass =
138138 ls = first_element : ls_tail
139139 predicate = (>= mid_value)
140140 in
141− case S.parseD (P.satisfy predicate) (S.fromList ls) of
141+ case runIdentity $ S.parseD (P.satisfy predicate) (S.fromList ls) of
142142 Right r -> r == first_element
143143 Left _ -> False
144144
145145satisfy :: Property
146146satisfy =
147147 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
148− case S.parseD (P.satisfy predicate) (S.fromList ls) of
148+ case runIdentity $ S.parseD (P.satisfy predicate) (S.fromList ls) of
149149 Right r -> case ls of
150150 [] -> False
151151 (x : _) -> predicate x && (r == x)
@@ -162,7 +162,7 @@ takeBetweenPass =
162162 forAll (chooseInt (m, max_value)) $ \n ->
163163 forAll (chooseInt (m, max_value)) $ \list_length ->
164164 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
165− case S.parseD (P.takeBetween m n FL.toList) (S.fromList ls) of
165+ case runIdentity $ S.parseD (P.takeBetween m n FL.toList) (S.fromList ls) of
166166 Right parsed_list ->
167167 let lpl = Prelude.length parsed_list
168168 in checkListEqual parsed_list (Prelude.take lpl ls)
@@ -180,13 +180,16 @@ takeBetween =
180180 let p = P.takeBetween m n FL.toList
181181 r <- run $ try $ S.parseD p (S.fromList ls)
182182 return $ case r of
183− Right parsed_list ->
184− if m <= list_length && n >= m
185− then
186− let len = Prelude.length parsed_list
187− in checkListEqual
188− parsed_list (Prelude.take len ls)
189− else property False
183+ Right x -> case x of
184+ Right parsed_list ->
185+ if m <= list_length && n >= m
186+ then
187+ let len = Prelude.length parsed_list
188+ in checkListEqual
189+ parsed_list (Prelude.take len ls)
190+ else property False
191+ Left _ ->
192+ property (m > n || list_length < m)
190193 Left (_ :: SomeException) ->
191194 property (m > n || list_length < m)
192195
@@ -194,7 +197,7 @@ take :: Property
194197take =
195198 forAll (chooseInt (min_value, max_value)) $ \n ->
196199 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
197− case S.parseD (P.fromFold $ FL.take n FL.toList) (S.fromList ls) of
200+ case runIdentity $ S.parseD (P.fromFold $ FL.take n FL.toList) (S.fromList ls) of
198201 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
199202 Left _ -> property False
200203
@@ -203,7 +206,7 @@ takeEQPass =
203206 forAll (chooseInt (min_value, max_value)) $ \n ->
204207 forAll (chooseInt (n, max_value)) $ \list_length ->
205208 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
206− case S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
209+ case runIdentity $ S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
207210 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
208211 Left _ -> property False
209212
@@ -214,7 +217,7 @@ takeEQ =
214217 let
215218 list_length = Prelude.length ls
216219 in
217− case S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
220+ case runIdentity $ S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
218221 Right parsed_list ->
219222 if (n <= list_length) then
220223 checkListEqual parsed_list (Prelude.take n ls)
@@ -227,7 +230,7 @@ takeGEPass =
227230 forAll (chooseInt (min_value, max_value)) $ \n ->
228231 forAll (chooseInt (n, max_value)) $ \list_length ->
229232 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
230− case S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
233+ case runIdentity $ S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
231234 Right parsed_list -> checkListEqual parsed_list ls
232235 Left _ -> property False
233236
@@ -238,7 +241,7 @@ takeGE =
238241 let
239242 list_length = Prelude.length ls
240243 in
241− case S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
244+ case runIdentity $ S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
242245 Right parsed_list ->
243246 if (n <= list_length) then
244247 checkListEqual parsed_list ls
@@ -248,15 +251,15 @@ takeGE =
248251
249252nLessThanEqual0 ::
250253 ( Int
251− -> FL.Fold (Either SomeException) Int [Int]
252− -> P.Parser Int (Either SomeException) [Int]
254+ -> FL.Fold Identity Int [Int]
255+ -> P.Parser Int Identity [Int]
253256 )
254257 -> (Int -> [Int] -> [Int])
255258 -> Property
256259nLessThanEqual0 tk ltk =
257260 forAll (elements [0, (-1)]) $ \n ->
258261 forAll (listOf arbitrary) $ \ls ->
259− case S.parseD (tk n FL.toList) (S.fromList ls) of
262+ case runIdentity $ S.parseD (tk n FL.toList) (S.fromList ls) of
260263 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
261264 Left _ -> property False
262265
@@ -283,7 +286,7 @@ lookAheadPass =
283286 in
284287 forAll (chooseInt (n+1, max_value)) $ \list_length ->
285288 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
286− case S.parseD parseTwice (S.fromList ls) of
289+ case runIdentity $ S.parseD parseTwice (S.fromList ls) of
287290 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
288291 Left _ -> property $ False
289292
@@ -298,7 +301,7 @@ lookAhead =
298301 return (parsed_list_1, parsed_list_2)
299302 in
300303 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
301− case S.parseD parseTwice (S.fromList ls) of
304+ case runIdentity $ S.parseD parseTwice (S.fromList ls) of
302305 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
303306 Left _ -> property ((list_length < n) || (list_length == n && n == 0))
304307 where
@@ -307,7 +310,7 @@ lookAhead =
307310takeWhile :: Property
308311takeWhile =
309312 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
310− case S.parseD (P.takeWhile predicate FL.toList) (S.fromList ls) of
313+ case runIdentity $ S.parseD (P.takeWhile predicate FL.toList) (S.fromList ls) of
311314 Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
312315 Left _ -> property False
313316 where
@@ -316,7 +319,7 @@ takeWhile =
316319takeWhile1 :: Property
317320takeWhile1 =
318321 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
319− case S.parseD (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
322+ case runIdentity $ S.parseD (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
320323 Right parsed_list -> case ls of
321324 [] -> property False
322325 (x : _) ->
@@ -334,7 +337,7 @@ groupBy :: Property
334337groupBy =
335338 forAll (listOf (chooseInt (0, 1)))
336339 $ \ls ->
337− case S.parseD parser (S.fromList ls) of
340+ case runIdentity $ S.parseD parser (S.fromList ls) of
338341 Right parsed -> checkListEqual parsed (groupByLF ls)
339342 Left _ -> property False
340343
@@ -350,7 +353,7 @@ groupByRolling :: Property
350353groupByRolling =
351354 forAll (listOf (chooseInt (0, 1)))
352355 $ \ls ->
353− case S.parseD parser (S.fromList ls) of
356+ case runIdentity $ S.parseD parser (S.fromList ls) of
354357 Right parsed -> checkListEqual parsed (groupByLF Nothing ls)
355358 Left _ -> property False
356359
@@ -369,7 +372,7 @@ takeEndByOrMax :: Property
369372takeEndByOrMax =
370373 forAll (chooseInt (min_value, max_value)) $ \n ->
371374 forAll (listOf (chooseInt (0, 1))) $ \ls ->
372− case S.parseD (P.fromFold $ FL.takeEndBy_ predicate (FL.take n FL.toList)) (S.fromList ls) of
375+ case runIdentity $ S.parseD (P.fromFold $ FL.takeEndBy_ predicate (FL.take n FL.toList)) (S.fromList ls) of
373376 Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
374377 Left _ -> property False
375378 where
@@ -379,7 +382,7 @@ wordBy :: Property
379382wordBy =
380383 forAll (listOf (elements [' ', 's']))
381384 $ \ls ->
382− case S.parseD parser (S.fromList ls) of
385+ case runIdentity $ S.parseD parser (S.fromList ls) of
383386 Right parsed -> checkListEqual parsed (words' ls)
384387 Left _ -> property False
385388
@@ -395,7 +398,7 @@ wordBy =
395398splitWith :: Property
396399splitWith =
397400 forAll (listOf (chooseInt (0, 1))) $ \ls ->
398− case S.parseD (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
401+ case runIdentity $ S.parseD (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
399402 Right (result_first, result_second) -> case ls of
400403 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
401404 _ -> False
@@ -405,19 +408,19 @@ splitWith =
405408
406409splitWithFailLeft :: Property
407410splitWithFailLeft =
408− property (case S.parseD (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
411+ property (case runIdentity $ S.parseD (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
409412 Right _ -> False
410413 Left _ -> True)
411414
412415splitWithFailRight :: Property
413416splitWithFailRight =
414− property (case S.parseD (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
417+ property (case runIdentity $ S.parseD (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
415418 Right _ -> False
416419 Left _ -> True)
417420
418421splitWithFailBoth :: Property
419422splitWithFailBoth =
420− property (case S.parseD (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
423+ property (case runIdentity $ S.parseD (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
421424 Right _ -> False
422425 Left _ -> True)
423426
@@ -528,14 +531,14 @@ many =
528531 prsr =
529532 flip P.many concatFold
530533 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
531− in case S.parseD prsr (S.fromList ls) of
534+ in case runIdentity $ S.parseD prsr (S.fromList ls) of
532535 Right res_list ->
533536 checkListEqual res_list (Prelude.filter (== 0) ls)
534537 Left _ -> property False
535538
536539many_empty :: Property
537540many_empty =
538− property (case S.parseD (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
541+ property (case runIdentity $ S.parseD (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
539542 Right res_list -> checkListEqual res_list ([] :: [Int])
540543 Left _ -> property False)
541544
@@ -548,13 +551,13 @@ some =
548551 prsr =
549552 flip P.some concatFold
550553 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
551− in case S.parseD prsr (S.fromList ls) of
554+ in case runIdentity $ S.parseD prsr (S.fromList ls) of
552555 Right res_list -> res_list == Prelude.filter (== 0) ls
553556 Left _ -> False
554557
555558someFail :: Property
556559someFail =
557− property (case S.parseD (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
560+ property (case runIdentity $ S.parseD (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
558561 Right _ -> False
559562 Left _ -> True)
560563
@@ -566,54 +569,66 @@ applicative :: Property
566569applicative =
567570 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
568571 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
572+ monadicIO $ do
569573 let parser =
570574 (,)
571575 <$> P.fromFold (FL.take (length list1) FL.toList)
572576 <*> P.fromFold (FL.take (length list2) FL.toList)
573− in monadicIO $ do
574− (olist1, olist2) <-
575− run $ S.parseD parser (S.fromList $ list1 ++ list2)
576− listEquals (==) olist1 list1
577− listEquals (==) olist2 list2
577+
578+ return $
579+ case runIdentity $ S.parseD parser (S.fromList $ list1 ++ list2) of
580+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
581+ Left _ -> False
578582
579583sequence :: Property
580584sequence =
581585 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
586+ monadicIO $ do
582587 let parsers = fmap (\xs -> P.fromFold $ FL.take (length xs) FL.toList) ins
583− in monadicIO $ do
584− outs <- run $
585− S.parseD
586− (Prelude.sequence parsers)
587− (S.fromList $ concat ins)
588− listEquals (==) outs ins
589−
590−altEOF1 :: Expectation
588+ outs <- S.parseD
589+ (Prelude.sequence parsers)
590+ (S.fromList $ concat ins)
591+ return $
592+ case outs of
593+ Right x -> x == ins
594+ Left _ -> False
595+
596+altEOF1 :: Property
591597altEOF1 =
592− S.parseD
598+ monadicIO $ do
599+ s1 <- S.parseD
593600 (P.satisfy (> 0) <|> return 66)
594601 (S.fromList ([]::[Int]))
595− `shouldReturn` 66
602+ return $
603+ case s1 of
604+ Right x -> x == 66
605+ Left _ -> False
596606
597−altEOF2 :: Expectation
607+altEOF2 :: Property
598608altEOF2 =
599− S.parseD
609+ monadicIO $ do
610+ s1 <- S.parseD
600611 ((P.takeEQ 2 FL.toList) <|> (P.takeEQ 1 FL.toList))
601612 (S.fromList ([51]::[Int]))
602− `shouldReturn` [51]
613+ return $
614+ case s1 of
615+ Right x -> x == [51]
616+ Left _ -> False
603617
604618monad :: Property
605619monad =
606620 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
607621 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
622+ monadicIO $ do
608623 let parser = do
609624 olist1 <- P.fromFold (FL.take (length list1) FL.toList)
610625 olist2 <- P.fromFold (FL.take (length list2) FL.toList)
611626 return (olist1, olist2)
612− in monadicIO $ do
613− (olist1, olist2) <-
614− run $ S.parseD parser (S.fromList $ list1 ++ list2)
615− listEquals (==) olist1 list1
616− listEquals (==) olist2 list2
627+ s1 <- S.parseD parser (S.fromList $ list1 ++ list2)
628+ return $
629+ case s1 of
630+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
631+ Left _ -> False
617632
618633-------------------------------------------------------------------------------
619634-- Stream parsing
@@ -624,13 +639,11 @@ parseMany =
624639 forAll (chooseInt (1,100)) $ \len ->
625640 forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
626641 monadicIO $ do
627− outs <-
628− ( run
629− $ toList
630− $ S.parseManyD
631− (P.fromFold $ FL.take len FL.toList) (S.fromList $ concat ins)
632− )
633− listEquals (==) outs ins
642+ outs <-
643+ (toList $ S.rights $ S.parseManyD
644+ (P.fromFold $ FL.take len FL.toList) (S.fromList $ concat ins)
645+ )
646+ return $ outs == ins
634647
635648-- basic sanity test for parsing from arrays
636649parseUnfold :: Property
@@ -664,13 +677,15 @@ parserSequence :: Property
664677parserSequence =
665678 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ins ->
666679 monadicIO $ do
667− let parsers = D.fromList
668− $ fmap (\xs -> P.fromFold $ FL.take (length xs) FL.sum) ins
669− let sequencedParser = P.sequence parsers FL.sum
670− outs <-
671− run $
672− S.parseD sequencedParser $ S.concatMap S.fromList (S.fromList ins)
673− equals (==) outs (sum $ map sum ins)
680+ let parsers = D.fromList
681+ $ fmap (\xs -> P.fromFold $ FL.take (length xs) FL.sum) ins
682+ let sequencedParser = P.sequence parsers FL.sum
683+ outs <-
684+ S.parseD sequencedParser $ S.concatMap S.fromList (S.fromList ins)
685+ return $
686+ case outs of
687+ Right x -> x == sum (map sum ins)
688+ Left _ -> False
674689
675690-------------------------------------------------------------------------------
676691-- Test for a particular case hit during fs events testing
@@ -722,6 +737,7 @@ parseMany2Events =
722737 xs <-
723738 ( run
724739 $ toList
740+ $ S.rights
725741 $ S.parseManyD readOneEvent
726742 $ S.fromList (concat (replicate 2 event))
727743 )
@@ -750,8 +766,8 @@ main =
750766
751767 describe "Instances" $ do
752768 prop "applicative" applicative
753− it "Alternative: end of input 1" altEOF1
754− it "Alternative: end of input 2" altEOF2
769+ prop "Alternative: end of input 1" altEOF1
770+ prop "Alternative: end of input 2" altEOF2
755771 prop "monad" monad
756772 prop "sequence" sequence
757773