git.iko.soy/typeable/attoparsec
on master help sign in
Refactor test suit into bytestring and text portions.

--HG-- rename : tests/QC.hs => tests/QC/ByteString.hs rename : tests/QC.hs => tests/QC/Text.hs

commite9ed7a9120ce9950bc25f610749d7491c72be924 authorBryan O'Sullivan <bos@serpentine.com> dateMon 28 Nov 2011, 15:45 (15 years ago) parent5454fb1
4 files changed+210−100
attoparsec.cabal+2 tests/QC.hs+4−100 tests/QC/ByteString.hs+102 tests/QC/Text.hs+102
attoparsec.cabal+2
@@ -69,6 +69,8 @@ test-suite tests
6969 type: exitcode-stdio-1.0
7070 hs-source-dirs: tests
7171 main-is: QC.hs
72+ other-modules: QC.ByteString
73+ QC.Text
7274
7375 ghc-options:
7476 -Wall -threaded -rtsopts
tests/QC.hs+4−100
@@ -1,108 +1,12 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-# OPTIONS_GHC -fno-warn-orphans #-}
31module Main (main) where
42
5−import Control.Applicative ((<$>))
6−import Prelude hiding (takeWhile)
3+import qualified QC.ByteString as ByteString
4+import qualified QC.Text as Text
75import Test.Framework (defaultMain, testGroup)
8−import Test.Framework.Providers.QuickCheck2 (testProperty)
9−import Test.QuickCheck
10−import qualified Data.Attoparsec as P
11−import qualified Data.ByteString as B
12−import qualified Data.ByteString as S
13−import qualified Data.ByteString.Lazy as L
14−
15−instance Arbitrary S.ByteString where
16− arbitrary = S.pack <$> arbitrary
17−
18−instance Arbitrary L.ByteString where
19− arbitrary = sized $ \n -> resize (round (sqrt (toEnum n :: Double)))
20− ((L.fromChunks . map (S.pack . nonEmpty)) <$> arbitrary)
21− where nonEmpty (NonEmpty a) = a
22−
23−-- Naming.
24−
25−{-
26−label (NonEmpty s) = case parse (anyWord8 <?> s) B.empty of
27− (_, Left err) -> s `isInfixOf` err
28− _ -> False
29−-}
30−
31−-- Basic byte-level combinators.
32−
33−maybeP p s = case P.parse p s `P.feed` B.empty of
34− P.Done _ i -> Just i
35− _ -> Nothing
36−
37−defP p s = P.parse p s `P.feed` B.empty
38−
39−satisfy w s = maybeP (P.satisfy (<=w)) (B.cons w s) == Just w
40−
41−word8 w s = maybeP (P.word8 w) (B.cons w s) == Just w
42−
43−anyWord8 s = maybeP P.anyWord8 s == if B.null s
44− then Nothing
45− else Just (B.head s)
46−
47−notWord8 w (NonEmpty s) = maybeP (P.notWord8 w) bs == if v == w
48− then Nothing
49− else Just v
50− where v = B.head bs
51− bs = B.pack s
52−
53−string s = maybeP (P.string s) s == Just s
54−
55−skipWhile w s =
56− let t = B.dropWhile (<=w) s
57− in case defP (P.skipWhile (<=w)) s of
58− P.Done t' () -> t == t'
59− _ -> False
60−
61−takeCount (Positive k) s =
62− case maybeP (P.take k) s of
63− Nothing -> k > B.length s
64− Just s' -> k <= B.length s
65−
66−takeWhile w s =
67− let (h,t) = B.span (==w) s
68− in case defP (P.takeWhile (==w)) s of
69− P.Done t' h' -> t == t' && h == h'
70− _ -> False
71−
72−takeWhile1 w s =
73− let s' = B.cons w s
74− (h,t) = B.span (<=w) s'
75− in case defP (P.takeWhile1 (<=w)) s' of
76− P.Done t' h' -> t == t' && h == h'
77− _ -> False
78−
79−takeTill w s =
80− let (h,t) = B.break (==w) s
81− in case defP (P.takeTill (==w)) s of
82− P.Done t' h' -> t == t' && h == h'
83− _ -> False
84−
85−takeWhile1_empty = maybeP (P.takeWhile1 undefined) B.empty == Nothing
86−
87−endOfInput s = maybeP P.endOfInput s == if B.null s
88− then Just ()
89− else Nothing
906
917main = defaultMain tests
928
939tests = [
94− testGroup "fnord" [
95− testProperty "satisfy" satisfy,
96− testProperty "word8" word8,
97− testProperty "notWord8" notWord8,
98− testProperty "anyWord8" anyWord8,
99− testProperty "string" string,
100− testProperty "skipWhile" skipWhile,
101− testProperty "takeCount" takeCount,
102− testProperty "takeWhile" takeWhile,
103− testProperty "takeWhile1" takeWhile1,
104− testProperty "takeWhile1_empty" takeWhile1_empty,
105− testProperty "takeTill" takeTill,
106− testProperty "endOfInput" endOfInput
107− ]
10+ testGroup "bs" ByteString.tests
11+ , testGroup "text" Text.tests
10812 ]
tests/QC/ByteString.hsnew file+102
@@ -0,0 +1,102 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# OPTIONS_GHC -fno-warn-orphans #-}
3+module QC.ByteString (tests) where
4+
5+import Control.Applicative ((<$>))
6+import Prelude hiding (takeWhile)
7+import Test.Framework.Providers.QuickCheck2 (testProperty)
8+import Test.QuickCheck
9+import qualified Data.Attoparsec.ByteString as P
10+import qualified Data.ByteString as B
11+import qualified Data.ByteString.Lazy as L
12+
13+instance Arbitrary B.ByteString where
14+ arbitrary = B.pack <$> arbitrary
15+
16+instance Arbitrary L.ByteString where
17+ arbitrary = sized $ \n -> resize (round (sqrt (toEnum n :: Double)))
18+ ((L.fromChunks . map (B.pack . nonEmpty)) <$> arbitrary)
19+ where nonEmpty (NonEmpty a) = a
20+
21+-- Naming.
22+
23+{-
24+label (NonEmpty s) = case parse (anyWord8 <?> s) B.empty of
25+ (_, Left err) -> s `isInfixOf` err
26+ _ -> False
27+-}
28+
29+-- Basic byte-level combinators.
30+
31+maybeP p s = case P.parse p s `P.feed` B.empty of
32+ P.Done _ i -> Just i
33+ _ -> Nothing
34+
35+defP p s = P.parse p s `P.feed` B.empty
36+
37+satisfy w s = maybeP (P.satisfy (<=w)) (B.cons w s) == Just w
38+
39+word8 w s = maybeP (P.word8 w) (B.cons w s) == Just w
40+
41+anyWord8 s = maybeP P.anyWord8 s == if B.null s
42+ then Nothing
43+ else Just (B.head s)
44+
45+notWord8 w (NonEmpty s) = maybeP (P.notWord8 w) bs == if v == w
46+ then Nothing
47+ else Just v
48+ where v = B.head bs
49+ bs = B.pack s
50+
51+string s = maybeP (P.string s) s == Just s
52+
53+skipWhile w s =
54+ let t = B.dropWhile (<=w) s
55+ in case defP (P.skipWhile (<=w)) s of
56+ P.Done t' () -> t == t'
57+ _ -> False
58+
59+takeCount (Positive k) s =
60+ case maybeP (P.take k) s of
61+ Nothing -> k > B.length s
62+ Just s' -> k <= B.length s
63+
64+takeWhile w s =
65+ let (h,t) = B.span (==w) s
66+ in case defP (P.takeWhile (==w)) s of
67+ P.Done t' h' -> t == t' && h == h'
68+ _ -> False
69+
70+takeWhile1 w s =
71+ let s' = B.cons w s
72+ (h,t) = B.span (<=w) s'
73+ in case defP (P.takeWhile1 (<=w)) s' of
74+ P.Done t' h' -> t == t' && h == h'
75+ _ -> False
76+
77+takeTill w s =
78+ let (h,t) = B.break (==w) s
79+ in case defP (P.takeTill (==w)) s of
80+ P.Done t' h' -> t == t' && h == h'
81+ _ -> False
82+
83+takeWhile1_empty = maybeP (P.takeWhile1 undefined) B.empty == Nothing
84+
85+endOfInput s = maybeP P.endOfInput s == if B.null s
86+ then Just ()
87+ else Nothing
88+
89+tests = [
90+ testProperty "satisfy" satisfy,
91+ testProperty "word8" word8,
92+ testProperty "notWord8" notWord8,
93+ testProperty "anyWord8" anyWord8,
94+ testProperty "string" string,
95+ testProperty "skipWhile" skipWhile,
96+ testProperty "takeCount" takeCount,
97+ testProperty "takeWhile" takeWhile,
98+ testProperty "takeWhile1" takeWhile1,
99+ testProperty "takeWhile1_empty" takeWhile1_empty,
100+ testProperty "takeTill" takeTill,
101+ testProperty "endOfInput" endOfInput
102+ ]
tests/QC/Text.hsnew file+102
@@ -0,0 +1,102 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# OPTIONS_GHC -fno-warn-orphans #-}
3+module QC.Text (tests) where
4+
5+import Control.Applicative ((<$>))
6+import Prelude hiding (takeWhile)
7+import Test.Framework.Providers.QuickCheck2 (testProperty)
8+import Test.QuickCheck
9+import qualified Data.Attoparsec.Text as P
10+import qualified Data.Text as T
11+import qualified Data.Text.Lazy as L
12+
13+instance Arbitrary T.Text where
14+ arbitrary = T.pack <$> arbitrary
15+
16+instance Arbitrary L.Text where
17+ arbitrary = sized $ \n -> resize (round (sqrt (toEnum n :: Double)))
18+ ((L.fromChunks . map (T.pack . nonEmpty)) <$> arbitrary)
19+ where nonEmpty (NonEmpty a) = a
20+
21+-- Naming.
22+
23+{-
24+label (NonEmpty s) = case parse (anyWord8 <?> s) B.empty of
25+ (_, Left err) -> s `isInfixOf` err
26+ _ -> False
27+-}
28+
29+-- Basic byte-level combinators.
30+
31+maybeP p s = case P.parse p s `P.feed` T.empty of
32+ P.Done _ i -> Just i
33+ _ -> Nothing
34+
35+defP p s = P.parse p s `P.feed` T.empty
36+
37+satisfy w s = maybeP (P.satisfy (<=w)) (T.cons w s) == Just w
38+
39+char c s = maybeP (P.char c) (T.cons c s) == Just c
40+
41+anyChar s = maybeP P.anyChar s == if T.null s
42+ then Nothing
43+ else Just (T.head s)
44+
45+notChar c (NonEmpty s) = maybeP (P.notChar c) bs == if v == c
46+ then Nothing
47+ else Just v
48+ where v = T.head bs
49+ bs = T.pack s
50+
51+string s = maybeP (P.string s) s == Just s
52+
53+skipWhile w s =
54+ let t = T.dropWhile (<=w) s
55+ in case defP (P.skipWhile (<=w)) s of
56+ P.Done t' () -> t == t'
57+ _ -> False
58+
59+takeCount (Positive k) s =
60+ case maybeP (P.take k) s of
61+ Nothing -> k > T.length s
62+ Just s' -> k <= T.length s
63+
64+takeWhile w s =
65+ let (h,t) = T.span (==w) s
66+ in case defP (P.takeWhile (==w)) s of
67+ P.Done t' h' -> t == t' && h == h'
68+ _ -> False
69+
70+takeWhile1 w s =
71+ let s' = T.cons w s
72+ (h,t) = T.span (<=w) s'
73+ in case defP (P.takeWhile1 (<=w)) s' of
74+ P.Done t' h' -> t == t' && h == h'
75+ _ -> False
76+
77+takeTill w s =
78+ let (h,t) = T.break (==w) s
79+ in case defP (P.takeTill (==w)) s of
80+ P.Done t' h' -> t == t' && h == h'
81+ _ -> False
82+
83+takeWhile1_empty = maybeP (P.takeWhile1 undefined) T.empty == Nothing
84+
85+endOfInput s = maybeP P.endOfInput s == if T.null s
86+ then Just ()
87+ else Nothing
88+
89+tests = [
90+ testProperty "satisfy" satisfy,
91+ testProperty "char" char,
92+ testProperty "notChar" notChar,
93+ testProperty "anyChar" anyChar,
94+ testProperty "string" string,
95+ testProperty "skipWhile" skipWhile,
96+ testProperty "takeCount" takeCount,
97+ testProperty "takeWhile" takeWhile,
98+ testProperty "takeWhile1" takeWhile1,
99+ testProperty "takeWhile1_empty" takeWhile1_empty,
100+ testProperty "takeTill" takeTill,
101+ testProperty "endOfInput" endOfInput
102+ ]