{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
import Test.Framework (defaultMain, testGroup, Test)
import Test.Framework.Providers.HUnit
import Test.HUnit hiding (Test)
import Text.Hamlet
import Data.ByteString.Lazy.UTF8 (toString)
main :: IO ()
main = defaultMain [testSuite]
testSuite :: Test
testSuite = testGroup "Text.Hamlet"
[ testCase "empty" caseEmpty
, testCase "static" caseStatic
, testCase "tag" caseTag
, testCase "var" caseVar
, testCase "var chain " caseVarChain
, testCase "url" caseUrl
, testCase "url chain " caseUrlChain
, testCase "embed" caseEmbed
, testCase "embed chain " caseEmbedChain
, testCase "if" caseIf
, testCase "if chain " caseIfChain
, testCase "else" caseElse
, testCase "else chain " caseElseChain
, testCase "elseif" caseElseIf
, testCase "elseif chain " caseElseIfChain
, testCase "list" caseList
, testCase "list chain" caseListChain
, testCase "script not empty" caseScriptNotEmpty
, testCase "meta empty" caseMetaEmpty
, testCase "input empty" caseInputEmpty
, testCase "multiple classes" caseMultiClass
, testCase "attrib order" caseAttribOrder
, testCase "nothing" caseNothing
, testCase "nothing chain " caseNothingChain
, testCase "just" caseJust
, testCase "just chain " caseJustChain
, testCase "constructor" caseConstructor
, testCase "url + params" caseUrlParams
, testCase "escape" caseEscape
, testCase "empty statement list" caseEmptyStatementList
, testCase "attribute conditionals" caseAttribCond
, testCase "non-ascii" caseNonAscii
, testCase "maybe function" caseMaybeFunction
, testCase "trailing dollar sign" caseTrailingDollarSign
, testCase "non leading percent sign" caseNonLeadingPercent
, testCase "quoted attributes" caseQuotedAttribs
, testCase "spaced derefs" caseSpacedDerefs
, testCase "attrib vars" caseAttribVars
, testCase "strings and html" caseStringsAndHtml
, testCase "nesting" caseNesting
, testCase "trailing space" caseTrailingSpace
, testCase "currency symbols" caseCurrency
, testCase "external" caseExternal
, testCase "parens" caseParens
, testCase "hamlet' and xhamlet'" caseHamlet'
, testCase "hamletDebug" caseHamletDebug
, testCase "hamlet runtime" caseHamletRT
]
data Url = Home | Sub SubUrl
data SubUrl = SubUrl
render :: Url -> String
render Home = "url"
render (Sub SubUrl) = "suburl"
data Arg url = Arg
{ getArg :: Arg url
, var :: Html ()
, url :: Url
, embed :: Hamlet url
, true :: Bool
, false :: Bool
, list :: [Arg url]
, nothing :: Maybe String
, just :: Maybe String
, urlParams :: (Url, [(String, String)])
}
theArg :: Arg url
theArg = Arg
{ getArg = theArg
, var = string ""
, url = Home
, embed = [$hamlet|embed|]
, true = True
, false = False
, list = [theArg, theArg, theArg]
, nothing = Nothing
, just = Just "just"
, urlParams = (Home, [("foo", "bar"), ("foo1", "bar1")])
}
helper :: String -> Hamlet Url -> Assertion
helper res h = do
let x = renderHamlet render h
res @=? toString x
caseEmpty :: Assertion
caseEmpty = helper "" [$hamlet||]
caseStatic :: Assertion
caseStatic = helper "some static content" [$hamlet|some static content|]
caseTag :: Assertion
caseTag = helper "
1 |
2 |
foo
\nbar
\n" [$hamletDebug| %p foo %p bar |] caseHamletRT :: Assertion caseHamletRT = do temp <- parseHamletRT defaultHamletSettings "$var$" rt <- parseHamletRT defaultHamletSettings $ unlines [ "$foo.bar.baz$ bin $" , "$forall list l" , " $l$" , "$maybe just j" , " $j$" , "$maybe nothing n" , "$nothing" , " nothing" , "^template^" , "@url@" , "$if false" , "$elseif false" , "$elseif true" , " a" , "$if false" , "$else" , " b" , "@?urlp@" ] let scope = HDMap [ ("foo", HDMap [ ("bar", HDMap [ ("baz", HDHtml $ preEscapedString "foo