@@ -8,7 +8,8 @@ import qualified Data.ByteString.Char8 as B8
88import Test.Framework.Providers.HUnit
99import Test.HUnit hiding (Test, path)
1010
11−import Data.Iteratee hiding (filter, length, foldl')
11+import qualified Data.Enumerator as E
12+import Data.Enumerator (($$))
1213import Data.List (foldl')
1314
1415import System.Directory
@@ -28,21 +29,19 @@ main = defaultMain
2829 , testCase "decode invalid document (without segfault)" caseDecodeInvalidDocument
2930 ]
3031
31−counter :: (Event -> Bool) -> Int -> IterateeG [] Event IO Int
32+counter :: (Event -> Bool) -> Int -> E.Step Event IO Int
3233counter pred' acc =
33− IterateeG go
34+ E.Continue go
3435 where
35− go (EOF x) = return $ Done acc $ EOF x
36− go (Chunk c) =
37− let acc' = length (filter pred' c) + acc
38− in return $ Cont (counter pred' acc') Nothing
36+ go E.EOF = E.yield acc E.EOF
37+ go (E.Chunks c) = E.returnI $ counter pred' $ length (filter pred' c) + acc
3938
4039caseHelper :: String
4140 -> (Event -> Bool)
4241 -> Int
4342 -> Assertion
4443caseHelper yamlString pred' expRes = do
45− res <- decode (B8.pack yamlString) (counter pred' 0) >>= run
44+ Right res <- E.run $ decode (B8.pack yamlString) (counter pred' 0)
4645 res @?= expRes
4746
4847caseCountScalarsWithAnchor :: Assertion
@@ -79,36 +78,34 @@ caseCountAliases =
7978
8079caseCountScalars :: Assertion
8180caseCountScalars = do
82− res <- decode yamlBS (counter' accum) >>= run
81+ Right res <- E.run $ decode yamlBS $ counter' accum
8382 res @?= (7, 1, 2)
84− where
85− yamlString = "foo:\n baz: [bin1, bin2, bin3]\nbaz: bazval"
86− yamlBS = B8.pack yamlString
87− counter' acc = IterateeG $ \s ->
88− case s of
89− EOF x -> return $ Done acc $ EOF x
90− Chunk c ->
91− let acc' = foldl' adder acc c
92− in return $ Cont (counter' acc') Nothing
93− adder (s, l, m) (EventScalar{}) = (s + 1, l, m)
94− adder (s, l, m) (EventSequenceStart{}) = (s, l + 1, m)
95− adder (s, l, m) (EventMappingStart{}) = (s, l, m + 1)
96− adder a _ = a
97− accum = (0, 0, 0) :: (Int, Int, Int)
83+ where
84+ yamlString = "foo:\n baz: [bin1, bin2, bin3]\nbaz: bazval"
85+ yamlBS = B8.pack yamlString
86+ counter' acc = E.Continue $ \s ->
87+ case s of
88+ E.EOF -> E.yield acc E.EOF
89+ E.Chunks c -> E.returnI $ counter' $ foldl' adder acc c
90+ adder (s, l, m) (EventScalar{}) = (s + 1, l, m)
91+ adder (s, l, m) (EventSequenceStart{}) = (s, l + 1, m)
92+ adder (s, l, m) (EventMappingStart{}) = (s, l, m + 1)
93+ adder a _ = a
94+ accum = (0, 0, 0) :: (Int, Int, Int)
9895
9996caseLargestString :: Assertion
10097caseLargestString = do
101− res <- decodeFile filePath (dec accum) >>= run
98+ Right res <- E.run $ decodeFile filePath $ dec accum
10299 res @?= (length expected, expected)
103100 where
104101 expected = "this one is just a little bit bigger than the others"
105102 filePath = "test/largest-string.yaml"
106− dec acc = IterateeG $ \s ->
103+ dec acc = E.Continue $ \s ->
107104 case s of
108− EOF x -> return $ Done acc $ EOF x
109− Chunk c ->
105+ E.EOF -> E.yield acc E.EOF
106+ E.Chunks c ->
110107 let acc' = foldl' adder acc c
111− in return $ Cont (dec acc') Nothing
108+ in E.returnI $ dec acc'
112109 adder (i, s) (EventScalar bs _ _ _) =
113110 let s' = B8.unpack bs
114111 i' = length s'
@@ -124,17 +121,13 @@ instance Eq MyEvent where
124121
125122caseEncodeDecode :: Assertion
126123caseEncodeDecode = do
127− eList <- decode yamlBS (dec []) >>= run
128− bs <- enumPure1Chunk eList encode >>= run
129− eList2 <- decode bs (dec []) >>= run
124+ Right eList <- E.run $ decode yamlBS $$ E.consume
125+ Right bs <- E.run $ E.enumList 1 eList $$ encode
126+ Right eList2 <- E.run $ decode bs $$ E.consume
130127 map MyEvent eList @=? map MyEvent eList2
131− where
132− yamlString = "foo: bar\nbaz:\n - bin1\n - bin2\n"
133− yamlBS = B8.pack yamlString
134− dec front = IterateeG $ \s ->
135− case s of
136− EOF x -> return $ Done front $ EOF x
137− Chunk c -> return $ Cont (dec $ front ++ c) Nothing
128+ where
129+ yamlString = "foo: bar\nbaz:\n - bin1\n - bin2\n"
130+ yamlBS = B8.pack yamlString
138131
139132removeFile' :: FilePath -> IO ()
140133removeFile' fp = do
@@ -144,46 +137,40 @@ removeFile' fp = do
144137caseEncodeDecodeFile :: Assertion
145138caseEncodeDecodeFile = do
146139 removeFile' tmpPath
147− eList <- decodeFile filePath (dec []) >>= run
148− enumPure1Chunk eList (encodeFile tmpPath) >>= run
149− eList2 <- decodeFile filePath (dec []) >>= run
140+ Right eList <- E.run $ decodeFile filePath $$ E.consume
141+ E.run $ E.enumList 1 eList $$ encodeFile tmpPath
142+ Right eList2 <- E.run $ decodeFile filePath $$ E.consume
150143 map MyEvent eList @=? map MyEvent eList2
151− where
152− filePath = "test/largest-string.yaml"
153− tmpPath = "tmp.yaml"
154− dec front = IterateeG $ \s ->
155− case s of
156− EOF x -> return $ Done front $ EOF x
157− Chunk c -> return $ Cont (dec $ front ++ c) Nothing
144+ where
145+ filePath = "test/largest-string.yaml"
146+ tmpPath = "tmp.yaml"
158147
159148caseInterleave :: Assertion
160149caseInterleave = do
161150 removeFile' tmpPath
162151 removeFile' tmpPath2
163− decodeFile filePath (encodeFile tmpPath :: IterateeG [] Event IO ()) >>= run
164− decodeFile tmpPath (encodeFile tmpPath2 :: IterateeG [] Event IO ()) >>= run
152+ Right () <- E.run $ decodeFile filePath $$ encodeFile tmpPath
153+ Right () <- E.run $ decodeFile tmpPath $$ encodeFile tmpPath2
165154 f1 <- readFile tmpPath
166155 f2 <- readFile tmpPath2
167156 f1 @=? f2
168− where
169− filePath = "test/largest-string.yaml"
170− tmpPath = "tmp.yaml"
171− tmpPath2 = "tmp2.yaml"
157+ where
158+ filePath = "test/largest-string.yaml"
159+ tmpPath = "tmp.yaml"
160+ tmpPath2 = "tmp2.yaml"
172161
173162caseDecodeInvalidDocument :: Assertion
174163caseDecodeInvalidDocument = do
175− x <- decode yamlBS ignore
176− y <- runIter x $ EOF Nothing
177− case y of
178− Cont _ _ -> return ()
179− _ -> do
164+ x <- E.run $ decode yamlBS ignore
165+ case x of
166+ Left _ -> return ()
167+ Right y -> do
180168 putStrLn $ "bad return value: " ++ show y
181169 assertFailure "expected parsing exception, but got no errors"
182170 where
183171 yamlString = " - foo\n - baz\nbuz"
184172 yamlBS = B8.pack yamlString
185− ignore :: IterateeG [] Event IO ()
186− ignore = IterateeG $ \s ->
173+ ignore = E.Continue $ \s ->
187174 case s of
188− EOF x -> return $ Done () $ EOF x
189− Chunk _c -> return $ Cont ignore Nothing
175+ E.EOF -> E.yield () E.EOF
176+ E.Chunks _-> E.returnI ignore