purescript-cheerio/test/Test/Cheerio.purs

173 lines
3.4 KiB
Haskell
Raw Normal View History

2018-01-16 04:35:48 +00:00
module Test.Cheerio where
import Prelude hiding (eq)
2018-07-28 07:40:04 +00:00
import Effect (Effect)
2018-01-16 04:35:48 +00:00
import Data.Maybe (Maybe(..))
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
import Test.Unit.Main (runTest)
import Cheerio
( Cheerio
, attr
2018-01-16 04:35:48 +00:00
, children
, eq
, find
, first
, hasClass
, html
, last
, length
, next
, parent
, prev
, siblings
, text
, toArray
2018-01-16 04:35:48 +00:00
)
import Cheerio.Static (loadRoot)
import Test.HtmlEx (htmlEx)
2018-07-28 07:40:04 +00:00
main :: Effect Unit
2018-01-16 04:35:48 +00:00
main = runTest suites
emptyCheerio :: Cheerio
emptyCheerio = loadRoot htmlEx # find ".no-such-element"
2018-07-28 07:40:04 +00:00
suites :: TestSuite
2018-01-16 04:35:48 +00:00
suites = do
suite "Attributes" do
test "attr" do
Assert.equal
(Just "fruits")
2018-01-16 04:35:48 +00:00
(loadRoot htmlEx # find "ul" # attr "id")
Assert.equal
Nothing
(loadRoot htmlEx # find "ul" # attr "no-such-attribute")
Assert.equal
Nothing
(emptyCheerio # attr "id")
2018-01-16 04:35:48 +00:00
test "hasClass" do
Assert.equal
true
(loadRoot htmlEx # find ".pear" # hasClass "pear")
Assert.equal
false
(loadRoot htmlEx # find "apple" # hasClass "fruit")
Assert.equal
true
(loadRoot htmlEx # find "li" # hasClass "pear")
Assert.equal
false
(emptyCheerio # hasClass "pear")
2018-01-16 04:35:48 +00:00
suite "Traversing" do
test "find" do
Assert.equal
3
(loadRoot htmlEx # find "#fruits" # find "li" # length)
test "parent" do
Assert.equal
(Just "fruits")
2018-01-16 04:35:48 +00:00
(loadRoot htmlEx # find ".pear" # parent # attr "id")
test "next" do
Assert.equal
true
(loadRoot htmlEx # find ".apple" # next # hasClass "orange")
test "prev" do
Assert.equal
true
(loadRoot htmlEx # find ".orange" # prev # hasClass "apple")
test "siblings" do
Assert.equal
2
(loadRoot htmlEx # find ".pear" # siblings # length)
test "children" do
Assert.equal
3
(loadRoot htmlEx # find "#fruits" # children # length)
test "first" do
Assert.equal
"Apple"
(loadRoot htmlEx # find "#fruits" # children # first # text)
test "last" do
Assert.equal
"Pear"
(loadRoot htmlEx # find "#fruits" # children # last # text)
test "eq" do
Assert.equal
"Apple"
(loadRoot htmlEx # find "li" # eq 0 # text)
Assert.equal
"Pear"
(loadRoot htmlEx # find "li" # eq (-1) # text)
suite "Rendering" do
test "html" do
Assert.equal
(Just "Apple")
(loadRoot htmlEx # find ".apple" # html)
Assert.equal
Nothing
(emptyCheerio # html)
2018-01-16 04:35:48 +00:00
test "text" do
Assert.equal
"Apple"
(loadRoot htmlEx # find ".apple" # text)
Assert.equal
""
(emptyCheerio # text)
suite "Miscellaneous" do
test "length" do
Assert.equal
0
(emptyCheerio # length)
Assert.equal
3
(loadRoot htmlEx # find "li" # length)
test "toArray" do
Assert.equal
[]
(emptyCheerio # toArray # map (attr "class"))
Assert.equal
(map Just ["apple", "orange", "pear"])
(loadRoot htmlEx # find "li" # toArray # map (attr "class"))
2018-01-16 04:35:48 +00:00
suite "More" do
test "Long chain" do
Assert.equal
"Apple"
( loadRoot htmlEx
# find ".apple"
# siblings
# eq 1
# parent
# children
# first
# text)