From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id ILrULTzLaWTLwwAASxT56A (envelope-from ) for ; Sun, 21 May 2023 09:41:48 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id 4CjnLTzLaWRcdAAA9RJhRA (envelope-from ) for ; Sun, 21 May 2023 09:41:48 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 858A4B4C2 for ; Sun, 21 May 2023 09:41:47 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q0dgR-0005N5-Qk; Sun, 21 May 2023 03:40:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q0dgQ-0005Mo-B2 for emacs-orgmode@gnu.org; Sun, 21 May 2023 03:40:50 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q0dgL-0005xz-TD for emacs-orgmode@gnu.org; Sun, 21 May 2023 03:40:50 -0400 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3f42ba32e24so29910375e9.3 for ; Sun, 21 May 2023 00:40:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684654844; x=1687246844; h=mime-version:date:references:in-reply-to:subject:cc:to:from :message-id:from:to:cc:subject:date:message-id:reply-to; bh=yj/l9H+aPq1MCoKf7uRThkxW2Dh9R2qrYRaoifXCImw=; b=sI/+BluOs2LnzFvPFSLtnLj9vuOEgDZfir6yGBy8bv4Yx0HSVZrGGUmYG1E9vclfXU EgUi9XYuw6B3iG427GcObcLFcoG35dEyNecCgmPdVCfYWD8mkoncU5IpmIY/p41BL0+c 0+V1odZ0e/NI4YzubSvLFXHoQAuGHGt3jY0XiZN1xIenWPgtqMZHaiQelBsBh9btyYBA fyBxyDL1sRXd7tW7cFpW0gRZqVzc25Hne00ptk2ApYHctFD2EOm3zl4oLajfretHSc4E r8ghB5MbNlbVdkDNuRo+LrMDRyyGA5UlQqNa0WwDFARdiOu0hh1rzCg3bZkpEVoJ2PRf 3gCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684654844; x=1687246844; h=mime-version:date:references:in-reply-to:subject:cc:to:from :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yj/l9H+aPq1MCoKf7uRThkxW2Dh9R2qrYRaoifXCImw=; b=J3MMqHlZxV1YWzkCv2/P832lMJVj2ny4fus8eXuA5ZvzMpAP1oCYYpN6AXo/Orkr6j gTs0IRvtFueNLMot5cCtwI3p/HMVgprThL2fP+98cRCNcDe1g10NpW/QYdYGuHfC5TgY d7IiVpO+wyRaT2vyqLZF2KZQ0RdzxvshA/bjU67Xp4lOvKCB9ImACw8qgt8Dja9YX6Cp y/8ehISacFoibF1H6JvYmq+seFhk8qq22JdKQDIvr4CNsRBhiSsVEjE2uW8SfYhYHPKe qzKFB1BT8olIiiegBLtiD4Efqd7t30SqICyRZW+4b/iJeFPIyQFll/UmZvCHoK1QjAJC t22g== X-Gm-Message-State: AC+VfDwKDmiwTjMUl3rbjQxwyi43SFkdKQzLiRVBdAzy8ZfgHrUZOljq QbbQn8j84dYAR86vWRAPwuw= X-Google-Smtp-Source: ACHHUZ4iciNRxTGSfDTfEdfhXUkMA175DqZlWKoDgTA4AIpXHg6VVU2PwbOprkoJj2jOZjgpKzW0NQ== X-Received: by 2002:a05:600c:2150:b0:3f5:d0ba:3c1a with SMTP id v16-20020a05600c215000b003f5d0ba3c1amr4442113wml.4.1684654843548; Sun, 21 May 2023 00:40:43 -0700 (PDT) Received: from keynux ([2a01:e0a:505:3460:169:7511:f49a:58eb]) by smtp.gmail.com with ESMTPSA id l11-20020a1ced0b000000b003f182a10106sm4350350wmh.8.2023.05.21.00.40.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 May 2023 00:40:42 -0700 (PDT) Message-ID: <6469cafa.1c0a0220.4db6f.0396@mx.google.com> Received: by keynux (sSMTP sendmail emulation); Sun, 21 May 2023 09:40:40 +0200 From: Bruno Barbier To: Ihor Radchenko Cc: emacs-orgmode@gnu.org Subject: Re: [PATCH] Add tests for ob-haskell (GHCi) In-Reply-To: <873547ks1t.fsf@localhost> References: <6416d214.5d0a0220.d9c1.54aa@mx.google.com> <875yaxvy93.fsf@localhost> <6416e4d9.df0a0220.ce03d.5c4b@mx.google.com> <87h6udozvh.fsf@localhost> <87r0texwor.fsf@localhost> <641ec686.050a0220.16dbc.a6ca@mx.google.com> <87fs9rvpyj.fsf@localhost> <6457663a.df0a0220.f6b38.a05e@mx.google.com> <873547ks1t.fsf@localhost> Date: Sun, 21 May 2023 09:40:40 +0200 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=brubar.cs@gmail.com; helo=mail-wm1-x334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Seal: i=1; s=key1; d=yhetil.org; t=1684654908; a=rsa-sha256; cv=none; b=gMt3iqUnQ0uU6HhT4v0XDDZV5UsVBzL3VlUp7p5aU52ungmBzxwiCzey+8e7FVUcRwls/8 xfBdLn2bzIMCGkFbBoaaPs9DHUu3V57YRuhL+kJ2wkUKnEhCX4NDMs952pMz/o+OZ+loco tOGzBBOBYHo3QUtyQFxr7wm2aPx/fcy5oFysWJBustaQCs4Wm1cXUhCUdowDkJBPpaKRa9 4NrpET4esADR7YQ/EzylzO6z9P5X0+JYPWN/svRRxZri13ZRCZlvG2tjbS4zeFWgfSbu6M zYcxxv5/DaqKK1HGkIT5GlNNM7c1D3VqpoLx+c4n04OWt8JXFmfDF24hWzI0XA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="sI/+BluO"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1684654908; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=yj/l9H+aPq1MCoKf7uRThkxW2Dh9R2qrYRaoifXCImw=; b=RMVhbnlR+SByNduGR7LiGB4v+0NaSDsLCxCLmrzbj8pSVZs8WlcaF+/860fuPKgS8srWiM yGNUiMZ7CXMHEcjnRKT6wXXchstzMYumW1ymHraI6MD8r690MTL/foob5/KKMrRCfj73vk DVfl2fnrVYDcCV10JNB0x57JxPRwbyJVsf/2DjfJmXIYhEbsDnfftKMGmYv5wdnNqXBVrZ mYsXI/xTcjnUM1EbQfuxuDZ1BFGmJTKoUn8gxs1A8xc7jFyBFo4IT8hfT6xne/mwjZRpUi cuTJ4RozpdHcq6UR2Ppl7a9bck9BGTQSMUuPL4HM3bItnlnxWPK3m3yFzDBmkg== X-Migadu-Scanner: scn1.migadu.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="sI/+BluO"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -6.34 X-Spam-Score: -6.34 X-Migadu-Queue-Id: 858A4B4C2 X-TUID: E4ChEoksyHVq --=-=-= Content-Type: text/plain Hi Ihor, Thanks for the review. Ihor Radchenko writes: > Bruno Barbier writes: > I can see that you limited the tests scope to :session blocks. > Would it be possible to extend the existing tests to :compile yes case? > From a glance, it does not look like you need to change much - Haskell > behaviour should be similar with or without ghci. Except for one line expressions, GHCi inputs and haskell modules are two different things. I doubt there will be much to share; that's why I've focused from the start on GHCi only. As I've a lot of other things that I'd like to do to improve my day to day workflow, and as I'm barely using ob-haskell, I can't promise I'll work on this any time soon. >> From 9972b926f55cb970e0b520f8726a3684118017b6 Mon Sep 17 00:00:00 2001 >> From: Ihor Radchenko >> Date: Fri, 24 Mar 2023 11:20:22 +0100 >> Subject: [PATCH 02/13] 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. > >> + (sleep-for 0.25) >> + ;; Disable secondary prompt. > > It would be useful to explain the purpose of disabling the secondary > prompt in the source code comment itself, not just in the commit > message. It will improve readability. Are you reviewing your own improvements ? :-) Fixed. I've copied/pasted your explanation in the code. >> From 352d18399961fedc45cc2d64007016426e1ecd40 Mon Sep 17 00:00:00 2001 >> From: Ihor Radchenko >> Date: Fri, 24 Mar 2023 11:26:00 +0100 >> Subject: [PATCH 04/13] * testing/lisp/test-ob-haskell-ghci.el: Enable fixed > > I do not see PATCH 03/13 in the attachments. Sorry. I forgot it the first time. The email, after handling the comments from Ruijie Yu, had it though. >> From 7d66cff5cc23bb786cb2843f4326d2869512ccac Mon Sep 17 00:00:00 2001 >> From: Bruno BARBIER >> Date: Sat, 25 Mar 2023 10:06:44 +0100 >> Subject: [PATCH 06/13] ob-haskell: Implement sessions >> >> + (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))) > > session is not a buffer or nil, if no buffer named session-name exists. The argument SESSION-NAME must be a string or nil (I added a test to make clear that it must be a string). Thus, session will either be nil or a live buffer. >> + (save-window-excursion >> + (or (org-babel-comint-buffer-livep session) > > Below, (org-babel-comint-buffer-livep session) is nil, which implies > either that session is nil, does not exist, not live, or does not have a > process attached. ok. So, in our case, session is either nil, or it's a live buffer without an attached process. > >> + (let ((inferior-haskell-buffer session)) >> + (when (and (bufferp session) (not (org-babel-comint-buffer-livep session))) > > (not (org-babel-comint-buffer-livep session)) is always t here. Right. I removed the test. Thanks. > Also, session may be a killed buffer object. It is still a buffer, but > not usable. See `buffer-live-p'. By construction, if 'session' is a buffer, then, it is a live buffer. > >> + (when (bufferp "*haskell*") (error "Conflicting buffer '*haskell*', rename it or kill it.")) >> + (with-current-buffer session (rename-buffer "*haskell*"))) > > So, you are now renaming the unique session buffer back to "*haskell*". > And never rename it back to expected :session . Users might be confused. I do rename it back once inf-haskell has initialized the buffer (after run-haskell in the last version). >> + (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)) > > This is a workaround for a nasty side effect of running > `inferior-haskell-start-process'. We should report this to haskell-mode > developers, leaving appropriate comment in the code. About 'run-haskell', I reverted my change: we're inside a 'save-window-excursion', which looks like the standard way to get rid of unwanted popups (like ob-shell does). About 'default-directory', I'm not sure. Maybe the side effect is done on purpose in inf-haskell. > >> + (sleep-for 0.25) >> + (setq session inferior-haskell-buffer) >> + (with-current-buffer session (rename-buffer session-name)) > > This generally looks like a brittle workaround for inner workings of > haskell-mode. I recommend sending an email to haskell-mode devs, > requesting multiple session support. Otherwise, this whole code > eventually be broken. Yes. It's a workaround. But it looks reasonably safe to me, as the default buffer name isn't going to change, even if they make it configurable. Plus, 'make-comint' (used by the function 'inferior-haskell-start-process' from the library inf-haskell) would also require to rename the buffer, or to forbid session names that don't start and end with "*", or to use buffer names that don't match the session names. > >> Subject: [PATCH 10/13] * testing/lisp/test-ob-haskell-ghci.el: Test output >> without EOL >> ... >> +(ert-deftest ob-haskell/output-without-eol-1 () >> + "Cannot get output from incomplete lines, when entered line by line." >> + :expected-result :failed >> + (should (equal "123" >> + (test-ob-haskell-ghci ":results output" " >> + putStr(\"1\") >> + putStr(\"2\") >> + putStr(\"3\") >> + putStr(\"\\n\") >> +")))) > > May you explain more about this bug? Sure. The function 'putStr' output the string without a newline on stdout (as opposed to the function putStrLn that does add a newline). So, in GHCi, entering: putStr("4") outputs "4" on stdout, then GHCi outputs the prompt, so we get: 4ghci> In the end, 'org-babel-comint-with-output' gets this 1ghci> 2ghci> 3ghci> ghci> org-babel-haskell-eoe ghci> ghci> and filters out everything as being GHCi prompts and the EOE. I'm not really expecting this to be fixed; I just wanted to record the fact. IMHO, users should use one of the alternatives, that are shown in the tests 'ob-haskell/output-without-eol-2' or 'ob-haskell/output-without-eol-3'. > >> Subject: [PATCH 11/13] lisp/ob-haskell.el: Fix how to use sessions >> >> + (org-babel-haskell-with-session > > This kind of names are usually dedicated to macro calls. But > `org-babel-haskell-with-session' is instead a function. I think a macro > will be better. And you will be able to get rid of unnecessary lambda. That looks kind of complicated just to avoid one lambda in one call. But, as I couldn't find a better name, I've translated it into a macro. > >> + params >> + (lambda (session) >> + (cl-labels >> + ((csend (txt) >> + (eom () >> + (with-output (todo) > > When using `cl-labels', please prefer longer, more descriptive function > names. These functions do not have a docstring and I now am left > guessing and reading the function code repeatedly to understand the > usage. I tried to use more descriptive names. I hope it's easier to read now. >> + (full-body (org-babel-expand-body:generic >> + body params >> + (org-babel-variable-assignments:haskell params))) > > I think we want `org-babel-expand-src-block' here instead of using > semi-internal ob-core.el parts. Are you sure about this ? I didn't modify this part and I didn't see this function used in other backends. I've also checked ob-python and ob-shell: they both use the same code as above. >> - (let ((buffer (org-babel-haskell-initiate-session session))) >> + (let ((buffer (org-babel-haskell-initiate-session session params))) > > PARAMS argument is ignored by `org-babel-haskell-initiate-session'. I am > not sure why you are trying to pass it here. We have the PARAMS, and, org-babel-haskell-initiate-session has a PARAMS arguments. So, at the API level, I think it's better to propagate it than to ignore it. But you're right that, today, the current implementation ignores it. I'm fine with dropping that change if you so prefer. >> Subject: [PATCH 12/13] * testing/lisp/test-ob-haskell-ghci.el: Modify >> `test-ob-haskell-ghci` > > Here and in some other patches you are undoing changes made in previous > patches. May you please consolidate transient changes by squashing > commits? It will make further reviews easier. I wasn't sure what would make the review easier: keep the history of changes or squash my updates. I've now squashed all my updates. I've attached the new version. Thanks again for the review. Bruno. --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-ob-haskell-Add-tests-for-GHCi.patch Content-Transfer-Encoding: quoted-printable >From 9218d6cebbabc3a22aba8b7dcf37c4e746f34360 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER 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 | 428 +++++++++++++++++++++++++++ 1 file changed, 428 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-ha= skell-ghci.el new file mode 100644 index 000000000..330937917 --- /dev/null +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -0,0 +1,428 @@ +;;; test-ob-haskell-ghci.el --- tests for ob-haskell.el GHCi -*- lexical-= binding: t; -*- + +;; Copyright (c) 2023 Free Software Foundation, Inc. +;; Authors: Bruno BARBIER + +;; This file is 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 . + +;;; Commentary: +;; + +;;;; Useful references +;; +;; - https://orgmode.org/worg/org-contrib/babel/languages/lang-compat.html +;; - GHCi manual: https://downloads.haskell.org/ghc/latest/docs/users_gui= de/ghci.html + +;;; 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-checking-buffers (todo) + "Check some buffer related invariants.." + (when (get-buffer "*haskell*") + (error "A buffer named '*haskell*' exists. Can't safely test haskell = blocks")) + (prog1 (funcall todo) + (when-let ((hb (get-buffer "*haskell*"))) + ;; We created a "*haskell*" buffer. That shouldn't happen. + (error "'ob-haskell' created a buffer named '*haskell*'")))) + + + +(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, check pre/post condit= ions." + (when (listp content) + (setq content (string-join content "\n"))) + (unless preamble + (setq preamble "")) + (let ((todo (lambda () + (prog1 (org-test-with-temp-text + (concat preamble "\n" "#+begin_src haskell :com= pile no " + args "\n" "" content "\n#+end_sr= c") + (org-babel-execute-src-block)))))) + (if unprotected (funcall todo) + (test-ob-haskell-ghci-checking-buffers 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 W= orld!\"")))) + +(ert-deftest ob-haskell/hello-world-output-multilines () + :expected-result :failed + (should (equal "Hello World!" + (test-ob-haskell-ghci ":results output" " +:{ +main :: IO () +main =3D 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=3D2" nil :unprotected) + (should (equal 2 (test-ob-haskell-ghci ":session s1" "x" nil :unprotect= ed))) + (test-ob-haskell-ghci ":session s2" "x=3D3" nil :unprotected) + (should-not (equal 3 (test-ob-haskell-ghci ":session s1" "x" nil :unpro= tected))) + )) + +(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=3D2" 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=3D1" "y=3D1+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=3D2; y=3D3 } 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=3D2 + ; y=3D3 + } + 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=3D2 + y=3D3 + 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' [] =3D 0 + length' (_:l) =3D 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' [] =3D 0 +length' (_:l) =3D 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 =3D filterPrime [2..] where + filterPrime (p:xs) =3D + p : filterPrime [x | x <- xs, x `mod` p /=3D 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=3Dint-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=3Dfloat-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=3Dstring-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=3Da-table" + "sum [sum r | r <- t]" + "#+name: a-table +#+begin_src haskell + [ [x..x+2] | x <- [1,4 .. 12] ] +#+end_src +")))) + + +;;;; Not defined errors +;; + +(ert-deftest ob-haskell/not-defined () + "Evaluation of undefined variables." + :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." + :expected-result :failed + (should-error (test-ob-haskell-ghci "" " +v :: Int +v =3D 4 +"))) + + +(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 =3D 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 =3D 4 :: Int")))) + + + +(ert-deftest ob-haskell/not-defined-then-defined-2 () + "Evaluation of undefined variables, followed by a correct one." + ;; ghci output is: + ;; | :2:1-4: error: + ;; | =E2=80=A2 Variable not in scope: main :: IO () + ;; | =E2=80=A2 Perhaps you meant =E2=80=98min=E2=80=99 (imported fr= om 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 =3D 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=3D65" " +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 =3D> 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 =E2=80=98GHC.List=E2= =80=99" + (test-ob-haskell-ghci ":results output" ":info repeat")))) + + +(provide 'test-ob-haskell-ghci) + +;;; test-ob-haskell-ghci.el ends here --=20 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-org-babel-haskell-initiate-session-Remove-secondary-.patch >From ee6c0869f9995de8690871b92b975b0750f03543 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko 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 | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index 909de19ab..8f9ae397a 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -169,7 +169,16 @@ (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: If we do not do this, + ;; org-comint may treat secondary prompts as a part of + ;; output. + (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.3 --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0003-testing-lisp-test-ob-haskell-ghci.el-Fix-some-tests.patch Content-Transfer-Encoding: quoted-printable >From 7ff5390fedd1717f9c16f04ac997490d050a9081 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko 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-ha= skell-ghci.el index 330937917..cf59edb25 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -235,8 +235,7 @@ (ert-deftest ob-haskell/2D-lists () =20 (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] @@ -419,8 +418,9 @@ (ert-deftest ob-haskell/ghci-type () =20 (ert-deftest ob-haskell/ghci-info () "The ghci meta command ':info' ." - (should (equal "repeat :: a -> [a] -- Defined in =E2=80=98GHC.List=E2= =80=99" - (test-ob-haskell-ghci ":results output" ":info repeat")))) + (should (string-match-p + "repeat :: a -> \\[a\\][ \t]+-- Defined in =E2=80=98GHC.List=E2= =80=99" + (test-ob-haskell-ghci ":results output" ":info repeat")))) =20 =20 (provide 'test-ob-haskell-ghci) --=20 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0004-testing-lisp-test-ob-haskell-ghci.el-Enable-fixed-te.patch >From 1f7bbe337ec69bfa02ff4c117e70685f288d01e6 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko 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 cf59edb25..868a56e63 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -91,7 +91,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" " :{ @@ -161,7 +160,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 @@ -173,7 +171,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 @@ -208,7 +205,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 "" " :{ @@ -334,7 +330,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.3 --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0005-lisp-ob-haskell-Request-the-last-value-from-GHCi.patch Content-Transfer-Encoding: quoted-printable >From d26447d0a0316058a3eb8cb5ea597f4f7ee24e14 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER 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. (org-babel-haskell-eoe): New default value. (org-babel-interpret-haskell): Update for the new value of `org-babel-haske= ll-eoe'. * testing/lisp/test-ob-haskell-ghci.el: Update tests related to output/valu= e. --- lisp/ob-haskell.el | 80 ++++++++++++++++++---------- testing/lisp/test-ob-haskell-ghci.el | 6 +-- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index 8f9ae397a..6cec21217 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -61,7 +61,7 @@ (defvar org-babel-default-header-args:haskell =20 (defvar org-babel-haskell-lhs2tex-command "lhs2tex") =20 -(defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"") +(defvar org-babel-haskell-eoe "org-babel-haskell-eoe") =20 (defvar haskell-prompt-regexp) =20 @@ -127,34 +127,56 @@ (defun org-babel-interpret-haskell (body params) (lambda () (setq-local comint-prompt-regexp (concat haskell-prompt-regexp "\\|^=CE=BB?> ")))) - (let* ((session (cdr (assq :session params))) - (result-type (cdr (assq :result-type params))) - (full-body (org-babel-expand-body:generic - body params - (org-babel-variable-assignments:haskell 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))) - (results (mapcar #'org-strip-quotes - (cdr (member org-babel-haskell-eoe - (reverse (mapcar #'org-trim raw))))= ))) - (org-babel-reassemble-table - (let ((result - (pcase result-type - (`output (mapconcat #'identity (reverse results) "\n")) - (`value (car results))))) - (org-babel-result-cond (cdr (assq :result-params params)) - result (when result (org-babel-script-escape result)))) - (org-babel-pick-name (cdr (assq :colname-names params)) - (cdr (assq :colname-names params))) - (org-babel-pick-name (cdr (assq :rowname-names params)) - (cdr (assq :rowname-names params)))))) + (org-babel-haskell-with-session session params + (cl-labels + ((send-txt-to-ghci (txt) + (insert txt) (comint-send-input nil t)) + (send-eoe () + (send-txt-to-ghci (concat "putStrLn \"" org-babel-haskell-eoe "= \"\n"))) + (comint-with-output (todo) + (let ((comint-preoutput-filter-functions + (cons 'ansi-color-filter-apply + comint-preoutput-filter-functions))) + (org-babel-comint-with-output + (session org-babel-haskell-eoe nil nil) + (funcall todo))))) + (let* ((result-type (cdr (assq :result-type params))) + (full-body (org-babel-expand-body:generic + body params + (org-babel-variable-assignments:haskell params))) + (raw (pcase result-type + (`output + (comint-with-output + (lambda () (send-txt-to-ghci (org-trim full-body)) (= send-eoe)))) + (`value + ;; We first compute the value and store it, + ;; ignoring any output. + (comint-with-output + (lambda () + (send-txt-to-ghci "__LAST_VALUE_IMPROBABLE_NAME__= =3D()::()\n") + (send-txt-to-ghci (org-trim full-body)) + (send-txt-to-ghci "__LAST_VALUE_IMPROBABLE_NAME__= =3Dit\n") + (send-eoe))) + ;; We now display and capture the value. + (comint-with-output + (lambda() + (send-txt-to-ghci "__LAST_VALUE_IMPROBABLE_NAME__\= n") + (send-eoe)))))) + (results (mapcar #'org-strip-quotes + (cdr (member org-babel-haskell-eoe + (reverse (mapcar #'org-trim raw= ))))))) + (org-babel-reassemble-table + (let ((result + (pcase result-type + (`output (mapconcat #'identity (reverse results) "\n")) + (`value (car results))))) + (org-babel-result-cond (cdr (assq :result-params params)) + result (when result (org-babel-script-escape result)))) + (org-babel-pick-name (cdr (assq :colname-names params)) + (cdr (assq :colname-names params))) + (org-babel-pick-name (cdr (assq :rowname-names params)) + (cdr (assq :rowname-names params)))))))) + =20 (defun org-babel-execute:haskell (body params) "Execute a block of Haskell code." diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-ha= skell-ghci.el index 868a56e63..5049bc01a 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -86,8 +86,8 @@ (ert-deftest ob-haskell/hello-world-output () (test-ob-haskell-ghci ":results output" "putStrLn \"Hello= World!\"")))) =20 (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 W= orld!\"")))) =20 (ert-deftest ob-haskell/hello-world-output-multilines () @@ -391,12 +391,10 @@ (ert-deftest ob-haskell/results-value-2 () =20 (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\= "")))) =20 (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 () --=20 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0006-ob-haskell-Implement-sessions.patch >From b9fb970c925235be16cd04496235ed13eb31fb63 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER 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. (org-babel-haskell-with-session): New macro to manage sessions. (org-babel-interpret-haskell): Refactor code. Use `org-babel-haskell-with-session` to manage sessions. (org-babel-prep-session:haskell): Don't ignore the PARAMS argument. * testing/lisp/test-ob-haskell-ghci.el: Update tests related to sessions. --- lisp/ob-haskell.el | 98 +++++++++++++++++++++++----- testing/lisp/test-ob-haskell-ghci.el | 47 +++++++++---- 2 files changed, 116 insertions(+), 29 deletions(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index 6cec21217..cd930266c 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -77,6 +77,32 @@ (defcustom org-babel-haskell-compiler "ghc" (defconst org-babel-header-args:haskell '((compile . :any)) "Haskell-specific header arguments.") + +(defun org-babel-haskell-with-session--worker (params todo) + "See `org-babel-haskell-with-session'." + (let* ((sn (cdr (assq :session params))) + (session (org-babel-haskell-initiate-session sn params)) + (one-shot (equal sn "none"))) + (unwind-protect + (funcall todo session) + (when (and one-shot (buffer-live-p session)) + ;; As we don't control how the session temporary buffer is + ;; created, we need to explicitly work around the hooks and + ;; query functions. + (with-current-buffer session + (let ((kill-buffer-query-functions nil) + (kill-buffer-hook nil)) + (kill-buffer session))))))) + +(defmacro org-babel-haskell-with-session (session-symbol params &rest body) + "Get the session identified by PARAMS and run BODY with it. + +Get or create a session, as needed to match PARAMS. Assign the session to +SESSION-SYMBOL. Execute BODY. Destroy the session if needed. +Return the value of the last form of BODY." + (declare (indent 2) (debug (symbolp form body))) + `(org-babel-haskell-with-session--worker ,params (lambda (,session-symbol) ,@body))) + (defun org-babel-haskell-execute (body params) "This function should only be called by `org-babel-execute:haskell'." (let* ((tmp-src-file (org-babel-temp-file "Haskell-src-" ".hs")) @@ -185,22 +211,64 @@ (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, i.e. the buffer for this session. +When SESSION-NAME is nil, use a global session named +\"*ob-haskell*\". When SESSION-NAME is the string \"none\", use +a temporary buffer. Else, (re)use the session named +SESSION-NAME. The buffer name is the session name. See also +`org-babel-haskell-with-session'." (org-require-package 'inf-haskell "haskell-mode") - (or (get-buffer "*haskell*") - (save-window-excursion - (run-haskell) - (sleep-for 0.25) - ;; Disable secondary prompt: If we do not do this, - ;; org-comint may treat secondary prompts as a part of - ;; output. - (org-babel-comint-input-command - (current-buffer) - ":set prompt-cont \"\"") - (current-buffer)))) + (cond + ((equal "none" session-name) + ;; Temporary buffer name. + (setq session-name (generate-new-buffer-name " *ob-haskell-tmp*"))) + ((eq nil session-name) + ;; The global default session. As haskell-mode is using the buffer + ;; named "*haskell*", we stay away from it. + (setq session-name "*ob-haskell*")) + ((not (stringp session-name)) + (error "session-name must be a string"))) + (let ((session (get-buffer session-name))) + ;; NOTE: By construction, as SESSION-NAME is a string, session is + ;; either nil or a live buffer. + (save-window-excursion + (or (org-babel-comint-buffer-livep session) + (let ((inferior-haskell-buffer session)) + ;; As inferior-haskell expects the buffer to be named + ;; "*haskell*", we rename it, unless the user explicitly + ;; requested to use the name "*haskell*". + (when (not (equal "*haskell*" session-name)) + (when (bufferp session) + (when (bufferp "*haskell*") + (user-error "Conflicting buffer '*haskell*', rename it or kill it")) + (with-current-buffer session (rename-buffer "*haskell*")))) + (unwind-protect + ;; We protect default-directory. + (let ((default-directory default-directory)) + (run-haskell) + (sleep-for 0.25) + (setq session inferior-haskell-buffer)) + (when (and (not (equal "*haskell*" session-name)) + (bufferp session)) + (with-current-buffer session (rename-buffer session-name)))) + ;; Disable secondary prompt: If we do not do this, + ;; org-comint may treat secondary prompts as a part of + ;; output. + (org-babel-comint-input-command + session + ":set prompt-cont \"\"") + session) + )) + session)) + (defun org-babel-load-session:haskell (session body params) "Load BODY into SESSION." @@ -215,7 +283,7 @@ (defun org-babel-load-session:haskell (session body params) (defun org-babel-prep-session:haskell (session params) "Prepare SESSION according to the header arguments in PARAMS." (save-window-excursion - (let ((buffer (org-babel-haskell-initiate-session session))) + (let ((buffer (org-babel-haskell-initiate-session session params))) (org-babel-comint-in-buffer buffer (mapc (lambda (line) (insert line) diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el index 5049bc01a..adc9f939c 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -106,20 +106,39 @@ (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))) - (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))))) + (test-ob-haskell-ghci ":session s1" "x=2" nil) + (should (equal 2 (test-ob-haskell-ghci ":session s1" "x" nil))) + (test-ob-haskell-ghci ":session s2" "x=3" nil) + (should-not (equal 3 (test-ob-haskell-ghci ":session s1" "x" nil))) + ) + +(ert-deftest ob-haskell/session-named-none-means-one-shot-sessions () + "When no session, use a new session. +\"none\" is a special name that means `no session'." + (test-ob-haskell-ghci ":session none" "x=2" nil) + (should-not (equal 2 (test-ob-haskell-ghci ":session \"none\"" "x" nil))) + (test-ob-haskell-ghci ":session none" "x=2" nil) + (should-not (equal 2 (test-ob-haskell-ghci ":session \"none\"" "x" nil)))) + +(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")))) + +(ert-deftest ob-haskell/may-use-the-*haskell*-session () + "The user may use the special *haskell* buffer." + (when (get-buffer "*haskell*") + (error "A buffer named '*haskell*' exists. Can't run this test")) + (unwind-protect + (progn + (test-ob-haskell-ghci ":session *haskell*" "x=2" nil :unprotected) + (should (equal 2 (test-ob-haskell-ghci ":session *haskell*" "x" nil :unprotected)))) + (with-current-buffer "*haskell*" + (let ((kill-buffer-query-functions nil) + (kill-buffer-hook nil)) + (kill-buffer "*haskell*"))))) + + ;;;; Values -- 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0007-testing-lisp-test-ob-haskell-ghci.el-Test-output-wit.patch >From 3295295f59e46e97205a36c6541182120d750a98 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 29 Apr 2023 10:27:57 +0200 Subject: [PATCH 7/7] * testing/lisp/test-ob-haskell-ghci.el: Test output without EOL (ob-haskell/output-without-eol-1): (ob-haskell/output-without-eol-2): (ob-haskell/output-without-eol-3): New tests. --- testing/lisp/test-ob-haskell-ghci.el | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el index adc9f939c..212e0cb6f 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -171,6 +171,38 @@ (ert-deftest ob-haskell/eval-strings () "Evaluation of strings." (should (equal "a string" (test-ob-haskell-ghci "" "\"a string\"")))) +;;;; Output without EOL +;; + +(ert-deftest ob-haskell/output-without-eol-1 () + "Cannot get output from incomplete lines, when entered line by line." + :expected-result :failed + (should (equal "123" + (test-ob-haskell-ghci ":results output" " + putStr(\"1\") + putStr(\"2\") + putStr(\"3\") + putStr(\"\\n\") +")))) + +(ert-deftest ob-haskell/output-without-eol-2 () + "Incomplete output lines are OK when using a multiline block." + (should (equal "123" + (test-ob-haskell-ghci ":results output" " +:{ + do putStr(\"1\") + putStr(\"2\") + putStr(\"3\") + putStr(\"\\n\") +:} +")))) + +(ert-deftest ob-haskell/output-without-eol-3 () + "Incomplete output lines are OK on one line." + (should (equal "123" + (test-ob-haskell-ghci ":results output" " +do { putStr(\"1\"); putStr(\"2\"); putStr(\"3\"); putStr(\"\\n\") } +")))) ;;;; Local variables (ert-deftest ob-haskell/let-one-line () -- 2.39.3 --=-=-=--