diff options
Diffstat (limited to 'bbcode/test')
-rw-r--r-- | bbcode/test/Text/BBCode/LexerSpec.hs | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/bbcode/test/Text/BBCode/LexerSpec.hs b/bbcode/test/Text/BBCode/LexerSpec.hs index d6e1427..bd2ab7f 100644 --- a/bbcode/test/Text/BBCode/LexerSpec.hs +++ b/bbcode/test/Text/BBCode/LexerSpec.hs | |||
@@ -14,6 +14,7 @@ import Data.Text (Text) | |||
14 | import qualified Data.Text as T (singleton, replace, last, null) | 14 | import qualified Data.Text as T (singleton, replace, last, null) |
15 | 15 | ||
16 | import Data.Monoid ((<>), mconcat, Endo(..)) | 16 | import Data.Monoid ((<>), mconcat, Endo(..)) |
17 | import Data.List (intersperse) | ||
17 | 18 | ||
18 | import Data.Attoparsec.Text (parseOnly, endOfInput) | 19 | import Data.Attoparsec.Text (parseOnly, endOfInput) |
19 | 20 | ||
@@ -22,7 +23,12 @@ import Control.Monad (zipWithM_) | |||
22 | 23 | ||
23 | coToken :: BBToken -> Text | 24 | coToken :: BBToken -> Text |
24 | -- ^ Inverse of `token` | 25 | -- ^ Inverse of `token` |
25 | coToken (BBOpen t) = "[" <> escape [']'] t <> "]" | 26 | coToken (BBOpen t []) = "[" <> escape [']'] t <> "]" |
27 | coToken (BBOpen t xs) = "[" <> escape [']'] t <> " " <> attrs <> " ]" | ||
28 | where | ||
29 | attrs = mconcat . intersperse " " $ map attr xs | ||
30 | attr (key, Nothing) = escape ['=', ']', ' '] key | ||
31 | attr (key, Just val) = escape ['=', ']', ' '] key <> "=\"" <> escape ['\"'] val <> "\"" | ||
26 | coToken (BBClose t) = "[/" <> escape [']'] t <> "]" | 32 | coToken (BBClose t) = "[/" <> escape [']'] t <> "]" |
27 | coToken (BBStr t) = escape ['['] t | 33 | coToken (BBStr t) = escape ['['] t |
28 | 34 | ||
@@ -49,26 +55,30 @@ spec = do | |||
49 | in specify str (tokenize s == Right ts) | 55 | in specify str (tokenize s == Right ts) |
50 | 56 | ||
51 | prop_token :: Text -> Property | 57 | prop_token :: Text -> Property |
52 | -- ^ prop> (mconcat . map coToken) <$> tokenize x == Right x | 58 | -- ^ prop> (==) <$> (((mconcat . map coToken) <$> tokenize x) >>= tokenize) <*> tokenize x |
53 | -- | 59 | -- |
54 | -- Where 'x' is restricted such that `tokenize` succeeds | 60 | -- Where 'x' is restricted such that `tokenize` succeeds |
55 | prop_token x = discardLeft (((== x) . mconcat . map coToken) <$> tokenize x) | 61 | -- |
62 | -- Without accounting for failure this is: | ||
63 | -- | ||
64 | -- > (tokenize . mconcat . map coToken . tokenize x) == tokenize x | ||
65 | prop_token x = discardLeft $ (==) <$> (((mconcat . map coToken) <$> tokenize x) >>= tokenize) <*> tokenize x | ||
56 | where | 66 | where |
57 | discardLeft = either (const $ property Discard) property | 67 | discardLeft = either (const $ property Discard) property |
58 | 68 | ||
59 | examples :: [(Text, [BBToken])] | 69 | examples :: [(Text, [BBToken])] |
60 | examples = [ ("[t]test[/t]" | 70 | examples = [ ("[t]test[/t]" |
61 | , [BBOpen "t", BBStr "test", BBClose "t"]) | 71 | , [BBOpen "t" [], BBStr "test", BBClose "t"]) |
62 | , ("[t]te\\st[/t]" | 72 | , ("[t]te\\st[/t]" |
63 | , [BBOpen "t", BBStr "te\\st", BBClose "t"]) | 73 | , [BBOpen "t" [], BBStr "te\\st", BBClose "t"]) |
64 | , ("[t]te\\[st[/t]" | 74 | , ("[t]te\\[st[/t]" |
65 | , [BBOpen "t", BBStr "te[st", BBClose "t"]) | 75 | , [BBOpen "t" [], BBStr "te[st", BBClose "t"]) |
66 | , ("[t]test\\\\[/t]" | 76 | , ("[t]test\\\\[/t]" |
67 | , [BBOpen "t", BBStr "test\\", BBClose "t"]) | 77 | , [BBOpen "t" [], BBStr "test\\", BBClose "t"]) |
68 | , ("[t]test\\[/t]" | 78 | , ("[t]test\\[/t]" |
69 | , [BBOpen "t", BBStr "test[/t]"]) | 79 | , [BBOpen "t" [], BBStr "test[/t]"]) |
70 | , ("[\\t]test[/t]" | 80 | , ("[\\t]test[/t]" |
71 | , [BBOpen "\\t", BBStr "test", BBClose "t"]) | 81 | , [BBOpen "\\t" [], BBStr "test", BBClose "t"]) |
72 | , ("[t]test[/t\\]]" | 82 | , ("[t]test[/t\\]]" |
73 | , [BBOpen "t", BBStr "test", BBClose "t]"]) | 83 | , [BBOpen "t" [], BBStr "test", BBClose "t]"]) |
74 | ] | 84 | ] |