2024-01-26 17:29:58 +01:00
|
|
|
-- Converts the test data to markdown
|
|
|
|
-- it uses raw html for the table parts
|
2024-01-09 01:07:47 +01:00
|
|
|
|
2024-01-10 08:40:24 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2024-01-09 01:07:47 +01:00
|
|
|
import Language.SQL.SimpleSQL.Tests
|
2024-01-26 17:29:58 +01:00
|
|
|
import Text.Show.Pretty (ppShow)
|
2024-01-10 08:40:24 +01:00
|
|
|
import qualified Language.SQL.SimpleSQL.Parse as P
|
|
|
|
import qualified Language.SQL.SimpleSQL.Lex as L
|
|
|
|
import qualified Data.Text as T
|
2024-01-09 01:07:47 +01:00
|
|
|
|
2024-01-26 17:29:58 +01:00
|
|
|
import qualified Data.Text.Lazy as L
|
|
|
|
import qualified Data.Text.Lazy.IO as L
|
2024-01-10 08:40:24 +01:00
|
|
|
|
2024-01-26 17:29:58 +01:00
|
|
|
data TableItem = Heading Int L.Text
|
|
|
|
| Row L.Text L.Text
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
doc :: Int -> TestItem -> [TableItem]
|
|
|
|
-- filter out some groups of tests
|
2024-01-26 17:29:58 +01:00
|
|
|
doc _ (Group nm _) | "generated" `T.isInfixOf` nm = []
|
2024-01-09 01:07:47 +01:00
|
|
|
doc n (Group nm is) =
|
2024-01-26 17:29:58 +01:00
|
|
|
Heading n (L.fromStrict nm)
|
2024-01-09 01:07:47 +01:00
|
|
|
: concatMap (doc (n + 1)) is
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (TestScalarExpr _ str e _) =
|
2024-01-26 17:29:58 +01:00
|
|
|
[Row (L.fromStrict str) (L.pack $ ppShow e)]
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (TestQueryExpr _ str e _) =
|
2024-01-26 17:29:58 +01:00
|
|
|
[Row (L.fromStrict str) (L.pack $ ppShow e)]
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (TestStatement _ str e _) =
|
2024-01-26 17:29:58 +01:00
|
|
|
[Row (L.fromStrict str) (L.pack $ ppShow e)]
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (TestStatements _ str e _) =
|
2024-01-26 17:29:58 +01:00
|
|
|
[Row (L.fromStrict str) (L.pack $ ppShow e)]
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (ParseQueryExpr d str _) =
|
2024-01-26 17:29:58 +01:00
|
|
|
[Row (L.fromStrict str) (showResult $ P.parseQueryExpr d "" Nothing str)]
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (ParseQueryExprFails d str _) =
|
2024-01-26 17:29:58 +01:00
|
|
|
[Row (L.fromStrict str) (showResult $ P.parseQueryExpr d "" Nothing str)]
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (ParseScalarExprFails d str _) =
|
2024-01-26 17:29:58 +01:00
|
|
|
[Row (L.fromStrict str) (showResult $ P.parseScalarExpr d "" Nothing str)]
|
2024-01-09 01:07:47 +01:00
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
doc _ (LexTest d str _ _) =
|
|
|
|
[Row (L.fromStrict str) (showResultL $ L.lexSQL d False "" Nothing str)]
|
|
|
|
|
|
|
|
doc _ (LexFails d str _) =
|
|
|
|
[Row (L.fromStrict str) (showResultL $ L.lexSQL d False "" Nothing str)]
|
|
|
|
doc _ (GeneralParseFailTest {}) = []
|
2024-01-09 01:07:47 +01:00
|
|
|
|
2024-01-10 08:40:24 +01:00
|
|
|
|
2024-01-26 17:29:58 +01:00
|
|
|
showResult :: Show a => Either P.ParseError a -> L.Text
|
|
|
|
showResult = either (("Left\n" <>) . L.fromStrict . P.prettyError) (L.pack . ppShow)
|
2024-01-10 18:05:56 +01:00
|
|
|
|
2024-01-26 17:29:58 +01:00
|
|
|
showResultL :: Show a => Either L.ParseError a -> L.Text
|
|
|
|
showResultL = either (("Left\n" <>) . L.fromStrict . L.prettyError) (L.pack . ppShow)
|
2024-01-10 18:05:56 +01:00
|
|
|
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
-- TODO: should put the dialect in the html output
|
|
|
|
|
|
|
|
|
2024-01-26 17:29:58 +01:00
|
|
|
render :: [TableItem] -> L.Text
|
2024-01-09 01:07:47 +01:00
|
|
|
render = go False
|
|
|
|
where
|
2024-01-26 17:29:58 +01:00
|
|
|
go _t (Heading level title : is) =
|
|
|
|
"</table>\n"
|
|
|
|
<>
|
2024-01-09 01:07:47 +01:00
|
|
|
-- slight hack
|
2024-01-26 17:29:58 +01:00
|
|
|
(if (level > 1)
|
|
|
|
then "\n" <> L.replicate (fromIntegral $ level - 1) "#" <> " " <> title <> "\n"
|
|
|
|
else "")
|
|
|
|
<> go False is
|
|
|
|
go t (Row sql hask : is) =
|
|
|
|
(if (not t)
|
|
|
|
then "<table>\n"
|
|
|
|
else "")
|
|
|
|
<> let sql' = "\n~~~~{.sql}\n" <> sql <> "\n~~~~\n"
|
|
|
|
hask' = "\n~~~~{.haskell}\n" <> hask <> "\n~~~~\n"
|
|
|
|
in "<tr><td>\n" <> sql' <> "</td><td>\n" <> hask' <> "</td></tr>\n"
|
|
|
|
<> go True is
|
|
|
|
go _t [] = "</table>\n"
|
|
|
|
{-escapePipe t = T.pack $ escapePipe' $ T.unpack t
|
2024-01-10 08:40:24 +01:00
|
|
|
escapePipe' [] = []
|
|
|
|
escapePipe' ('\\':'|':xs) = '\\' : '\\' : '\\' : '|' : escapePipe' xs
|
|
|
|
escapePipe' ('|':xs) = '\\' : '|' : escapePipe' xs
|
2024-01-26 17:29:58 +01:00
|
|
|
escapePipe' (x:xs) = x : escapePipe' xs-}
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
main :: IO ()
|
2024-01-26 17:29:58 +01:00
|
|
|
main = L.putStrLn $ render $ doc 1 testData
|
|
|
|
|