From 40270bc62f951cfd20916c17e2dfc52863d6b8e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= Date: Mon, 29 Apr 2024 21:42:04 +0200 Subject: [PATCH] ob-lua: Quote list-like strings, sort tables, and parse bare returns * lisp/ob-lua.el (org-babel-lua-multiple-values-separator): Change the default value from ", " to "|" to avoid '\\,' appearing in strings. (org-babel-lua-wrapper-method): Improve 3 aspects: (1) Quote strings with list-like content to prevent Org Babel from incorrectly guessing their type, (2) sort pretty-printed non-sequential tables to make them reproducible, and (3) handle implicit nils produced by 'return' used with no arguments. * testing/lisp/test-ob-lua.el (test-ob-lua/result/nil): (test-ob-lua/result/nil/explicit): (test-ob-lua/result/boolean): (test-ob-lua/results/number/integer): (test-ob-lua/results/number/integer/negative): (test-ob-lua/results/number/integer/multiple): (test-ob-lua/results/number/real): (test-ob-lua/results/number/real/multiple): (test-ob-lua/results/number/infinity): (test-ob-lua/results/string/single-quotes): (test-ob-lua/results/string/double-quotes): (test-ob-lua/results/string/multiple): (test-ob-lua/results/string/list-like): (test-ob-lua/results/string/list-like/brackets): (test-ob-lua/results/string/list-like/curlies): (test-ob-lua/result/table): (test-ob-lua/result/table/pretty-print): (test-ob-lua/result/table/pretty-print/sorted): (test-ob-lua/results/value-separator): New tests. --- lisp/ob-lua.el | 41 ++++-- testing/lisp/test-ob-lua.el | 264 ++++++++++++++++++++++++++++++++---- 2 files changed, 266 insertions(+), 39 deletions(-) diff --git a/lisp/ob-lua.el b/lisp/ob-lua.el index 041abfabc..5a0fdb18b 100644 --- a/lisp/ob-lua.el +++ b/lisp/ob-lua.el @@ -81,7 +81,7 @@ This will typically be `lua-mode'." :package-version '(Org . "8.3") :type 'symbol) -(defcustom org-babel-lua-multiple-values-separator ", " +(defcustom org-babel-lua-multiple-values-separator "|" "Separate multiple values with this string." :group 'org-babel :package-version '(Org . "9.7") @@ -261,26 +261,33 @@ function dump(it, indent) if indent == nil then indent = '' end + if type(it) == 'table' and %s then - local count = 0 - for _ in pairs(it) do - count = count + 1 - end local result = '' + if #indent ~= 0 then result = result .. '\\n' end - for key, value in pairs(it) do + + local keys = {} + for key in pairs(it) do + table.insert(keys, key) + end + + table.sort(keys) + + for index, key in pairs(keys) do + local value = it[key] result = result .. indent .. dump(key) .. ' = ' .. dump(value, indent .. ' ') - count = count - 1 - if count ~= 0 then + if index ~= #keys then result = result .. '\\n' end end + return result else return tostring(it) @@ -288,11 +295,27 @@ function dump(it, indent) end function combine(...) + local quotes = '\"' local result = {} + for index = 1, select('#', ...) do result[index] = dump(select(index, ...)) end - return table.concat(result, '%s') + + if #result == 0 then + return dump(nil) + end + + if #result == 1 then + local value = result[1] + if string.find(value, '[%%(%%[{]') == 1 then + return quotes .. value .. quotes + else + return value + end + end + + return quotes .. table.concat(result, '%s') .. quotes end output = io.open('%s', 'w') diff --git a/testing/lisp/test-ob-lua.el b/testing/lisp/test-ob-lua.el index 0a60c68ca..775a5cf14 100644 --- a/testing/lisp/test-ob-lua.el +++ b/testing/lisp/test-ob-lua.el @@ -136,45 +136,249 @@ return x (org-babel-next-src-block) (org-babel-execute-src-block))))) -(ert-deftest test-ob-lua/types () - "Test returning different types." +(ert-deftest test-ob-lua/result/nil () + "Test returning nothing." (should - (equal "nil" - (org-test-with-temp-text "src_lua{return nil}" - (org-babel-execute-src-block)))) + (equal + "src_lua{return} {{{results(=nil=)}}}" + (org-test-with-temp-text "src_lua{return}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/result/nil/explicit () + "Test returning nothing explicitly." + (should + (equal + "src_lua{return nil} {{{results(=nil=)}}}" + (org-test-with-temp-text "src_lua{return nil}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/result/boolean () + "Test returning the boolean values true and false." + (should + (equal + "src_lua{return true} {{{results(=true=)}}}" + (org-test-with-temp-text "src_lua{return true}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max))))) (should - (equal "true" - (org-test-with-temp-text "src_lua{return true}" - (org-babel-execute-src-block)))) + (equal + "src_lua{return false} {{{results(=false=)}}}" + (org-test-with-temp-text "src_lua{return false}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/number/integer () + "Test returning integers." (should - (equal "false" - (org-test-with-temp-text "src_lua{return false}" - (org-babel-execute-src-block)))) + (equal + "src_lua{return 1} {{{results(=1=)}}}" + (org-test-with-temp-text "src_lua{return 1}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/number/integer/negative () + "Test returning negative integers." (should - (equal 1 - (org-test-with-temp-text "src_lua{return 1}" - (org-babel-execute-src-block)))) + (equal + "src_lua{return -1} {{{results(=-1=)}}}" + (org-test-with-temp-text "src_lua{return -1}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/number/integer/multiple () + "Test returning multiple integers at once." (should - (equal "hello world" - (org-test-with-temp-text "src_lua{return 'hello world'}" - (org-babel-execute-src-block)))) + (equal + "src_lua{return 1, 2, 3} {{{results(=1|2|3=)}}}" + (org-test-with-temp-text "src_lua{return 1, 2, 3}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/number/real () + "Test returning real numbers." (should - (equal 0 - (string-match "table: 0x[0-9A-F]+" - (org-test-with-temp-text "src_lua{return {}}" - (org-babel-execute-src-block)))))) + (equal + "src_lua{return 1.5} {{{results(=1.5=)}}}" + (org-test-with-temp-text "src_lua{return 1.5}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) -(ert-deftest test-ob-lua/multiple-values () - "Test returning multiple values." +(ert-deftest test-ob-lua/results/number/real/multiple () + "Test returning multiple real numbers at once." (should - (equal "1, 2, 3" - (org-test-with-temp-text "src_lua{return 1, 2, 3}" - (org-babel-execute-src-block)))) + (equal + "src_lua{return 1.5, 2.5, 3.5} {{{results(=1.5|2.5|3.5=)}}}" + (org-test-with-temp-text "src_lua{return 1.5, 2.5, 3.5}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/number/infinity () + "Test returning the infinity." + (should + (equal + "src_lua{return 1 / 0} {{{results(=inf=)}}}" + (org-test-with-temp-text "src_lua{return 1 / 0}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/string/single-quotes () + "Test returning strings in single quotes." + (should + (equal + "src_lua{return 'hello world'} {{{results(=hello world=)}}}" + (org-test-with-temp-text "src_lua{return 'hello world'}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/string/double-quotes () + "Test returning strings in double quotes." + (should + (equal + "src_lua{return \"hello world\"} {{{results(=hello world=)}}}" + (org-test-with-temp-text "src_lua{return \"hello world\"}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/string/multiple () + "Test returning multiple strings at once." + (should + (equal + "src_lua{return 'a', 'b'} {{{results(=a|b=)}}}" + (org-test-with-temp-text "src_lua{return 'a', 'b'}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/string/list-like () + "Test returning strings that look like \"(...)\" lists." + (should + (equal + (concat "src_lua{return string.match('A (B) C', '%b()')}" + " {{{results(=(B)=)}}}") + (org-test-with-temp-text + "src_lua{return string.match('A (B) C', '%b()')}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/string/list-like/brackets () + "Test returning strings that look like \"[...]\" lists." + (should + (equal + (concat "src_lua{return string.match('A [B] C', '%b[]')}" + " {{{results(=[B]=)}}}") + (org-test-with-temp-text + "src_lua{return string.match('A [B] C', '%b[]')}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/string/list-like/curlies () + "Test returning strings that look like \"{...}\" lists." + (should + (equal + (concat "src_lua{return string.match('A {B} C', '%b{}')}" + " {{{results(={B}=)}}}") + (org-test-with-temp-text + "src_lua{return string.match('A {B} C', '%b{}')}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/result/table () + "Test returning table references." + (should + (equal + 0 + (string-match + "src_lua{return {}} {{{results(=table: 0x[0-9A-F]+=)}}}" + (org-test-with-temp-text "src_lua{return {}}" + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max))))))) + +(ert-deftest test-ob-lua/result/table/pretty-print () + "Test returning and pretty-printing sequential tables." + (should + (equal (string-join + '("#+BEGIN_SRC lua :results pp" + "return {10, {20, 30, {40, 50}, 60}, 70}" + "#+END_SRC" + "" + "#+RESULTS:" + ": 1 = 10" + ": 2 = " ; FIXME Trailing space. + ": 1 = 20" + ": 2 = 30" + ": 3 = " ; FIXME Trailing space. + ": 1 = 40" + ": 2 = 50" + ": 4 = 60" + ": 3 = 70" + "") + "\n") + (org-test-with-temp-text + (string-join + '("#+BEGIN_SRC lua :results pp" + "return {10, {20, 30, {40, 50}, 60}, 70}" + "#+END_SRC") + "\n") + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/result/table/pretty-print/sorted () + "Test returning and pretty-printing non-sequential tables." + (should + (equal (string-join + '("#+BEGIN_SRC lua :results pp" + "return {b = 20, c = 30, a = 10}" + "#+END_SRC" + "" + "#+RESULTS:" + ;; NOTE The keys are sorted alphabetically. + ": a = 10" + ": b = 20" + ": c = 30" + "") + "\n") + (org-test-with-temp-text + (string-join + '("#+BEGIN_SRC lua :results pp" + "return {b = 20, c = 30, a = 10}" + "#+END_SRC") + "\n") + (org-babel-execute-src-block) + (buffer-substring-no-properties (point-min) + (point-max)))))) + +(ert-deftest test-ob-lua/results/value-separator () + "Test customizing the separator of multiple values." + ;; TODO Once Org Babel supports returning lists from inline blocks, + ;; instead of trapping with the user error: "Inline error: list + ;; result cannot be used", use those for multiple values. (should - (equal "1|2|3" - (let ((org-babel-lua-multiple-values-separator "|")) - (org-test-with-temp-text "src_lua{return 1, 2, 3}" - (org-babel-execute-src-block)))))) + (equal + "src_lua{return 1, 2, 3} {{{results(=1\t2\t3=)}}}" + (org-test-with-temp-text "src_lua{return 1, 2, 3}" + (let ((org-babel-lua-multiple-values-separator "\t")) + (org-babel-execute-src-block)) + (buffer-substring-no-properties (point-min) + (point-max)))))) (provide 'test-ob-lua) -- 2.39.3 (Apple Git-146)