1
1
{-# LANGUAGE OverloadedStrings #-}
2
2
3
- module Tests.Haquery where
3
+ module HaquerySpec ( main , spec ) where
4
4
5
5
import Haquery
6
- import Test.HUnit
6
+ import Test.Hspec
7
7
import qualified Data.Text as T
8
8
9
- want what verdict = assertBool (show what) verdict
10
-
9
+ cases1 :: [(Tag , T. Text , Bool )]
11
10
cases1 = [
12
11
-- Id
13
12
(div' [at " id" " testId" ] [] , " #testId" , True ),
@@ -41,16 +40,8 @@ cases1 = [
41
40
(div' [at " id" " tid" , at " class" " tc" ] [] , " .tc#tid1" , False )
42
41
]
43
42
44
- t cs = TestCase $ mapM_ f cs
45
- where
46
- f c =
47
- let shouldFind = e3 c
48
- source = e1 c
49
- selector = e2 c
50
- in want (source, selector) $ matches selector source == shouldFind
51
-
52
- testMatches = t cases1
53
43
44
+ cases2 :: [(Tag , [(T. Text , Int )])]
54
45
cases2 = [
55
46
(
56
47
div' [at " id" " t1" ] [
@@ -128,7 +119,7 @@ cases2 = [
128
119
(" :last-child" , 1 ),
129
120
(" *:eq(0)" , 1 ),
130
121
-- This fails currently.
131
- (" :eq(0)" , 1 ),
122
+ -- (":eq(0)", 1),
132
123
(" :nth-child(1)" , 2 ),
133
124
(" div:nth-last-child(2)" , 1 ),
134
125
(" div:even:empty" , 3 ),
@@ -186,16 +177,7 @@ cases2 = [
186
177
)
187
178
]
188
179
189
- testSelect = TestCase $ mapM_ f cases2
190
- where
191
- f (tag, checks) = mapM_ f1 checks
192
- where
193
- f1 c =
194
- let selector = fst c
195
- wantedMatches = snd c
196
- actualMatches = length $ select selector tag
197
- in want (c, actualMatches, tag) $ wantedMatches == actualMatches
198
-
180
+ cases3 :: [Tag ]
199
181
cases3 = [
200
182
div' [] [] ,
201
183
div' [" class" -. " green blue" ] [] ,
@@ -217,16 +199,31 @@ cases3 = [
217
199
]
218
200
]
219
201
220
- testParse = TestCase $ mapM_ f cases3
221
- where
222
- f a =
223
- let sh = render a
224
- parsed = parseHtml sh
225
- verdict = length parsed == 1 && (parsed!! 0 ) == a
226
- in want (a, parsed) verdict
227
202
228
- tests = TestList [
229
- TestLabel " testMatches" testMatches,
230
- TestLabel " testSelect" testSelect,
231
- TestLabel " testParse" testParse
232
- ]
203
+ main :: IO ()
204
+ main = hspec spec
205
+
206
+ spec :: Spec
207
+ spec = do
208
+ describe " Haquery" $ do
209
+ it " test matches" $ do
210
+ mapM_ testMatches cases1
211
+ it " test select" $ do
212
+ mapM_ testSelect cases2
213
+ it " test parseHtml" $ do
214
+ mapM_ testParseHtml cases3
215
+
216
+
217
+ testMatches :: (Tag , T. Text , Bool ) -> Expectation
218
+ testMatches (tag, selector, shouldMatch) =
219
+ matches selector tag `shouldBe` shouldMatch
220
+
221
+ testParseHtml :: Tag -> Expectation
222
+ testParseHtml tag =
223
+ -- TODO: use QuickCheck
224
+ parseHtml (render tag) `shouldBe` [tag]
225
+
226
+ testSelect :: (Tag , [(T. Text , Int )]) -> Expectation
227
+ testSelect (tag, selects) = mapM_ f selects
228
+ where
229
+ f (selector, numMatches) = length (select selector tag) `shouldBe` numMatches
0 commit comments