diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-25 01:44:35 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-25 01:44:35 +0000 |
commit | da0aef961fef08e2690f0dff272b57340dc1d151 (patch) | |
tree | f65fdf390a83d508131fe724ab077f8c3e737377 /tp-bbcode/test | |
parent | 133a91f949025308a9985e6ab9db7d542bbd6678 (diff) | |
download | thermoprint-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.hs | 27 |
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 | ||
4 | module Thermoprint.Printout.BBCodeSpec (spec) where | 5 | module Thermoprint.Printout.BBCodeSpec (spec) where |
5 | 6 | ||
6 | import Test.Hspec | 7 | import Test.Hspec |
7 | import Test.Hspec.QuickCheck (prop) | 8 | import Test.Hspec.QuickCheck (prop) |
9 | import Test.QuickCheck (Property, Discard(..), property) | ||
8 | import Test.QuickCheck.Instances | 10 | import Test.QuickCheck.Instances |
9 | 11 | ||
10 | import Thermoprint.Printout.BBCode | 12 | import Thermoprint.Printout.BBCode |
@@ -15,11 +17,13 @@ import qualified Data.Text.Lazy as TL (pack) | |||
15 | 17 | ||
16 | import Data.String (IsString(..)) | 18 | import Data.String (IsString(..)) |
17 | 19 | ||
18 | import Control.Monad (zipWithM_) | 20 | import Control.Monad (zipWithM_, join) |
19 | import Data.Monoid ((<>)) | 21 | import Data.Monoid ((<>)) |
20 | import Data.Function (on) | 22 | import Data.Function (on) |
23 | import Data.Bifunctor | ||
21 | 24 | ||
22 | import Data.Sequence (Seq) | 25 | import Data.Sequence (Seq) |
26 | import qualified Data.Sequence as Seq | ||
23 | 27 | ||
24 | instance Eq Block where | 28 | instance Eq Block where |
25 | (==) = (==) `on` cotext | 29 | (==) = (==) `on` cotext |
@@ -30,10 +34,23 @@ instance IsString Line where | |||
30 | 34 | ||
31 | spec :: Spec | 35 | spec :: Spec |
32 | spec = do | 36 | spec = 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 | |||
44 | bbcodeInv :: Printout -> Property | ||
45 | bbcodeInv (cobbcode -> Left _) = property True | ||
46 | bbcodeInv p@(cobbcode -> Right t) = either (const $ property True) property $ (==) <$> normalize (Right p) <*> normalize (bbcode t) | ||
47 | |||
48 | normalize = (!! 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 | ||
38 | pOut :: Seq Block -> Printout | 55 | pOut :: Seq Block -> Printout |
39 | pOut = fmap (pure . Cooked) | 56 | pOut = fmap (pure . Cooked) |