packages feed

gemmula-altera-2.1.1: test/Main.hs

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}

import Control.Monad
import Data.Text (Text)
import System.Exit
import Test.HUnit
import Text.RawString.QQ

import Text.Gemini (GemDocument, GemItem (..))
import qualified Text.Gemini.Markdown as M
import qualified Text.Gemini.Web as W


-- | Query the test functions.
main :: IO ()
main = do
    count <-
        runTestTT $
            TestList
                [ -- Markdown functions
                  markdownEncode
                , markdownRewrite
                , -- Web functions
                  htmlEncode
                , htmlRewrite
                , -- Others
                  getTitle
                , webify
                ]
    when (failures count > 0) exitFailure


-- | Test the Markdown encoding.
markdownEncode :: Test
markdownEncode =
    TestCase
        $ assertEqual
            "Comparing the expected result of Markdown encoding,"
            markdown
        $ M.encode gemtext

-- | Test the @.md@ local link rewriting.
markdownRewrite :: Test
markdownRewrite =
    TestCase
        $ assertEqual
            "Comparing the expected result of Markdown local link rewrite,"
            (GemLink "test/file.md" Nothing)
        $ M.rewriteLink (GemLink "test/file.gmi" Nothing)

-- | Test the HTML encoding.
htmlEncode :: Test
htmlEncode =
    TestCase
        $ assertEqual
            "Comparing the expected result of HTML encoding,"
            html
        $ W.encode gemtext

-- | Test the @.html@ local link rewriting.
htmlRewrite :: Test
htmlRewrite =
    TestCase
        $ assertEqual
            "Comparing the expected result of HTML local link rewrite,"
            (GemLink "test/file.html" Nothing)
        $ W.rewriteLink (GemLink "test/file.gmi" Nothing)

-- | Test the title finding.
getTitle :: Test
getTitle =
    TestCase
        $ assertEqual
            "Comparing the expected result of getting the title,"
            (Just "title")
        $ W.getTitle [GemHeading 3 "test", GemHeading 1 "title", GemHeading 1 "ignore"]

-- | Test HTTP link rewriting.
webify :: Test
webify =
    TestCase $
        assertEqual
            "Comparing the expected result of HTTP link rewriting,"
            (GemLink "http://geminiprotocol.net/docs/faq.gmi" Nothing)
            =<< W.webifyLink (GemLink "gemini://geminiprotocol.net/docs/faq.gmi" Nothing)


-- | The supposedly resulting Markdown file.
markdown :: Text
markdown =
    [r|
# Encoding Test

\- This is the \*gemtext\* file for testing \`gemmula-altera\`.

 => [gemmula-altera](https://hackage.haskell.org/package/gemmula-altera)  

 > Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
 > laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
 > architecto beatae vitae dicta sunt explicabo.

 * Emerald Green
 * Toxic Artificial Dye.

```md
This is a preformatted text.
    ``` beautiful, isn't it?
```

\`\``Exciting stuff next!!

\## Shenanigans!!

```
```

 => [link.gmi](link.gmi)  
|]

-- | The supposedly resulting HTML file.
html :: Text
html =
    [r|
<h1>Encoding Test</h1>
<br />
<p>- This is the *gemtext* file for testing `gemmula-altera`.</p>
<a href="https://hackage.haskell.org/package/gemmula-altera">gemmula-altera</a>
<blockquote>
  Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
  laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
  architecto beatae vitae dicta sunt explicabo.
</blockquote>
<ul>
  <li>Emerald Green</li>
  <li>Toxic Artificial Dye.</li>
</ul>
<pre title="md">
This is a preformatted text.
``` beautiful, isn't it?
</pre>
<p>```Exciting stuff next!!</p>
<p>## Shenanigans!!</p>
<br />
<pre>
</pre>
<a href="link.gmi">link.gmi</a>
|]

-- | The Gemini document to test the encoding from.
gemtext :: GemDocument
gemtext =
    [ GemHeading 1 "Encoding Test"
    , GemText ""
    , GemText " - This is the *gemtext* file for testing `gemmula-altera`."
    , GemLink "https://hackage.haskell.org/package/gemmula-altera" (Just "gemmula-altera")
    , GemQuote
        "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo."
    , GemList ["Emerald Green", "Toxic Artificial Dye."]
    , GemPre ["This is a preformatted text.", "``` beautiful, isn't it?"] (Just "md")
    , GemText "```Exciting stuff next!!"
    , GemText "## Shenanigans!!"
    , GemText " "
    , GemList []
    , GemPre [] Nothing
    , GemLink "link.gmi" Nothing
    ]