From 299e452463b07f16a434a847612cae1ab7a8132f Mon Sep 17 00:00:00 2001
From: Albert Krewinkel <albert@zeitkraut.de>
Date: Tue, 19 Dec 2017 21:31:30 +0100
Subject: Test more pandoc Lua module functions

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

Change: minor
---
 test/Tests/Lua.hs              | 11 +++++++
 test/lua/test-pandoc-utils.lua | 69 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 test/lua/test-pandoc-utils.lua

(limited to 'test')

diff --git a/test/Tests/Lua.hs b/test/Tests/Lua.hs
index 4f14a834b..0e76249fe 100644
--- a/test/Tests/Lua.hs
+++ b/test/Tests/Lua.hs
@@ -91,6 +91,17 @@ tests = map (localOption (QuickCheckTests 20))
       "attr-test.lua"
       (doc $ divWith ("", [], kv_before) (para "nil"))
       (doc $ divWith ("", [], kv_after) (para "nil"))
+
+  , testCase "Test module pandoc.utils" $
+    assertFilterConversion "pandoc.utils doesn't work as expected."
+      "test-pandoc-utils.lua"
+      (doc $ para "doesn't matter")
+      (doc $ mconcat [ plain (str "sha1: OK")
+                     , plain (str "pipe: OK")
+                     , plain (str "failing pipe: OK")
+                     , plain (str "read: OK")
+                     , plain (str "failing read: OK")
+                     ])
   ]
 
 assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
diff --git a/test/lua/test-pandoc-utils.lua b/test/lua/test-pandoc-utils.lua
new file mode 100644
index 000000000..7354496f9
--- /dev/null
+++ b/test/lua/test-pandoc-utils.lua
@@ -0,0 +1,69 @@
+utils = require 'pandoc'
+
+-- 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 = utils.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(utils.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
+
+
+-- Return result
+------------------------------------------------------------------------
+function run(fn)
+  return fn() and "OK" or "FAIL"
+end
+
+function Para (el)
+  return {
+    pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
+    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))},
+  }
+end
-- 
cgit v1.2.3