2018-01-16 04:35:48 +00:00
|
|
|
module Test.Cheerio where
|
|
|
|
|
|
|
|
import Prelude hiding (eq)
|
|
|
|
|
2023-10-28 20:49:52 +00:00
|
|
|
import Cheerio (CheerioNode)
|
|
|
|
import Cheerio as Cheerio
|
|
|
|
import Control.Monad.Error.Class (liftMaybe)
|
|
|
|
import Data.Array as Array
|
2018-01-16 04:35:48 +00:00
|
|
|
import Data.Maybe (Maybe(..))
|
2023-10-28 20:49:52 +00:00
|
|
|
import Effect (Effect)
|
|
|
|
import Effect.Class (liftEffect)
|
|
|
|
import Effect.Exception (error)
|
|
|
|
import Test.HtmlEx (htmlEx)
|
2018-01-16 04:35:48 +00:00
|
|
|
import Test.Unit (TestSuite, suite, test)
|
|
|
|
import Test.Unit.Assert as Assert
|
|
|
|
import Test.Unit.Main (runTest)
|
|
|
|
|
2018-07-28 07:40:04 +00:00
|
|
|
main :: Effect Unit
|
2018-01-16 04:35:48 +00:00
|
|
|
main = runTest suites
|
|
|
|
|
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
|
2023-10-28 20:49:52 +00:00
|
|
|
doc <- liftEffect $ Cheerio.load htmlEx
|
|
|
|
fruits <- liftEffect
|
|
|
|
$ liftMaybe (error "ul should have id")
|
|
|
|
=<< Cheerio.attr "id"
|
|
|
|
=<< liftMaybe (error "ul should exist")
|
|
|
|
=<< Cheerio.findFirst "ul" doc
|
|
|
|
Assert.equal "fruits" fruits
|
2018-07-29 13:31:38 +00:00
|
|
|
|
2018-01-16 04:35:48 +00:00
|
|
|
suite "Traversing" do
|
|
|
|
test "find" do
|
2023-10-28 20:49:52 +00:00
|
|
|
doc <- liftEffect $ Cheerio.load htmlEx
|
|
|
|
lis <- liftEffect $ Cheerio.find "li" doc
|
|
|
|
Assert.equal 3 (Array.length lis)
|
2018-01-16 04:35:48 +00:00
|
|
|
|
|
|
|
test "parent" do
|
2023-10-28 20:49:52 +00:00
|
|
|
doc <- liftEffect $ Cheerio.load htmlEx
|
|
|
|
pear <- liftEffect $ liftMaybe (error "pear should exist") =<< Cheerio.findFirst ".pear" doc
|
|
|
|
parentId <- liftEffect
|
|
|
|
$ liftMaybe (error "parent should have id")
|
|
|
|
=<< Cheerio.attr "id"
|
|
|
|
=<< liftMaybe (error "parent should exist")
|
|
|
|
=<< Cheerio.parent pear
|
|
|
|
Assert.equal "fruits" parentId
|
2018-01-16 04:35:48 +00:00
|
|
|
|
|
|
|
test "siblings" do
|
2023-10-28 20:49:52 +00:00
|
|
|
doc <- liftEffect $ Cheerio.load htmlEx
|
|
|
|
pear <- liftEffect $ liftMaybe (error "pear should exist") =<< Cheerio.findFirst ".pear" doc
|
|
|
|
siblings <- liftEffect $ Cheerio.siblings pear
|
|
|
|
Assert.equal 2 (Array.length siblings)
|
2018-01-16 04:35:48 +00:00
|
|
|
|
|
|
|
test "children" do
|
2023-10-28 20:49:52 +00:00
|
|
|
doc <- liftEffect $ Cheerio.load htmlEx
|
|
|
|
fruitsRoot <- liftEffect $ liftMaybe (error "fruits should exist") =<< Cheerio.findFirst "#fruits" doc
|
|
|
|
fruits <- liftEffect $ Cheerio.children fruitsRoot
|
|
|
|
Assert.equal 3 (Array.length fruits)
|
2018-01-16 04:35:48 +00:00
|
|
|
|
|
|
|
suite "Rendering" do
|
|
|
|
test "html" do
|
2023-10-28 20:49:52 +00:00
|
|
|
doc <- liftEffect $ Cheerio.load htmlEx
|
|
|
|
appleHtml <- liftEffect $ Cheerio.html =<< liftMaybe (error "apple should exist") =<< Cheerio.findFirst ".apple" doc
|
|
|
|
Assert.equal "Apple" appleHtml
|
2018-07-29 13:31:38 +00:00
|
|
|
|
2018-01-16 04:35:48 +00:00
|
|
|
test "text" do
|
2023-10-28 20:49:52 +00:00
|
|
|
doc <- liftEffect $ Cheerio.load htmlEx
|
|
|
|
appleText <- liftEffect $ Cheerio.text =<< liftMaybe (error "apple should exist") =<< Cheerio.findFirst ".apple" doc
|
|
|
|
Assert.equal "Apple" appleText
|