{-# LANGUAGE FlexibleInstances, OverloadedStrings, ViewPatterns #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Main where import Control.Applicative import Data.Monoid (Monoid(..),(<>)) import Data.ByteString (ByteString) import qualified Data.ByteString.Char8 as S import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Data.Conduit as C import qualified Control.Monad.Trans.Resource as C import qualified Data.Conduit.List as CL import Test.Hspec import Test.Hspec.QuickCheck (prop) import Test.HUnit hiding (Test) import Test.QuickCheck import Text.HTML.TagStream import qualified Text.HTML.TagStream.ByteString as S import qualified Text.HTML.TagStream.Text as T main :: IO () main = hspec $ do describe "[ByteString] Property" $ do prop "Text nodes can't be empty" propTextNotEmpty prop "Parse results can't empty" propResultNotEmpty describe "[Text] Property" $ do prop "Text nodes can't be empty" propTextNotEmptyText prop "Parse results can't empty" propResultNotEmptyText describe "[ByteString]One pass parse" onePassTests describe "[ByteString]Streamline parse" streamlineTests describe "[Text]One pass parse" onePassTestsText describe "[Text]Streamline parse" streamlineTestsText propTextNotEmpty :: ByteString -> Bool propTextNotEmpty = either (const False) text_not_empty . S.decode where text_not_empty = all not_empty not_empty (Text s) = S.length s > 0 not_empty _ = True propResultNotEmpty :: ByteString -> Bool propResultNotEmpty s = either (const False) not_empty . S.decode $ s where not_empty tokens = (S.null s && null tokens) || (not (S.null s) && not (null tokens)) propTextNotEmptyText :: Text -> Bool propTextNotEmptyText = either (const False) text_not_empty . T.decode where text_not_empty = all not_empty not_empty (Text s) = not (T.null s) not_empty _ = True propResultNotEmptyText :: Text -> Bool propResultNotEmptyText s = either (const False) not_empty . T.decode $ s where not_empty tokens = (T.null s && null tokens) || (not (T.null s) && not (null tokens)) encodeTokenUtf8 :: Token' Text -> Token' ByteString encodeTokenUtf8 = fmap T.encodeUtf8 onePassTests :: Spec onePassTests = mapM_ one testcases where one (T.encodeUtf8 -> str, map encodeTokenUtf8 -> tokens) = it (S.unpack str) $ do result <- combineText <$> assertDecodeBS str assertEqual "one-pass parse result incorrect" tokens result onePassTestsText :: Spec onePassTestsText = mapM_ one testcases where one (str, tokens) = it (T.unpack str) $ do result <- combineText <$> assertDecodeText str assertEqual "one-pass parse result incorrect" tokens result streamlineTests :: Spec streamlineTests = mapM_ one testcases where isIncomplete (Incomplete _) = True isIncomplete _ = False one (T.encodeUtf8 -> str, map encodeTokenUtf8 -> tokens) = it (S.unpack str) $ do -- streamline parse result don't contain the trailing Incomplete token. let tokens' = reverse . dropWhile isIncomplete . reverse $ tokens result <- combineText <$> C.runResourceT ( CL.sourceList (map S.singleton (S.unpack str)) C.$= S.tokenStream C.$$ CL.consume ) assertEqual "streamline parse result incorrect" tokens' result streamlineTestsText :: Spec streamlineTestsText = mapM_ one testcases where isIncomplete (Incomplete _) = True isIncomplete _ = False one (T.encodeUtf8 -> str, tokens) = it (S.unpack str) $ do -- streamline parse result don't contain the trailing Incomplete token. let tokens' = reverse . dropWhile isIncomplete . reverse $ tokens result <- combineText <$> C.runResourceT ( CL.sourceList (map S.singleton (S.unpack str)) C.$= T.tokenStreamBS C.$$ CL.consume ) assertEqual "streamline parse result incorrect" tokens' result testcases :: [(Text, [Token' Text])] testcases = -- attributes {{{ [ ( "