{-# LANGUAGE FlexibleContexts #-} module Main (main) where import Text.HTML.Scalpel import Control.Applicative import System.Exit import Test.HUnit import qualified Text.HTML.TagSoup as TagSoup import qualified Text.Regex.TDFA main = exit . failures =<< runTestTT (TestList [ scrapeTests , scrapeHtmlsTests , scrapeHtmlTests ]) exit :: Int -> IO () exit 0 = exitSuccess exit n = exitWith $ ExitFailure n re :: String -> Text.Regex.TDFA.Regex re = Text.Regex.TDFA.makeRegex scrapeTests = "scrapeTests" ~: TestList [ scrapeTest "foo" (Just ["foo"]) (htmls ("a" @: [])) , scrapeTest "foobar" (Just ["foo", "bar"]) (htmls ("a" @: [])) , scrapeTest "foo" (Just ["foo"]) (htmls ("a" @: [])) , scrapeTest "foo" (Just ["foo", "foo"]) (htmls ("a" @: [])) , scrapeTest "foo" Nothing (htmls ("b" @: [])) , scrapeTest "foo" (Just [""]) (htmls ("a" @: [])) , scrapeTest "foobar" (Just ["bar"]) (htmls ("a" @: ["key" @= "value"])) , scrapeTest "foo" (Just ["foo"]) (htmls ("a" // "b" @: [] // "c")) , scrapeTest "foobarbaz" (Just ["foo", "bar"]) (htmls ("a" // "b")) , scrapeTest "foo" (Just ["foo"]) (htmls ("a" @: [hasClass "a"])) , scrapeTest "foo" Nothing (htmls ("a" @: [hasClass "c"])) , scrapeTest "foo" (Just ["foo"]) (htmls ("a" @: ["key" @=~ re "va(foo|bar|lu)e"])) , scrapeTest "foobar" (Just ["foo", "bar"]) (htmls ("a" @: [Any @= "value"])) , scrapeTest "foobar" (Just ["bar"]) (htmls ("a" @: [Any @= "value"])) , scrapeTest "foobar" (Just ["foo", "bar"]) (htmls (Any @: [Any @= "value"])) , scrapeTest "foobar" (Just ["bar"]) (htmls (Any @: [Any @= "value"])) , scrapeTest "foo" (Just "foo") (text "a") , scrapeTest "foobar" (Just "foo") (text "a") , scrapeTest "foobar" (Just ["foo", "bar"]) (texts "a") , scrapeTest "foobar" (Just [True, False]) (map (== "foo") <$> texts "a") , scrapeTest "" (Just "foo") (attr "key" "a") , scrapeTest "" (Just "baz") (attr "key2" $ "a" @: ["key1" @= "bar"]) , scrapeTest "foobar" (Just ["foo"]) (chroot "a" $ texts "b") , scrapeTest "foobar" (Just ["foo", "bar"]) (chroots "a" $ text "b") , scrapeTest "foobar" (Just "foo") (text ("a" // "b") <|> text ("a" // "c")) , scrapeTest "foobar" (Just "bar") (text ("a" // "d") <|> text ("a" // "c")) , scrapeTest "" (Just "foobar") (attr "src" "img") , scrapeTest "" (Just "foobar") (attr "src" "img") , scrapeTest "foobar" (Just ["foo", "bar"]) (texts "a") , scrapeTest "foobar" (Just ["foo", "bar"]) (texts "A") , scrapeTest "foo" (Just ["foo"]) (texts $ "A" @: ["b" @= "C"]) , scrapeTest "foo" Nothing (texts $ "A" @: ["b" @= "c"]) ] scrapeTest :: (Eq a, Show a) => String -> Maybe a -> Scraper String a -> Test scrapeTest html expected scraper = label ~: expected @=? actual where label = "scrape (" ++ show html ++ ")" actual = scrape scraper (TagSoup.parseTags html) scrapeHtmlTests = "scrapeTests" ~: TestList [ scrapeTest "foo" (Just "foo") (html "a") , scrapeTest "
" (Just "
  • 1
  • ") (html "li") , scrapeTest "
    " (Just "
    ") (html "div") ] scrapeHtmlsTests = "scrapeTests" ~: TestList [ scrapeTest "foobar" (Just ["foo","bar"]) (htmls "a") , scrapeTest "
    " (Just ["
  • 1
  • ", "
  • 2
  • "]) (htmls "li") , scrapeTest "
    " (Just ["
    "]) (htmls "div") ]