git.iko.soy/typeable/attoparsec
on master help sign in
Add regression test for #105
commit097a5bf4ff9b4cf044924b0093bed8fadc0aebdf authorTomas Carnecky <tomas.carnecky@gmail.com> dateFri 24 Jun 2016, 00:25 (10 years ago) parentf9896c4
3 files changed+57
attoparsec.cabal+1 tests/QC/Text.hs+2 tests/QC/Text/Regressions.hs+54
attoparsec.cabal+1
@@ -91,6 +91,7 @@ test-suite tests
9191 QC.Simple
9292 QC.Text
9393 QC.Text.FastSet
94+ QC.Text.Regressions
9495
9596 ghc-options:
9697 -Wall -threaded -rtsopts
tests/QC/Text.hs+2
@@ -9,6 +9,7 @@ import Data.Int (Int64)
99import Prelude hiding (take, takeWhile)
1010import QC.Common (liftOp, parseT)
1111import qualified QC.Text.FastSet as FastSet
12+import qualified QC.Text.Regressions as Regressions
1213import Test.Tasty (TestTree, testGroup)
1314import Test.Tasty.QuickCheck (testProperty)
1415import Test.QuickCheck
@@ -190,4 +191,5 @@ tests = [
190191 , testProperty "members" members
191192 , testProperty "nonmembers" nonmembers
192193 , testGroup "FastSet" FastSet.tests
194+ , testGroup "Regressions" Regressions.tests
193195 ]
tests/QC/Text/Regressions.hsnew file+54
@@ -0,0 +1,54 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module QC.Text.Regressions (
4+ tests
5+ ) where
6+
7+import Data.Text (Text)
8+import qualified Data.Text as T
9+import Data.List (foldl')
10+import Data.Maybe (fromMaybe)
11+import Data.Char (isLower)
12+import Data.Monoid ((<>))
13+import QC.Rechunked (rechunkT)
14+import Test.Tasty (TestTree)
15+import Test.Tasty.QuickCheck (testProperty)
16+import Test.QuickCheck (Property, counterexample, forAll)
17+import qualified Data.Attoparsec.Text as A
18+
19+
20+--------------------------------------------------------------------------------
21+-- 105 was about runScanner not always returning the final state. The result
22+-- did depend on how the data was fed to the parser.
23+
24+t_issue105 :: Property
25+t_issue105 = expect issue105 "lowER" (A.Done "ER" "low")
26+
27+issue105 :: A.Parser Text
28+issue105 = do
29+ (_, firstFourLowercaseLetters) <- A.runScanner "" f
30+ return $ firstFourLowercaseLetters
31+
32+ where
33+ f :: Text -> Char -> Maybe Text
34+ f acc c = if T.length acc < 4 && isLower c
35+ then Just $ acc <> T.singleton c
36+ else Nothing
37+
38+
39+expect :: (Show r, Eq r) => A.Parser r -> Text -> A.Result r -> Property
40+expect p input wanted =
41+ forAll (rechunkT input) $ \in' ->
42+ let result = parse p in'
43+ in counterexample (show result ++ " /= " ++ show wanted) $
44+ fromMaybe False (A.compareResults result wanted)
45+
46+parse :: A.Parser r -> [Text] -> A.Result r
47+parse p (x:xs) = foldl' A.feed (A.parse p x) xs
48+parse p [] = A.parse p ""
49+
50+
51+tests :: [TestTree]
52+tests = [
53+ testProperty "issue105" t_issue105
54+ ]