Skip to content

Improve message produced by eitherResult for Partial #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Data/Attoparsec/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ maybeResult _ = Nothing
-- | Convert a 'Result' value to an 'Either' value. A 'T.Partial'
-- result is treated as failure.
eitherResult :: Result r -> Either String r
eitherResult (T.Done _ r) = Right r
eitherResult (T.Fail _ [] msg) = Left msg
eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
eitherResult _ = Left "Result: incomplete input"
eitherResult result = case feed result B.empty of
T.Done _ r -> Right r
T.Fail _ [] msg -> Left msg
T.Fail _ ctxs msg -> Left (intercalate " > " ctxs ++ ": " ++ msg)
T.Partial _ -> error "eitherResult: Partial"
9 changes: 5 additions & 4 deletions Data/Attoparsec/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ maybeResult _ = Nothing
-- | Convert a 'Result' value to an 'Either' value. A 'Partial' result
-- is treated as failure.
eitherResult :: Result r -> Either String r
eitherResult (T.Done _ r) = Right r
eitherResult (T.Fail _ [] msg) = Left msg
eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
eitherResult _ = Left "Result: incomplete input"
eitherResult result = case feed result T.empty of
T.Done _ r -> Right r
T.Fail _ [] msg -> Left msg
T.Fail _ ctxs msg -> Left (intercalate " > " ctxs ++ ": " ++ msg)
T.Partial _ -> error "eitherResult: Partial"

-- | A predicate that matches either a carriage return @\'\\r\'@ or
-- newline @\'\\n\'@ character.
Expand Down
8 changes: 8 additions & 0 deletions tests/QC/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ nonmembers :: [Word8] -> [Word8] -> Property
nonmembers s s' = property . not . any (`S.memberWord8` set) $ filter (not . (`elem` s)) s'
where set = S.fromList s

eitherResultPartialError :: Property
eitherResultPartialError = P.eitherResult (P.parse p input) === Left msg
where
p = (,) <$> P8.letter_ascii <*> P8.digit P.<?> "thing"
input = "a"
msg = "thing > digit: not enough input"

tests :: [TestTree]
tests = [
testProperty "anyWord8" anyWord8
Expand Down Expand Up @@ -211,4 +218,5 @@ tests = [
, testProperty "word8" word8
, testProperty "members" members
, testProperty "nonmembers" nonmembers
, testProperty "eitherResultPartialError" eitherResultPartialError
]
8 changes: 8 additions & 0 deletions tests/QC/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ nonmembers :: String -> String -> Property
nonmembers s s' = property . not . any (`S.member` set) $ filter (not . (`elem` s)) s'
where set = S.fromList s

eitherResultPartialError :: Property
eitherResultPartialError = P.eitherResult (P.parse p input) === Left msg
where
input = "a"
p = (,) <$> P.letter <*> P.digit P.<?> "thing"
msg = "thing > digit: not enough input"

tests :: [TestTree]
tests = [
testProperty "anyChar" anyChar
Expand Down Expand Up @@ -198,6 +205,7 @@ tests = [
, testProperty "takeWhile1_empty" takeWhile1_empty
, testProperty "members" members
, testProperty "nonmembers" nonmembers
, testProperty "eitherResultPartialError" eitherResultPartialError
, testGroup "FastSet" FastSet.tests
, testGroup "Regressions" Regressions.tests
]