From 0fa579016222f3bf90672e9700d6b9b4eecd5c2c Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 25 Mar 2023 09:59:31 +0100 Subject: [PATCH 05/13] lisp/ob-haskell: Request the last value from GHCi * lisp/ob-haskell.el (org-babel-interpret-haskell): When the result type is 'value, use the last value as defined by GHCi. * testing/lisp/test-ob-haskell-ghci.el: Update tests related to output/value. --- lisp/ob-haskell.el | 32 ++++++++++++++++++++++------ testing/lisp/test-ob-haskell-ghci.el | 6 ++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index 500be89a2..961ae9c8a 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -135,12 +135,32 @@ (defun org-babel-interpret-haskell (body params) (session (org-babel-haskell-initiate-session session params)) (comint-preoutput-filter-functions (cons 'ansi-color-filter-apply comint-preoutput-filter-functions)) - (raw (org-babel-comint-with-output - (session org-babel-haskell-eoe nil full-body) - (insert (org-trim full-body)) - (comint-send-input nil t) - (insert org-babel-haskell-eoe) - (comint-send-input nil t))) + (raw (pcase result-type + (`output + (org-babel-comint-with-output + (session org-babel-haskell-eoe nil full-body) + (insert (org-trim full-body)) + (comint-send-input nil t) + (insert (concat "putStrLn (\"\\\"\" ++ " org-babel-haskell-eoe " ++ \"\\\"\")\n")) + (comint-send-input nil t))) + (`value (org-babel-comint-with-output + (session org-babel-haskell-eoe nil full-body) + (insert "__LAST_VALUE_IMPROBABLE_NAME__=()::()\n") + (comint-send-input nil t) + (insert full-body) + (comint-send-input nil t) + (insert "__LAST_VALUE_IMPROBABLE_NAME__=it\n") + (comint-send-input nil t) + (insert (concat "putStrLn (\"\\\"\" ++ " org-babel-haskell-eoe " ++ \"\\\"\")\n")) + (comint-send-input nil t)) + (org-babel-comint-with-output + (session org-babel-haskell-eoe nil) + (insert "__LAST_VALUE_IMPROBABLE_NAME__\n") + (comint-send-input nil t) + (insert (concat "putStrLn (\"\\\"\" ++ " org-babel-haskell-eoe " ++ \"\\\"\")\n")) + (comint-send-input nil t)) + ) + )) (results (mapcar #'org-strip-quotes (cdr (member org-babel-haskell-eoe (reverse (mapcar #'org-trim raw))))))) diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el index 3663f21f9..2ddfb4de7 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -113,8 +113,8 @@ (ert-deftest ob-haskell/hello-world-output () (test-ob-haskell-ghci ":results output" "putStrLn \"Hello World!\"")))) (ert-deftest ob-haskell/hello-world-output-nothing () - :expected-result :failed - (should (equal "" + ;; GHCi prints the value on standard output. So, the last value is part of the output. + (should (equal "Hello World!" (test-ob-haskell-ghci ":results output" "return \"Hello World!\"")))) (ert-deftest ob-haskell/hello-world-output-multilines () @@ -416,12 +416,10 @@ (ert-deftest ob-haskell/results-value-2 () (ert-deftest ob-haskell/results-value-3 () "Don't confuse output and values: nothing." - :expected-result :failed (should (equal nil (test-ob-haskell-ghci ":results value" "putStrLn \"3\"")))) (ert-deftest ob-haskell/results-value-4 () "Don't confuse output and values: nothing." - :expected-result :failed (should (equal nil (test-ob-haskell-ghci ":results value" " putStrLn \"3\" return () -- 2.39.3