summaryrefslogtreecommitdiff
path: root/test/lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/lua')
-rw-r--r--test/lua/attr-test.lua6
-rw-r--r--test/lua/block-count.lua11
-rw-r--r--test/lua/hello-world-doc.lua10
-rw-r--r--test/lua/implicit-doc-filter.lua6
-rw-r--r--test/lua/markdown-reader.lua12
-rw-r--r--test/lua/metatable-catch-all.lua20
-rw-r--r--test/lua/plain-to-para.lua6
-rw-r--r--test/lua/script-name.lua3
-rw-r--r--test/lua/single-to-double-quoted.lua10
-rw-r--r--test/lua/smallcaps-title.lua12
-rw-r--r--test/lua/strmacro.lua11
-rw-r--r--test/lua/test-pandoc-utils.lua115
-rw-r--r--test/lua/undiv.lua3
-rw-r--r--test/lua/uppercase-header.lua9
14 files changed, 234 insertions, 0 deletions
diff --git a/test/lua/attr-test.lua b/test/lua/attr-test.lua
new file mode 100644
index 000000000..68dc0012d
--- /dev/null
+++ b/test/lua/attr-test.lua
@@ -0,0 +1,6 @@
+function Div (div)
+ div.attributes.five = ("%d"):format(div.attributes.two + div.attributes.three)
+ div.attributes.two = nil
+ div.attributes.one = "eins"
+ return div
+end
diff --git a/test/lua/block-count.lua b/test/lua/block-count.lua
new file mode 100644
index 000000000..508b05ea8
--- /dev/null
+++ b/test/lua/block-count.lua
@@ -0,0 +1,11 @@
+local num_blocks = 0
+
+function Block(el)
+ num_blocks = num_blocks + 1
+end
+
+function Pandoc(blocks, meta)
+ return pandoc.Pandoc {
+ pandoc.Para{pandoc.Str(num_blocks)}
+ }
+end
diff --git a/test/lua/hello-world-doc.lua b/test/lua/hello-world-doc.lua
new file mode 100644
index 000000000..62236584e
--- /dev/null
+++ b/test/lua/hello-world-doc.lua
@@ -0,0 +1,10 @@
+return {
+ {
+ Pandoc = function(doc)
+ local meta = {}
+ local hello = { pandoc.Str "Hello,", pandoc.Space(), pandoc.Str "World!" }
+ local blocks = { pandoc.Para(hello) }
+ return pandoc.Pandoc(blocks, meta)
+ end
+ }
+}
diff --git a/test/lua/implicit-doc-filter.lua b/test/lua/implicit-doc-filter.lua
new file mode 100644
index 000000000..253462d1c
--- /dev/null
+++ b/test/lua/implicit-doc-filter.lua
@@ -0,0 +1,6 @@
+function Doc (doc)
+ local meta = {}
+ local hello = { pandoc.Str "Hello,", pandoc.Space(), pandoc.Str "World!" }
+ local blocks = { pandoc.Para(hello) }
+ return pandoc.Pandoc(blocks, meta)
+end
diff --git a/test/lua/markdown-reader.lua b/test/lua/markdown-reader.lua
new file mode 100644
index 000000000..1530a15a2
--- /dev/null
+++ b/test/lua/markdown-reader.lua
@@ -0,0 +1,12 @@
+return {
+ {
+ RawBlock = function (elem)
+ if elem.format == "markdown" then
+ local pd = pandoc.read(elem.text, "markdown")
+ return pd.blocks[1]
+ else
+ return elem
+ end
+ end,
+ }
+}
diff --git a/test/lua/metatable-catch-all.lua b/test/lua/metatable-catch-all.lua
new file mode 100644
index 000000000..05df16bbf
--- /dev/null
+++ b/test/lua/metatable-catch-all.lua
@@ -0,0 +1,20 @@
+local num_inlines = 0
+
+function catch_all(el)
+ if el.tag and pandoc.Inline.constructor[el.tag] then
+ num_inlines = num_inlines + 1
+ end
+end
+
+function Pandoc(blocks, meta)
+ return pandoc.Pandoc {
+ pandoc.Para{pandoc.Str(num_inlines)}
+ }
+end
+
+return {
+ setmetatable(
+ {Pandoc = Pandoc},
+ {__index = function(_) return catch_all end}
+ )
+}
diff --git a/test/lua/plain-to-para.lua b/test/lua/plain-to-para.lua
new file mode 100644
index 000000000..aa12a97d3
--- /dev/null
+++ b/test/lua/plain-to-para.lua
@@ -0,0 +1,6 @@
+return {
+ { Plain = function (elem)
+ return pandoc.Para(elem.content)
+ end,
+ }
+}
diff --git a/test/lua/script-name.lua b/test/lua/script-name.lua
new file mode 100644
index 000000000..4b5a223f0
--- /dev/null
+++ b/test/lua/script-name.lua
@@ -0,0 +1,3 @@
+function Para (_)
+ return pandoc.Para{pandoc.Str(PANDOC_SCRIPT_FILE)}
+end
diff --git a/test/lua/single-to-double-quoted.lua b/test/lua/single-to-double-quoted.lua
new file mode 100644
index 000000000..b985b215c
--- /dev/null
+++ b/test/lua/single-to-double-quoted.lua
@@ -0,0 +1,10 @@
+return {
+ {
+ Quoted = function (elem)
+ if elem.quotetype == "SingleQuote" then
+ elem.quotetype = "DoubleQuote"
+ end
+ return elem
+ end,
+ }
+}
diff --git a/test/lua/smallcaps-title.lua b/test/lua/smallcaps-title.lua
new file mode 100644
index 000000000..b839ee131
--- /dev/null
+++ b/test/lua/smallcaps-title.lua
@@ -0,0 +1,12 @@
+return {
+ {
+ Meta = function(meta)
+ -- The call to `MetaInlines` is redundant and used for testing purposes
+ -- only. The explicit use of a MetaValue constructor is only useful when
+ -- used with an empty table: `MetaInlines{}` is read differently than
+ -- `MetaBlocks{}`.
+ meta.title = pandoc.MetaInlines{pandoc.SmallCaps(meta.title)}
+ return meta
+ end
+ }
+}
diff --git a/test/lua/strmacro.lua b/test/lua/strmacro.lua
new file mode 100644
index 000000000..a2711798a
--- /dev/null
+++ b/test/lua/strmacro.lua
@@ -0,0 +1,11 @@
+return {
+ {
+ Str = function (elem)
+ if elem.text == "{{helloworld}}" then
+ return pandoc.Emph {pandoc.Str "Hello, World"}
+ else
+ return elem
+ end
+ end,
+ }
+}
diff --git a/test/lua/test-pandoc-utils.lua b/test/lua/test-pandoc-utils.lua
new file mode 100644
index 000000000..c732d2f85
--- /dev/null
+++ b/test/lua/test-pandoc-utils.lua
@@ -0,0 +1,115 @@
+utils = require 'pandoc.utils'
+
+-- 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
+
+-- 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
+
+function test_pipe ()
+ if not file_exists('/bin/sed') then
+ warn 'Did not find /bin/sed, skipping test'
+ return true
+ end
+ local pipe_result = pandoc.pipe('/bin/sed', {'-e', 's/a/b/'}, 'abc')
+ return pipe_result == 'bbc'
+end
+
+function test_failing_pipe ()
+ if not file_exists('/bin/false') then
+ warn 'Did not find /bin/false, skipping test'
+ return true
+ end
+ local res, err = pcall(pandoc.pipe, '/bin/false', {}, 'abc')
+ return not res and
+ err.command == '/bin/false' and
+ err.error_code == 1 and
+ err.output == ''
+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
+
+-- 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
+
+-- 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
+
+-- normalize_date
+------------------------------------------------------------------------
+function test_normalize_date ()
+ return utils.normalize_date("12/31/2017") == '2017-12-31'
+ and utils.normalize_date("pandoc") == nil
+end
+
+-- Return result
+------------------------------------------------------------------------
+function run(fn)
+ return fn() and "OK" or "FAIL"
+end
+
+function Para (el)
+ return {
+ pandoc.Plain{pandoc.Str("hierarchicalize: " .. run(test_hierarchicalize))},
+ pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
+ 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))},
+ pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
+ pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
+ pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
+ }
+end
diff --git a/test/lua/undiv.lua b/test/lua/undiv.lua
new file mode 100644
index 000000000..1cbb6d30e
--- /dev/null
+++ b/test/lua/undiv.lua
@@ -0,0 +1,3 @@
+function Div(el)
+ return el.content
+end
diff --git a/test/lua/uppercase-header.lua b/test/lua/uppercase-header.lua
new file mode 100644
index 000000000..8e86911c0
--- /dev/null
+++ b/test/lua/uppercase-header.lua
@@ -0,0 +1,9 @@
+local text = require 'text'
+
+local function str_to_uppercase (s)
+ return pandoc.Str(text.upper(s.text))
+end
+
+function Header (el)
+ return pandoc.walk_block(el, {Str = str_to_uppercase})
+end