Skip to content

Commit

Permalink
Add expectNeedleInHaystack field to NeedleHaystack
Browse files Browse the repository at this point in the history
  • Loading branch information
philderbeast committed Dec 19, 2024
1 parent 1fe81d4 commit 7e875de
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions cabal-testsuite/src/Test/Cabal/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ recordMode mode = withReaderT (\env -> env {
-- text to search in.
data NeedleHaystack =
NeedleHaystack {
expectNeedleInHaystack :: Bool,
needleBwd :: (String -> String),
needleFwd :: (String -> String),
haystackBwd :: (String -> String),
Expand All @@ -810,40 +811,37 @@ data NeedleHaystack =
-- | Symmetric needle and haystack functions, the same conversion for each going
-- forward and the same coversion for each going backward.
symNeedleHaystack :: (String -> String) -> (String -> String) -> NeedleHaystack
symNeedleHaystack bwd fwd = NeedleHaystack bwd fwd bwd fwd
symNeedleHaystack bwd fwd = NeedleHaystack True bwd fwd bwd fwd

-- | Needle and haystack functions that do not change the strings.
needleHaystack :: NeedleHaystack
needleHaystack = NeedleHaystack id id id id

assertOutputContainsOn :: MonadIO m => WithCallStack (NeedleHaystack -> String -> Result -> m ())
assertOutputContainsOn NeedleHaystack{..} (needleFwd -> needle) (haystackFwd . resultOutput -> output) =
withFrozenCallStack $
unless (needle `isInfixOf` output) $
assertFailure $ "expected:\n" ++ needleBwd needle ++
"\nin output:\n" ++ haystackBwd output

assertOutputDoesNotContainOn :: MonadIO m => WithCallStack (NeedleHaystack -> String -> Result -> m ())
assertOutputDoesNotContainOn NeedleHaystack{..} (needleFwd -> needle) (haystackFwd . resultOutput -> output) =
withFrozenCallStack $
when (needle `isInfixOf` output) $
assertFailure $ "unexpected:\n" ++ needleBwd needle ++
"\nin output:\n" ++ haystackBwd output
needleHaystack = NeedleHaystack True id id id id

assertOn :: MonadIO m => WithCallStack (NeedleHaystack -> String -> Result -> m ())
assertOn NeedleHaystack{..} (needleFwd -> needle) (haystackFwd . resultOutput -> output) =
withFrozenCallStack $
if expectNeedleInHaystack
then unless (needle `isInfixOf` output)
$ assertFailure $ "expected:\n" ++ needle ++
"\nin output:\n" ++ output
else when (needle `isInfixOf` output)
$ assertFailure $ "unexpected:\n" ++ needle ++
"\nin output:\n" ++ output

assertOutputContainsWrapped :: MonadIO m => WithCallStack (String -> Result -> m ())
assertOutputContainsWrapped = assertOutputContainsOn needleHaystack{haystackFwd = lineBreaksToSpaces}
assertOutputContainsWrapped = assertOn needleHaystack{haystackFwd = lineBreaksToSpaces}

assertOutputContains :: MonadIO m => WithCallStack (String -> Result -> m ())
assertOutputContains = assertOutputContainsOn needleHaystack{haystackBwd = decodeLfMarkLines, haystackFwd = encodeLf}
assertOutputContains = assertOn needleHaystack{haystackBwd = decodeLfMarkLines, haystackFwd = encodeLf}

assertOutputDoesNotContain :: MonadIO m => WithCallStack (String -> Result -> m ())
assertOutputDoesNotContain = assertOutputDoesNotContainOn needleHaystack{haystackBwd = decodeLfMarkLines, haystackFwd = encodeLf}
assertOutputDoesNotContain = assertOn needleHaystack{expectNeedleInHaystack = False, haystackBwd = decodeLfMarkLines, haystackFwd = encodeLf}

assertOutputContainsMultiline :: MonadIO m => WithCallStack (String -> Result -> m ())
assertOutputContainsMultiline = assertOutputContainsOn (symNeedleHaystack decodeLfMarkLines encodeLf)
assertOutputContainsMultiline = assertOn (symNeedleHaystack decodeLfMarkLines encodeLf)

assertOutputDoesNotContainMultiline :: MonadIO m => WithCallStack (String -> Result -> m ())
assertOutputDoesNotContainMultiline = assertOutputDoesNotContainOn (symNeedleHaystack decodeLfMarkLines encodeLf)
assertOutputDoesNotContainMultiline = assertOn (symNeedleHaystack decodeLfMarkLines encodeLf){expectNeedleInHaystack = False}

assertFindInFile :: MonadIO m => WithCallStack (String -> FilePath -> m ())
assertFindInFile needle path =
Expand Down

0 comments on commit 7e875de

Please sign in to comment.