-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.hs
117 lines (96 loc) · 4.44 KB
/
Test.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
{-# LANGUAGE OverloadedStrings #-}
import Test.Hspec
import Combinators
import Grammar (html)
import DOM (DOMTree(..), findById, findByTag, findByClass, findByAttribute, addChild)
import Data.Map (fromList)
main :: IO ()
main = hspec $ do
-- 🔍 1️⃣ **Lexer Tests**
-- describe "Lexer Tests" $ do
-- it "Tokenizes a simple HTML string" $ do
-- let result = runParser tagParser "<html>"
-- result `shouldBe` Right (Tag "html", "")
-- it "Tokenizes a closing tag" $ do
-- let result = runParser tagParser "</html>"
-- result `shouldBe` Right (ClosingTag "html", "")
-- it "Tokenizes a tag with hyphens" $ do
-- let result = runParser tagParser "<custom-tag>"
-- result `shouldBe` Right (Tag "custom-tag", "")
-- it "Tokenizes a closing tag with hyphens" $ do
-- let result = runParser tagParser "</custom-tag>"
-- result `shouldBe` Right (ClosingTag "custom-tag", "")
-- 🌲 2️⃣ **DOM Parser Tests**
describe "DOM Parser Tests" $ do
it "Parses a simple DOM Tree" $ do
let input = "<html><body><h1>Welcome</h1></body></html>"
let expected = Right
( HTMLElement "html" (fromList [])
[ HTMLElement "body" (fromList [])
[ HTMLElement "h1" (fromList [])
[ TextNode "Welcome" ]
]
]
, ""
)
runParser html input `shouldBe` expected
it "Parses nested tags properly" $ do
let input = "<div><p>Hello <strong>World</strong></p></div>"
let expected = Right
( HTMLElement "div" (fromList [])
[ HTMLElement "p" (fromList [])
[ TextNode "Hello "
, HTMLElement "strong" (fromList []) [TextNode "World"]
]
]
, ""
)
runParser html input `shouldBe` expected
it "Fails with unmatched tags" $ do
let input = "<html><body><div></body></html>"
let result = runParser html input
result `shouldBe` Left [TagsNotMatched]
-- 🔍 3️⃣ **DOM Operations Tests**
describe "DOM Operations Tests" $ do
let domTree =
HTMLElement "html" (fromList [])
[ HTMLElement "body" (fromList [])
[ HTMLElement "div" (fromList [("id", "main"), ("class", "container")])
[ TextNode "Content" ]
, HTMLElement "p" (fromList [("class", "text-muted")])
[ TextNode "A paragraph" ]
]
]
it "Finds an element by ID" $ do
let result = findById "main" domTree
result `shouldBe` Just (HTMLElement "div" (fromList [("id", "main"), ("class", "container")]) [TextNode "Content"])
it "Returns Nothing if ID is not found" $ do
let result = findById "nonexistent" domTree
result `shouldBe` Nothing
it "Finds elements by class name" $ do
let result = findByClass "container" domTree
result `shouldBe` [HTMLElement "div" (fromList [("id", "main"), ("class", "container")]) [TextNode "Content"]]
it "Finds elements by multiple class names" $ do
let result = findByClass "text-muted" domTree
result `shouldBe` [HTMLElement "p" (fromList [("class", "text-muted")]) [TextNode "A paragraph"]]
it "Finds elements by tag name" $ do
let result = findByTag "div" domTree
result `shouldBe` [HTMLElement "div" (fromList [("id", "main"), ("class", "container")]) [TextNode "Content"]]
it "Finds elements by attributes" $ do
let result = findByAttribute "id" "main" domTree
result `shouldBe` [HTMLElement "div" (fromList [("id", "main"), ("class", "container")]) [TextNode "Content"]]
it "Finds multiple elements by attributes" $ do
let result = findByAttribute "class" "text-muted" domTree
result `shouldBe` [HTMLElement "p" (fromList [("class", "text-muted")]) [TextNode "A paragraph"]]
it "Adds a child to a DOM tree" $ do
let newChild = HTMLElement "footer" (fromList []) [TextNode "Footer content"]
let updatedTree = addChild domTree newChild
updatedTree `shouldBe` HTMLElement "html" (fromList [])
[ HTMLElement "body" (fromList [])
[ HTMLElement "div" (fromList [("id", "main"), ("class", "container")])
[ TextNode "Content" ]
, HTMLElement "p" (fromList [("class", "text-muted")])
[ TextNode "A paragraph" ]
]
, HTMLElement "footer" (fromList []) [TextNode "Footer content"]
]