git.iko.soy/typeable/pandoc
on 4e53c7b help sign in
typeable/pandoc/test/lua/test-pandoc-utils.lua
69 lines 2.0 kB history raw
1utils = require 'pandoc'
3-- SHA1
4------------------------------------------------------------------------
5function test_sha1 ()
6 local ref_hash = '0a0a9f2a6772942557ab5355d76af442f8f65e01'
7 local hash = utils.sha1 'Hello, World!'
8 return hash == ref_hash
9end
11-- Pipe
12------------------------------------------------------------------------
13function file_exists (filename)
14 local fh = io.open(filename, 'r')
15 return fh ~= nil and (fh:close() or true)
16end
18function warn (...) io.stderr:write(...) end
20function 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'
27end
29function 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 == ''
39end
41-- Read
42------------------------------------------------------------------------
43function 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'
47end
49function test_failing_read ()
50 local res, err = pcall(pandoc.read, 'foo', 'nosuchreader')
51 return not res and err:match 'Unknown reader: nosuchreader'
52end
55-- Return result
56------------------------------------------------------------------------
57function run(fn)
58 return fn() and "OK" or "FAIL"
59end
61function 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 }
69end