1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
import Text.Hamlet.XML
import qualified Text.XML as X
import Test.HUnit
import Test.Hspec
import qualified Data.Map as Map
main :: IO ()
main = hspec $ do
describe "xml-hamlet" $ do
it "handles plain tags" $ [xml|
<foo>
<baz>
|] @?=
[ X.NodeElement $ X.Element "foo" Map.empty
[ X.NodeElement $ X.Element "baz" Map.empty []
]
]
it "handles raw text" $ [xml|
<foo>
<baz>bin
|] @?=
[ X.NodeElement $ X.Element "foo" Map.empty
[ X.NodeElement $ X.Element "baz" Map.empty
[ X.NodeContent "bin"
]
]
]
it "handles variables" $ [xml|
<foo>
<baz>#{bin}
|] @?=
[ X.NodeElement $ X.Element "foo" Map.empty
[ X.NodeElement $ X.Element "baz" Map.empty
[ X.NodeContent "bin"
]
]
]
it "handles embed" $ [xml|
<foo>
<baz>^{nodes}
|] @?=
[ X.NodeElement $ X.Element "foo" Map.empty
[ X.NodeElement $ X.Element "baz" Map.empty nodes
]
]
it "handles attributes" $ [xml|
<foo>
<bar here=there>
<baz>
|] @?=
[ X.NodeElement $ X.Element "foo" Map.empty
[ X.NodeElement $ X.Element "bar" (Map.singleton "here" "there")
[ X.NodeElement $ X.Element "baz" Map.empty []
]
]
]
it "handles attributes" $ [xml|
<foo>
<bar here=there>
<baz :False:false=false :True:true=#{true} *{attrs}>
|] @?=
[ X.NodeElement $ X.Element "foo" Map.empty
[ X.NodeElement $ X.Element "bar" (Map.singleton "here" "there")
[ X.NodeElement $ X.Element "baz" (Map.fromList (("true", "true") : attrs)) []
]
]
]
it "handles forall" $ [xml|
$forall x <- xs
<word>#{x}
|] @?=
[ X.NodeElement $ X.Element "word" Map.empty [X.NodeContent "foo"]
, X.NodeElement $ X.Element "word" Map.empty [X.NodeContent "bar"]
, X.NodeElement $ X.Element "word" Map.empty [X.NodeContent "baz"]
]
it "handles with" $ [xml|
$with ys <- xs
$forall x <- ys
<word>#{x}
|] @?=
[ X.NodeElement $ X.Element "word" Map.empty [X.NodeContent "foo"]
, X.NodeElement $ X.Element "word" Map.empty [X.NodeContent "bar"]
, X.NodeElement $ X.Element "word" Map.empty [X.NodeContent "baz"]
]
it "handles maybe" $ [xml|
$maybe _x <- Just five
<one>
$nothing
<two>
$maybe _x <- Nothing
<three>
$nothing
<four>
|] @?=
[ X.NodeElement $ X.Element "one" Map.empty []
, X.NodeElement $ X.Element "four" Map.empty []
]
it "handles conditionals" $ [xml|
$if True
<one>
$else
<two>
$if False
<three>
$elseif True
<four>
$if False
<five>
$elseif False
<six>
$else
<seven>
|] @?=
[ X.NodeElement $ X.Element "one" Map.empty []
, X.NodeElement $ X.Element "four" Map.empty []
, X.NodeElement $ X.Element "seven" Map.empty []
]
it "case on Maybe" $
let nothing = Nothing
justTrue = Just True
in [xml|
$case nothing
$of Just _val
$of Nothing
<one>
$case justTrue
$of Just val
$if val
<two>
$of Nothing
$case (Just $ not False)
$of Nothing
$of Just val
$if val
<three>
$case Nothing
$of Just _val
$of _
<four>
|] @?=
[ X.NodeElement $ X.Element "one" Map.empty []
, X.NodeElement $ X.Element "two" Map.empty []
, X.NodeElement $ X.Element "three" Map.empty []
, X.NodeElement $ X.Element "four" Map.empty []
]
it "recognizes clark notation" $ [xml|
<{foo}bar {baz}bin="x">
|] @?= [X.NodeElement $ X.Element "{foo}bar" (Map.singleton "{baz}bin" "x") []]
it "recognizes clark with URLs" $ [xml|
<{http://www.example.com/foo/bar}baz>
|] @?= [X.NodeElement $ X.Element "{http://www.example.com/foo/bar}baz" Map.empty []]
it "allow embedding comments" $[xml|^{comment}|] @?= comment
it "multiline tags" $ [xml|
<foo bar=baz
bin=bin>content
|] @?= [xml|<foo bar=baz bin=bin>content|]
it "short circuiting of attributes" $ [xml|<foo :False:x=#{undefined}>|] @?= [xml|<foo>|]
it "Hash in attribute value" $ [xml|<a href=#>|] @?= [xml|<a href="#">|]
where
bin = "bin"
nodes = [X.NodeInstruction $ X.Instruction "ifoo" "ibar"]
true = "true"
attrs = [("one","a"), ("two","b")]
xs = ["foo", "bar", "baz"]
comment = [X.NodeComment "somecomment"]
five :: Int
five = 5
|