module MdToHtmlTest where
import MdToHTML
import Test.HUnit
check_equal :: String -> String -> String -> Test
check_equal desc expected actual = TestCase (assertEqual desc expected actual)
convert :: String -> String
convert md = show . fst $ leftmostLongestParse parseDocument md
headerTests =
TestList
[ check_equal "Should convert H1 heading" "
Hello
" (convert "# Hello"),
check_equal "Should convert H2 heading" "Hello
" (convert "## Hello"),
check_equal "Should convert H3 heading" "Hello
" (convert "### Hello"),
check_equal "Should convert H4 heading" "Hello
" (convert "#### Hello"),
check_equal "Should convert H5 heading" "Hello
" (convert "##### Hello"),
check_equal "Should convert H6 heading" "Hello
" (convert "###### Hello")
]
boldTests =
TestList
[ check_equal "Should convert bold" "Hello
" (convert "__Hello__"),
check_equal "Should convert italic" "Hello
" (convert "_Hello_"),
check_equal "Should convert bold and italic in a sentence" "It is a wonderful day
" (convert "It _is_ a __wonderful__ day"),
check_equal "Should convert nested bold and italic" "Bold then Italic
" (convert "**Bold then *Italic***"),
check_equal "Should convert nested bold and italic" "Italic then Bold
" (convert "*Italic then **Bold***")
]
strikethroughTests =
TestList
[ check_equal "Should convert strikethrough" "Hello
" (convert "~~Hello~~"),
check_equal "Should convert long sentence with tilde" "The universe is ~7 days old. The universe is 13 billion years old.
" (convert "~~The universe is ~7 days old~~. The universe is 13 billion years old.")
]
linkTests =
TestList
[ check_equal "Should convert normal link" "This is an example link.
" (convert "[This is an example link.](https://example.com)"),
check_equal "Should convert styled link" "Fancy!!!
" (convert "[__Fancy__!!!](https://example.com)")
]
escapedCharTests =
TestList
[ check_equal "Should print literal underscore" "This is an underscore - _
" (convert "This is an underscore - \\_"),
check_equal "Should print literal asterisk" "This is an asterisk - *
" (convert "This is an asterisk - \\*"),
check_equal "Should print literal asterisk in bold" "This is a bolded asterisk - *
" (convert "This is a bolded asterisk - **\\***")
]
blockquoteTests =
TestList
[ check_equal "Should wrap para in blockquote" "What a truly lovely day!!!
" (convert "> What a __truly__ _lovely_ day!!!"),
check_equal "Simple nested blockquotes" "Hello
World
" (convert "> Hello\n>\n>> World"),
check_equal
"Nested blockquotes"
"Dorothy followed her through many \
\of the beautiful rooms in her castle.
The Witch \
\bade her clean the pots and kettles and sweep the floor and keep the fire \
\fed with wood.
"
( convert
"> Dorothy followed her through many of the \
\beautiful rooms in her castle.\n> \n>> The Witch bade her \
\clean the pots and kettles and sweep the floor and keep the fire fed with wood."
)
]
unorderedListTests =
TestList
[ check_equal "Basic unordered list" "" (convert "* Item 1\n* Item 2\n* Item 3"),
check_equal "Mixing list indicators" "" (convert "* Item 1\n+ Item 2\n- Item 3"),
check_equal "Formatted lists" "" (convert "* __Item 1__\n+ _Item 2_\n- ***Item 3***"),
check_equal "Nested list" "" (convert "* Item 1\n* Item 2\n* Item 3\n * Subitem 1\n * Subitem 2"),
check_equal "Paragraph in list" "- Item 1
Item 2
More stuff
- Item 3
" (convert "- Item 1\n- Item 2\n\n More stuff\n\n- Item 3"),
check_equal "Paragraph before list" "This is a list
" (convert "This is a list\n\n* Item 1\n* Item 2"),
check_equal "Paragraph before list" "This is a list
" (convert "### This is a list\n\n* Item 1\n* Item 2"),
check_equal "Nested list then back" "" (convert "- Item 1\n- Item 2\n - Item 3\n - Item 4\n- Item 5"),
check_equal "Blockquote in list" "" (convert "- Item 1\n- Item 2\n\n > Quote\n\n- Item 3"),
check_equal "Ordered list in unordered list" "- Item 1
- Item 2
- Item 1
- Item 2
- Item 3
" (convert "- Item 1\n- Item 2\n 1. Item 1\n 2. Item 2\n- Item 3")
]
orderedListTests =
TestList
[ check_equal "Basic ordered list" "- Item 1
- Item 2
- Item 3
" (convert "1. Item 1\n2. Item 2\n3. Item 3"),
check_equal "Mixing list numbering" "- Item 1
- Item 2
- Item 3
" (convert "1. Item 1\n3. Item 2\n2. Item 3"),
check_equal "Should not convert list without number 1" "2. Item 1 1. Item 2
" (convert "2. Item 1\n1. Item 2"),
check_equal "Formatted lists" "- Item 1
- Item 2
- Item 3
" (convert "1. __Item 1__\n2. _Item 2_\n3. ***Item 3***"),
check_equal "Nested list" "- Item 1
- Item 2
- Item 3
- Subitem 1
- Subitem 2
" (convert "1. Item 1\n2. Item 2\n3. Item 3\n 1. Subitem 1\n 2. Subitem 2"),
check_equal "Paragraph in list" "- Item 1
Item 2
More stuff
- Item 3
" (convert "1. Item 1\n2. Item 2\n\n More stuff\n\n1. Item 3"),
check_equal "Paragraph before list" "This is a list
- Item 1
- Item 2
" (convert "This is a list\n\n1. Item 1\n1. Item 2"),
check_equal "Paragraph before list" "This is a list
- Item 1
- Item 2
" (convert "### This is a list\n\n1. Item 1\n200. Item 2"),
check_equal "Nested list then back" "- Item 1
- Item 2
- Item 3
- Item 4
- Item 5
" (convert "1. Item 1\n2. Item 2\n 1. Item 3\n 3. Item 4\n5. Item 5"),
check_equal "Blockquote in list" "- Item 1
Item 2
Quote
- Item 3
" (convert "1. Item 1\n2. Item 2\n\n > Quote\n\n3. Item 3"),
check_equal "Unordered list in ordered list" "- Item 1
- Item 2
- Item 3
" (convert "1. Item 1\n2. Item 2\n - Item 1\n * Item 2\n4. Item 3")
]
codeTests =
TestList
[ check_equal "Code by itself" "Hello world!
" (convert "`Hello world!`"),
check_equal "Code in a paragraph" "The following text
is code
" (convert "The following `text` is code"),
check_equal "Code across paragraphs (shouldn't work" "`Incomplete
Code`
" (convert "`Incomplete\n\nCode`")
]
integrationTests =
TestList
[ check_equal "Integration 1" "Sample Markdown
This is some basic, sample markdown.
Second Heading
" (convert "# Sample Markdown\n\n This is some basic, sample markdown.\n\n ## __Second__ _Heading_"),
check_equal "Integration 2" "Hello World
" (convert "__Hello__\n_World_"),
check_equal "Integration 3" "Hello
World
" (convert "# Hello\nWorld"),
check_equal "Integration 4" "a b
" (convert "a\nb"),
check_equal "Integration 5" "Hello
" (convert "# Hello\n"),
check_equal "Integration 6" "First line
Second line
" (convert "First line \nSecond line"),
check_equal
"Integration 7"
"Sample Markdown
This is some basic, sample markdown.
Second \
\Heading
- Unordered lists, and:
- One
- Two
- \
\Three
- More
Blockquote
\
\
And bold, italics, and even italics \
\and later bold. Even strikethrough. \
\A link to somewhere.
"
( convert
"# Sample Markdown\n\nThis is some basic, sample markdown.\n\n## Second \
\Heading\n\n- Unordered lists, and:\n 1. One\n 2. Two\n 3. Three\n\
\- More\n\n> Blockquote\n\nAnd **bold**, *italics*, and even *italics and \
\later **bold***. Even ~~strikethrough~~. [A link](https://markdowntohtml.com) to somewhere."
)
]
tests =
TestList
[ headerTests,
boldTests,
strikethroughTests,
linkTests,
escapedCharTests,
blockquoteTests,
unorderedListTests,
orderedListTests,
codeTests,
integrationTests
]
runTests = runTestTT tests