-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMarkTests.hs
86 lines (68 loc) · 2.33 KB
/
CMarkTests.hs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{-# LANGUAGE DeriveGeneric #-}
module CMarkTests where
import qualified Data.ByteString.Lazy as B
import Data.Aeson
import GHC.Generics
import Control.Monad (mzero)
import Test.HUnit (Test(..), (~?=), (~:), runTestTT)
import Data.Either (isLeft, fromRight)
import Lucid (renderText, renderToFile)
import Data.Text.Lazy (unpack)
import Data.Set (Set)
import qualified Data.Set as Set
import Text.Parsec.Char
import Text.ParserCombinators.Parsec hiding (runParser)
-- import Test.QuickCheck (Arbitrary(..), Testable(..), Gen, elements,
-- oneof, frequency, sized, quickCheckWith, stdArgs, maxSize,
-- classify, maxSuccess, listOf, resize, scale, (==>))
import MarkDownParse hiding (main)
import HtmlConvert
data CMarkTest = CMarkTest {
markdown :: String,
html :: String,
example :: Int,
start_line :: Int,
end_line :: Int,
section :: String
} deriving (Generic, Show)
instance FromJSON CMarkTest
instance ToJSON CMarkTest
jsonFile :: FilePath
jsonFile = "tests.json"
getJSON :: IO B.ByteString
getJSON = B.readFile jsonFile
main :: IO ()
main = do
d <- (eitherDecode <$> getJSON) :: IO (Either String [CMarkTest])
-- case d of
-- Left err -> putStrLn err
-- Right ps -> print ps
let (Right ps) = d
-- let ps' = filter (\p -> Set.member (section p) includedSections) ps
_ <- runTestTT (TestList (testList ps))
return ()
-- Features not supporting:
-- tabs, escapings, link reference, 1) lists, block quotes,
-- Setext headings, HTML blocks, autolinks for images
includedSections :: Set String
includedSections = Set.fromList ["Tabs"]
testList :: [CMarkTest] -> [Test]
testList = map (\x -> "Example No:" ++ show (example x) ~: render (markdown x) ~?= html x)
decoder :: Parser String
decoder = many $ choice [
try $ '&' <$ string "&",
try $ '<' <$ string "<",
try $ '>' <$ string ">",
try $ '"' <$ string """,
try $ '\'' <$ string "'",
try $ '/' <$ string " /",
anyChar
]
render :: String -> String
render x = case runParser documentP x of
Left err -> ""
Right doc -> (unpack . renderText . convertDocumentNoDoctype) doc
--fromRight "" $ runParser decoder ((unpack . renderText . convertDocumentNoDoctype) doc)
-- tArrayTest :: Test
-- tArrayTest = TestList (testArray 4)
-- testArray :: [a]