emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob adc9f939c18b663f15703588d8f0bacc96617f83 12817 bytes (raw)
name: testing/lisp/test-ob-haskell-ghci.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
 
;;; test-ob-haskell-ghci.el --- tests for ob-haskell.el GHCi  -*- lexical-binding: t; -*-

;; Copyright (c) 2023 Free Software Foundation, Inc.
;; Authors: Bruno BARBIER <brubar.cs@gmail.com>

;; This 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 <https://www.gnu.org/licenses/>.

;;; Commentary:
;;

;;;; Useful references
;;
;;  - https://orgmode.org/worg/org-contrib/babel/languages/lang-compat.html
;;  - GHCi manual: https://downloads.haskell.org/ghc/latest/docs/users_guide/ghci.html

;;; 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 conditions."
  (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 :compile no "
                                   args "\n" "<point>" content "\n#+end_src")
                         (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 ()
  ;; 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 ()
  (should (equal "Hello World!"
                 (test-ob-haskell-ghci ":results output" "
:{
main :: IO ()
main = putStrLn \"Hello World!\"
:}

main
"))))

;;;; Sessions
;;

(ert-deftest ob-haskell/sessions-must-not-share-variables ()
  "Sessions must not share variables."
  (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
;;

(ert-deftest ob-haskell/value-is-the-last-expression ()
  "Return the value of the last expression."
  (should (equal 3 (test-ob-haskell-ghci "" '("1" "1+1" "1+1+1"))))
  (should (equal 3 (test-ob-haskell-ghci "" '("x=1" "y=1+1" "x+y")))))

(ert-deftest ob-haskell/value-is-the-last-expression-2 ()
  "Return the value of the last expression."
  (should (equal 7 (test-ob-haskell-ghci "" "
putStrLn \"a string\"
return \"useless\"
3+4
"))))



(ert-deftest ob-haskell/eval-numbers ()
  "Evaluation of numbers."
  (should (equal 7 (test-ob-haskell-ghci "" "7")))
  (should (equal 7.5 (test-ob-haskell-ghci "" "7.5")))
  (should (equal 10.0 (test-ob-haskell-ghci "" "10::Double")))
  (should (equal 10   (test-ob-haskell-ghci "" "10::Int"))))


(ert-deftest ob-haskell/eval-strings ()
  "Evaluation of strings."
  (should (equal "a string" (test-ob-haskell-ghci "" "\"a string\""))))


;;;; Local variables
(ert-deftest ob-haskell/let-one-line ()
  "Local definitions on one line."
  (should (equal 6 (test-ob-haskell-ghci "" "let { x=2; y=3 } in x*y"))))

(ert-deftest ob-haskell/let-multilines-1 ()
  "Local definitions on multiple lines."
  (should (equal 6 (test-ob-haskell-ghci "" "
:{
 let { x=2
     ; y=3
     }
 in x*y
:}
"))))

(ert-deftest ob-haskell/let-multilines-2 ()
  "Local definitions on multiple lines, relying on indentation."
  (should (equal 6 (test-ob-haskell-ghci "" "
:{
  let x=2
      y=3
  in x*y
:}
"))))

;;;; Declarations with multiple lines.
(ert-deftest ob-haskell/decl-multilines-1 ()
  "A multiline declaration, then use it."
  (should (equal 3 (test-ob-haskell-ghci "" "
:{
let length' []    = 0
    length' (_:l) = 1 + length' l
:}
length' [1,2,3]
"))))

(ert-deftest ob-haskell/decl-multilines-2 ()
  "A multiline declaration, then use it."
  (should (equal 5 (test-ob-haskell-ghci "" "
:{
length'       :: [a] -> Int
length' []    =  0
length' (_:l) =  1 + length' l
:}

length' [1..5]
"))))


(ert-deftest ob-haskell/primes ()
  "From haskell.org."""
  (should (equal '(2 3 5 7 11 13 17 19 23 29)
                 (test-ob-haskell-ghci "" "
:{
primes = filterPrime [2..] where
  filterPrime (p:xs) =
    p : filterPrime [x | x <- xs, x `mod` p /= 0]
:}

take 10 primes
"))))

;;;; Lists
;;

(ert-deftest ob-haskell/a-simple-list ()
  "Evaluation of list of values."
  (should (equal '(1 2 3) (test-ob-haskell-ghci "" "[1,2,3]"))))


(ert-deftest ob-haskell/2D-lists ()
  "Evaluation of nested lists into a table."
  (should (equal '((1 2 3) (4 5 6))
                 (test-ob-haskell-ghci "" "[[1..3], [4..6]]"))))

(ert-deftest ob-haskell/2D-lists-multilines ()
  "Evaluation of nested lists into a table, as multilines."
  (should (equal '((1 2 3) (4 5 6) (7 8 9))
                 (test-ob-haskell-ghci "" "
:{
[ [1..3]
, [4..6]
, [7..9]
]
:}
"))))


;;;; Tuples
;;

(ert-deftest ob-haskell/a-simple-tuple ()
  "Evaluation of tuple of values."
  (should (equal '(1 2 3) (test-ob-haskell-ghci "" "(1,2,3)"))))


(ert-deftest ob-haskell/2D-tuples ()
  "Evaluation of nested tuples into a table."
  (should (equal '((1 2 3) (4 5 6))
                 (test-ob-haskell-ghci "" "((1,2,3), (4,5,6))"))))

(ert-deftest ob-haskell/2D-tuples-multilines ()
  "Evaluation of nested tuples into a table, as multilines."
  (should (equal '((1 2 3) (4 5 6) (7 8 9))
                 (test-ob-haskell-ghci "" "
:{
( (1,2,3)
, (4,5,6)
, (7,8,9)
)
:}
"))))


;;;; Data tables
;;

(ert-deftest ob-haskell/int-table-data ()
  "From worg: int-table-data."
  (should (equal 10 (test-ob-haskell-ghci ":var t=int-table-data"
                                          "sum [sum r | r <- t]"
                                          "#+name: int-table-data
    | 1 | 2 |
    | 3 | 4 |"))))

(ert-deftest ob-haskell/float-table-data ()
  "From worg: float-table-data."
  (should (equal 11.0 (test-ob-haskell-ghci ":var t=float-table-data"
                                            "sum [sum r | r <- t]"
                                            "#+name: float-table-data
    | 1.1 | 2.2 |
    | 3.3 | 4.4 |"))))

(ert-deftest ob-haskell/string-table-data ()
  "From worg: string-table-data."
  (should (equal "abcd" (test-ob-haskell-ghci ":var t=string-table-data"
                                              "concat [concat r | r <- t]"
                                              "#+name: string-table-data
    | a | b |
    | c | d |"))))

;;;; Reuse results
;;
(ert-deftest ob-haskell/reuse-table ()
  "Reusing a computed tables."
  (should (equal 78 (test-ob-haskell-ghci ":var t=a-table"
                                          "sum [sum r | r <- t]"
                                          "#+name: a-table
#+begin_src haskell
   [ [x..x+2] | x <- [1,4 .. 12] ]
#+end_src
"))))


;;;; Not 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 = 4
")))


(ert-deftest ob-haskell/not-defined-then-defined-1-fixed ()
  "Like not-defined-then-defined-1, but using the mutiline marks."
  (let ((r (test-ob-haskell-ghci "" "
:{
  v :: Int
  v = 4
:}
")))
    (should (eq nil r))))

(ert-deftest ob-haskell/not-defined-then-defined-1-fixed-2 ()
  "Like not-defined-then-defined-1, but using one line."
  (should (eq nil (test-ob-haskell-ghci "" "v = 4 :: Int"))))



(ert-deftest ob-haskell/not-defined-then-defined-2 ()
  "Evaluation of undefined variables, followed by a correct one."
  ;; ghci output is:
  ;;  | <interactive>:2:1-4: error:
  ;;  |     • Variable not in scope: main :: IO ()
  ;;  |     • Perhaps you meant ‘min’ (imported from Prelude)
  ;;  | Hello, World!
  ;; and ob-haskell just reports the last line "Hello, World!".
  (should (string-match "Variable not in scope"
                        (test-ob-haskell-ghci ":results output" "
main :: IO ()
main = putStrLn \"Hello, World!\"
main
"))))

;;;; Imports
;;

(ert-deftest ob-haskell/import ()
  "Import and use library."
  (should (equal 65 (test-ob-haskell-ghci "" "
import Data.IORef
r <- newIORef 65
readIORef r
"))))

(ert-deftest ob-haskell/import-with-vars ()
  "Import and use library with vars."
  (should (equal 65 (test-ob-haskell-ghci ":var x=65" "
import Data.IORef
r <- newIORef x
readIORef r
"))))

;;;; What is the result?
;;

(ert-deftest ob-haskell/results-value-1 ()
  "Don't confuse output and values: nothing."
  (should (equal nil (test-ob-haskell-ghci ":results value" "return ()"))))

(ert-deftest ob-haskell/results-value-2 ()
  "Don't confuse output and values: a list."
  (should (equal '(1 2) (test-ob-haskell-ghci ":results value" "return [1,2]"))))

(ert-deftest ob-haskell/results-value-3 ()
  "Don't confuse output and values: nothing."
  (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."
  (should (equal nil (test-ob-haskell-ghci ":results value" "
putStrLn \"3\"
return ()
"))))


;;;; GHCi commands
;;

(ert-deftest ob-haskell/ghci-type ()
  "The ghci meta command ':type'."
  (should (equal "3 :: Num p => p"
                 (test-ob-haskell-ghci ":results output" ":type 3"))))

(ert-deftest ob-haskell/ghci-info ()
  "The ghci meta command ':info' ."
  (should (string-match-p
           "repeat :: a -> \\[a\\][ \t]+-- Defined in ‘GHC.List’"
           (test-ob-haskell-ghci ":results output" ":info repeat"))))


(provide 'test-ob-haskell-ghci)

;;; test-ob-haskell-ghci.el ends here

debug log:

solving adc9f939c ...
found adc9f939c in https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
found 5049bc01a in https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
found 868a56e63 in https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
found cf59edb25 in https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
found 330937917 in https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/

applying [1/5] https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
new file mode 100644
index 000000000..330937917


applying [2/5] https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index 330937917..cf59edb25 100644


applying [3/5] https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index cf59edb25..868a56e63 100644


applying [4/5] https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index 868a56e63..5049bc01a 100644


applying [5/5] https://yhetil.org/orgmode/6469cafa.1c0a0220.4db6f.0396@mx.google.com/
diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el
index 5049bc01a..adc9f939c 100644

Checking patch testing/lisp/test-ob-haskell-ghci.el...
Applied patch testing/lisp/test-ob-haskell-ghci.el cleanly.
Checking patch testing/lisp/test-ob-haskell-ghci.el...
Applied patch testing/lisp/test-ob-haskell-ghci.el cleanly.
Checking patch testing/lisp/test-ob-haskell-ghci.el...
Applied patch testing/lisp/test-ob-haskell-ghci.el cleanly.
Checking patch testing/lisp/test-ob-haskell-ghci.el...
Applied patch testing/lisp/test-ob-haskell-ghci.el cleanly.
Checking patch testing/lisp/test-ob-haskell-ghci.el...
Applied patch testing/lisp/test-ob-haskell-ghci.el cleanly.

index at:
100644 adc9f939c18b663f15703588d8f0bacc96617f83	testing/lisp/test-ob-haskell-ghci.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).