git.iko.soy/typeable/pandoc
on master help sign in
Test more pandoc Lua module functions

The functions `sha1`, `read`, and `pipe` are now tested. Change: minor

commit299e452463b07f16a434a847612cae1ab7a8132f authorAlbert Krewinkel <albert@zeitkraut.de> dateTue 19 Dec 2017, 21:31 (9 years ago) parent2b2dae8
2 files changed+80
test/Tests/Lua.hs+11 test/lua/test-pandoc-utils.lua+69
test/Tests/Lua.hs+11
@@ -91,6 +91,17 @@ tests = map (localOption (QuickCheckTests 20))
9191 "attr-test.lua"
9292 (doc $ divWith ("", [], kv_before) (para "nil"))
9393 (doc $ divWith ("", [], kv_after) (para "nil"))
94+
95+ , testCase "Test module pandoc.utils" $
96+ assertFilterConversion "pandoc.utils doesn't work as expected."
97+ "test-pandoc-utils.lua"
98+ (doc $ para "doesn't matter")
99+ (doc $ mconcat [ plain (str "sha1: OK")
100+ , plain (str "pipe: OK")
101+ , plain (str "failing pipe: OK")
102+ , plain (str "read: OK")
103+ , plain (str "failing read: OK")
104+ ])
94105 ]
95106
96107assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
test/lua/test-pandoc-utils.luanew file+69
@@ -0,0 +1,69 @@
1+utils = require 'pandoc'
2+
3+-- SHA1
4+------------------------------------------------------------------------
5+function test_sha1 ()
6+ local ref_hash = '0a0a9f2a6772942557ab5355d76af442f8f65e01'
7+ local hash = utils.sha1 'Hello, World!'
8+ return hash == ref_hash
9+end
10+
11+-- Pipe
12+------------------------------------------------------------------------
13+function file_exists (filename)
14+ local fh = io.open(filename, 'r')
15+ return fh ~= nil and (fh:close() or true)
16+end
17+
18+function warn (...) io.stderr:write(...) end
19+
20+function test_pipe ()
21+ if not file_exists('/bin/sed') then
22+ warn 'Did not find /bin/sed, skipping test'
23+ return true
24+ end
25+ local pipe_result = utils.pipe('/bin/sed', {'-e', 's/a/b/'}, 'abc')
26+ return pipe_result == 'bbc'
27+end
28+
29+function test_failing_pipe ()
30+ if not file_exists('/bin/false') then
31+ warn 'Did not find /bin/false, skipping test'
32+ return true
33+ end
34+ local res, err = pcall(utils.pipe, '/bin/false', {}, 'abc')
35+ return not res and
36+ err.command == '/bin/false' and
37+ err.error_code == 1 and
38+ err.output == ''
39+end
40+
41+-- Read
42+------------------------------------------------------------------------
43+function test_read ()
44+ local valid_markdown = '*Hello*, World!\n'
45+ local res = pandoc.read(valid_markdown).blocks[1].content
46+ return res[1].t == 'Emph' and res[3].t == 'Space' and res[4].t == 'Str'
47+end
48+
49+function test_failing_read ()
50+ local res, err = pcall(pandoc.read, 'foo', 'nosuchreader')
51+ return not res and err:match 'Unknown reader: nosuchreader'
52+end
53+
54+
55+-- Return result
56+------------------------------------------------------------------------
57+function run(fn)
58+ return fn() and "OK" or "FAIL"
59+end
60+
61+function Para (el)
62+ return {
63+ pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
64+ pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
65+ pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
66+ pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
67+ pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
68+ }
69+end