Browse Source

ob-lua.el: Update library for Org 9.0

* lisp/ob-lua.el (org-babel-lua-evaluate-external-process):
(org-babel-lua-evaluate-session): Update for Org 9.0 compatibility.

* testing/test-ob-lua.el: New file.

TINYCHANGE
thibault 3 years ago
parent
commit
2090efaed7
2 changed files with 166 additions and 25 deletions
  1. 25 25
      lisp/ob-lua.el
  2. 141 0
      testing/lisp/test-ob-lua.el

+ 25 - 25
lisp/ob-lua.el

@@ -1,6 +1,6 @@
 ;;; ob-lua.el --- Org Babel functions for Lua evaluation
 
-;; Copyright (C) 2014 Free Software Foundation, Inc.
+;; Copyright (C) 2014, 2016 Free Software Foundation, Inc.
 
 ;; Authors: Dieter Schoen
 ;; Keywords: literate programming, reproducible research
@@ -128,7 +128,7 @@ VARS contains resolved variable references"
      (format "%s=%s"
 	     (car pair)
 	     (org-babel-lua-var-to-lua (cdr pair))))
-   (mapcar #'cdr (org-babel-get-header params :var))))
+   (org-babel--get-vars params)))
 
 (defun org-babel-lua-var-to-lua (var)
   "Convert an elisp value to a lua variable.
@@ -288,30 +288,30 @@ If RESULT-TYPE equals 'output then return standard output as a
 string.  If RESULT-TYPE equals 'value then return the value of the
 last statement in BODY, as elisp."
   (let ((raw
-         (case result-type
-           (output (org-babel-eval org-babel-lua-command
-                                   (concat (if preamble (concat preamble "\n"))
-                                           body)))
-           (value (let ((tmp-file (org-babel-temp-file "lua-")))
-                    (org-babel-eval
-                     org-babel-lua-command
-                     (concat
-                      (if preamble (concat preamble "\n") "")
-                      (format
-                       (if (member "pp" result-params)
-                           org-babel-lua-pp-wrapper-method
-                         org-babel-lua-wrapper-method)
-                       (mapconcat
-                        (lambda (line) (format "\t%s" line))
-                        (split-string
-                         (org-remove-indentation
-                          (org-babel-trim body))
-                         "[\r\n]") "\n")
-                       (org-babel-process-file-name tmp-file 'noquote))))
-                    (org-babel-eval-read-file tmp-file))))))
+         (pcase result-type
+           (`output (org-babel-eval org-babel-lua-command
+				    (concat (if preamble (concat preamble "\n"))
+					    body)))
+           (`value (let ((tmp-file (org-babel-temp-file "lua-")))
+		     (org-babel-eval
+		      org-babel-lua-command
+		      (concat
+		       (if preamble (concat preamble "\n") "")
+		       (format
+			(if (member "pp" result-params)
+			    org-babel-lua-pp-wrapper-method
+			  org-babel-lua-wrapper-method)
+			(mapconcat
+			 (lambda (line) (format "\t%s" line))
+			 (split-string
+			  (org-remove-indentation
+			   (org-trim body))
+			  "[\r\n]") "\n")
+			(org-babel-process-file-name tmp-file 'noquote))))
+		     (org-babel-eval-read-file tmp-file))))))
     (org-babel-result-cond result-params
       raw
-      (org-babel-lua-table-or-string (org-babel-trim raw)))))
+      (org-babel-lua-table-or-string (org-trim raw)))))
 
 (defun org-babel-lua-evaluate-session
     (session body &optional result-type result-params)
@@ -365,7 +365,7 @@ fd:close()"
           (case result-type
             (output
              (mapconcat
-              #'org-babel-trim
+              #'org-trim
               (butlast
                (org-babel-comint-with-output
                    (session org-babel-lua-eoe-indicator t body)

+ 141 - 0
testing/lisp/test-ob-lua.el

@@ -0,0 +1,141 @@
+;;; test-ob-lua.el --- tests for ob-lua.el
+
+;; Copyright (c) 2016 Thibault Marin
+;; Authors: Thibault Marin
+
+;; This file is not part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+(unless (featurep 'ob-lua)
+  (signal 'missing-test-dependency "Support for Lua code blocks"))
+
+(ert-deftest test-ob-lua/simple-value ()
+  "Test associative array return by value."
+  (should
+   (= 2
+      (org-test-with-temp-text
+	  "#+name: eg
+| a   | 1 |
+| b   | 2 |
+
+#+header: :results value
+#+header: :var x = eg
+#+begin_src lua
+return x['b']
+#+end_src"
+        (org-babel-next-src-block)
+        (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-lua/simple-output ()
+  "Test text output from table."
+  (should
+   (equal "result: c\n"
+	  (org-test-with-temp-text
+	      "#+name: eg
+| a | b | c | d |
+
+#+header: :results output
+#+header: :var x = eg
+#+begin_src lua
+print('result: ' .. x[1][3])
+#+end_src"
+	    (org-babel-next-src-block)
+	    (org-babel-execute-src-block)))))
+
+
+(ert-deftest test-ob-lua/colnames-yes-header-argument ()
+  "Test table passing with `colnames' header."
+  (should
+   (equal "a"
+	  (org-test-with-temp-text
+	      "#+name: eg
+| col |
+|-----|
+| a   |
+| b   |
+
+#+header: :colnames yes
+#+header: :var x = eg
+#+begin_src lua
+return x[1]
+#+end_src"
+	    (org-babel-next-src-block)
+	    (org-babel-execute-src-block)))))
+
+
+(ert-deftest test-ob-lua/colnames-yes-header-argument-pp ()
+  "Test table passing with `colnames' header and pp option."
+  (should
+   (equal "a = 12\nb = 13\n"
+	  (org-test-with-temp-text
+	      "#+name: eg
+| col | val |
+|-----+-----|
+| a   |  12 |
+| b   |  13 |
+
+#+header: :results value pp
+#+header: :colnames yes
+#+header: :var x = eg
+#+begin_src lua
+return x
+#+end_src"
+	    (org-babel-next-src-block)
+	    (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-lua/colnames-nil-header-argument ()
+  "Test table with `colnames' set to `nil'."
+  (should
+   (equal "1 = a\n2 = b\n"
+	  (org-test-with-temp-text
+	      "#+name: eg
+| col |
+|-----|
+| a   |
+| b   |
+
+#+header: :colnames nil
+#+header: :var x = eg
+#+header: :results value pp
+#+begin_src lua
+return x
+#+end_src"
+	    (org-babel-next-src-block)
+	    (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-lua/colnames-no-header-argument ()
+  "Test table passing without `colnames'."
+  (should
+   (equal "1 = col\n2 = a\n3 = b\n"
+	  (org-test-with-temp-text
+	      "#+name: eg
+| col |
+|-----|
+| a   |
+| b   |
+
+#+header: :colnames no
+#+header: :var x = eg
+#+header: :results value pp
+#+begin_src lua
+return x
+#+end_src"
+	    (org-babel-next-src-block)
+	    (org-babel-execute-src-block)))))
+
+(provide 'test-ob-lua)
+
+;;; test-ob-lua.el ends here