From bbb1a66bac1ef2ae920b8739d61c13cb0023e987 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Sun, 3 Mar 2013 17:16:10 +0000 Subject: [PATCH 1/2] add a htmlAnyContain function --- yesod-test/Yesod/Test.hs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index be57a2d3..c62fa50a 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -253,6 +253,16 @@ htmlAllContain query search = do _ -> liftIO $ HUnit.assertBool ("Not all "++T.unpack query++" contain "++search) $ DL.all (DL.isInfixOf search) (map (TL.unpack . decodeUtf8) matches) +-- | Queries the html using a css selector, and passes if any matched +-- element contains the given string. +htmlAnyContain :: HoldsResponse a => Query -> String -> ST.StateT a IO () +htmlAnyContain query search = do + matches <- htmlQuery query + case matches of + [] -> failure $ "Nothing matched css query: " <> query + _ -> liftIO $ HUnit.assertBool ("None of "++T.unpack query++" contain "++search) $ + DL.any (DL.isInfixOf search) (map (TL.unpack . decodeUtf8) matches) + -- | Performs a css query on the last response and asserts the matched elements -- are as many as expected. htmlCount :: HoldsResponse a => Query -> Int -> ST.StateT a IO () From d06e4530f9d15cc8d3d2725112f34917384ddcd4 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Sun, 3 Mar 2013 17:25:26 +0000 Subject: [PATCH 2/2] and export the new function from the module --- yesod-test/Yesod/Test.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index c62fa50a..55193405 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -52,7 +52,7 @@ module Yesod.Test ( -- * Assertions assertEqual, assertHeader, assertNoHeader, statusIs, bodyEquals, bodyContains, - htmlAllContain, htmlCount, + htmlAllContain, htmlAnyContain, htmlCount, -- * Utils for debugging tests printBody, printMatches,