aboutsummaryrefslogtreecommitdiff
path: root/tp-bbcode/test
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-02-25 01:44:35 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-02-25 01:44:35 +0000
commitda0aef961fef08e2690f0dff272b57340dc1d151 (patch)
treef65fdf390a83d508131fe724ab077f8c3e737377 /tp-bbcode/test
parent133a91f949025308a9985e6ab9db7d542bbd6678 (diff)
downloadthermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar.gz
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar.bz2
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar.xz
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.zip
Inverse to bbcode
Diffstat (limited to 'tp-bbcode/test')
-rw-r--r--tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs27
1 files changed, 22 insertions, 5 deletions
diff --git a/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs b/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs
index d2cdeed..09b3147 100644
--- a/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs
+++ b/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs
@@ -1,10 +1,12 @@
1{-# LANGUAGE OverloadedStrings, OverloadedLists #-} 1{-# LANGUAGE OverloadedStrings, OverloadedLists #-}
2{-# LANGUAGE StandaloneDeriving #-} 2{-# LANGUAGE StandaloneDeriving #-}
3{-# LANGUAGE ViewPatterns #-}
3 4
4module Thermoprint.Printout.BBCodeSpec (spec) where 5module Thermoprint.Printout.BBCodeSpec (spec) where
5 6
6import Test.Hspec 7import Test.Hspec
7import Test.Hspec.QuickCheck (prop) 8import Test.Hspec.QuickCheck (prop)
9import Test.QuickCheck (Property, Discard(..), property)
8import Test.QuickCheck.Instances 10import Test.QuickCheck.Instances
9 11
10import Thermoprint.Printout.BBCode 12import Thermoprint.Printout.BBCode
@@ -15,11 +17,13 @@ import qualified Data.Text.Lazy as TL (pack)
15 17
16import Data.String (IsString(..)) 18import Data.String (IsString(..))
17 19
18import Control.Monad (zipWithM_) 20import Control.Monad (zipWithM_, join)
19import Data.Monoid ((<>)) 21import Data.Monoid ((<>))
20import Data.Function (on) 22import Data.Function (on)
23import Data.Bifunctor
21 24
22import Data.Sequence (Seq) 25import Data.Sequence (Seq)
26import qualified Data.Sequence as Seq
23 27
24instance Eq Block where 28instance Eq Block where
25 (==) = (==) `on` cotext 29 (==) = (==) `on` cotext
@@ -30,10 +34,23 @@ instance IsString Line where
30 34
31spec :: Spec 35spec :: Spec
32spec = do 36spec = do
33 zipWithM_ example [1..] examples 37 describe "example texts for bbcode" $
34 where 38 zipWithM_ example [1..] examples
35 example n (s, ts) = let str = "Example " <> show n 39 describe "cobbcode" $ prop "is inverse to bbcode" bbcodeInv
36 in specify str $ bbcode s == (pOut <$> ts) 40 where
41 example n (s, ts) = let str = show s
42 in specify str $ bbcode s == (pOut <$> ts)
43
44bbcodeInv :: Printout -> Property
45bbcodeInv (cobbcode -> Left _) = property True
46bbcodeInv p@(cobbcode -> Right t) = either (const $ property True) property $ (==) <$> normalize (Right p) <*> normalize (bbcode t)
47
48normalize = (!! 3) . iterate normalize' . first (const ())
49 where
50 normalize' t = join' (bbcode <$> (join' $ cobbcode <$> t))
51 join' :: Either a (Either b c) -> Either () c
52 join' (Right (Right a)) = Right a
53 join' _ = Left ()
37 54
38pOut :: Seq Block -> Printout 55pOut :: Seq Block -> Printout
39pOut = fmap (pure . Cooked) 56pOut = fmap (pure . Cooked)