From d321f69d93a91eac4994fb6080c270f03db38a01 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Sat, 15 Jun 2019 18:30:49 +1200 Subject: [PATCH] Unit test the style index. --- src/Stylish/Style/Index.hs | 6 +++--- test/Test.hs | 42 +++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Stylish/Style/Index.hs b/src/Stylish/Style/Index.hs index 8da202b..b452cb8 100644 --- a/src/Stylish/Style/Index.hs +++ b/src/Stylish/Style/Index.hs @@ -1,11 +1,12 @@ {-# LANGUAGE OverloadedStrings #-} module Stylish.Style.Index ( - StyleIndex(..), + StyleIndex(..), styleIndex, rulesForElement ) where -- TODO do performance tests to decide beside between strict/lazy. import Data.HashMap.Strict +import Data.List (nub) import Stylish.Parse import Stylish.Element @@ -48,11 +49,10 @@ selectorKey (Property prop _ : _) = Property prop Exists ---- rulesForElement :: StyleIndex -> Element -> [StyleRule] -rulesForElement self element = Prelude.foldr (++) [] rules +rulesForElement self element = nub $ Prelude.foldr (++) [] rules where get key = lookup' key index index = indexed self - rules :: [[StyleRule]] rules = unindexed self : Prelude.map get (testsForElement element) testsForElement :: Element -> [SimpleSelector] diff --git a/test/Test.hs b/test/Test.hs index 4e66ac7..788878a 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -7,6 +7,7 @@ import Data.CSS.Syntax.Tokens import Stylish.Parse import Stylish.Style.Index +import Stylish.Element main = hspec spec @@ -68,8 +69,43 @@ spec = do parse emptyStyle "a + b {}" `shouldBe` TrivialStyleSheet [ StyleRule (Adjacent (Element [Tag "a"]) [Tag "b"]) [] ] + describe "Style Index" $ do + it "Retrieves appropriate styles" $ do + let index = addRule styleIndex 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 = addRule styleIndex rule + rulesForElement index element `shouldBe` [rule] + rulesForElement index element2 `shouldBe` [] + + let rule = StyleRule (Element [Id "mysite"]) [("color", [Ident "green"])] + let index = addRule styleIndex rule + rulesForElement index element `shouldBe` [rule] + rulesForElement index element2 `shouldBe` [] + + let rule = StyleRule (Element [Property "href" $ Prefix "https://"]) [("color", [Ident "green"])] + let index = addRule styleIndex rule + rulesForElement index element `shouldBe` [rule] + rulesForElement index element2 `shouldBe` [] emptyStyle = TrivialStyleSheet [] -linkStyle = TrivialStyleSheet [ - StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] - ] +linkStyle = TrivialStyleSheet [sampleRule] +sampleRule = StyleRule (Element [Tag "a"]) [("color", [Ident "green"])] -- 2.30.2