From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id 4LcIKIaIV2Q7ZwAASxT56A (envelope-from ) for ; Sun, 07 May 2023 13:16:22 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id EIf6J4aIV2RLtwAA9RJhRA (envelope-from ) for ; Sun, 07 May 2023 13:16:22 +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 9DAFBB34E for ; Sun, 7 May 2023 13:16:21 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pvcMP-0002nV-NW; Sun, 07 May 2023 07:15:25 -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 1pvcMO-0002nB-Go for emacs-orgmode@gnu.org; Sun, 07 May 2023 07:15:24 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pvcMC-00010B-8r for emacs-orgmode@gnu.org; Sun, 07 May 2023 07:15:18 -0400 Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-307664010fdso2977466f8f.0 for ; Sun, 07 May 2023 04:15:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683458110; x=1686050110; h=mime-version:date:references:in-reply-to:subject:cc:to:from :message-id:from:to:cc:subject:date:message-id:reply-to; bh=a7TbQVwtGlsVMnfnm2gnpx00L55tXlAfCaJ1MDG9iwU=; b=WOaBJpWWZUDTie+H6RF4/9iyymsAO+z5KaaKV+knCCV9RZGEK7VxFmmyN0InNX9tK5 dTBNbsrpPUuK9q0gen8htBleskEWjIVSj48/GN70d0a+8PiK8XUj8JTWxI19ys2NmU8s UocakuPFRdkwG6kL90M2vfr6S6sjlXj/7K10YgJV+/gTif3yCxcUZxNmISfKcD2Ed8a6 RuMcTA2yF7S2t1w6r51Z1UjFmAvscMsoqp8BpS2cjBrP6SFrkZWOquUjhuJL0X/KozzW f+0Yde3is5dZjBh/Gb822Pf6M/as86ndE/XWW+wux+q/bUGr1k+rePX+puio2FLZCkg4 q4nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683458110; x=1686050110; 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=a7TbQVwtGlsVMnfnm2gnpx00L55tXlAfCaJ1MDG9iwU=; b=ebinOmxg88bYTWoDeNtrDJtrDKiBQD1uNCz2v0gNb8lw6dNInRWUj+pPl/eCBxcKwa CviFMl/+hPUimVUsZNvhT/1wumZ1vd9pP1ynuye3T1xW0dvnIQKSxPxK8JQ5dpoKmeiM G8qO1IOuyC5lL43iFqGtnY2MBkwEp+TI985tF/yjr9J89bMtA9tvTaCiUQLUVAYmVvEE bPahSpuzPgjiar/ZmpZRHttpVzYnHWG9nMieKy+f27Yy3puDBJXff07BEsDtkdisX4mn EFIMcA9uAZ0R3WwqCT/ID6sQJhso0b2o++rRhyJKORtDd6zdn0e9iTMXSmPTIKJvbHEf 7tgw== X-Gm-Message-State: AC+VfDxousWNrb5n9dg1ohRYGaW1Jhxyd3WIro5vY3b2RipwKzi5n4Dr zP/XzL/YvHPu2aeKWzxgNeeanGMvl9Y= X-Google-Smtp-Source: ACHHUZ5lRaTWeJDU5mPPCVE2MGHYzAeoM+qOY3R4fHEpJAF7UZ0KZY0aTza2Jh0/VOO1Z1eCtzpoVg== X-Received: by 2002:adf:ff85:0:b0:307:92e8:ec60 with SMTP id j5-20020adfff85000000b0030792e8ec60mr727073wrr.39.1683458109553; Sun, 07 May 2023 04:15:09 -0700 (PDT) Received: from keynux ([2a01:e0a:505:3460:169:7511:f49a:58eb]) by smtp.gmail.com with ESMTPSA id r12-20020a5d494c000000b003077f3dfcc8sm7212098wrs.32.2023.05.07.04.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 May 2023 04:15:07 -0700 (PDT) Message-ID: <6457883b.5d0a0220.2b9fc.8fd2@mx.google.com> Received: by keynux (sSMTP sendmail emulation); Sun, 07 May 2023 13:15:05 +0200 From: Bruno Barbier To: Ruijie Yu Cc: Ihor Radchenko , emacs-orgmode@gnu.org Subject: Re: [PATCH] Add tests for ob-haskell (GHCi) In-Reply-To: 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> Date: Sun, 07 May 2023 13:15:05 +0200 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2a00:1450:4864:20::42c; envelope-from=brubar.cs@gmail.com; helo=mail-wr1-x42c.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-Country: US X-Migadu-Flow: FLOW_IN ARC-Seal: i=1; s=key1; d=yhetil.org; t=1683458181; a=rsa-sha256; cv=none; b=uefVtvlqYk2tbpvtiIY6YsGf4xWgD8XGQPnorwHxzVCGqcGceSoTx5JyGu54kD2Rou7ppn RBFoVQIaSoQJGMgGXf74LH4j8BwotdZ0O0egeUGQkvAhfQUBeh/e4NRa2gZmSFIJYUvVPZ M5AAVwRcUxMlBj7XwMzuNZGECcinYeweF29FOGR/liLtYWmUkuIU1ASPCCGw8Nrh+lipfs aEPdPXe2Wl0SS8y1CNEAjSp//sAfXmLSnbuUrRHgFg3VoX1Uo5ZowGGJLNyMV14RvxH9Ec pUDahEvfL48D8Sl9rRvfJ81MxO5H0kasUq5iYN7T55B1rNekUyQ8u84BLNtfgQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=WOaBJpWW; 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=1683458181; 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=a7TbQVwtGlsVMnfnm2gnpx00L55tXlAfCaJ1MDG9iwU=; b=JO1QPiAg+OBFjbu329fgzAMJ085e/PaGu43esolsDmXNT1QvSWXNQ4r5jYzbaJ4ukXQ/uZ 3yfNgo3HCCrzyBmYtflZCK7wqYbohMDz7+rrcC29dVMSCRVBD2WGkP9WKteJwj1nX1Z359 EJQAmmMAyKKj1w2g2R6L0MIJAwNrDiVP218EH4lIBZWTtjwSlRujGuYVv3beqHHF9F3bsM ZuPsZTL8EWBtZ1RUCmHGd7rcUiJva8WuPHYde0yEDJbw+A463LCkYuzb6kngi71OpD5g+u XO4SipiPWG3+fFpRPsk2nSmHdu4o7FTfGvnOznamM4xnnqit2IANTAzD6qd95g== X-Migadu-Spam-Score: -8.30 X-Spam-Score: -8.30 X-Migadu-Queue-Id: 9DAFBB34E X-Migadu-Scanner: scn0.migadu.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=WOaBJpWW; 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-TUID: ZTtEJhSx3H6U --=-=-= Content-Type: text/plain Ruijie Yu writes: > Minor remarks below regarding the patchset. > > Bruno Barbier writes: > >> +;; Copyright (c) 2023 Free Software Foundation, Inc. > > lisp/org.el has only a single space, so probably single space here as well. Done. >> + >> +;; Authors: Bruno BARBIER >> + >> +;; 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. > > Do we need the text for "part of GNU Emacs"? > I guess it doesn't harm: I added it, thanks. >> + >> +(defun test-ob-haskell-ghci--with-global-session-worker (todo) >> + "See `test-ob-haskell-ghci--with-global-session-worker'." > > This docstring doesn't say much and only refers to itself. Maybe > explain what it does? (Or now that I look at it, potentially you wanted > to refer to the macro `test-ob-haskell-ghci-with-global-session' > instead.) I've rewritten that function later ... which made the documentation even worse :-) I've fixed it, thanks. > >> +(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 () > > One space. AFAICS, the last version has only one space here. >> + >> +;;;; Not define errors >> +;; > Single space? It was an invisible 'd' actually; I repainted in black :-) Thanks. Thank you for your 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 136878a096eb9f459e97da6617f94ba84085db9b Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Fri, 18 Nov 2022 20:14:20 +0100 Subject: [PATCH 01/13] ob-haskell: Add tests for GHCi testing/lisp/test-ob-haskell-ghci.el: New file. --- testing/lisp/test-ob-haskell-ghci.el | 454 +++++++++++++++++++++++++++ 1 file changed, 454 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..4023873de --- /dev/null +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -0,0 +1,454 @@ +;;; 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 +;;;; 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=3Dhaskell' BTEST_RE=3D'haskell' tes= t-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 n= o " + args "\n" "" 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 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." + (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 =3D 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 =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 21bfe4a1c932b1cb3a40c8df21e08c1907f08b31 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. --- 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 909de19ab..500be89a2 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.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 040c505ede7b207b8c847660471d53233a956531 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Fri, 24 Mar 2023 11:25:19 +0100 Subject: [PATCH 03/13] * testing/lisp/test-ob-haskell-ghci.el: Fix some tes= ts (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 4023873de..1a060a412 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -263,8 +263,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] @@ -445,8 +444,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 d37a3db8eb5ff1f8cdeb7625bff7b07e2e5bfe83 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 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 1a060a412..0a5e83280 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -119,7 +119,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" " :{ @@ -189,7 +188,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 @@ -201,7 +199,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 @@ -236,7 +233,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 "" " :{ @@ -360,7 +356,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 Content-Disposition: attachment; filename=0005-lisp-ob-haskell-Request-the-last-value-from-GHCi.patch >From 6857bce319ea181ddf40e0b1dadd16c35badbbe4 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 0a5e83280..eefa26042 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -114,8 +114,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 () @@ -417,12 +417,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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0006-ob-haskell-Implement-sessions.patch >From f8a7978467551057439f0baa2342f4a6945df43a 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 * 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 961ae9c8a..6bbc91439 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 eefa26042..0a7e5738c 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -134,7 +134,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))) @@ -144,11 +143,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.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0007-ob-haskell-Update-tests-about-errors.patch >From b8c8038f79247b9ab1643c4766c2c85ff2a2f6d8 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 25 Mar 2023 10:09:26 +0100 Subject: [PATCH 07/13] 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 0a7e5738c..089042553 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -345,18 +345,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.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0008-testing-lisp-test-ob-haskell-ghci.el-Cleanup-comment.patch >From ad1ce32a7d281a842ba3b790b79099254b619fff Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 1 Apr 2023 10:00:30 +0200 Subject: [PATCH 08/13] * testing/lisp/test-ob-haskell-ghci.el: Cleanup comments --- testing/lisp/test-ob-haskell-ghci.el | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el index 089042553..2bcff5ee0 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -25,24 +25,6 @@ ;; ;; - 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: ;; -- 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0009-lisp-ob-haskell.el-Simplify-org-babel-haskell-eoe.patch >From 6ca6e108979eced50ae10d1efc0bf0f55d5f6a75 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 1 Apr 2023 10:19:24 +0200 Subject: [PATCH 09/13] lisp/ob-haskell.el: Simplify org-babel-haskell-eoe lisp/ob-haskell.el (org-babel-haskell-eoe): New default value. (org-babel-interpret-haskell): Update for the new value of `org-babel-haskell-eoe'. --- lisp/ob-haskell.el | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index 6bbc91439..98b1b10f0 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -63,7 +63,7 @@ (defvar org-babel-default-header-args:haskell (defvar org-babel-haskell-lhs2tex-command "lhs2tex") -(defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"") +(defvar org-babel-haskell-eoe "org-babel-haskell-eoe") (defvar haskell-prompt-regexp) @@ -143,7 +143,7 @@ (defun org-babel-interpret-haskell (body params) (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")) + (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) @@ -153,13 +153,13 @@ (defun org-babel-interpret-haskell (body params) (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")) + (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")) + (insert (concat "putStrLn \"" org-babel-haskell-eoe "\"\n")) (comint-send-input nil t)) ) )) -- 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0010-testing-lisp-test-ob-haskell-ghci.el-Test-output-wit.patch >From c4adca4caa3d6d10ba7b65c4de7c132aa56871d0 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 29 Apr 2023 10:27:57 +0200 Subject: [PATCH 10/13] * 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 2bcff5ee0..36f745e61 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -166,6 +166,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 --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0011-lisp-ob-haskell.el-Fix-how-to-use-sessions.patch Content-Transfer-Encoding: quoted-printable >From 2bae53acd280b394c8f1624aef2426bbe6720f03 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 29 Apr 2023 10:43:16 +0200 Subject: [PATCH 11/13] lisp/ob-haskell.el: Fix how to use sessions * lisp/ob-haskell.el (org-babel-haskell-initiate-session): Redesign how to handle session names. (org-babel-haskell-with-session): New function 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. --- lisp/ob-haskell.el | 182 +++++++++++++++++++++++++++------------------ 1 file changed, 110 insertions(+), 72 deletions(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index 98b1b10f0..deaa434f8 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -129,54 +129,58 @@ (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 (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__=3D()::()= \n") - (comint-send-input nil t) - (insert full-body) - (comint-send-input nil t) - (insert "__LAST_VALUE_IMPROBABLE_NAME__=3Dit\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))))= ))) - (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 + params + (lambda (session) + (cl-labels + ((csend (txt) + (insert txt) (comint-send-input nil t)) + (eom () + (csend (concat "putStrLn \"" org-babel-haskell-eoe "\"\n"))) + (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 + (with-output + (lambda () (csend (org-trim full-body)) (eom)))) + (`value + ;; We first compute the value and store the + ;; value, ignoring any output. + (with-output + (lambda () + (csend "__LAST_VALUE_IMPROBABLE_NAME__=3D()::()\n= ") + (csend (org-trim full-body)) + (csend "__LAST_VALUE_IMPROBABLE_NAME__=3Dit\n") + (eom))) + ;; We now display and capture the value. + (with-output + (lambda() + (csend "__LAST_VALUE_IMPROBABLE_NAME__\n") + (eom)))))) + (results (mapcar #'org-strip-quotes + (cdr (member org-babel-haskell-eoe + (reverse (mapcar #'org-trim ra= w))))))) + (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." @@ -186,6 +190,23 @@ (defun org-babel-execute:haskell (body params) (org-babel-haskell-execute body params)))) =20 =20 +(defun org-babel-haskell-with-session (params todo) + "Call TODO with a suitable session buffer. +Use PARAMS to get/create/destroy the session as needed. +Return the result of the call." + (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))))))) =20 =20 ;; Variable defined in inf-haskell (haskell-mode package). @@ -193,34 +214,51 @@ (defvar inferior-haskell-buffer) =20 (defun org-babel-haskell-initiate-session (&optional session-name _params) "Initiate a haskell session. -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") - (when (and session-name (string=3D 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*"))) + (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*"))) (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-liv= ep session))) - (when (bufferp "*haskell*") (error "Conflicting buffer '*has= kell*', 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)))) + ;; 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 (and (bufferp session) + (not (org-babel-comint-buffer-livep session))) + (when (bufferp "*haskell*") + (user-error "Conflicting buffer '*haskell*', rename it o= r kill it")) + (with-current-buffer session (rename-buffer "*haskell*")))) + (unwind-protect + (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)) + (when (and (not (equal "*haskell*" session-name)) + (bufferp session)) + (with-current-buffer session (rename-buffer session-name))= )) + ;; Disable secondary prompt. + (org-babel-comint-input-command + session + ":set prompt-cont \"\"") + session) + )) session)) =20 =20 @@ -237,7 +275,7 @@ (defun org-babel-load-session:haskell (session body par= ams) (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) --=20 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0012-testing-lisp-test-ob-haskell-ghci.el-Modify-test-ob-.patch >From 61b7727d950bf884c13110f39c6b17eba0237b32 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 29 Apr 2023 11:06:45 +0200 Subject: [PATCH 12/13] * testing/lisp/test-ob-haskell-ghci.el: Modify `test-ob-haskell-ghci` * testing/lisp/test-ob-haskell-ghci.el (test-ob-haskell-ghci--with-global-session-worker) (test-ob-haskell-ghci-with-global-session): Deleted. (test-ob-haskell-ghci-checking-buffers): New function. (test-ob-haskell-ghci): Update to handle the new meaning of sessions. --- testing/lisp/test-ob-haskell-ghci.el | 42 +++++++++++----------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el index 36f745e61..fcce06365 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -38,43 +38,33 @@ (unless (featurep 'haskell-mode) ;;; Helpers ;; -(defun test-ob-haskell-ghci--with-global-session-worker (todo) - "See `test-ob-haskell-ghci--with-global-session-worker'." +(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")) - (unwind-protect (funcall todo) - ;; Kill the "*haskell*" buffer to not pollute other tests. + (prog1 (funcall todo) (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))) + ;; 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, don't control -which session is used (i.e. don't call -`test-ob-haskell-ghci--with-global-session-worker')." +before the code block. When UNPROTECTED is non-nil, check pre/post conditions." (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" "" content "\n#+end_src") - (org-babel-execute-src-block))))) - (if unprotected (funcall todo) - (test-ob-haskell-ghci-with-global-session (funcall todo))))) + (let ((todo (lambda () + (prog1 (org-test-with-temp-text + (concat preamble "\n" "#+begin_src haskell :compile no " + args "\n" "" content "\n#+end_src") + (org-babel-execute-src-block)))))) + (if unprotected (funcall todo) + (test-ob-haskell-ghci-checking-buffers todo)))) + ;;; Tests -- 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0013-testing-lisp-test-ob-haskell-ghci.el-Update-session-.patch >From 52b7b78e3413e93cb8098e699a0b44c04e94bb37 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 29 Apr 2023 11:10:42 +0200 Subject: [PATCH 13/13] * testing/lisp/test-ob-haskell-ghci.el: Update session tests * testing/lisp/test-ob-haskell-ghci.el (ob-haskell/no-session-means-one-shot-sessions): Deleted. (ob-haskell/session-named-none-means-one-shot-sessions): New test. (ob-haskell/sessions-must-not-share-variables): Rewrite tests to match the new meaning of session names. (ob-haskell/may-use-the-*haskell*-session): New test. --- testing/lisp/test-ob-haskell-ghci.el | 36 +++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el index fcce06365..f49ebcc40 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -106,24 +106,38 @@ (ert-deftest ob-haskell/hello-world-output-multilines () (ert-deftest ob-haskell/sessions-must-not-share-variables () "Sessions must not share variables." - (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 () + (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." - (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 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 --=-=-=--