aboutsummaryrefslogtreecommitdiff
path: root/haddock-library/fixtures/Fixtures.hs
blob: 282fd10d0aa1e7f764b1e11f8e363c7e9d4bfb7b (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{-# LANGUAGE DeriveGeneric, StandaloneDeriving #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main (main) where

import Control.Applicative ((<|>))
import Control.Exception (IOException, catch)
import Control.Monad (when)
import Data.Foldable (traverse_)
import Data.List (foldl')
import Data.Traversable (for)
import GHC.Generics (Generic)
import Prelude ()
import Prelude.Compat
import System.Directory (getDirectoryContents)
import System.Exit (exitFailure)
import System.FilePath
import System.IO

import Data.TreeDiff
import Data.TreeDiff.Golden

import qualified Options.Applicative as O

import           Documentation.Haddock.Types
import qualified Documentation.Haddock.Parser as Parse

type Doc id = DocH () id

data Fixture = Fixture
    { fixtureName   :: FilePath
    , fixtureOutput :: FilePath
    }
  deriving Show

data  Result = Result
    { _resultSuccess :: !Int
    , _resultTotal   :: !Int
    }
  deriving Show

combineResults :: Result -> Result -> Result
combineResults (Result s t) (Result s' t') = Result (s + s') (t + t')

readFixtures :: IO [Fixture]
readFixtures = do
    let dir = "fixtures/examples"
    files <- getDirectoryContents dir
    let inputs = filter (\fp -> takeExtension fp == ".input") files
    return $ flip map inputs $ \fp -> Fixture
        { fixtureName = dir </> fp
        , fixtureOutput = dir </> fp -<.> "parsed"
        }

goldenFixture
    :: String
    -> IO Expr
    -> IO Expr
    -> (Expr -> Expr -> IO (Maybe String))
    -> (Expr -> IO ())
    -> IO Result
goldenFixture name expect actual cmp wrt = do
    putStrLn $ "running " ++ name
    a <- actual
    e <- expect `catch` handler a
    mres <- cmp e a
    case mres of
        Nothing  -> return (Result 1 1)
        Just str -> do
            putStrLn str
            return (Result 0 1)
  where
    handler :: Expr -> IOException -> IO Expr
    handler a exc = do
        putStrLn $ "Caught " ++ show exc
        putStrLn "Accepting the test"
        wrt a
        return a

runFixtures :: [Fixture] -> IO ()
runFixtures fixtures = do
    results <- for fixtures $ \(Fixture i o) -> do
        let name = takeBaseName i
        let readDoc = do
                input <- readFile i
                return (parseString input)
        ediffGolden goldenFixture name o readDoc
    case foldl' combineResults (Result 0 0) results of
        Result s t -> do
            putStrLn $ "Fixtures: success " ++ show s ++ "; total " ++ show t
            when (s /= t) exitFailure

listFixtures :: [Fixture] -> IO ()
listFixtures = traverse_ $ \(Fixture i _) -> do
    let name = takeBaseName i
    putStrLn name

acceptFixtures :: [Fixture] -> IO ()
acceptFixtures = traverse_ $ \(Fixture i o) -> do
    input <- readFile i
    let doc = parseString input
    let actual = show (prettyExpr $ toExpr doc) ++ "\n"
    writeFile o actual

parseString :: String -> Doc String
parseString = Parse.toRegular . _doc . Parse.parseParas

data Cmd = CmdRun | CmdAccept | CmdList

main :: IO ()
main = do
    hSetBuffering stdout NoBuffering -- For interleaved output when debugging
    runCmd =<< O.execParser opts
  where
    opts = O.info (O.helper <*> cmdParser) O.fullDesc

    cmdParser :: O.Parser Cmd
    cmdParser = cmdRun <|> cmdAccept <|> cmdList <|> pure CmdRun

    cmdRun = O.flag' CmdRun $ mconcat
        [ O.long "run"
        , O.help "Run parser fixtures"
        ]

    cmdAccept = O.flag' CmdAccept $ mconcat
        [ O.long "accept"
        , O.help "Run & accept parser fixtures"
        ]

    cmdList = O.flag' CmdList $ mconcat
        [ O.long "list"
        , O.help "List fixtures"
        ]

runCmd :: Cmd -> IO ()
runCmd CmdRun    = readFixtures >>= runFixtures
runCmd CmdList   = readFixtures >>= listFixtures
runCmd CmdAccept = readFixtures >>= acceptFixtures

-------------------------------------------------------------------------------
-- Orphans
-------------------------------------------------------------------------------

deriving instance Generic (DocH mod id)
instance (ToExpr mod, ToExpr id)  => ToExpr (DocH mod id)

deriving instance Generic (Header id)
instance ToExpr id => ToExpr (Header id)

deriving instance Generic Hyperlink
instance ToExpr Hyperlink

deriving instance Generic Picture
instance ToExpr Picture

deriving instance Generic Example
instance ToExpr Example

deriving instance Generic (Table id)
instance ToExpr id => ToExpr (Table id)

deriving instance Generic (TableRow id)
instance ToExpr id => ToExpr (TableRow id)

deriving instance Generic (TableCell id)
instance ToExpr id => ToExpr (TableCell id)