2017-12-22 20:08:51 +01:00
|
|
|
utils = require 'pandoc.utils'
|
2017-12-19 21:31:30 +01:00
|
|
|
|
2018-07-30 19:55:25 +02:00
|
|
|
-- Squash blocks to inlines
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function test_blocks_to_inlines ()
|
|
|
|
local blocks = {
|
|
|
|
pandoc.Para{ pandoc.Str 'Paragraph1' },
|
|
|
|
pandoc.Para{ pandoc.Emph 'Paragraph2' }
|
|
|
|
}
|
|
|
|
local inlines = utils.blocks_to_inlines(blocks, {pandoc.LineBreak()})
|
|
|
|
return #inlines == 3
|
|
|
|
and inlines[1].text == "Paragraph1"
|
|
|
|
and inlines[2].t == 'LineBreak'
|
|
|
|
and inlines[3].content[1].text == "Paragraph2"
|
|
|
|
end
|
|
|
|
|
2017-12-23 22:39:05 +01:00
|
|
|
-- hierarchicalize
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function test_hierarchicalize ()
|
|
|
|
local blks = {
|
|
|
|
pandoc.Header(1, {pandoc.Str 'First'}),
|
|
|
|
pandoc.Header(2, {pandoc.Str 'Second'}),
|
|
|
|
pandoc.Header(2, {pandoc.Str 'Third'}),
|
|
|
|
}
|
|
|
|
local hblks = utils.hierarchicalize(blks)
|
|
|
|
return hblks[1].t == "Sec"
|
|
|
|
and hblks[1].contents[1].t == "Sec"
|
|
|
|
and hblks[1].contents[2].numbering[1] == 1
|
|
|
|
and hblks[1].contents[2].numbering[2] == 2
|
|
|
|
end
|
|
|
|
|
2017-12-19 21:31:30 +01:00
|
|
|
-- SHA1
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function test_sha1 ()
|
|
|
|
local ref_hash = '0a0a9f2a6772942557ab5355d76af442f8f65e01'
|
|
|
|
local hash = utils.sha1 'Hello, World!'
|
|
|
|
return hash == ref_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Pipe
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function file_exists (filename)
|
|
|
|
local fh = io.open(filename, 'r')
|
|
|
|
return fh ~= nil and (fh:close() or true)
|
|
|
|
end
|
|
|
|
|
|
|
|
function warn (...) io.stderr:write(...) end
|
|
|
|
|
2018-05-08 20:07:57 +02:00
|
|
|
function os_is_windows ()
|
|
|
|
return package.config:sub(1,1) == '\\'
|
|
|
|
end
|
|
|
|
|
2017-12-19 21:31:30 +01:00
|
|
|
function test_pipe ()
|
2018-05-08 20:07:57 +02:00
|
|
|
if os_is_windows() then
|
2018-05-08 21:28:12 +02:00
|
|
|
local pipe_result = pandoc.pipe('find', {'hi'}, 'hi')
|
2018-05-09 07:32:44 +02:00
|
|
|
return pipe_result:match("%a+") == 'hi'
|
2018-05-08 20:07:57 +02:00
|
|
|
else
|
2018-05-08 20:51:50 +02:00
|
|
|
local pipe_result = pandoc.pipe('tr', {'a', 'b'}, 'abc')
|
2018-05-09 07:32:44 +02:00
|
|
|
return pipe_result:match("%a+") == 'bbc'
|
2017-12-19 21:31:30 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function test_failing_pipe ()
|
2018-05-08 20:07:57 +02:00
|
|
|
if os_is_windows() then
|
2018-05-08 21:28:12 +02:00
|
|
|
local res, err = pcall(pandoc.pipe, 'find', {'/a'}, 'hi')
|
2018-05-08 20:07:57 +02:00
|
|
|
return not res and
|
2018-05-08 21:28:12 +02:00
|
|
|
err.command == 'find' and
|
|
|
|
err.error_code ~= 0
|
2018-05-08 20:07:57 +02:00
|
|
|
else
|
|
|
|
local res, err = pcall(pandoc.pipe, 'false', {}, 'abc')
|
|
|
|
return not res and
|
|
|
|
err.command == 'false' and
|
|
|
|
err.error_code == 1 and
|
|
|
|
err.output == ''
|
2017-12-19 21:31:30 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Read
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function test_read ()
|
|
|
|
local valid_markdown = '*Hello*, World!\n'
|
|
|
|
local res = pandoc.read(valid_markdown).blocks[1].content
|
|
|
|
return res[1].t == 'Emph' and res[3].t == 'Space' and res[4].t == 'Str'
|
|
|
|
end
|
|
|
|
|
|
|
|
function test_failing_read ()
|
|
|
|
local res, err = pcall(pandoc.read, 'foo', 'nosuchreader')
|
|
|
|
return not res and err:match 'Unknown reader: nosuchreader'
|
|
|
|
end
|
|
|
|
|
2017-12-22 20:08:51 +01:00
|
|
|
-- Stringify
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function test_stringify ()
|
|
|
|
local inline = pandoc.Emph{
|
|
|
|
pandoc.Str 'Cogito',
|
|
|
|
pandoc.Space(),
|
|
|
|
pandoc.Str 'ergo',
|
|
|
|
pandoc.Space(),
|
|
|
|
pandoc.Str 'sum.',
|
|
|
|
}
|
|
|
|
return utils.stringify(inline) == 'Cogito ergo sum.'
|
|
|
|
end
|
2017-12-19 21:31:30 +01:00
|
|
|
|
2017-12-23 11:53:26 +01:00
|
|
|
-- to_roman_numeral
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function test_to_roman_numeral ()
|
|
|
|
return utils.to_roman_numeral(1888) == 'MDCCCLXXXVIII'
|
|
|
|
-- calling with a string fails
|
|
|
|
and not pcall(utils.to_roman_numeral, 'not a number')
|
|
|
|
end
|
|
|
|
|
2017-12-23 13:35:27 +01:00
|
|
|
-- normalize_date
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function test_normalize_date ()
|
|
|
|
return utils.normalize_date("12/31/2017") == '2017-12-31'
|
|
|
|
and utils.normalize_date("pandoc") == nil
|
|
|
|
end
|
|
|
|
|
2017-12-19 21:31:30 +01:00
|
|
|
-- Return result
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
function run(fn)
|
|
|
|
return fn() and "OK" or "FAIL"
|
|
|
|
end
|
|
|
|
|
|
|
|
function Para (el)
|
|
|
|
return {
|
2018-07-30 19:55:25 +02:00
|
|
|
pandoc.Plain{pandoc.Str("blocks_to_inlines: " .. run(test_blocks_to_inlines))},
|
2017-12-23 22:39:05 +01:00
|
|
|
pandoc.Plain{pandoc.Str("hierarchicalize: " .. run(test_hierarchicalize))},
|
2017-12-23 13:35:27 +01:00
|
|
|
pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
|
2017-12-19 21:31:30 +01:00
|
|
|
pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
|
|
|
|
pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
|
|
|
|
pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
|
|
|
|
pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
|
2017-12-23 13:35:27 +01:00
|
|
|
pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
|
2017-12-22 20:08:51 +01:00
|
|
|
pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
|
2017-12-23 11:53:26 +01:00
|
|
|
pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
|
2017-12-19 21:31:30 +01:00
|
|
|
}
|
|
|
|
end
|