File: test-pandoc-lua-marshal.hs

package info (click to toggle)
haskell-pandoc-lua-marshal 0.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 364 kB
  • sloc: haskell: 2,659; makefile: 6
file content (204 lines) | stat: -rw-r--r-- 5,895 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-|
Module      : Main
Copyright   : © 2017-2021 Albert Krewinkel
License     : MIT
Maintainer  : Albert Krewinkel <albert+hslua@zeitkraut.de>

Tests for the pandoc types handling in Lua.
-}
module Main (main) where

import Control.Monad (forM_, when)
import Data.Data (Data, dataTypeConstrs, dataTypeOf, showConstr)
import Data.Proxy (Proxy (Proxy))
import Data.String (fromString)
import HsLua as Lua
import Test.Tasty.QuickCheck (ioProperty, testProperty)
import Test.Tasty (TestTree, defaultMain, testGroup)
import Test.Tasty.Lua (translateResultsFromFile)
import Text.Pandoc.Arbitrary ()
import Text.Pandoc.Definition
import Text.Pandoc.Lua.Marshal.AST

main :: IO ()
main = do
  listAttributeTests <- run @Lua.Exception $ do
    openlibs
    register' mkListAttributes
    registerConstants (Proxy @ListNumberStyle)
    registerConstants (Proxy @ListNumberDelim)
    translateResultsFromFile "test/test-listattributes.lua"

  attrTests <- run @Lua.Exception $ do
    openlibs
    pushListModule *> setglobal "List"
    register' mkAttr
    register' mkAttributeList
    translateResultsFromFile "test/test-attr.lua"

  citationTests <- run @Lua.Exception $ do
    openlibs
    pushListModule *> setglobal "List"
    register' mkCitation
    registerConstants (Proxy @CitationMode)
    forM_ inlineConstructors register'
    translateResultsFromFile "test/test-citation.lua"

  inlineTests <- run @Lua.Exception $ do
    registerDefault
    translateResultsFromFile "test/test-inline.lua"

  blockTests <- run @Lua.Exception $ do
    registerDefault
    translateResultsFromFile "test/test-block.lua"

  cellTests <- run @Lua.Exception $ do
    registerDefault
    translateResultsFromFile "test/test-cell.lua"

  simpleTableTests <- run @Lua.Exception $ do
    registerDefault
    translateResultsFromFile "test/test-simpletable.lua"

  metavalueTests <- run @Lua.Exception $ do
    openlibs
    pushListModule *> setglobal "List"
    forM_ metaValueConstructors register'
    translateResultsFromFile "test/test-metavalue.lua"

  pandocTests <- run @Lua.Exception $ do
    registerDefault
    translateResultsFromFile "test/test-pandoc.lua"

  defaultMain $ testGroup "pandoc-lua-marshal"
    [ roundtrips
    , listAttributeTests
    , attrTests
    , citationTests
    , inlineTests
    , blockTests
    , cellTests
    , simpleTableTests
    , metavalueTests
    , pandocTests
    ]

-- | Registers all constructors and string constants in the global
-- environment.
registerDefault :: LuaError e => LuaE e ()
registerDefault = do
  openlibs
  pushListModule *> setglobal "List"
  register' mkAttr
  register' mkBlocks
  register' mkCell
  register' mkCitation
  register' mkInlines
  register' mkListAttributes
  register' mkPandoc
  register' mkRow
  register' mkSimpleTable
  register' mkTableHead
  register' mkTableFoot
  registerConstants (Proxy @Alignment)
  registerConstants (Proxy @ListNumberStyle)
  registerConstants (Proxy @ListNumberStyle)
  registerConstants (Proxy @MathType)
  registerConstants (Proxy @QuoteType)
  forM_ inlineConstructors register'
  forM_ blockConstructors register'

register' :: LuaError e => DocumentedFunction e -> LuaE e ()
register' f = do
  pushDocumentedFunction f
  setglobal (functionName f)

registerConstants :: forall a e. (Data a, LuaError e) => Proxy a -> LuaE e ()
registerConstants proxy =
  forM_ (constructors proxy) $ \c -> do
    pushString c
    setglobal (fromString c)

constructors :: forall a. Data a => Proxy a -> [String]
constructors _ = map showConstr . dataTypeConstrs . dataTypeOf @a $ undefined

--
-- Roundtrips
--

-- | Basic tests
roundtrips :: TestTree
roundtrips = testGroup "Roundtrip through Lua stack"
  [ testProperty "Alignment" $
    ioProperty . roundtripEqual pushAlignment peekAlignment

  , testProperty "Block" $
    ioProperty . roundtripEqual pushBlock peekBlockFuzzy

  , testProperty "[Block]" $
    ioProperty . roundtripEqual pushBlocks peekBlocksFuzzy

  , testProperty "Caption" $
    ioProperty . roundtripEqual pushCaption peekCaption

  , testProperty "Cell" $
    ioProperty . roundtripEqual pushCell peekCell

  , testProperty "Citation" $
    ioProperty . roundtripEqual pushCitation peekCitation

  , testProperty "CitationMode" $
    ioProperty . roundtripEqual pushCitationMode peekCitationMode

  , testProperty "Inline" $
    ioProperty . roundtripEqual pushInline peekInlineFuzzy

  , testProperty "[Inline]" $
    ioProperty . roundtripEqual pushInlines peekInlinesFuzzy

  , testProperty "ListNumberStyle" $
    ioProperty . roundtripEqual pushListNumberStyle peekListNumberStyle

  , testProperty "ListNumberDelim" $
    ioProperty . roundtripEqual pushListNumberDelim peekListNumberDelim

  , testProperty "MathType" $
    ioProperty . roundtripEqual pushMathType peekMathType

  , testProperty "Meta" $
    ioProperty . roundtripEqual pushMeta peekMeta

  , testProperty "Pandoc" $
    ioProperty . roundtripEqual pushPandoc peekPandoc

  , testProperty "Row" $
    ioProperty . roundtripEqual pushRow peekRow

  , testProperty "QuoteType" $
    ioProperty . roundtripEqual pushQuoteType peekQuoteType

  , testProperty "TableBody" $
    ioProperty . roundtripEqual pushTableBody peekTableBody

  , testProperty "TableHead" $
    ioProperty . roundtripEqual pushTableHead peekTableHead
  ]

roundtripEqual :: forall a. Eq a
               => Pusher Lua.Exception a -> Peeker Lua.Exception a
               -> a -> IO Bool
roundtripEqual pushX peekX x = (x ==) <$> roundtripped
 where
  roundtripped :: IO a
  roundtripped = run $ do
    openlibs
    pushListModule <* pop 1
    oldSize <- gettop
    pushX x
    size <- gettop
    when (size - oldSize /= 1) $
      Prelude.error ("Only one value should have been pushed" ++ show size)
    forcePeek $ peekX top