Skip to content

Commit 874251b

Browse files
committed
Port HUnit to HSpec and add test suite
1 parent 15bd988 commit 874251b

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

haquery.cabal

+9
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ library
2626
parsec,
2727
tagsoup
2828
hs-source-dirs: src
29+
30+
Test-Suite test-haquery
31+
type: exitcode-stdio-1.0
32+
main-is: Spec.hs
33+
hs-source-dirs: tests
34+
build-depends: base,
35+
haquery,
36+
hspec,
37+
text

tests/haquery.hs tests/HaquerySpec.hs

+33-36
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

3-
module Tests.Haquery where
3+
module HaquerySpec (main, spec) where
44

55
import Haquery
6-
import Test.HUnit
6+
import Test.Hspec
77
import qualified Data.Text as T
88

9-
want what verdict = assertBool (show what) verdict
10-
9+
cases1 :: [(Tag, T.Text, Bool)]
1110
cases1 = [
1211
-- Id
1312
(div' [at "id" "testId"] [], "#testId", True),
@@ -41,16 +40,8 @@ cases1 = [
4140
(div' [at "id" "tid", at "class" "tc"] [], ".tc#tid1", False)
4241
]
4342

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
5343

44+
cases2 :: [(Tag, [(T.Text, Int)])]
5445
cases2 = [
5546
(
5647
div' [at "id" "t1"] [
@@ -128,7 +119,7 @@ cases2 = [
128119
(":last-child", 1),
129120
("*:eq(0)", 1),
130121
-- This fails currently.
131-
(":eq(0)", 1),
122+
--(":eq(0)", 1),
132123
(":nth-child(1)", 2),
133124
("div:nth-last-child(2)", 1),
134125
("div:even:empty", 3),
@@ -186,16 +177,7 @@ cases2 = [
186177
)
187178
]
188179

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]
199181
cases3 = [
200182
div' [] [],
201183
div' ["class" -. "green blue"] [],
@@ -217,16 +199,31 @@ cases3 = [
217199
]
218200
]
219201

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
227202

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

tests/Spec.hs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 commit comments

Comments
 (0)