~alcinnz/haskell-stylist

ref: de0ec27e2e0f928021bc5f58860d276c4bda38e2 haskell-stylist/test/Test.hs -rw-r--r-- 28.1 KiB
de0ec27e — Adrian Cochrane Merge branch 'main' of git.argonaut-constellation.org:~alcinnz/haskell-stylist into main 1 year, 8 months ago
                                                                                
ceb3b528 Adrian Cochrane
3bd6ae52 Adrian Cochrane
d27996eb Adrian Cochrane
349b947a Adrian Cochrane
c66e9303 Adrian Cochrane
04976d4d Adrian Cochrane
ceb3b528 Adrian Cochrane
3bd6ae52 Adrian Cochrane
04976d4d Adrian Cochrane
af343c17 Adrian Cochrane
d27996eb Adrian Cochrane
af343c17 Adrian Cochrane
2ba0a92a Adrian Cochrane
df951629 Adrian Cochrane
10a6e634 Adrian Cochrane
04976d4d Adrian Cochrane
2ba0a92a Adrian Cochrane
10a6e634 Adrian Cochrane
ee89bcfd Adrian Cochrane
ceb3b528 Adrian Cochrane
6d2fa464 Adrian Cochrane
ceb3b528 Adrian Cochrane
c66e9303 Adrian Cochrane
ceb3b528 Adrian Cochrane
df951629 Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
df951629 Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
5ff74dcf Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
6344dc8e Adrian Cochrane
ceb3b528 Adrian Cochrane
2eff549d Adrian Cochrane
6344dc8e Adrian Cochrane
2eff549d Adrian Cochrane
6344dc8e Adrian Cochrane
2eff549d Adrian Cochrane
d321f69d Adrian Cochrane
0b37dedf Adrian Cochrane
d321f69d Adrian Cochrane
5ff74dcf Adrian Cochrane
d321f69d Adrian Cochrane
edfaf74d Adrian Cochrane
d321f69d Adrian Cochrane
5ff74dcf Adrian Cochrane
d321f69d Adrian Cochrane
6344dc8e Adrian Cochrane
0b37dedf Adrian Cochrane
6d2fa464 Adrian Cochrane
d321f69d Adrian Cochrane
6344dc8e Adrian Cochrane
0b37dedf Adrian Cochrane
6d2fa464 Adrian Cochrane
d321f69d Adrian Cochrane
5ff74dcf Adrian Cochrane
0b37dedf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
062bdd98 Adrian Cochrane
edfaf74d Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
5ff74dcf Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
062bdd98 Adrian Cochrane
6d2fa464 Adrian Cochrane
ceb3b528 Adrian Cochrane
3bd6ae52 Adrian Cochrane
5ff74dcf Adrian Cochrane
3bd6ae52 Adrian Cochrane
edfaf74d Adrian Cochrane
3bd6ae52 Adrian Cochrane
349b947a Adrian Cochrane
3bd6ae52 Adrian Cochrane
a35a8a23 Adrian Cochrane
5ff74dcf Adrian Cochrane
a35a8a23 Adrian Cochrane
edfaf74d Adrian Cochrane
a35a8a23 Adrian Cochrane
349b947a Adrian Cochrane
a35a8a23 Adrian Cochrane
6d2fa464 Adrian Cochrane
349b947a Adrian Cochrane
6d2fa464 Adrian Cochrane
a35a8a23 Adrian Cochrane
5ff74dcf Adrian Cochrane
a35a8a23 Adrian Cochrane
edfaf74d Adrian Cochrane
a35a8a23 Adrian Cochrane
0b37dedf Adrian Cochrane
349b947a Adrian Cochrane
a35a8a23 Adrian Cochrane
6d2fa464 Adrian Cochrane
a35a8a23 Adrian Cochrane
5ff74dcf Adrian Cochrane
a35a8a23 Adrian Cochrane
edfaf74d Adrian Cochrane
a35a8a23 Adrian Cochrane
0b37dedf Adrian Cochrane
349b947a Adrian Cochrane
6d2fa464 Adrian Cochrane
7083855b Adrian Cochrane
5ff74dcf Adrian Cochrane
7083855b Adrian Cochrane
edfaf74d Adrian Cochrane
7083855b Adrian Cochrane
349b947a Adrian Cochrane
7083855b Adrian Cochrane
62b97a58 Adrian Cochrane
2eff549d Adrian Cochrane
62b97a58 Adrian Cochrane
6344dc8e Adrian Cochrane
be4bb6f0 Adrian Cochrane
6344dc8e Adrian Cochrane
be4bb6f0 Adrian Cochrane
6344dc8e Adrian Cochrane
62b97a58 Adrian Cochrane
be4bb6f0 Adrian Cochrane
3bd6ae52 Adrian Cochrane
349b947a Adrian Cochrane
5ff74dcf Adrian Cochrane
349b947a Adrian Cochrane
5ff74dcf Adrian Cochrane
349b947a Adrian Cochrane
5ff74dcf Adrian Cochrane
349b947a Adrian Cochrane
5ff74dcf Adrian Cochrane
349b947a Adrian Cochrane
c66e9303 Adrian Cochrane
04976d4d Adrian Cochrane
c66e9303 Adrian Cochrane
04976d4d Adrian Cochrane
349b947a Adrian Cochrane
2ba0a92a Adrian Cochrane
a862da93 Adrian Cochrane
d27996eb Adrian Cochrane
a862da93 Adrian Cochrane
ee89bcfd Adrian Cochrane
349b947a Adrian Cochrane
3bd6ae52 Adrian Cochrane
6d2fa464 Adrian Cochrane
ceb3b528 Adrian Cochrane
c66e9303 Adrian Cochrane
6d2fa464 Adrian Cochrane
d321f69d Adrian Cochrane
6d2fa464 Adrian Cochrane
6344dc8e Adrian Cochrane
04976d4d Adrian Cochrane
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Test.Hspec
import Data.HashMap.Strict
import qualified Data.HashMap.Lazy as L
import Data.Maybe (fromJust)
import Network.URI
import Data.Scientific (toRealFloat)

import Data.CSS.Syntax.Tokens
import Data.CSS.Syntax.StyleSheet (parse, StyleSheet(..), TrivialStyleSheet(..), scanAtRule, scanValue)
import Data.CSS.Syntax.Selector

import Data.CSS.Syntax.AtLayer

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

import Data.CSS.Preprocessor.Conditions
import Data.CSS.Preprocessor.Conditions.Expr (Datum(..), Op(..), parse', eval)
import qualified Data.CSS.Preprocessor.Text as Txt

main :: IO ()
main = hspec spec

spec :: Spec
spec = do
    describe "Canary" $ do
        it "Test framework works" $ do
            True `shouldBe` True
    describe "Parsing" $ do
        it "Can scan @rules" $ do
            scanAtRule [Ident "utf-8", Semicolon, Ident "a"] `shouldBe` ([Ident "utf-8", Semicolon], [Ident "a"])
            scanAtRule [Ident "before", LeftCurlyBracket, Ident "inside", RightCurlyBracket, Ident "after"] `shouldBe` (
                [Ident "before", LeftCurlyBracket, Ident "inside", RightCurlyBracket],
                [Ident "after"])
        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 Nothing "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"]) [] ""
                ]
            parse emptyStyle "a::before {}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "a"]) [] "before"
                ]
            parse emptyStyle "a:before {}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "a", Psuedoclass "before" []]) [] ""
                ]
    describe "Style Index" $ do
        it "Retrieves appropriate styles" $ do
            let index = addStyleRule styleIndex [0] $ styleRule' sampleRule
            let element = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [
                    Attribute "class" "" "external",
                    Attribute "href" "" "https://adrian.geek.nz/",
                    Attribute "id" "" "mysite"
                ]
            }
            let element2 = ElementNode {
                name = "b",
                namespace = "",
                parent = Just element,
                previous = Just element, -- Invalid tree, oh well.
                attributes = []
            }
            rulesForElement index element `shouldBe` [sampleRule]
            rulesForElement index element2 `shouldBe` []

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

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

            let rule3 = StyleRule (Element [Property Nothing "href" $ Prefix "https://"]) [("color", [Ident "green"])] ""
            let index3 = addStyleRule styleIndex [0] $ styleRule' rule3
            rulesForElement index3 element `shouldBe` [rule3]
            rulesForElement index3 element2 `shouldBe` []
    describe "Selector Compiler" $ do
        it "Correctly evaluates selectors" $ do
            let parentEl = ElementNode {
                name = "a",
                namespace = "",
                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",
                namespace = "",
                parent = Just parentEl,
                previous = Nothing,
                attributes = []
            }
            let child = ElementNode {
                name = "b",
                namespace = "",
                parent = Just parentEl,
                previous = Just sibling,
                attributes = []
            }

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

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

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

            let selector4 = compile (Element [Property Nothing "lang" Exists])
            selector4 parentEl `shouldBe` True
            selector4 sibling `shouldBe` False
            selector4 child `shouldBe` False

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

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

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

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

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

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

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

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

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

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

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

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

            let rules2 = parse queryable "a {color: red} a {color: green}"
            let VarParser _ (TrivialPropertyParser style2) = cascade rules2 el [] temp::(VarParser TrivialPropertyParser)
            style2 ! "color" `shouldBe` [Ident "green"]
        it "respects stylesheet precedence" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                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 (VarParser TrivialPropertyParser)
            let VarParser _ (TrivialPropertyParser style) = cascade rules2 el [] temp::(VarParser TrivialPropertyParser)
            style ! "color" `shouldBe` [Ident "green"]

            let el' = ElementNode {
                name = "a",
                namespace = "",
                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 (VarParser TrivialPropertyParser)
            let VarParser _ (TrivialPropertyParser style') = cascade rules2' el' [] temp::(VarParser TrivialPropertyParser)
            style' ! "color" `shouldBe` [Ident "green"]
        it "respects overrides" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = [Attribute "class" "" "link"]
            }
            let rules = parse queryable "a {color: red;}"
            let VarParser _ (TrivialPropertyParser style) = cascade rules el [("color", [Ident "green"])] temp::(VarParser TrivialPropertyParser)
            style ! "color" `shouldBe` [Ident "green"]
    describe "Parser freezes" $ do
        it "does not regress" $ do
            parse emptyStyle "output: {content: 'Output'; pitch: high}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "output"]) [] ""
                ] -- Turned out to just be incorrect parsing
            parse emptyStyle "input, output {content: attr(value)}"
                `shouldBe` TrivialStyleSheet [
                    StyleRule (Element [Tag "output"]) [
                        ("content", [Function "attr", Ident "value", RightParen])
                    ] "",
                    StyleRule (Element [Tag "input"]) [
                        ("content", [Function "attr", Ident "value", RightParen])
                    ] ""
                ]
        it "paren balancing" $ do
            scanValue [RightParen] `shouldBe` ([], [RightParen])
            scanValue [LeftParen] `shouldBe` ([LeftParen], [])
            scanValue [Function "fn", LeftParen] `shouldBe` ([Function "fn", LeftParen], [])
            scanValue [Function "fn", Ident "arg", LeftParen] `shouldBe`
                ([Function "fn", Ident "arg", LeftParen], [])

    describe "CSS Variables" $ do
        it "are captured" $ do
            let parser = temp :: VarParser TrivialPropertyParser
            vars parser `shouldBe` []
            let parser1 = setVars [("--var", [Ident "value"])] parser
            vars parser1 `shouldBe` [("--var", [Ident "value"])]
            let parser2 = fromJust $ longhand parser parser1 "property" [Function "var", Ident "--var", RightParen]
            vars parser2 `shouldBe` [("--var", [Ident "value"])]
            let VarParser _ (TrivialPropertyParser style) = parser2
            style ! "property" `shouldBe` [Ident "value"]

            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = []
            }
            let rules = parse queryable "a {--var: value}"
            let VarParser v _ = cascade rules el [] temp
            v `shouldBe` [("--var", [Ident "value"])]
        it "applies within element" $ do
            let el = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = []
            }
            let rules = parse queryable "a {--link: #f00; color: var(--link)}"
            let VarParser vars (TrivialPropertyParser style) = cascade rules el [] temp
            style ! "color" `shouldBe` [Hash HId "f00"]
            style ! "--link" `shouldBe` [Hash HId "f00"]
            vars `shouldBe` [("--link", [Hash HId "f00"])]
        it "inherits" $ do
            let parent = ElementNode {
                name = "a",
                namespace = "",
                parent = Nothing,
                previous = Nothing,
                attributes = []
            }
            let el = ElementNode {
                name = "b",
                namespace = "",
                parent = Just parent,
                previous = Nothing,
                attributes = []
            }
            let rules = parse queryable "a {--link: #f00} b {color: var(--link)}"
            let VarParser vars (TrivialPropertyParser style) = cascade rules el [] $ cascade rules parent [] temp
            vars `shouldBe` [("--link", [Hash HId "f00"])]
            style ! "color" `shouldBe` [Hash HId "f00"]
    describe "Conditional @rules" $ do
        it "handles normal rules" $ do
            let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "@rule; a {color: green}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green} @rule;"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "a {color: green} @font {}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "@font {} a {color: green}"
            styles `shouldBe` [StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
        it "handles @document" $ do
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url(about:blank) { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url(about:credits) { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url-prefix('about:') { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document url-prefix('https:') { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document media-document('test') { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@document media-document('other') { a {color: red} }"
            styles `shouldBe` []
        it "handles @media" $ do
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen { a {color: red} }"
            styles `shouldBe` []

            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test or screen { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test or test {a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen or screen {a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen or test {a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]

            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test and screen { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media test and test { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen and screen { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media screen and test {a {color: red} }"
            styles `shouldBe` []

            let TrivialStyleSheet styles = resolve' $ parse conditional "@media 2 < 3 { a {color: green} }"
            styles `shouldBe` [StyleRule (Element []) [] "", StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""]
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media 2 < 2 { a {color: red} }"
            styles `shouldBe` []
            let TrivialStyleSheet styles = resolve' $ parse conditional "@media 2 < 1 { a {color: red} }"
            styles `shouldBe` []
        it "handles @import" $ do
            let styles = parse conditional "@import url(about:style.css);"
            extractImports' styles `shouldBe` [URI "about:" Nothing "style.css" "" ""]
            let styles = parse conditional "@import 'about:style.css';"
            extractImports' styles `shouldBe` [URI "about:" Nothing "style.css" "" ""]

            let styles = parse conditional "@import url(about:style.css) test;"
            extractImports' styles `shouldBe` [URI "about:" Nothing "style.css" "" ""]
            let styles = parse conditional "@import url(about:style.css) screen;"
            extractImports' styles `shouldBe` []
        -- TODO @supports is harder to test

    describe "CSS Counters" $ do
        it "Propagates other properties" $ do
            let textStyle = temp :: Txt.TextStyle TrivialPropertyParser
            let textStyle1 = fromJust $ longhand temp textStyle "foo" [Ident "bar"]
            style (Txt.resolve $ StyleTree textStyle1 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle2 = fromJust $ longhand temp textStyle1 "counter-reset" [Ident "heading"]
            style (Txt.resolve $ StyleTree textStyle2 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle3 = fromJust $ longhand temp textStyle2 "counter-set" [Ident "heading"]
            style (Txt.resolve $ StyleTree textStyle3 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle4 = fromJust $ longhand temp textStyle3 "counter-increment" [Ident "heading"]
            style (Txt.resolve $ StyleTree textStyle4 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"])])

            let textStyle5 = fromJust $ longhand temp textStyle4 "white-space" [Ident "normal"]
            style (Txt.resolve $ StyleTree textStyle5 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "normal"])])

            let textStyle6 = fromJust $ longhand temp textStyle4 "white-space" [Ident "pre"]
            style (Txt.resolve $ StyleTree textStyle6 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "nowrap"])])

            let textStyle7 = fromJust $ longhand temp textStyle4 "white-space" [Ident "nowrap"]
            style (Txt.resolve $ StyleTree textStyle7 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "nowrap"])])

            let textStyle8 = fromJust $ longhand temp textStyle4 "white-space" [Ident "pre-wrap"]
            style (Txt.resolve $ StyleTree textStyle8 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "normal"])])

            let textStyle9 = fromJust $ longhand temp textStyle4 "white-space" [Ident "pre-line"]
            style (Txt.resolve $ StyleTree textStyle9 []) `shouldBe`
                TrivialPropertyParser (fromList [("foo", [Ident "bar"]), ("white-space", [Ident "normal"])])

        it "Inserts counters" $ do
            let textStyle = temp :: Txt.TextStyle TrivialPropertyParser
            shorthand textStyle "content" [Function "counter", Ident "-rhaps-ol", RightParen] `shouldBe`
                [("content", [Function "counter", Ident "-rhaps-ol", RightParen])]
            shorthand textStyle "content" [Function "counters", Ident "-rhaps-ol", Comma, String ".", RightParen] `shouldBe`
                [("content", [Function "counters", Ident "-rhaps-ol", Comma, String ".", RightParen])]

            let textStyle1 = fromJust $ longhand temp textStyle "content" [Function "counter", Ident "-rhaps-ol", RightParen]
            style (Txt.resolve $ StyleTree textStyle1 []) `shouldBe` TrivialPropertyParser (fromList [("content", [])])

            let textStyle2 = fromJust $ longhand temp textStyle1 "counter-reset" [Ident "-rhaps-ol"]
            style (Txt.resolve $ StyleTree textStyle2 []) `shouldBe` TrivialPropertyParser (fromList [("content", [String "0"])])

            let textStyle3 = fromJust $ longhand temp textStyle1 "counter-set" [Ident "-rhaps-ol"]
            style (Txt.resolve $ StyleTree textStyle3 []) `shouldBe` TrivialPropertyParser (fromList [("content", [String "0"])])

            let textStyle4 = fromJust $ longhand temp textStyle1 "counter-increment" [Ident "-rhaps-ol"]
            style (Txt.resolve $ StyleTree textStyle4 []) `shouldBe` TrivialPropertyParser (fromList [("content", [String "1"])])
    describe "@layer" $ do
        it "Deduplicates names" $ do
            let init = Tree L.empty
            let tree2 = registerLayer ["LeagueOfGentlemenAdventurers", "The Stranger"] init
            let tree3 = registerLayer ["JusticeUnion", "TomTomorrow"] tree2
            let tree4 = registerLayer ["HomeTeam", "DocRocket"] tree3
            let tree5 = registerLayer ["JusticeUnion", "TheOgre"] tree4

            layerPath ["JusticeUnion", "TheOgre"] tree5 `shouldBe` [2, 2]
            layerPath ["HomeTeam"] tree5 `shouldBe` [3]
            layerPath ["LeagueOfGentlemenAdventurers"] tree5 `shouldBe` [1]
            uniqueName ["HomeTeam"] tree5 `shouldBe` ["HomeTeam", "1"]

styleIndex :: StyleIndex
styleIndex = new
queryable :: QueryableStyleSheet (VarParser TrivialPropertyParser)
queryable = queryableStyleSheet
emptyStyle :: TrivialStyleSheet
emptyStyle = TrivialStyleSheet []
conditional :: ConditionalStyles TrivialPropertyParser
conditional = conditionalStyles (fromJust $ parseURI "about:blank") "test"
linkStyle :: TrivialStyleSheet
linkStyle = TrivialStyleSheet [sampleRule]
sampleRule :: StyleRule
sampleRule = StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] ""
resolve' = resolve (\var -> B (var == "test")) evalToken emptyStyle
    where
        evalToken (Number _ (NVInteger x)) = N $ fromInteger x
        evalToken (Number _ (NVNumber x)) = N $ toRealFloat x
        evalToken _ = B False
extractImports' = extractImports (\var -> B (var == "test")) evalToken
    where
        evalToken (Number _ (NVInteger x)) = N $ fromInteger x
        evalToken (Number _ (NVNumber x)) = N $ toRealFloat x
        evalToken _ = B False