Skip to content

Commit

Permalink
Reduces code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
solatis committed Feb 18, 2015
1 parent e6990f6 commit 7a5634e
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions test/Network/AttoparsecSpec.hs
Expand Up @@ -59,31 +59,29 @@ spec = do
in (pairSockets writeSocket readSocket) `shouldReturn` Right 1234

it "it should leave unconsumed data in tact" $
let writeSocket s = do
NS.send s "1234ab"
let writeSocket s = NS.send s "1234ab"

readSocket s = runErrorT $ Atto.parseOne s (Atto.parse numberParser)
numberParser = decimal

in (pairSockets writeSocket readSocket) `shouldReturn` Left "Unconsumed data left on socket: \"ab\""

it "it should throw an error when the provided data is incorrect" $
let writeSocket s = do
NS.send s "ab"

let writeSocket s = NS.send s "ab"
readSocket s = runErrorT $ Atto.parseOne s (Atto.parse numberParser)
numberParser = decimal

in (pairSockets writeSocket readSocket) `shouldReturn` Left "An error occured while parsing input: \"ab\""

describe "when parsing multiple matching objects" $ do
let numberParser = do
num <- decimal
endOfLine
return num

it "should return error when using single object parser" $
let writeSocket s = NS.send s "1234\n5678\n"
readSocket s = runErrorT $ Atto.parseOne s (Atto.parse numberParser)
numberParser = do
num <- decimal
endOfLine
return num

in (pairSockets writeSocket readSocket) `shouldReturn` Left "Unconsumed data left on socket: \"5678\\n\""

Expand All @@ -92,22 +90,13 @@ spec = do
readSocket s = runErrorT $ do
(_, xs) <- Atto.parseMany s (Atto.parse numberParser) (Atto.parse numberParser)
return xs
numberParser = do
num <- decimal
endOfLine
return num

in (pairSockets writeSocket readSocket) `shouldReturn` Right [1234, 5678]

it "should return single object when using multi object parser and providing partial data" $
let writeSocket s = NS.send s "1234\n56"
readSocket s = runErrorT $ do
(_, xs) <- Atto.parseMany s (Atto.parse numberParser) (Atto.parse numberParser)
return xs
numberParser = do
num <- decimal
endOfLine
return num

in (pairSockets writeSocket readSocket) `shouldReturn` Right [1234]

Expand All @@ -120,10 +109,6 @@ spec = do
(p, xs1) <- Atto.parseMany s (Atto.parse numberParser) (Atto.parse numberParser)
(_, xs2) <- Atto.parseMany s (Atto.parse numberParser) p
return (xs1 ++ xs2)
numberParser = do
num <- decimal
endOfLine
return num

in (pairSockets writeSocket readSocket) `shouldReturn` Right [1234, 5678, 9012]

Expand All @@ -132,10 +117,6 @@ spec = do
readSocket s = runErrorT $ do
(_, xs) <- Atto.parseMany s (Atto.parse numberParser) (Atto.parse numberParser)
return xs
numberParser = do
num <- decimal
endOfLine
return num

in (pairSockets writeSocket readSocket) `shouldReturn` Right []

Expand All @@ -148,9 +129,5 @@ spec = do
(p, xs1) <- Atto.parseMany s (Atto.parse numberParser) (Atto.parse numberParser)
(_, xs2) <- Atto.parseMany s (Atto.parse numberParser) p
return (xs1 ++ xs2)
numberParser = do
num <- decimal
endOfLine
return num

in (pairSockets writeSocket readSocket) `shouldReturn` Right [1234]

0 comments on commit 7a5634e

Please sign in to comment.