git.iko.soy/typeable/attoparsec
on 0.8.5.1 help sign in
116 lines 3.5 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Main (main) where
4import Control.Monad (forM_)
5import Data.Maybe (isJust)
6import Data.Word (Word8)
7import Prelude hiding (takeWhile)
8import QCSupport
9import Test.Framework (defaultMain, testGroup)
10import Test.Framework.Providers.QuickCheck2 (testProperty)
11import Test.QuickCheck hiding (NonEmpty)
12import qualified Data.Attoparsec as P
13import qualified Data.ByteString as B
14import qualified Data.ByteString.Char8 as C
16-- Make sure that structures whose types claim they are non-empty
17-- really are.
19nonEmptyList l = length (nonEmpty l) > 0
20 where types = l :: NonEmpty [Int]
21nonEmptyBS l = B.length (nonEmpty l) > 0
23-- Naming.
25{-
26label (NonEmpty s) = case parse (anyWord8 <?> s) B.empty of
27 (_, Left err) -> s `isInfixOf` err
28 _ -> False
29-}
31-- Basic byte-level combinators.
33maybeP p s = case P.parse p s `P.feed` B.empty of
34 P.Done _ i -> Just i
35 _ -> Nothing
37defP p s = P.parse p s `P.feed` B.empty
39satisfy w s = maybeP (P.satisfy (<=w)) (B.cons w s) == Just w
41word8 w s = maybeP (P.word8 w) (B.cons w s) == Just w
43anyWord8 s = maybeP P.anyWord8 s == if B.null s
44 then Nothing
45 else Just (B.head s)
47notWord8 w (NonEmpty s) = maybeP (P.notWord8 w) s == if v == w
48 then Nothing
49 else Just v
50 where v = B.head s
52string s = maybeP (P.string s) s == Just s
54skipWhile w s =
55 let t = B.dropWhile (<=w) s
56 in case defP (P.skipWhile (<=w)) s of
57 P.Done t' () -> t == t'
58 _ -> False
60takeCount (Positive k) s =
61 case maybeP (P.take k) s of
62 Nothing -> k > B.length s
63 Just s' -> k <= B.length s
65takeWhile w s =
66 let (h,t) = B.span (==w) s
67 in case defP (P.takeWhile (==w)) s of
68 P.Done t' h' -> t == t' && h == h'
69 _ -> False
71takeWhile1 w s =
72 let s' = B.cons w s
73 (h,t) = B.span (<=w) s'
74 in case defP (P.takeWhile1 (<=w)) s' of
75 P.Done t' h' -> t == t' && h == h'
76 _ -> False
78takeTill w s =
79 let (h,t) = B.break (==w) s
80 in case defP (P.takeTill (==w)) s of
81 P.Done t' h' -> t == t' && h == h'
82 _ -> False
84ensure n s = case defP (P.ensure m) s of
85 P.Done _ () -> B.length s >= m
86 _ -> B.length s < m
87 where m = (n `mod` 220) - 20
89takeWhile1_empty = maybeP (P.takeWhile1 undefined) B.empty == Nothing
91endOfInput s = maybeP P.endOfInput s == if B.null s
92 then Just ()
93 else Nothing
95main = defaultMain tests
97tests = [
98 testGroup "fnord" [
99 testProperty "nonEmptyList" nonEmptyList,
100 testProperty "nonEmptyBS" nonEmptyBS,
101 testProperty "satisfy" satisfy,
102 testProperty "word8" word8,
103 testProperty "notWord8" notWord8,
104 testProperty "anyWord8" anyWord8,
105 testProperty "string" string,
106 testProperty "skipWhile" skipWhile,
107 testProperty "takeCount" takeCount,
108 testProperty "takeWhile" takeWhile,
109 testProperty "takeWhile1" takeWhile1,
110 testProperty "takeWhile1_empty" takeWhile1_empty,
111 testProperty "takeTill" takeTill,
112 testProperty "endOfInput" endOfInput,
113 testProperty "ensure" ensure
114 ]
116 ]