Skip to content

Commit

Permalink
Reduce code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
fisx committed Jun 23, 2020
1 parent 36d602b commit 86c0f7b
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/Test/Hspec/Wai/Matcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,16 @@ data MatchHeader = MatchHeader ([Header] -> Body -> Maybe String)
data MatchBody = MatchBody ([Header] -> Body -> Maybe String)

bodyEquals :: Body -> MatchBody
bodyEquals body = MatchBody (\_ actual -> bodyMatcher actual body)
where
bodyMatcher :: Body -> Body -> Maybe String
bodyMatcher (toStrict -> actual) (toStrict -> expected) = actualExpected "body mismatch:" actual_ expected_ <$ guard (actual /= expected)
where
(actual_, expected_) = case (safeToString actual, safeToString expected) of
(Just x, Just y) -> (x, y)
_ -> (show actual, show expected)
bodyEquals body = bodySatisfies body (==)

bodyContains :: Body -> MatchBody
bodyContains body = MatchBody (\_ actual -> bodyMatcher actual body)
bodyContains body = bodySatisfies body SB.isInfixOf

bodySatisfies :: Body -> (ByteString -> ByteString -> Bool) -> MatchBody
bodySatisfies body prop = MatchBody (\_ actual -> bodyMatcher actual body)
where
bodyMatcher :: Body -> Body -> Maybe String
bodyMatcher (toStrict -> actual) (toStrict -> expected) = actualExpected "body mismatch:" actual_ expected_ <$
guard (not $ expected `SB.isInfixOf` actual)
bodyMatcher (toStrict -> actual) (toStrict -> expected) = actualExpected "body mismatch:" actual_ expected_ <$ guard (not $ expected `prop` actual)
where
(actual_, expected_) = case (safeToString actual, safeToString expected) of
(Just x, Just y) -> (x, y)
Expand Down

0 comments on commit 86c0f7b

Please sign in to comment.