all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Bruno Barbier <brubar.cs@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: [PATCH] Add tests for ob-haskell (GHCi)
Date: Sat, 25 Mar 2023 11:01:40 +0100	[thread overview]
Message-ID: <641ec686.050a0220.16dbc.a6ca@mx.google.com> (raw)
In-Reply-To: <87r0texwor.fsf@localhost>

[-- Attachment #1: Type: text/plain, Size: 1242 bytes --]

Ihor Radchenko <yantar92@posteo.net> writes:

> I investigated further and now applied a set of patches that improves
> prompt filtering in org-comint.
> ..

Thanks Ihor.

I don't get random failures anymore.  And thanks for fixing the tests
that were incorrect.

Thanks Pareto for mentioning the possibiliy to customize GHCi prompts.



> The remaining tests fall into two categories:
> 1. Tests trying to test :results value vs. :results output in sessions.
> 2. Tests trying to test for multiple sessions not interfering each
>    other.

About :results output vs :results value, I decided to use the "it"
variable, i.e. to ask GHCi what was the last value (see [1]).

About sessions, I decided to rename the buffer as you suggested. That's
a partial workaround that should work well enough, until haskell-mode
provides a way to choose the buffer name.

I've updated the tests and the expected results.

Note that I've changed the tests about errors; I'm now expecting
ob-haskell to raise errors. I'm not sure what we should expect to be
consistent with other org babel backends.

I also did some modifications in my initial patch in-place.

Bruno


[1]:https://downloads.haskell.org/ghc/latest/docs/users_guide/ghci.html#the-it-variable


[-- Attachment #2: 0001-ob-haskell-Add-tests-for-GHCi.patch --]
[-- Type: text/x-patch, Size: 15043 bytes --]

From 46e8fa78574908a15fe6eb82a2cca5d2f537c78e Mon Sep 17 00:00:00 2001
From: Bruno BARBIER <brubar.cs@gmail.com>
Date: Fri, 18 Nov 2022 20:14:20 +0100
Subject: [PATCH 1/7] ob-haskell: Add tests for GHCi

testing/lisp/test-ob-haskell-ghci.el: New file.
---
 testing/lisp/test-ob-haskell-ghci.el | 453 +++++++++++++++++++++++++++
 1 file changed, 453 insertions(+)
 create mode 100644 testing/lisp/test-ob-haskell-ghci.el

diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
new file mode 100644
index 000000000..aba94d73f
--- /dev/null
+++ b/testing/lisp/test-ob-haskell-ghci.el
@@ -0,0 +1,453 @@
+;;; test-ob-haskell-ghci.el --- tests for ob-haskell.el GHCi  -*- lexical-binding: t; -*-
+
+;; Copyright (c) 2023  Free Software Foundation, Inc.
+
+;; Authors: Bruno BARBIER <brubar.cs@gmail.com>
+
+;; 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 <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+
+;;;; Useful references
+;;
+;;  - https://orgmode.org/worg/org-contrib/babel/languages/lang-compat.html
+;;  - GHCi manual: https://downloads.haskell.org/ghc/latest/docs/users_guide/ghci.html
+;;;; FIXME: Random failures
+;;
+;; To increase the chances of failure when running tests, you can use this command line:
+;;
+;;    (for I in 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10; do make 'BTEST_OB_LANGUAGES=haskell' BTEST_RE='haskell' test-dirty & done) 2>&1 | grep FAILED
+;;
+
+;;;; Status
+;;
+;; All the tests should succeed (except for random failures); those
+;; flagged with ":expected-result :failed" are known
+;; limitations/bugs.  Tested with (2023-03-18):
+;;
+;;     | emacs-version |                      29.0.60 |
+;;     | org-version   | main@4cad6c8ea (Mar 16 2023) |
+;;     | haskell-mode  | master@20d4e23 (Mar 4  2023) |
+;;     | ghci          |                        9.0.2 |
+
+
+;;; Code:
+;;
+
+(require 'org-test "../testing/org-test")
+(org-test-for-executable "ghci")
+(unless (featurep 'haskell-mode)
+  (signal 'missing-test-dependency "haskell-mode"))
+
+
+;;; Helpers
+;;
+
+(defun test-ob-haskell-ghci--with-global-session-worker (todo)
+  "See `test-ob-haskell-ghci--with-global-session-worker'."
+  (when (get-buffer "*haskell*")
+    (error "A buffer named '*haskell*' exists.  Can't safely test haskell blocks"))
+  (unwind-protect (funcall todo)
+    ;; Kill the "*haskell*" buffer to not pollute other tests.
+    (when-let ((hb (get-buffer "*haskell*")))
+      (with-current-buffer hb
+        (let ((kill-buffer-query-functions nil)
+              (kill-buffer-hook nil))
+          (kill-buffer hb))))))
+
+(defmacro test-ob-haskell-ghci-with-global-session (&rest body)
+  "Eval BODY in a new session, then destroy the session.
+The library ob-haskell doesn't implement session yet.  It will
+always use a buffer named \"*haskell*\".  We kill that buffer
+after the source block execution.  To be safe, we fail if such a
+buffer already exists."
+  `(test-ob-haskell-ghci--with-global-session-worker (lambda () ,@body)))
+
+(defun test-ob-haskell-ghci (args content &optional preamble unprotected)
+  "Execute the code block CONTENT in a new GHCi session; return the result.
+Add ARGS to the code block argument line.  Insert PREAMBLE
+before the code block.  When UNPROTECTED is non-nil, don't control
+which session is used (i.e. don't call
+`test-ob-haskell-ghci--with-global-session-worker')."
+  (when (listp content)
+    (setq content (string-join content "\n")))
+  (unless preamble
+    (setq preamble ""))
+  (let ((todo  (lambda ()
+                 (org-test-with-temp-text
+                     (concat preamble "\n" "#+begin_src haskell :compile no "
+                             args "\n" "<point>" content "\n#+end_src")
+                   (org-babel-execute-src-block)))))
+  (if unprotected (funcall todo)
+    (test-ob-haskell-ghci-with-global-session (funcall todo)))))
+
+
+;;; Tests
+
+
+;;;; Hello Worlds.
+;;
+
+(ert-deftest ob-haskell/hello-world-value-pure ()
+  (should (equal "Hello World!"
+                 (test-ob-haskell-ghci "" "\"Hello World!\""))))
+
+(ert-deftest ob-haskell/hello-world-value-IO ()
+  (should (equal "Hello World!"
+                 (test-ob-haskell-ghci "" "return \"Hello World!\""))))
+
+(ert-deftest ob-haskell/hello-world-output ()
+  (should (equal "Hello World!"
+                 (test-ob-haskell-ghci ":results output" "putStrLn \"Hello World!\""))))
+
+(ert-deftest ob-haskell/hello-world-output-nothing ()
+  :expected-result :failed
+  (should (equal ""
+                 (test-ob-haskell-ghci ":results output" "return \"Hello World!\""))))
+
+(ert-deftest ob-haskell/hello-world-output-multilines ()
+  :expected-result :failed
+  (should (equal "Hello World!"
+                 (test-ob-haskell-ghci ":results output" "
+:{
+main :: IO ()
+main = putStrLn \"Hello World!\"
+:}
+
+main
+"))))
+
+;;;; Sessions
+;;
+
+(ert-deftest ob-haskell/sessions-must-not-share-variables ()
+  "Sessions must not share variables."
+  :expected-result :failed
+  (test-ob-haskell-ghci-with-global-session
+   (test-ob-haskell-ghci ":session s1" "x=2" nil :unprotected)
+   (should (equal 2 (test-ob-haskell-ghci ":session s1" "x" nil :unprotected)))
+   (test-ob-haskell-ghci ":session s2" "x=3" nil :unprotected)
+   (should-not (equal 3 (test-ob-haskell-ghci ":session s1" "x" nil :unprotected)))
+   ))
+
+(ert-deftest ob-haskell/no-session-means-one-shot-sessions ()
+  "When no session, use a new session."
+  :expected-result :failed
+  (test-ob-haskell-ghci-with-global-session
+   (test-ob-haskell-ghci "" "x=2" nil :unprotected)
+   (should-not (equal 2 (test-ob-haskell-ghci "" "x" nil :unprotected)))))
+
+
+;;;; Values
+;;
+
+(ert-deftest ob-haskell/value-is-the-last-expression ()
+  "Return the value of the last expression."
+  (should (equal 3 (test-ob-haskell-ghci "" '("1" "1+1" "1+1+1"))))
+  (should (equal 3 (test-ob-haskell-ghci "" '("x=1" "y=1+1" "x+y")))))
+
+(ert-deftest ob-haskell/value-is-the-last-expression-2 ()
+  "Return the value of the last expression."
+  (should (equal 7 (test-ob-haskell-ghci "" "
+putStrLn \"a string\"
+return \"useless\"
+3+4
+"))))
+
+
+
+(ert-deftest ob-haskell/eval-numbers ()
+  "Evaluation of numbers."
+  (should (equal 7 (test-ob-haskell-ghci "" "7")))
+  (should (equal 7.5 (test-ob-haskell-ghci "" "7.5")))
+  (should (equal 10.0 (test-ob-haskell-ghci "" "10::Double")))
+  (should (equal 10   (test-ob-haskell-ghci "" "10::Int"))))
+
+
+(ert-deftest ob-haskell/eval-strings ()
+  "Evaluation of strings."
+  (should (equal "a string" (test-ob-haskell-ghci "" "\"a string\""))))
+
+
+;;;; Local variables
+(ert-deftest ob-haskell/let-one-line ()
+  "Local definitions on one line."
+  (should (equal 6 (test-ob-haskell-ghci "" "let { x=2; y=3 } in x*y"))))
+
+(ert-deftest ob-haskell/let-multilines-1 ()
+  "Local definitions on multiple lines."
+  :expected-result :failed
+  (should (equal 6 (test-ob-haskell-ghci "" "
+:{
+ let { x=2
+     ; y=3
+     }
+ in x*y
+:}
+"))))
+
+(ert-deftest ob-haskell/let-multilines-2 ()
+  "Local definitions on multiple lines, relying on indentation."
+  :expected-result :failed
+  (should (equal 6 (test-ob-haskell-ghci "" "
+:{
+  let x=2
+      y=3
+  in x*y
+:}
+"))))
+
+;;;; Declarations with multiple lines.
+(ert-deftest ob-haskell/decl-multilines-1 ()
+  "A multiline declaration, then use it."
+  (should (equal 3 (test-ob-haskell-ghci "" "
+:{
+let length' []    = 0
+    length' (_:l) = 1 + length' l
+:}
+length' [1,2,3]
+"))))
+
+(ert-deftest ob-haskell/decl-multilines-2 ()
+  "A multiline declaration, then use it."
+  (should (equal 5 (test-ob-haskell-ghci "" "
+:{
+length'       :: [a] -> Int
+length' []    =  0
+length' (_:l) =  1 + length' l
+:}
+
+length' [1..5]
+"))))
+
+
+(ert-deftest ob-haskell/primes ()
+  "From haskell.org."""
+  :expected-result :failed
+  (should (equal '(2 3 5 7 11 13 17 19 23 29)
+                 (test-ob-haskell-ghci "" "
+:{
+primes = filterPrime [2..] where
+  filterPrime (p:xs) =
+    p : filterPrime [x | x <- xs, x `mod` p /= 0]
+:}
+
+take 10 primes
+"))))
+
+;;;; Lists
+;;
+
+(ert-deftest ob-haskell/a-simple-list ()
+  "Evaluation of list of values."
+  (should (equal '(1 2 3) (test-ob-haskell-ghci "" "[1,2,3]"))))
+
+
+(ert-deftest ob-haskell/2D-lists ()
+  "Evaluation of nested lists into a table."
+  (should (equal '((1 2 3) (4 5 6))
+                 (test-ob-haskell-ghci "" "[[1..3], [4..6]]"))))
+
+(ert-deftest ob-haskell/2D-lists-multilines ()
+  "Evaluation of nested lists into a table, as multilines."
+  :expected-result :failed
+  (should (equal '((1 2 3) (4 5 6))
+                 (test-ob-haskell-ghci "" "
+:{
+[ [1..3]
+, [4..6]
+, [7..9]
+]
+:}
+"))))
+
+
+;;;; Tuples
+;;
+
+(ert-deftest ob-haskell/a-simple-tuple ()
+  "Evaluation of tuple of values."
+  (should (equal '(1 2 3) (test-ob-haskell-ghci "" "(1,2,3)"))))
+
+
+(ert-deftest ob-haskell/2D-tuples ()
+  "Evaluation of nested tuples into a table."
+  (should (equal '((1 2 3) (4 5 6))
+                 (test-ob-haskell-ghci "" "((1,2,3), (4,5,6))"))))
+
+(ert-deftest ob-haskell/2D-tuples-multilines ()
+  "Evaluation of nested tuples into a table, as multilines."
+  (should (equal '((1 2 3) (4 5 6) (7 8 9))
+                 (test-ob-haskell-ghci "" "
+:{
+( (1,2,3)
+, (4,5,6)
+, (7,8,9)
+)
+:}
+"))))
+
+
+;;;; Data tables
+;;
+
+(ert-deftest ob-haskell/int-table-data ()
+  "From worg: int-table-data."
+  (should (equal 10 (test-ob-haskell-ghci ":var t=int-table-data"
+                                          "sum [sum r | r <- t]"
+                                          "#+name: int-table-data
+    | 1 | 2 |
+    | 3 | 4 |"))))
+
+(ert-deftest ob-haskell/float-table-data ()
+  "From worg: float-table-data."
+  (should (equal 11.0 (test-ob-haskell-ghci ":var t=float-table-data"
+                                            "sum [sum r | r <- t]"
+                                            "#+name: float-table-data
+    | 1.1 | 2.2 |
+    | 3.3 | 4.4 |"))))
+
+(ert-deftest ob-haskell/string-table-data ()
+  "From worg: string-table-data."
+  (should (equal "abcd" (test-ob-haskell-ghci ":var t=string-table-data"
+                                              "concat [concat r | r <- t]"
+                                              "#+name: string-table-data
+    | a | b |
+    | c | d |"))))
+
+;;;; Reuse results
+;;
+(ert-deftest ob-haskell/reuse-table ()
+  "Reusing a computed tables."
+  (should (equal 78 (test-ob-haskell-ghci ":var t=a-table"
+                                          "sum [sum r | r <- t]"
+                                          "#+name: a-table
+#+begin_src haskell
+   [ [x..x+2] | x <- [1,4 .. 12] ]
+#+end_src
+"))))
+
+
+;;;; Not define  errors
+;;
+
+(ert-deftest ob-haskell/not-defined ()
+  "Evaluation of undefined variables."
+  (should (string-match "Variable not in scope"
+                        (test-ob-haskell-ghci "" "notDefined :: IO Int"))))
+
+(ert-deftest ob-haskell/not-defined-then-defined-1 ()
+  "Evaluation of undefined variables.
+This is a valid haskell source, but, invalid when entered one
+line at a time in GHCi."
+  (let ((r (test-ob-haskell-ghci "" "
+v :: Int
+v = 4
+")))
+    (should (and r (string-match "Variable not in scope" r)))))
+
+(ert-deftest ob-haskell/not-defined-then-defined-1-fixed ()
+  "Like not-defined-then-defined-1, but using the mutiline marks."
+  :expected-result :failed
+  (let ((r (test-ob-haskell-ghci "" "
+:{
+  v :: Int
+  v = 4
+:}
+")))
+    (should (eq nil r))))
+
+(ert-deftest ob-haskell/not-defined-then-defined-1-fixed-2 ()
+  "Like not-defined-then-defined-1, but using one line."
+  (should (eq nil (test-ob-haskell-ghci "" "v = 4 :: Int"))))
+
+
+
+(ert-deftest ob-haskell/not-defined-then-defined-2 ()
+  "Evaluation of undefined variables, followed by a correct one."
+  ;; ghci output is:
+  ;;  | <interactive>:2:1-4: error:
+  ;;  |     • Variable not in scope: main :: IO ()
+  ;;  |     • Perhaps you meant ‘min’ (imported from Prelude)
+  ;;  | Hello, World!
+  ;; and ob-haskell just reports the last line "Hello, World!".
+  (should (string-match "Variable not in scope"
+                        (test-ob-haskell-ghci ":results output" "
+main :: IO ()
+main = putStrLn \"Hello, World!\"
+main
+"))))
+
+;;;; Imports
+;;
+
+(ert-deftest ob-haskell/import ()
+  "Import and use library."
+  (should (equal 65 (test-ob-haskell-ghci "" "
+import Data.IORef
+r <- newIORef 65
+readIORef r
+"))))
+
+(ert-deftest ob-haskell/import-with-vars ()
+  "Import and use library with vars."
+  (should (equal 65 (test-ob-haskell-ghci ":var x=65" "
+import Data.IORef
+r <- newIORef x
+readIORef r
+"))))
+
+;;;; What is the result?
+;;
+
+(ert-deftest ob-haskell/results-value-1 ()
+  "Don't confuse output and values: nothing."
+  (should (equal nil (test-ob-haskell-ghci ":results value" "return ()"))))
+
+(ert-deftest ob-haskell/results-value-2 ()
+  "Don't confuse output and values: a list."
+  (should (equal '(1 2) (test-ob-haskell-ghci ":results value" "return [1,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 ()
+"))))
+
+
+;;;; GHCi commands
+;;
+
+(ert-deftest ob-haskell/ghci-type ()
+  "The ghci meta command ':type'."
+  (should (equal "3 :: Num p => p"
+                 (test-ob-haskell-ghci ":results output" ":type 3"))))
+
+(ert-deftest ob-haskell/ghci-info ()
+  "The ghci meta command ':info' ."
+  (should (equal "repeat :: a -> [a]    -- Defined in ‘GHC.List’"
+                 (test-ob-haskell-ghci ":results output" ":info repeat"))))
+
+
+(provide 'test-ob-haskell-ghci)
+
+;;; test-ob-haskell-ghci.el ends here
-- 
2.39.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-org-babel-haskell-initiate-session-Remove-secondary-.patch --]
[-- Type: text/x-patch, Size: 1292 bytes --]

From 9d2a08caccde50d95dc8d7b8429db757b99068b9 Mon Sep 17 00:00:00 2001
From: Ihor Radchenko <yantar92@posteo.net>
Date: Fri, 24 Mar 2023 11:20:22 +0100
Subject: [PATCH 2/7] org-babel-haskell-initiate-session: Remove secondary
 prompt

* lisp/ob-haskell.el (org-babel-haskell-initiate-session): Set
secondary prompt to "".  If we do not do this, org-comint may treat
secondary prompts as a part of output.
---
 lisp/ob-haskell.el | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el
index 2b1441c2a..f4197654c 100644
--- a/lisp/ob-haskell.el
+++ b/lisp/ob-haskell.el
@@ -169,7 +169,14 @@ (defun org-babel-haskell-initiate-session (&optional _session _params)
 then create one.  Return the initialized session."
   (org-require-package 'inf-haskell "haskell-mode")
   (or (get-buffer "*haskell*")
-      (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
+      (save-window-excursion
+        (run-haskell)
+        (sleep-for 0.25)
+        ;; Disable secondary prompt.
+        (org-babel-comint-input-command
+         (current-buffer)
+         ":set prompt-cont \"\"")
+        (current-buffer))))
 
 (defun org-babel-load-session:haskell (session body params)
   "Load BODY into SESSION."
-- 
2.39.1


[-- Attachment #4: 0003-testing-lisp-test-ob-haskell-ghci.el-Fix-some-tests.patch --]
[-- Type: text/x-patch, Size: 1508 bytes --]

From f77135089618c8c950c15a3232ac956763f39670 Mon Sep 17 00:00:00 2001
From: Ihor Radchenko <yantar92@posteo.net>
Date: Fri, 24 Mar 2023 11:25:19 +0100
Subject: [PATCH 3/7] * testing/lisp/test-ob-haskell-ghci.el: Fix some tests

(ob-haskell/2D-lists-multilines):
(ob-haskell/ghci-info): Fix incorrect test assertions.
---
 testing/lisp/test-ob-haskell-ghci.el | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index aba94d73f..e05ac3b6e 100644
--- a/testing/lisp/test-ob-haskell-ghci.el
+++ b/testing/lisp/test-ob-haskell-ghci.el
@@ -262,8 +262,7 @@ (ert-deftest ob-haskell/2D-lists ()
 
 (ert-deftest ob-haskell/2D-lists-multilines ()
   "Evaluation of nested lists into a table, as multilines."
-  :expected-result :failed
-  (should (equal '((1 2 3) (4 5 6))
+  (should (equal '((1 2 3) (4 5 6) (7 8 9))
                  (test-ob-haskell-ghci "" "
 :{
 [ [1..3]
@@ -444,8 +443,9 @@ (ert-deftest ob-haskell/ghci-type ()
 
 (ert-deftest ob-haskell/ghci-info ()
   "The ghci meta command ':info' ."
-  (should (equal "repeat :: a -> [a]    -- Defined in ‘GHC.List’"
-                 (test-ob-haskell-ghci ":results output" ":info repeat"))))
+  (should (string-match-p
+           "repeat :: a -> \\[a\\][ \t]+-- Defined in ‘GHC.List’"
+           (test-ob-haskell-ghci ":results output" ":info repeat"))))
 
 
 (provide 'test-ob-haskell-ghci)
-- 
2.39.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: 0004-testing-lisp-test-ob-haskell-ghci.el-Enable-fixed-te.patch --]
[-- Type: text/x-patch, Size: 2124 bytes --]

From a71b47ceb3edb26341b2b319c862d26fbbddc6a6 Mon Sep 17 00:00:00 2001
From: Ihor Radchenko <yantar92@posteo.net>
Date: Fri, 24 Mar 2023 11:26:00 +0100
Subject: [PATCH 4/7] * testing/lisp/test-ob-haskell-ghci.el: Enable fixed
 tests

(ob-haskell/hello-world-output-multilines):
(ob-haskell/let-multilines-1):
(ob-haskell/let-multilines-2):
(ob-haskell/primes):
(ob-haskell/not-defined-then-defined-1-fixed): Re-enable tests.
---
 testing/lisp/test-ob-haskell-ghci.el | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index e05ac3b6e..3663f21f9 100644
--- a/testing/lisp/test-ob-haskell-ghci.el
+++ b/testing/lisp/test-ob-haskell-ghci.el
@@ -118,7 +118,6 @@ (ert-deftest ob-haskell/hello-world-output-nothing ()
                  (test-ob-haskell-ghci ":results output" "return \"Hello World!\""))))
 
 (ert-deftest ob-haskell/hello-world-output-multilines ()
-  :expected-result :failed
   (should (equal "Hello World!"
                  (test-ob-haskell-ghci ":results output" "
 :{
@@ -188,7 +187,6 @@ (ert-deftest ob-haskell/let-one-line ()
 
 (ert-deftest ob-haskell/let-multilines-1 ()
   "Local definitions on multiple lines."
-  :expected-result :failed
   (should (equal 6 (test-ob-haskell-ghci "" "
 :{
  let { x=2
@@ -200,7 +198,6 @@ (ert-deftest ob-haskell/let-multilines-1 ()
 
 (ert-deftest ob-haskell/let-multilines-2 ()
   "Local definitions on multiple lines, relying on indentation."
-  :expected-result :failed
   (should (equal 6 (test-ob-haskell-ghci "" "
 :{
   let x=2
@@ -235,7 +232,6 @@ (ert-deftest ob-haskell/decl-multilines-2 ()
 
 (ert-deftest ob-haskell/primes ()
   "From haskell.org."""
-  :expected-result :failed
   (should (equal '(2 3 5 7 11 13 17 19 23 29)
                  (test-ob-haskell-ghci "" "
 :{
@@ -359,7 +355,6 @@ (ert-deftest ob-haskell/not-defined-then-defined-1 ()
 
 (ert-deftest ob-haskell/not-defined-then-defined-1-fixed ()
   "Like not-defined-then-defined-1, but using the mutiline marks."
-  :expected-result :failed
   (let ((r (test-ob-haskell-ghci "" "
 :{
   v :: Int
-- 
2.39.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #6: 0005-lisp-ob-haskell-Request-the-last-value-from-GHCi.patch --]
[-- Type: text/x-patch, Size: 4159 bytes --]

From 939c3b28a1587e0709aeadff16dc87f3dc06d120 Mon Sep 17 00:00:00 2001
From: Bruno BARBIER <brubar.cs@gmail.com>
Date: Sat, 25 Mar 2023 09:59:31 +0100
Subject: [PATCH 5/7] 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 f4197654c..a8a8df9d8 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.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #7: 0006-ob-haskell-Implement-sessions.patch --]
[-- Type: text/x-patch, Size: 4755 bytes --]

From b3dbcc39ab119162a9a0c5a172a33f6367a13845 Mon Sep 17 00:00:00 2001
From: Bruno BARBIER <brubar.cs@gmail.com>
Date: Sat, 25 Mar 2023 10:06:44 +0100
Subject: [PATCH 6/7] ob-haskell: Implement sessions

* lisp/ob-haskell.el (org-babel-haskell-initiate-session): Implement
sessions.

* testing/lisp/test-ob-haskell-ghci.el: Update tests related to
sessions.
---
 lisp/ob-haskell.el                   | 50 +++++++++++++++++++++-------
 testing/lisp/test-ob-haskell-ghci.el |  8 +++--
 2 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el
index a8a8df9d8..f7acb1011 100644
--- a/lisp/ob-haskell.el
+++ b/lisp/ob-haskell.el
@@ -51,6 +51,8 @@ (declare-function haskell-mode "ext:haskell-mode" ())
 (declare-function run-haskell "ext:inf-haskell" (&optional arg))
 (declare-function inferior-haskell-load-file
 		  "ext:inf-haskell" (&optional reload))
+(declare-function inferior-haskell-start-process
+                  "ext:inf-haskell" ())
 (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
 
 (defvar org-babel-tangle-lang-exts)
@@ -183,20 +185,44 @@ (defun org-babel-execute:haskell (body params)
 	(org-babel-interpret-haskell body params)
       (org-babel-haskell-execute body params))))
 
-(defun org-babel-haskell-initiate-session (&optional _session _params)
+
+
+
+;; Variable defined in inf-haskell (haskell-mode package).
+(defvar inferior-haskell-buffer)
+
+(defun org-babel-haskell-initiate-session (&optional session-name _params)
   "Initiate a haskell session.
-If there is not a current inferior-process-buffer in SESSION
-then create one.  Return the initialized session."
+Return the initialized session."
   (org-require-package 'inf-haskell "haskell-mode")
-  (or (get-buffer "*haskell*")
-      (save-window-excursion
-        (run-haskell)
-        (sleep-for 0.25)
-        ;; Disable secondary prompt.
-        (org-babel-comint-input-command
-         (current-buffer)
-         ":set prompt-cont \"\"")
-        (current-buffer))))
+  (when (and session-name (string= session-name "none"))
+    (setq session-name nil))
+  (unless session-name
+    ;; As haskell-mode is using the buffer name "*haskell*", we stay
+    ;; away from it.
+    (setq session-name (generate-new-buffer-name "*ob-haskell*")))
+  (let ((session (get-buffer session-name)))
+    (save-window-excursion
+      (or (org-babel-comint-buffer-livep session)
+          (let ((inferior-haskell-buffer session))
+            (when (and (bufferp session) (not (org-babel-comint-buffer-livep session)))
+              (when (bufferp "*haskell*") (error "Conflicting buffer '*haskell*', rename it or kill it."))
+              (with-current-buffer session (rename-buffer "*haskell*")))
+            (save-window-excursion
+              ;; We don't use `run-haskell' to not popup the buffer.
+              ;; And we protect default-directory.
+              (let ((default-directory default-directory))
+                (inferior-haskell-start-process))
+              (sleep-for 0.25)
+              (setq session inferior-haskell-buffer)
+              (with-current-buffer session (rename-buffer session-name))
+              ;; Disable secondary prompt.
+              (org-babel-comint-input-command
+               session
+               ":set prompt-cont \"\"")
+              session))))
+    session))
+
 
 (defun org-babel-load-session:haskell (session body params)
   "Load BODY into SESSION."
diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index 2ddfb4de7..907487960 100644
--- a/testing/lisp/test-ob-haskell-ghci.el
+++ b/testing/lisp/test-ob-haskell-ghci.el
@@ -133,7 +133,6 @@ (ert-deftest ob-haskell/hello-world-output-multilines ()
 
 (ert-deftest ob-haskell/sessions-must-not-share-variables ()
   "Sessions must not share variables."
-  :expected-result :failed
   (test-ob-haskell-ghci-with-global-session
    (test-ob-haskell-ghci ":session s1" "x=2" nil :unprotected)
    (should (equal 2 (test-ob-haskell-ghci ":session s1" "x" nil :unprotected)))
@@ -143,11 +142,16 @@ (ert-deftest ob-haskell/sessions-must-not-share-variables ()
 
 (ert-deftest ob-haskell/no-session-means-one-shot-sessions ()
   "When no session, use a new session."
-  :expected-result :failed
   (test-ob-haskell-ghci-with-global-session
    (test-ob-haskell-ghci "" "x=2" nil :unprotected)
    (should-not (equal 2 (test-ob-haskell-ghci "" "x" nil :unprotected)))))
 
+(ert-deftest ob-haskell/reuse-variables-in-same-session ()
+  "Reuse variables between blocks using the same session."
+  (test-ob-haskell-ghci ":session s1" "x=2" nil)
+  (should (equal 2 (test-ob-haskell-ghci ":session s1" "x"))))
+
+
 
 ;;;; Values
 ;;
-- 
2.39.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #8: 0007-ob-haskell-Update-tests-about-errors.patch --]
[-- Type: text/x-patch, Size: 1489 bytes --]

From bd99c392e7c4aecdd94a636b2cc05ee55b681154 Mon Sep 17 00:00:00 2001
From: Bruno BARBIER <brubar.cs@gmail.com>
Date: Sat, 25 Mar 2023 10:09:26 +0100
Subject: [PATCH 7/7] ob-haskell: Update tests about errors

testing/lisp/test-ob-haskell-ghci.el: Update tests about errors.
---
 testing/lisp/test-ob-haskell-ghci.el | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index 907487960..54705852f 100644
--- a/testing/lisp/test-ob-haskell-ghci.el
+++ b/testing/lisp/test-ob-haskell-ghci.el
@@ -344,18 +344,20 @@ (ert-deftest ob-haskell/reuse-table ()
 
 (ert-deftest ob-haskell/not-defined ()
   "Evaluation of undefined variables."
-  (should (string-match "Variable not in scope"
-                        (test-ob-haskell-ghci "" "notDefined :: IO Int"))))
+  :expected-result :failed
+  (should-error (test-ob-haskell-ghci "" "notDefined :: IO Int")))
+
 
 (ert-deftest ob-haskell/not-defined-then-defined-1 ()
   "Evaluation of undefined variables.
 This is a valid haskell source, but, invalid when entered one
 line at a time in GHCi."
-  (let ((r (test-ob-haskell-ghci "" "
+  :expected-result :failed
+  (should-error (test-ob-haskell-ghci "" "
 v :: Int
 v = 4
 ")))
-    (should (and r (string-match "Variable not in scope" r)))))
+
 
 (ert-deftest ob-haskell/not-defined-then-defined-1-fixed ()
   "Like not-defined-then-defined-1, but using the mutiline marks."
-- 
2.39.1


  reply	other threads:[~2023-03-25 19:01 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-19  9:12 [PATCH] Add tests for ob-haskell (GHCi) Bruno Barbier
2023-03-19 10:20 ` Ihor Radchenko
2023-03-19 10:28   ` Ihor Radchenko
2023-03-19 10:32   ` Bruno Barbier
2023-03-22 10:16     ` Ihor Radchenko
2023-03-24 10:36       ` Ihor Radchenko
2023-03-25 10:01         ` Bruno Barbier [this message]
2023-03-26  9:09           ` Ihor Radchenko
2023-03-26  9:40             ` Bruno Barbier
2023-03-26  9:46               ` Ihor Radchenko
     [not found]             ` <notmuch-sha1-0807e1720f829950d42ef560bc30e56bd152766c>
2023-05-07  8:50               ` Bruno Barbier
2023-05-07  9:18                 ` Ruijie Yu via General discussions about Org-mode.
2023-05-07 11:15                   ` Bruno Barbier
2023-05-08 10:59                 ` Ihor Radchenko
2023-05-21  7:40                   ` Bruno Barbier
2023-06-02  8:44                     ` Ihor Radchenko
2023-08-10 12:51                       ` Ihor Radchenko
2023-08-25 19:10                         ` Bruno Barbier
2023-09-07 14:21                       ` Bruno Barbier
2023-09-08  8:23                         ` Ihor Radchenko
2023-09-08  9:49                           ` Bruno Barbier
2023-03-23 10:35 ` Ihor Radchenko
2023-03-23 21:01   ` ParetoOptimalDev
2023-03-23 21:30     ` ParetoOptimalDev
2023-03-24 10:40     ` Ihor Radchenko
2023-03-26  3:27       ` ParetoOptimalDev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=641ec686.050a0220.16dbc.a6ca@mx.google.com \
    --to=brubar.cs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.