From d9c61349d2cc6b07460fd9078c2ca29aef20b999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= Date: Fri, 14 Jun 2024 22:43:15 +0200 Subject: [PATCH 5/5] ob-lua: Do not confuse list-like strings with lists * lisp/ob-lua.el (org-babel-lua-wrapper-method): Prevent Org Babel from confusing strings that look like lists with actual lists. * testing/lisp/test-ob-lua.el (test-ob-lua/result/none): (test-ob-lua/result/nil): (test-ob-lua/result/nil/multiple): (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/results/string/list-like/multiple): (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): Add new tests. --- lisp/ob-lua.el | 28 +++- testing/lisp/test-ob-lua.el | 289 ++++++++++++++++++++++++++++++++---- 2 files changed, 279 insertions(+), 38 deletions(-) diff --git a/lisp/ob-lua.el b/lisp/ob-lua.el index 10001626a..f7950eadd 100644 --- a/lisp/ob-lua.el +++ b/lisp/ob-lua.el @@ -161,32 +161,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 + local keys = {} for key in pairs(it) do table.insert(keys, key) end + table.sort(keys) - for _, key in pairs(keys) do + + 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 string.gsub(tostring(it), '\"', '\\\"') @@ -195,10 +196,21 @@ end function combine(...) local result = {} + for index = 1, select('#', ...) do result[index] = dump(select(index, ...)) end - return table.concat(result, '%s') + + if #result == 1 then + local value = result[1] + if string.find(value, '[%%(%%[{]') == 1 then + return '\"' .. value .. '\"' + else + return value + end + end + + return '\"' .. table.concat(result, '%s') .. '\"' end output = io.open('%s', 'w') diff --git a/testing/lisp/test-ob-lua.el b/testing/lisp/test-ob-lua.el index 58a120e25..48648d1c7 100644 --- a/testing/lisp/test-ob-lua.el +++ b/testing/lisp/test-ob-lua.el @@ -136,45 +136,274 @@ 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/none () + "Test returning nothing." (should - (equal "nil" - (org-test-with-temp-text "src_lua{return nil}" - (org-babel-execute-src-block)))) + (equal + ;; FIXME Update `ob-core' to output e.g. "{{{results(n/a)}}}" or + ;; "{{{results(/no results/)}}}", for the empty verbatim breaks + ;; e.g. HTML export. + "src_lua{return} {{{results(==)}}}" + (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 () + "Test returning nothing." (should - (equal "true" - (org-test-with-temp-text "src_lua{return true}" - (org-babel-execute-src-block)))) + (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/nil/multiple () + "Test returning multiple nothings." + (should + (equal + "src_lua{return nil, nil} {{{results(=nil\\, nil=)}}}" + (org-test-with-temp-text "src_lua{return nil, 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 "false" - (org-test-with-temp-text "src_lua{return false}" - (org-babel-execute-src-block)))) + (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 1 - (org-test-with-temp-text "src_lua{return 1}" - (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 "hello world" - (org-test-with-temp-text "src_lua{return 'hello world'}" - (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 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} {{{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/multiple-values () - "Test returning multiple values." +(ert-deftest test-ob-lua/results/number/integer/multiple () + "Test returning multiple integers 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, 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 + "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/results/number/real/multiple () + "Test returning multiple real numbers at once." + (should + (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 "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 \"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/results/string/list-like/multiple () + "Test returning multiple strings that look like \"(...)\" lists." + (should + (equal + (concat "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/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 + "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)))))) (ert-deftest test-ob-lua/escaping-quotes () (should -- 2.39.3 (Apple Git-146)