packages feed

gemmula-1.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 as G


-- | Query the test functions.
main :: IO ()
main = do
    count <-
        runTestTT $
            TestList
                [ gemDecode
                , gemEncode
                ]
    when (failures count > 0) exitFailure


-- | Test the decoding of gemtext documents.
gemDecode :: Test
gemDecode =
    TestCase
        $ assertEqual
            "Comparing the expected result of gemtext decoding,"
            document
        $ G.decode raw

-- | Test the encoding of gemtext documents.
gemEncode :: Test
gemEncode =
    TestCase
        $ assertEqual
            "Comparing the expected result of gemtext encoding,"
            pretty
        $ G.encode document


-- | The expected encoded file to compare against
pretty :: Text
pretty =
    [r| text!!
another   text
=> link.gmi a link
=> gemini://geminiprotocol.net/index.gmi
=>
# heading
## sub-heading
###
### subsubheading but it works
### same as level 3

*text
 * also text!
* list item
* another item
*
> quote
>
```alt
> preformat with alt
```
```

* preformat without alt
```
> qq
```empty
```
|]

-- | The expected decoded document to compare against
document :: GemDocument
document =
    [ GemText " text!!"
    , GemText "another   text"
    , GemLink "link.gmi" (Just "a link")
    , GemLink "gemini://geminiprotocol.net/index.gmi" Nothing
    , GemText "=>"
    , GemHeading 1 "heading"
    , GemHeading 2 "sub-heading"
    , GemText "###"
    , GemHeading 3 "subsubheading but it works"
    , GemHeading 3 "same as level 3"
    , GemText ""
    , GemText "*text"
    , GemText " * also text!"
    , GemList ["list item", "another item"]
    , GemText "*"
    , GemQuote "quote"
    , GemText ">"
    , GemPre ["> preformat with alt"] (Just "alt")
    , GemPre ["", "* preformat without alt"] Nothing
    , GemQuote "qq"
    , GemPre [] (Just "empty")
    ]

-- | The raw file to try to decode
raw :: Text
raw =
    [r| text!! 
another   text
=>link.gmi    a link  
=>  gemini://geminiprotocol.net/index.gmi
=> 
# heading 
##    sub-heading
###  
###subsubheading but it works
#### same as level 3
  
*text
 * also text!
* list item
* another item
*
> quote
> 
```alt 
> preformat with alt
```
```  

* preformat without alt
```
>qq 
```empty
```ignored 
|]