git.iko.soy/typeable/pandoc
on master help sign in
Improve output of Lua tests (#5499)

This makes use of tasty-lua, a package to write tests in Lua and integrate the results into Tasty output. Test output becomes more informative: individual tests and test groups become visible in test output. Failures are reported with helpful error messages.

commit6208d4e7fcf1792203b3069d0002ad5cb1ec05dd authorAlbert Krewinkel <albert@zeitkraut.de> dateMon 20 May 2019, 18:52 (7 years ago) parent90141e7
8 files changed+202−165
pandoc.cabal+4 stack.yaml+2 test/Tests/Lua.hs+10−26 test/Tests/Lua/Module.hs+27 test/lua/module/pandoc.lua+58 test/lua/module/pandoc.utils.lua+96 test/lua/test-pandoc-utils.lua−138 test/test-pandoc.hs+5−1
pandoc.cabal+4
@@ -339,6 +339,8 @@ extra-source-files:
339339 test/odt/markdown/*.md
340340 test/odt/native/*.native
341341 test/lua/*.lua
342+ test/lua/module/pandoc.lua
343+ test/lua/module/pandoc.utils.lua
342344source-repository head
343345 type: git
344346 location: git://github.com/jgm/pandoc.git
@@ -681,6 +683,7 @@ test-suite test-pandoc
681683 Diff >= 0.2 && < 0.4,
682684 tasty >= 0.11 && < 1.3,
683685 tasty-hunit >= 0.9 && < 0.11,
686+ tasty-lua >= 0.2 && < 0.3,
684687 tasty-quickcheck >= 0.8 && < 0.11,
685688 tasty-golden >= 2.3 && < 2.4,
686689 QuickCheck >= 2.4 && < 2.14,
@@ -699,6 +702,7 @@ test-suite test-pandoc
699702 Tests.Command
700703 Tests.Helpers
701704 Tests.Lua
705+ Tests.Lua.Module
702706 Tests.Shared
703707 Tests.Readers.LaTeX
704708 Tests.Readers.HTML
stack.yaml+2
@@ -14,7 +14,9 @@ extra-deps:
1414- pandoc-citeproc-0.16.2
1515- ipynb-0.1
1616- cmark-gfm-0.2.0
17+- hslua-1.0.3.1
1718- hslua-module-system-0.2.0
19+- tasty-lua-0.2.0
1820ghc-options:
1921 "$locals": -Wall -fno-warn-unused-do-bind -Wincomplete-record-updates -Wnoncanonical-monad-instances -Wnoncanonical-monadfail-instances -Wincomplete-uni-patterns -Widentities -Wcpp-undef -fhide-source-paths
2022resolver: lts-13.17
test/Tests/Lua.hs+10−26
@@ -11,7 +11,7 @@
1111
1212Unit and integration tests for pandoc's Lua subsystem.
1313-}
14−module Tests.Lua ( tests ) where
14+module Tests.Lua ( runLuaTest, tests ) where
1515
1616import Prelude
1717import Control.Monad (when)
@@ -128,29 +128,13 @@ tests = map (localOption (QuickCheckTests 20))
128128 (doc $ divWith ("", [], kv_before) (para "nil"))
129129 (doc $ divWith ("", [], kv_after) (para "nil"))
130130
131− , testCase "Test module pandoc.utils" $
132− assertFilterConversion "pandoc.utils doesn't work as expected."
133− "test-pandoc-utils.lua"
134− (doc $ para "doesn't matter")
135− (doc $ mconcat [ plain (str "blocks_to_inlines: OK")
136− , plain (str "hierarchicalize: OK")
137− , plain (str "normalize_date: OK")
138− , plain (str "pipe: OK")
139− , plain (str "failing pipe: OK")
140− , plain (str "read: OK")
141− , plain (str "failing read: OK")
142− , plain (str "sha1: OK")
143− , plain (str "stringify: OK")
144− , plain (str "to_roman_numeral: OK")
145− ])
146−
147131 , testCase "Script filename is set" $
148132 assertFilterConversion "unexpected script name"
149133 "script-name.lua"
150134 (doc $ para "ignored")
151135 (doc $ para (str $ "lua" </> "script-name.lua"))
152136
153− , testCase "Pandoc version is set" . runLua' $ do
137+ , testCase "Pandoc version is set" . runLuaTest $ do
154138 Lua.getglobal' "table.concat"
155139 Lua.getglobal "PANDOC_VERSION"
156140 Lua.push ("." :: String) -- separator
@@ -158,13 +142,13 @@ tests = map (localOption (QuickCheckTests 20))
158142 Lua.liftIO . assertEqual "pandoc version is wrong" pandocVersion
159143 =<< Lua.peek Lua.stackTop
160144
161− , testCase "Pandoc types version is set" . runLua' $ do
145+ , testCase "Pandoc types version is set" . runLuaTest $ do
162146 let versionNums = versionBranch pandocTypesVersion
163147 Lua.getglobal "PANDOC_API_VERSION"
164148 Lua.liftIO . assertEqual "pandoc-types version is wrong" versionNums
165149 =<< Lua.peek Lua.stackTop
166150
167− , testCase "Allow singleton inline in constructors" . runLua' $ do
151+ , testCase "Allow singleton inline in constructors" . runLuaTest $ do
168152 Lua.liftIO . assertEqual "Not the exptected Emph" (Emph [Str "test"])
169153 =<< Lua.callFunc "pandoc.Emph" (Str "test")
170154 Lua.liftIO . assertEqual "Unexpected element" (Para [Str "test"])
@@ -178,19 +162,19 @@ tests = map (localOption (QuickCheckTests 20))
178162 Lua.peek Lua.stackTop
179163 )
180164
181− , testCase "Elements with Attr have `attr` accessor" . runLua' $ do
165+ , testCase "Elements with Attr have `attr` accessor" . runLuaTest $ do
182166 Lua.push (Div ("hi", ["moin"], [])
183167 [Para [Str "ignored"]])
184168 Lua.getfield Lua.stackTop "attr"
185169 Lua.liftIO . assertEqual "no accessor" (("hi", ["moin"], []) :: Attr)
186170 =<< Lua.peek Lua.stackTop
187171
188− , testCase "module `pandoc.system` is present" . runLua' $ do
172+ , testCase "module `pandoc.system` is present" . runLuaTest $ do
189173 Lua.getglobal' "pandoc.system"
190174 ty <- Lua.ltype Lua.stackTop
191175 Lua.liftIO $ assertEqual "module should be a table" Lua.TypeTable ty
192176
193− , testCase "informative error messages" . runLua' $ do
177+ , testCase "informative error messages" . runLuaTest $ do
194178 Lua.pushboolean True
195179 err <- Lua.peekEither Lua.stackTop
196180 case (err :: Either String Pandoc) of
@@ -212,7 +196,7 @@ roundtripEqual :: (Eq a, Lua.Peekable a, Lua.Pushable a) => a -> IO Bool
212196roundtripEqual x = (x ==) <$> roundtripped
213197 where
214198 roundtripped :: (Lua.Peekable a, Lua.Pushable a) => IO a
215− roundtripped = runLua' $ do
199+ roundtripped = runLuaTest $ do
216200 oldSize <- Lua.gettop
217201 Lua.push x
218202 size <- Lua.gettop
@@ -223,8 +207,8 @@ roundtripEqual x = (x ==) <$> roundtripped
223207 Left e -> error (show e)
224208 Right y -> return y
225209
226−runLua' :: Lua.Lua a -> IO a
227−runLua' op = runIOorExplode $ do
210+runLuaTest :: Lua.Lua a -> IO a
211+runLuaTest op = runIOorExplode $ do
228212 setUserDataDir (Just "../data")
229213 res <- runLua op
230214 case res of
test/Tests/Lua/Module.hsnew file+27
@@ -0,0 +1,27 @@
1+{- |
2+Module : Tests.Lua.Module
3+Copyright : © 2019 Albert Krewinkel
4+License : GNU GPL, version 2 or above
5+
6+Maintainer : Albert Krewinkel <albert@zeitkraut.de>
7+Stability : alpha
8+Portability : portable
9+
10+Lua module tests
11+-}
12+module Tests.Lua.Module (tests) where
13+
14+import System.FilePath ((</>))
15+import Test.Tasty (TestName, TestTree)
16+import Test.Tasty.Lua (testLuaFile)
17+
18+import Tests.Lua (runLuaTest)
19+
20+tests :: [TestTree]
21+tests =
22+ [ testPandocLua "pandoc" ("lua" </> "module" </> "pandoc.lua")
23+ , testPandocLua "pandoc.util" ("lua" </> "module" </> "pandoc.utils.lua")
24+ ]
25+
26+testPandocLua :: TestName -> FilePath -> TestTree
27+testPandocLua = testLuaFile runLuaTest
test/lua/module/pandoc.luanew file+58
@@ -0,0 +1,58 @@
1+local tasty = require 'tasty'
2+
3+local test = tasty.test_case
4+local group = tasty.test_group
5+local assert = tasty.assert
6+
7+function os_is_windows ()
8+ return package.config:sub(1,1) == '\\'
9+end
10+
11+return {
12+ group 'pipe' {
13+ test('external string processing', function ()
14+ if os_is_windows() then
15+ local pipe_result = pandoc.pipe('find', {'hi'}, 'hi')
16+ assert.are_equal('hi', pipe_result:match '%a+')
17+ else
18+ local pipe_result = pandoc.pipe('tr', {'a', 'b'}, 'abc')
19+ assert.are_equal('bbc', pipe_result:match '%a+')
20+ end
21+ end),
22+ test('failing pipe', function ()
23+ if os_is_windows() then
24+ local success, err = pcall(pandoc.pipe, 'find', {'/a'}, 'hi')
25+ assert.is_falsy(success)
26+ assert.are_equal('find', err.command)
27+ assert.is_truthy(err.error_code ~= 0)
28+ else
29+ local success, err = pcall(pandoc.pipe, 'false', {}, 'abc')
30+ assert.is_falsy(success)
31+ assert.are_equal('false', err.command)
32+ assert.are_equal(1, err.error_code)
33+ assert.are_equal('', err.output)
34+ end
35+ end)
36+ },
37+
38+ group 'read' {
39+ test('Markdown', function ()
40+ local valid_markdown = '*Hello*, World!\n'
41+ local expected = pandoc.Pandoc({
42+ pandoc.Para {
43+ pandoc.Emph { pandoc.Str 'Hello' },
44+ pandoc.Str ',',
45+ pandoc.Space(),
46+ pandoc.Str 'World!'
47+ }
48+ })
49+ assert.are_same(expected, pandoc.read(valid_markdown))
50+ end),
51+ test('failing read', function ()
52+ assert.error_matches(
53+ function () pandoc.read('foo', 'nosuchreader') end,
54+ 'Unknown reader: nosuchreader'
55+ )
56+ end)
57+ },
58+}
test/lua/module/pandoc.utils.luanew file+96
@@ -0,0 +1,96 @@
1+local tasty = require 'tasty'
2+local utils = require 'pandoc.utils'
3+
4+local assert = tasty.assert
5+local test = tasty.test_case
6+local group = tasty.test_group
7+
8+return {
9+ group 'blocks_to_inlines' {
10+ test('default separator', function ()
11+ local blocks = {
12+ pandoc.Para { pandoc.Str 'Paragraph1' },
13+ pandoc.Para { pandoc.Emph { pandoc.Str 'Paragraph2' } }
14+ }
15+ local expected = {
16+ pandoc.Str 'Paragraph1',
17+ pandoc.Space(), pandoc.Str '¶', pandoc.Space(),
18+ pandoc.Emph { pandoc.Str 'Paragraph2' }
19+ }
20+ assert.are_same(
21+ expected,
22+ utils.blocks_to_inlines(blocks)
23+ )
24+ end),
25+ test('custom separator', function ()
26+ local blocks = {
27+ pandoc.Para{ pandoc.Str 'Paragraph1' },
28+ pandoc.Para{ pandoc.Emph 'Paragraph2' }
29+ }
30+ local expected = {
31+ pandoc.Str 'Paragraph1',
32+ pandoc.LineBreak(),
33+ pandoc.Emph { pandoc.Str 'Paragraph2' }
34+ }
35+ assert.are_same(
36+ expected,
37+ utils.blocks_to_inlines(blocks, { pandoc.LineBreak() })
38+ )
39+ end)
40+ },
41+
42+ group 'hierarchicalize' {
43+ test('sanity check', function ()
44+ local blks = {
45+ pandoc.Header(1, {pandoc.Str 'First'}),
46+ pandoc.Header(2, {pandoc.Str 'Second'}),
47+ pandoc.Header(2, {pandoc.Str 'Third'}),
48+ }
49+ local hblks = utils.hierarchicalize(blks)
50+ -- cannot create Elements directly; performing only an approximate
51+ -- sanity checking instead of a full equality comparison.
52+ assert.are_equal('Sec', hblks[1].t)
53+ assert.are_equal('Sec', hblks[1].contents[1].t)
54+ assert.are_equal(1, hblks[1].contents[2].numbering[1])
55+ assert.are_equal(2, hblks[1].contents[2].numbering[2])
56+ end)
57+ },
58+
59+ group 'normalize_date' {
60+ test('09 Nov 1989', function ()
61+ assert.are_equal('1989-11-09', utils.normalize_date '09 Nov 1989')
62+ end),
63+ test('12/31/2017', function ()
64+ assert.are_equal('2017-12-31', utils.normalize_date '12/31/2017')
65+ end),
66+ },
67+
68+ group 'sha1' {
69+ test('hashing', function ()
70+ local ref_hash = '0a0a9f2a6772942557ab5355d76af442f8f65e01'
71+ assert.are_equal(ref_hash, utils.sha1 'Hello, World!')
72+ end)
73+ },
74+
75+ group 'stringify' {
76+ test('inlines', function ()
77+ local inline = pandoc.Emph{
78+ pandoc.Str 'Cogito',
79+ pandoc.Space(),
80+ pandoc.Str 'ergo',
81+ pandoc.Space(),
82+ pandoc.Str 'sum.',
83+ }
84+ assert.are_equal('Cogito ergo sum.', utils.stringify(inline))
85+ end)
86+ },
87+
88+ group 'to_roman_numeral' {
89+ test('convertes number', function ()
90+ assert.are_equal('MDCCCLXXXVIII', utils.to_roman_numeral(1888))
91+ end),
92+ test('fails on non-convertible argument', function ()
93+ assert.is_falsy(pcall(utils.to_roman_numeral, 'not a number'))
94+ end)
95+ },
96+}
test/lua/test-pandoc-utils.luadeleted−138
@@ -1,138 +0,0 @@
1−utils = require 'pandoc.utils'
2−
3−-- Squash blocks to inlines
4−------------------------------------------------------------------------
5−function test_blocks_to_inlines ()
6− local blocks = {
7− pandoc.Para{ pandoc.Str 'Paragraph1' },
8− pandoc.Para{ pandoc.Emph 'Paragraph2' }
9− }
10− local inlines = utils.blocks_to_inlines(blocks, {pandoc.LineBreak()})
11− return #inlines == 3
12− and inlines[1].text == "Paragraph1"
13− and inlines[2].t == 'LineBreak'
14− and inlines[3].content[1].text == "Paragraph2"
15−end
16−
17−-- hierarchicalize
18−------------------------------------------------------------------------
19−function test_hierarchicalize ()
20− local blks = {
21− pandoc.Header(1, {pandoc.Str 'First'}),
22− pandoc.Header(2, {pandoc.Str 'Second'}),
23− pandoc.Header(2, {pandoc.Str 'Third'}),
24− }
25− local hblks = utils.hierarchicalize(blks)
26− return hblks[1].t == "Sec"
27− and hblks[1].contents[1].t == "Sec"
28− and hblks[1].contents[2].numbering[1] == 1
29− and hblks[1].contents[2].numbering[2] == 2
30−end
31−
32−-- SHA1
33−------------------------------------------------------------------------
34−function test_sha1 ()
35− local ref_hash = '0a0a9f2a6772942557ab5355d76af442f8f65e01'
36− local hash = utils.sha1 'Hello, World!'
37− return hash == ref_hash
38−end
39−
40−-- Pipe
41−------------------------------------------------------------------------
42−function file_exists (filename)
43− local fh = io.open(filename, 'r')
44− return fh ~= nil and (fh:close() or true)
45−end
46−
47−function warn (...) io.stderr:write(...) end
48−
49−function os_is_windows ()
50− return package.config:sub(1,1) == '\\'
51−end
52−
53−function test_pipe ()
54− if os_is_windows() then
55− local pipe_result = pandoc.pipe('find', {'hi'}, 'hi')
56− return pipe_result:match("%a+") == 'hi'
57− else
58− local pipe_result = pandoc.pipe('tr', {'a', 'b'}, 'abc')
59− return pipe_result:match("%a+") == 'bbc'
60− end
61−end
62−
63−function test_failing_pipe ()
64− if os_is_windows() then
65− local res, err = pcall(pandoc.pipe, 'find', {'/a'}, 'hi')
66− return not res and
67− err.command == 'find' and
68− err.error_code ~= 0
69− else
70− local res, err = pcall(pandoc.pipe, 'false', {}, 'abc')
71− return not res and
72− err.command == 'false' and
73− err.error_code == 1 and
74− err.output == ''
75− end
76−end
77−
78−-- Read
79−------------------------------------------------------------------------
80−function test_read ()
81− local valid_markdown = '*Hello*, World!\n'
82− local res = pandoc.read(valid_markdown).blocks[1].content
83− return res[1].t == 'Emph' and res[3].t == 'Space' and res[4].t == 'Str'
84−end
85−
86−function test_failing_read ()
87− local res, err = pcall(pandoc.read, 'foo', 'nosuchreader')
88− return not res and err:match 'Unknown reader: nosuchreader'
89−end
90−
91−-- Stringify
92−------------------------------------------------------------------------
93−function test_stringify ()
94− local inline = pandoc.Emph{
95− pandoc.Str 'Cogito',
96− pandoc.Space(),
97− pandoc.Str 'ergo',
98− pandoc.Space(),
99− pandoc.Str 'sum.',
100− }
101− return utils.stringify(inline) == 'Cogito ergo sum.'
102−end
103−
104−-- to_roman_numeral
105−------------------------------------------------------------------------
106−function test_to_roman_numeral ()
107− return utils.to_roman_numeral(1888) == 'MDCCCLXXXVIII'
108− -- calling with a string fails
109− and not pcall(utils.to_roman_numeral, 'not a number')
110−end
111−
112−-- normalize_date
113−------------------------------------------------------------------------
114−function test_normalize_date ()
115− return utils.normalize_date("12/31/2017") == '2017-12-31'
116− and utils.normalize_date("pandoc") == nil
117−end
118−
119−-- Return result
120−------------------------------------------------------------------------
121−function run(fn)
122− return fn() and "OK" or "FAIL"
123−end
124−
125−function Para (el)
126− return {
127− pandoc.Plain{pandoc.Str("blocks_to_inlines: " .. run(test_blocks_to_inlines))},
128− pandoc.Plain{pandoc.Str("hierarchicalize: " .. run(test_hierarchicalize))},
129− pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
130− pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
131− pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
132− pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
133− pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
134− pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
135− pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
136− pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
137− }
138−end
test/test-pandoc.hs+5−1
@@ -8,6 +8,7 @@ import GHC.IO.Encoding
88import Test.Tasty
99import qualified Tests.Command
1010import qualified Tests.Lua
11+import qualified Tests.Lua.Module
1112import qualified Tests.Old
1213import qualified Tests.Readers.Creole
1314import qualified Tests.Readers.Docx
@@ -84,7 +85,10 @@ tests pandocPath = testGroup "pandoc tests"
8485 , testGroup "FB2" Tests.Readers.FB2.tests
8586 , testGroup "DokuWiki" Tests.Readers.DokuWiki.tests
8687 ]
87− , testGroup "Lua filters" Tests.Lua.tests
88+ , testGroup "Lua"
89+ [ testGroup "Lua filters" Tests.Lua.tests
90+ , testGroup "Lua modules" Tests.Lua.Module.tests
91+ ]
8892 ]
8993
9094main :: IO ()