forked from astynax/hemmet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spec.hs
140 lines (130 loc) · 4.37 KB
/
Spec.hs
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
import Data.Text as T
import Test.Hspec
import Test.Tasty
import Test.Tasty.Hspec
import Text.Megaparsec
import Hemmet.BEM.Template as BEM
import Hemmet.BEM.Tree
import Hemmet.Tree
import Golden
main :: IO ()
main = tests >>= defaultMain
tests :: IO TestTree
tests = do
us <- testGroup "Unit tests" <$> makeUnitTests
gs <- testGroup "Golden tests" <$> makeGoldenTests "test/tests"
return $ testGroup "Tests" [us, gs]
makeUnitTests :: IO [TestTree]
makeUnitTests =
testSpecs $ do
parserSpec
transformerSpec
parserSpec :: Spec
parserSpec =
describe "Lib.parse" $ do
it "parses single block" $ do
"div:foo" `shouldMean` tb "div" "foo" [] []
":foo" `shouldMean` tb "" "foo" [] []
it "parses single element" $ do
":b>span.xyz" `shouldMean` te "span" "xyz" [] []
":b>.xyz" `shouldMean` te "" "xyz" [] []
it "parses block/element modifiers" $ do
":b~m1~m2" `shouldMean` tb "" "b" [Mod "m1", Mod "m2"] []
":b>.e~m1~m2" `shouldMean` te "" "e" [Mod "m1", Mod "m2"] []
it "parses block/element vars" $ do
":b$m1$m2" `shouldMean` tb "" "b" [Var "m1", Var "m2"] []
":b>.e$m1$m2" `shouldMean` te "" "e" [Var "m1", Var "m2"] []
it "ensures proper naming" $ do
shouldMean "_underscored:x" $ tb "_underscored" "x" [] []
shouldFail "1leadingDigit:x"
-- block names
shouldFail ":snake_case"
shouldFail ":uPPercase"
shouldFail ":1leading-digit"
-- elem names
shouldFail ":b>snake_case"
shouldFail ":b>uPPercase"
shouldFail ":b>1leading-digit"
-- modifier names
shouldMean ":b~snake_case" $ tb "" "b" [Mod "snake_case"] []
shouldFail ":b~uPPer_case"
shouldFail ":b~1leading-digit"
-- var names
shouldFail ":b$kebab-case"
shouldMean ":b$_underscored" $ tb "" "b" [Var "_underscored"] []
shouldMean ":b$uPPer_case" $ tb "" "b" [Var "uPPer_case"] []
shouldFail ":b$1leading-digit"
it "parses a chain of items" $ do
":a+:b" `shouldMean` (b "a" [] ++ b "b" [])
"(:a+:b)" `shouldMean` (b "a" [] ++ b "b" [])
":a>.e1+.e2" `shouldMean` b "a" [e "e1", e "e2"]
":a>(.e1+.e2)" `shouldMean` b "a" [e "e1", e "e2"]
":a>(.e1)+:b" `shouldMean` (b "a" [e "e1"] ++ b "b" [])
it "parses an element+block mix" $
":b>.be:s>.se" `shouldMean`
b "b" [Left . ElementBlock "be" [] $ Params "" "s" [] [e "se"]]
it "parses an element+block mix with addons" $
":b>.e~em:s~sm" `shouldMean`
b "b"
[Left . ElementBlock "e" [Mod "em"] $
Params "" "s" [Mod "sm"] []]
it "parses a complex example" $
q exampleQuery `shouldBe` Just exampleTemplate
where
shouldFail s = q s `shouldBe` Nothing
shouldMean s bs = q s `shouldBe` Just (Template bs)
q = either (const Nothing) Just . parse BEM.template "foo"
-- shortcuts
el t n as cs = Left . Element $ Params t n as cs
tb t n as cs = [Block $ Params t n as cs]
te t n as cs = tb "" "b" [] [el t n as cs]
b c = tb "" c []
e c = el "" c [] []
-- "transform" spec
transformerSpec :: Spec
transformerSpec =
describe "Hemmet.toTree" $
it "transformes a complex example" $
toTree exampleTemplate `shouldBe` exampleNodes
-- complex examples
exampleQuery :: Text
exampleQuery =
"form:search-form$theme>\
\input.query>\
\(div.help~hidden_t)\
\+\
\span.submit-button~disabled_t:button~text_small\
\>.hint"
exampleTemplate :: Template
exampleTemplate =
Template
[ Block
$ Params "form" "search-form" [Var "theme"]
[ Left $ Element
$ Params "input" "query" []
[Left $ Element $ Params "div" "help" [Mod "hidden_t"] []]
, Left
$ ElementBlock "submit-button" [Mod "disabled_t"]
$ Params "span" "button" [Mod "text_small"]
[Left $ Element $ Params "" "hint" [] []]
]
]
exampleNodes :: Tree BemPayload
exampleNodes =
BemPayload [] []
[ node "form" ["search-form"] ["theme"]
[ node "input" ["search-form__query"] []
[ node "div" ["search-form__help", "search-form__help_hidden_t"] []
[]
]
, node "span"
[ "button"
, "button_text_small"
, "search-form__submit-button"
, "search-form__submit-button_disabled_t"
] []
[node "" ["button__hint"] [] []]
]
]
where
node n cs vs ns = Node n (BemPayload cs vs ns)