A
B
C
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Text.HTML.Scalpel.Core
import Control.Applicative
import Control.Monad (guard)
import Data.List (isInfixOf)
import System.Exit
import Test.HUnit
import qualified Text.HTML.TagSoup as TagSoup
import qualified Text.Regex.TDFA
main = exit . failures =<< runTestTT (TestList [scrapeTests])
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"
(Just [])
(htmls ("b" @: []))
, scrapeTest
"foo"
(Just [""])
(htmls ("a" @: []))
, scrapeTest
"foobar"
(Just ["bar"])
(htmls ("a" @: ["key" @= "value"]))
, scrapeTest
"
A
B
C
A
B
C
p1
p2
p3
p4
" (Just ["p1", "p2", "p3", "p4"]) (texts "p") , scrapeTest "123" (Just ["1","2","3"]) (texts "a") , scrapeTest "123" (Just ["1","2","3"]) (texts $ "a" // "b") , scrapeTest "123" (Just ["1","2","3"]) (texts "b") ] 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)