git.iko.soy/typeable/pandoc
on 790dc25 help sign in
typeable/pandoc/test/lua/test-pandoc-utils.lua
99 lines 3.1 kB history raw
1utils = require 'pandoc.utils'
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 = pandoc.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(pandoc.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
54-- Stringify
55------------------------------------------------------------------------
56function test_stringify ()
57 local inline = pandoc.Emph{
58 pandoc.Str 'Cogito',
59 pandoc.Space(),
60 pandoc.Str 'ergo',
61 pandoc.Space(),
62 pandoc.Str 'sum.',
63 }
64 return utils.stringify(inline) == 'Cogito ergo sum.'
65end
67-- to_roman_numeral
68------------------------------------------------------------------------
69function test_to_roman_numeral ()
70 return utils.to_roman_numeral(1888) == 'MDCCCLXXXVIII'
71 -- calling with a string fails
72 and not pcall(utils.to_roman_numeral, 'not a number')
73end
75-- normalize_date
76------------------------------------------------------------------------
77function test_normalize_date ()
78 return utils.normalize_date("12/31/2017") == '2017-12-31'
79 and utils.normalize_date("pandoc") == nil
80end
82-- Return result
83------------------------------------------------------------------------
84function run(fn)
85 return fn() and "OK" or "FAIL"
86end
88function Para (el)
89 return {
90 pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
91 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
92 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
93 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
94 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
95 pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
96 pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
97 pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
98 }
99end