aboutsummaryrefslogtreecommitdiff
path: root/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs
blob: d2cdeed1356acd5453a260a596a6b8dbc421ae2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE OverloadedStrings, OverloadedLists #-}
{-# LANGUAGE StandaloneDeriving #-}

module Thermoprint.Printout.BBCodeSpec (spec) where

import Test.Hspec
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck.Instances

import Thermoprint.Printout.BBCode
import Thermoprint.Printout

import Data.Text (Text)
import qualified Data.Text.Lazy as TL (pack)

import Data.String (IsString(..))

import Control.Monad (zipWithM_)
import Data.Monoid ((<>))
import Data.Function (on)

import Data.Sequence (Seq)

instance Eq Block where
  (==) = (==) `on` cotext
deriving instance Eq Chunk

instance IsString Line where
  fromString = (\(Right l) -> l) . text . TL.pack

spec :: Spec
spec = do
  zipWithM_ example [1..] examples
    where
      example n (s, ts) = let str = "Example " <> show n
                          in specify str $ bbcode s == (pOut <$> ts)

pOut :: Seq Block -> Printout
pOut = fmap (pure . Cooked)

examples :: [(Text, Either BBCodeError (Seq Block))]
examples = [ ("Hello World!"
             , Right [Line (JuxtaPos ["Hello", HSpace 1, "World!"])])
           , ("Hello [hspace width=2/] World!"
             , Right [Line (JuxtaPos ["Hello", HSpace 4, "World!"])])
           , ("Par1\n\nPar2\n\nPar3 Word2"
             , Right [Line ("Par1"), Line ("Par2"), Line (JuxtaPos ["Par3", HSpace 1, "Word2"])])
           , ("Par1 [hspace=3/]\n\n[vspace=2/]Par2\n\nPar3 Word2"
             , Right [Line (JuxtaPos ["Par1", HSpace 4]), NewlSep [VSpace 2, Line ("Par2")], Line (JuxtaPos ["Par3", HSpace 1, "Word2"])])
           ]