~alcinnz/haskell-stylist

ref: 5f4c8cd6b007083b5215758c05b782e8d1c2c33b haskell-stylist/test/Test.hs -rw-r--r-- 12.2 KiB
5f4c8cd6 — Adrian Cochrane Aid attribute sorting, fix install of internal modules. 5 years ago
                                                                                
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Test.Hspec
import Test.Hspec.QuickCheck
import Data.HashMap.Strict

import Data.CSS.Syntax.Tokens
import Data.CSS.Syntax.StyleSheet
import Data.CSS.Syntax.Selector

import Data.CSS.Style.Common
import Data.CSS.Style.Selector.Index
import Data.CSS.Style.Selector.Interpret
import Data.CSS.Style

main :: IO ()
main = hspec spec

spec = do
    describe "Canary" $ do
        it "Test framework works" $ do
            True `shouldBe` True
    describe "Parsing" $ do
        it "Ignores @rules" $ do
            parse emptyStyle "@encoding 'utf-8';" `shouldBe` emptyStyle
            parse emptyStyle "  @encoding 'utf-8';" `shouldBe` emptyStyle
            parse emptyStyle "@encoding 'utf-8';  " `shouldBe` emptyStyle
            parse emptyStyle "@media print { a:link {color: green;} }" `shouldBe` emptyStyle
            parse emptyStyle "  @media print { a:link {color: green;} }" `shouldBe` emptyStyle
            parse emptyStyle "@media print { a:link {color: green;} }  " `shouldBe` emptyStyle

            parse emptyStyle "@encoding 'utf-8'; a {color:green}" `shouldBe` linkStyle
            parse emptyStyle "a {color:green}@encoding 'utf-8';" `shouldBe` linkStyle
            parse emptyStyle "@media print{a{color:black;}}a {color:green}" `shouldBe` linkStyle
            parse emptyStyle "a {color:green} @media print {a{color:black;}}" `shouldBe` linkStyle
        it "Parses style rules" $ do
            -- Syntax examples from "Head First HTML & CSS with XHTML"
            parse emptyStyle "bedroom { drapes: blue; carpet: wool shag; }" `shouldBe` TrivialStyleSheet [
                StyleRule (Element [Tag "bedroom"]) [
                    ("drapes", [Ident "blue"]),
                    ("carpet", [Ident "wool", Ident "shag"])
                ]]
            parse emptyStyle "  bathroom{tile :1in white;drapes :pink}" `shouldBe` TrivialStyleSheet [
                StyleRule (Element [Tag "bathroom"]) [
                    ("tile", [Dimension "1" (NVInteger 1) "in", Ident "white"]),
                    ("drapes", [Ident "pink"])
                ]]
        it "Parses selectors" $ do
            parse emptyStyle ".class {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Class "class"]) []
                ]
            parse emptyStyle "*.class {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Class "class"]) []
                ]
            parse emptyStyle "#id {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Id "id"]) []
                ]
            parse emptyStyle "[attr] {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Property "attr" Exists]) []
                ]
            parse emptyStyle "a , b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "b"]) [],
                    StyleRule (Element [Tag "a"]) []
                ]
            parse emptyStyle "a b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Descendant (Element [Tag "a"]) [Tag "b"]) []
                ]
            parse emptyStyle "a > b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Child (Element [Tag "a"]) [Tag "b"]) []
                ]
            parse emptyStyle "a ~ b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Sibling (Element [Tag "a"]) [Tag "b"]) []
                ]
            parse emptyStyle "a + b {}" `shouldBe` TrivialStyleSheet [
                    StyleRule (Adjacent (Element [Tag "a"]) [Tag "b"]) []
                ]
    describe "Style Index" $ do
        it "Retrieves appropriate styles" $ do
            let index = addStyleRule styleIndex 0 $ styleRule' sampleRule
            let element = ElementNode {
                name = "a",
                parent = Nothing,
                previous = Nothing,
                attributes = [
                    Attribute "class" "external",
                    Attribute "href" "https://adrian.geek.nz/",
                    Attribute "id" "mysite"
                ]
            }
            let element2 = ElementNode {
                name = "b",
                parent = Just element,
                previous = Just element, -- Invalid tree, oh well.
                attributes = []
            }
            rulesForElement index element `shouldBe` [sampleRule]
            rulesForElement index element2 `shouldBe` []

            let rule = StyleRule (Element [Class "external"]) [("color", [Ident "green"])]
            let index = addStyleRule styleIndex 0 $ styleRule' rule
            rulesForElement index element `shouldBe` [rule]
            rulesForElement index element2 `shouldBe` []

            let rule = StyleRule (Element [Id "mysite"]) [("color", [Ident "green"])]
            let index = addStyleRule styleIndex 0 $ styleRule' rule
            rulesForElement index element `shouldBe` [rule]
            rulesForElement index element2 `shouldBe` []

            let rule = StyleRule (Element [Property "href" $ Prefix "https://"]) [("color", [Ident "green"])]
            let index = addStyleRule styleIndex 0 $ styleRule' rule
            rulesForElement index element `shouldBe` [rule]
            rulesForElement index element2 `shouldBe` []
    describe "Selector Compiler" $ do
        it "Correctly evaluates selectors" $ do
            let parent = ElementNode {
                name = "a",
                parent = Nothing,
                previous = Nothing,
                attributes = [
                    Attribute "class" "external secure link",
                    Attribute "href" "https://adrian.geek.nz/index.html",
                    Attribute "id" "mysite",
                    Attribute "lang" "en-US"
                ]
            }
            let sibling = ElementNode {
                name = "img",
                parent = Just parent,
                previous = Nothing,
                attributes = []
            }
            let child = ElementNode {
                name = "b",
                parent = Just parent,
                previous = Just sibling,
                attributes = []
            }

            let selector = compile (Element [Tag "a"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Class "external"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Id "mysite"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "lang" Exists])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "class" $ Include "secure"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "href" $ Prefix "https://"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "href" $ Suffix ".html"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "href" $ Substring ".geek.nz"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "lang" $ Dash "en"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "lang" $ Dash "en-US"])
            selector parent `shouldBe` True
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            let selector = compile (Element [Property "lang" $ Dash "en-UK"])
            selector parent `shouldBe` False
            selector sibling `shouldBe` False
            selector child `shouldBe` False

            -- TODO These could be tested better.
            let selector = compile $ Child (Element [Tag "a"]) [Tag "b"]
            selector parent `shouldBe` False
            selector sibling `shouldBe` False
            selector child `shouldBe` True

            let selector = compile $ Descendant (Element [Tag "a"]) [Tag "b"]
            selector parent `shouldBe` False
            selector sibling `shouldBe` False
            selector child `shouldBe` True

            let selector = compile $ Sibling (Element [Tag "img"]) [Tag "b"]
            selector parent `shouldBe` False
            selector sibling `shouldBe` False
            selector child `shouldBe` True

            let selector = compile $ Adjacent (Element [Tag "img"]) [Tag "b"]
            selector parent `shouldBe` False
            selector sibling `shouldBe` False
            selector child `shouldBe` True

    describe "Style resolution" $ do
        it "respects selector specificity" $ do
            let el = ElementNode {
                name = "a",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "link"]
            }
            let rules = parse queryable "a.link {color: green} a {color: red}"
            let TrivialPropertyParser style = cascade rules el [] temp::TrivialPropertyParser
            style ! "color" `shouldBe` [Ident "green"]
        it "respects syntax order" $ do
            let el = ElementNode {
                name = "a",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "link"]
            }
            let rules = parse queryable "a {color: red; color: green}"
            let TrivialPropertyParser style = cascade rules el [] temp::TrivialPropertyParser
            style ! "color" `shouldBe` [Ident "green"]

            let rules = parse queryable "a {color: red} a {color: green}"
            let TrivialPropertyParser style = cascade rules el [] temp::TrivialPropertyParser
            style ! "color" `shouldBe` [Ident "green"]
        it "respects stylesheet precedence" $ do
            let el = ElementNode {
                name = "a",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "link"]
            }
            let rules = parse (queryable {priority = 1}) "a {color: green}"
            let rules2 = parse (rules {priority = 2}) "a {color: red}" :: QueryableStyleSheet TrivialPropertyParser
            let TrivialPropertyParser style = cascade rules2 el [] temp::TrivialPropertyParser
            style ! "color" `shouldBe` [Ident "green"]

            let el = ElementNode {
                name = "a",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "link"]
            }
            let rules = parse (queryable {priority = 1}) "a {color: red}"
            let rules2 = parse (rules {priority = 2}) "a {color: green !important}" :: QueryableStyleSheet TrivialPropertyParser
            let TrivialPropertyParser style = cascade rules2 el [] temp::TrivialPropertyParser
            style ! "color" `shouldBe` [Ident "green"]
        it "respects overrides" $ do
            let el = ElementNode {
                name = "a",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "link"]
            }
            let rules = parse queryable "a {color: red;}"
            let TrivialPropertyParser style = cascade rules el [("color", [Ident "green"])] temp::TrivialPropertyParser
            style ! "color" `shouldBe` [Ident "green"]

styleIndex :: StyleIndex
styleIndex = new
queryable :: QueryableStyleSheet TrivialPropertyParser
queryable = queryableStyleSheet
emptyStyle = TrivialStyleSheet []
linkStyle = TrivialStyleSheet [sampleRule]
sampleRule = StyleRule (Element [Tag "a"]) [("color", [Ident "green"])]