Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
-- | A whole bunch of simple test cases | ||
-- | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Text.Blaze.Tests.Cases | ||
( tests | ||
) where | ||
|
||
import Prelude hiding (div, id) | ||
import Control.Monad (forM_) | ||
|
||
import Test.HUnit ((@=?)) | ||
import Test.Framework.Providers.HUnit (testCase) | ||
import Test.Framework (Test) | ||
import qualified Data.ByteString.Lazy.Char8 as LBC | ||
|
||
import Text.Blaze.Html5 hiding (map) | ||
import qualified Text.Blaze.Html5 as H | ||
import Text.Blaze.Html5.Attributes | ||
import Text.Blaze.Tests.Util | ||
|
||
-- | Type for a simple HTML test. This data type contains the expected output | ||
-- and the HTML template. | ||
-- | ||
data HtmlTest = HtmlTest LBC.ByteString Html | ||
|
||
-- | Create tests from an HTML test | ||
-- | ||
makeTests :: String -> HtmlTest -> [Test] | ||
makeTests baseName (HtmlTest expected h) = | ||
[ testCase (baseName ++ " (String)") $ expected @=? renderUsingString h | ||
, testCase (baseName ++ " (Text)") $ expected @=? renderUsingText h | ||
, testCase (baseName ++ " (Utf8)") $ expected @=? renderUsingUtf8 h | ||
] | ||
|
||
-- | Actual tests | ||
-- | ||
tests :: [Test] | ||
tests = concatMap (uncurry makeTests) $ zip names | ||
-- Escaping cases | ||
[ HtmlTest ""&"" $ string "\"&\"" | ||
|
||
, HtmlTest "<img>" $ text "<img>" | ||
|
||
-- Simple cases | ||
, HtmlTest "<div id=\"foo\"><p>banana</p><span>banana</span></div>" $ | ||
div ! id "foo" $ do | ||
p "banana" | ||
H.span "banana" | ||
|
||
, HtmlTest "<img src=\"foo.png\" alt=\"bar\" />" $ | ||
img ! src "foo.png" ! alt "bar" | ||
|
||
-- Unicode cases | ||
, HtmlTest "<span id=\"&\">\206\187</span>" $ | ||
H.span ! id "&" $ "λ" | ||
|
||
, HtmlTest "\226\136\128x. x \226\136\136 A" | ||
"∀x. x ∈ A" | ||
|
||
, HtmlTest "$6, \226\130\172\&7.01, \194\163\&75" | ||
"$6, €7.01, £75" | ||
|
||
-- Control cases | ||
, HtmlTest "<li>4</li><li>5</li><li>6</li>" $ | ||
forM_ [4 :: Int .. 6] (li . showHtml) | ||
|
||
, HtmlTest "<br /><img /><area />" $ | ||
sequence_ [br, img, area] | ||
|
||
-- Attribute tests | ||
, HtmlTest "<p data-foo=\"bar\">A paragraph</p>" $ | ||
p ! (dataAttribute "foo" "bar") $ "A paragraph" | ||
|
||
, HtmlTest "<p dojoType=\"select\">A paragraph</p>" $ | ||
p ! (customAttribute "dojoType" "select") $ "A paragraph" | ||
] | ||
where | ||
names = map (("Test case " ++) . show) [1 :: Int ..] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-- | Utility functions for the blaze tests | ||
-- | ||
module Text.Blaze.Tests.Util | ||
( renderUsingString | ||
, renderUsingText | ||
, renderUsingUtf8 | ||
) where | ||
|
||
import Text.Blaze.Html5 hiding (map) | ||
import qualified Data.ByteString.Lazy as LB | ||
import qualified Text.Blaze.Renderer.Utf8 as Utf8 (renderHtml) | ||
import qualified Text.Blaze.Renderer.Text as Text (renderHtml) | ||
import qualified Text.Blaze.Renderer.String as String (renderHtml) | ||
import Blaze.ByteString.Builder as B (toLazyByteString) | ||
import Blaze.ByteString.Builder.Char.Utf8 as B (fromString) | ||
import Data.Text.Lazy.Encoding (encodeUtf8) | ||
|
||
-- | Render HTML to an UTF-8 encoded ByteString using the String renderer | ||
-- | ||
renderUsingString :: Html -> LB.ByteString | ||
renderUsingString = toLazyByteString . fromString . String.renderHtml | ||
|
||
-- | Render HTML to an UTF-8 encoded ByteString using the Text renderer | ||
-- | ||
renderUsingText :: Html -> LB.ByteString | ||
renderUsingText = encodeUtf8 . Text.renderHtml | ||
|
||
-- | Render HTML to an UTF-8 encoded ByteString using the Utf8 renderer | ||
-- | ||
renderUsingUtf8 :: Html -> LB.ByteString | ||
renderUsingUtf8 = Utf8.renderHtml |