hasciidoc/tests/Main.hs

135 lines
5.2 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Attoparsec.Text
import qualified Data.Text as T
import Test.Tasty
import Test.Tasty.Hspec as HS
import Hasciidoc
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "Tests" [specs]
-- Hspec Tests
specs :: TestTree
specs = testGroup "Specifications"
[ HS.testCase "Header Parsing" headerParseSpec
]
headerParseSpec :: Spec
headerParseSpec = do
describe "Document Header" $ do
describe "Document Title" $ do
describe "pTitle" $ do
it "parses a one-line level 0 title with no right-hand delimiter" $
parseOnly pTitle "= This is a level 0 title\n"
`shouldBe`
Right (0, "This is a level 0 title")
it "parses a one-line level 0 title with a right-hand delimeter" $
parseOnly pTitle "= This is another level 0 title =\n"
`shouldBe`
Right (0, "This is another level 0 title")
it "parses a two-line level 0 title with a full-length underline" $
parseOnly pTitle "This is a two-line level 0 title\n\
\================================\n"
`shouldBe`
Right (0, "This is a two-line level 0 title")
it "parses a two-line level 0 title with a minimal underline" $
parseOnly pTitle "Another two-line level 0 title\n\
\==\n"
`shouldBe`
Right (0, "Another two-line level 0 title")
it "parses a one-line level 1 title" $
parseOnly pTitle "== Level 1\n" `shouldBe` Right (1, "Level 1")
it "parses a one-line level 1 title with right-delimiter" $
parseOnly pTitle "== Level 1 ==\n" `shouldBe` Right (1, "Level 1")
it "parses a one-line level 2 title" $
parseOnly pTitle "=== Level 2\n" `shouldBe` Right (2, "Level 2")
it "parses a one-line level 2 title with right-delimiter" $
parseOnly pTitle "=== Level 2 ===\n" `shouldBe` Right (2, "Level 2")
it "parses a one-line level 3 title" $
parseOnly pTitle "==== Level 3\n" `shouldBe` Right (3, "Level 3")
it "parses a one-line level 3 title with right-delimiter" $
parseOnly pTitle "==== Level 3 ====\n" `shouldBe` Right (3, "Level 3")
it "parses a one-line level 4 title" $
parseOnly pTitle "===== Level 4\n" `shouldBe` Right (4, "Level 4")
it "parses a one-line level 4 title with right-delimiter" $
parseOnly pTitle "===== Level 4 =====\n" `shouldBe` Right (4, "Level 4")
it "parses a two-line level 1 title" $
parseOnly pTitle "A two-line level 1 title\n--\n"
`shouldBe` Right (1, "A two-line level 1 title")
it "parses a two-line level 2 title" $
parseOnly pTitle "A two-line level 2 title\n~~\n"
`shouldBe` Right (2, "A two-line level 2 title")
it "parses a two-line level 3 title" $
parseOnly pTitle "A two-line level 3 title\n^^\n"
`shouldBe` Right (3, "A two-line level 3 title")
it "parses a two-line level 4 title" $
parseOnly pTitle "A two-line level 4 title\n++\n"
`shouldBe` Right (4, "A two-line level 4 title")
describe "Document Author" $ do
describe "pAuthorLine" $ do
it "parses a line with just a first name" $
parseOnly pAuthorLine "Joe\n"
`shouldBe` Right (Author ["Joe"] Nothing Nothing Nothing)
it "parses a line with a two-word first name" $
parseOnly pAuthorLine "Joe_Dee\n"
`shouldBe` Right (Author ["Joe", "Dee"] Nothing Nothing Nothing)
it "parses a line with a first and last name" $
parseOnly pAuthorLine "Joe Bloggs\n"
`shouldBe` Right (Author ["Joe"] Nothing (Just ["Bloggs"]) Nothing)
it "parses a line with two first and two last names" $
parseOnly pAuthorLine "Joe_Dee de_Winter\n"
`shouldBe`
Right (Author ["Joe", "Dee"] Nothing (Just ["de", "Winter"]) Nothing)
it "parses a first, middle, and last name" $
parseOnly pAuthorLine "Joe Jay Bloggs\n"
`shouldBe`
Right (Author ["Joe"] (Just ["Jay"]) (Just ["Bloggs"]) Nothing)
it "parses multiple first, middle, and last names" $
parseOnly pAuthorLine "Joe_Dee Dee_Ann de_Winter\n"
`shouldBe`
Right (Author ["Joe", "Dee"]
(Just ["Dee", "Ann"])
(Just ["de", "Winter"]) Nothing)
it "parses first name and email address" $
parseOnly pAuthorLine "Joe <JoeBloggs@mail.com>\n"
`shouldBe`
Right (Author ["Joe"] Nothing Nothing (Just "JoeBloggs@mail.com"))
it "parses first and last names and email address" $
parseOnly pAuthorLine "Joe Bloggs <JoeBloggs@mail.com>\n"
`shouldBe`
Right (Author ["Joe"] Nothing (Just ["Bloggs"]) (Just "JoeBloggs@mail.com"))
it "Parses first, middle and last names and email address" $
parseOnly pAuthorLine "Joe Jay Bloggs <JoeBloggs@mail.com>\n"
`shouldBe`
Right (Author ["Joe"]
(Just ["Jay"])
(Just ["Bloggs"]) (Just "JoeBloggs@mail.com"))