2012-05-17 20:12:40 +02:00
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
(require 'ert)
|
|
|
|
|
2015-02-02 13:20:24 -06:00
|
|
|
(require 'ein-loaddefs)
|
2012-05-17 20:12:40 +02:00
|
|
|
(require 'ein-notebooklist)
|
2012-05-26 22:39:39 +02:00
|
|
|
(require 'wid-edit)
|
2012-08-23 19:32:47 +02:00
|
|
|
(require 'ein-testing)
|
2012-09-17 22:39:11 +02:00
|
|
|
(require 'ein-testing-cell)
|
2012-08-23 19:32:47 +02:00
|
|
|
|
2013-01-16 21:00:43 +01:00
|
|
|
(let ((backend (getenv "EL_REQUEST_BACKEND")))
|
|
|
|
(when (and backend (not (equal backend "")))
|
|
|
|
(setq request-backend (intern backend))
|
|
|
|
(message "Using request-backend = %S" request-backend)))
|
|
|
|
|
2012-08-23 19:32:47 +02:00
|
|
|
(ein:setq-if-not ein:testing-dump-file-log "func-test-batch-log.log")
|
|
|
|
(ein:setq-if-not ein:testing-dump-file-messages "func-test-batch-messages.log")
|
2012-09-18 22:50:52 +02:00
|
|
|
(setq message-log-max t)
|
2012-05-17 20:12:40 +02:00
|
|
|
|
|
|
|
|
2012-09-17 17:54:46 +02:00
|
|
|
(defvar ein:testing-port 8889)
|
2012-05-17 20:12:40 +02:00
|
|
|
|
2012-09-17 17:54:46 +02:00
|
|
|
(defun ein:testing-wait-until (predicate &optional predargs max-count)
|
2012-05-17 21:25:03 +02:00
|
|
|
"Wait until PREDICATE function returns non-`nil'.
|
|
|
|
PREDARGS is argument list for the PREDICATE function.
|
|
|
|
Make MAX-COUNT larger \(default 50) to wait longer before timeout."
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:log 'debug "TESTING-WAIT-UNTIL start")
|
2012-05-17 20:47:23 +02:00
|
|
|
(unless (setq max-count 50))
|
2012-05-17 21:25:03 +02:00
|
|
|
(unless (loop repeat max-count
|
|
|
|
when (apply predicate predargs)
|
|
|
|
return t
|
|
|
|
;; borrowed from `deferred:sync!':
|
|
|
|
do (sit-for 0.05)
|
|
|
|
do (sleep-for 0.05))
|
2012-08-23 21:17:19 +02:00
|
|
|
(error "Timeout"))
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:log 'debug "TESTING-WAIT-UNTIL end"))
|
2012-05-17 20:12:40 +02:00
|
|
|
|
2014-12-04 13:52:36 -06:00
|
|
|
(defun ein:testing-get-notebook-by-name (url-or-port notebook-name &optional path)
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:log 'debug "TESTING-GET-NOTEBOOK-BY-NAME start")
|
2012-06-13 01:19:49 +02:00
|
|
|
;; Kill notebook list buffer here to make sure next
|
2012-09-17 17:54:46 +02:00
|
|
|
;; `ein:testing-wait-until' works properly.
|
2012-06-13 01:19:49 +02:00
|
|
|
(kill-buffer (ein:notebooklist-get-buffer url-or-port))
|
2014-12-04 13:52:36 -06:00
|
|
|
(unless path (setq path ""))
|
2012-05-17 20:12:40 +02:00
|
|
|
(with-current-buffer (ein:notebooklist-open url-or-port nil)
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:testing-wait-until (lambda () ein:%notebooklist%))
|
2012-08-23 21:17:19 +02:00
|
|
|
(prog1
|
|
|
|
(ein:notebooklist-open-notebook-by-name notebook-name)
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:log 'debug "TESTING-GET-NOTEBOOK-BY-NAME end"))))
|
2012-05-17 20:12:40 +02:00
|
|
|
|
2014-12-04 13:52:36 -06:00
|
|
|
(defun ein:testing-get-untitled0-or-create (url-or-port &optional path)
|
|
|
|
(unless path (setq path ""))
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:log 'debug "TESTING-GET-UNTITLED0-OR-CREATE start")
|
|
|
|
(let ((notebook (ein:testing-get-notebook-by-name url-or-port "Untitled0")))
|
2012-05-17 20:12:40 +02:00
|
|
|
(if notebook
|
2012-08-23 21:17:19 +02:00
|
|
|
(progn (ein:log 'debug
|
2012-09-17 17:54:46 +02:00
|
|
|
"TESTING-GET-UNTITLED0-OR-CREATE notebook already exists")
|
2012-08-23 21:17:19 +02:00
|
|
|
notebook)
|
2012-09-17 20:24:55 +02:00
|
|
|
(ein:log 'debug
|
|
|
|
"TESTING-GET-UNTITLED0-OR-CREATE creating notebook")
|
2016-05-17 16:22:05 -05:00
|
|
|
(let ((created nil)
|
|
|
|
(kernelspec (first (ein:list-available-kernels url-or-port))))
|
|
|
|
(ein:notebooklist-new-notebook url-or-port kernelspec path
|
2012-09-17 20:24:55 +02:00
|
|
|
(lambda (&rest -ignore-)
|
|
|
|
(setq created t)))
|
|
|
|
(ein:testing-wait-until (lambda () created)))
|
2012-08-23 21:17:19 +02:00
|
|
|
(prog1
|
2014-12-04 13:52:36 -06:00
|
|
|
(ein:testing-get-notebook-by-name url-or-port "Untitled0" path)
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:log 'debug "TESTING-GET-UNTITLED0-OR-CREATE end")))))
|
2012-05-17 20:12:40 +02:00
|
|
|
|
2012-12-30 15:59:16 +01:00
|
|
|
(defvar ein:notebooklist-after-open-hook nil)
|
|
|
|
|
|
|
|
(defadvice ein:notebooklist-url-retrieve-callback
|
|
|
|
(after ein:testing-notebooklist-url-retrieve-callback activate)
|
|
|
|
"Advice to add `ein:notebooklist-after-open-hook'."
|
|
|
|
(run-hooks 'ein:notebooklist-after-open-hook))
|
|
|
|
|
2012-09-17 17:54:46 +02:00
|
|
|
(defun ein:testing-delete-notebook-by-name (url-or-port notebook-name)
|
|
|
|
(ein:log 'debug "TESTING-DELETE-NOTEBOOK-BY-NAME start")
|
2012-12-30 15:59:16 +01:00
|
|
|
(lexical-let (called-p)
|
|
|
|
(let ((ein:notebooklist-after-open-hook
|
|
|
|
(list (lambda () (setq called-p t)))))
|
|
|
|
(with-current-buffer (ein:notebooklist-open url-or-port nil)
|
|
|
|
(ein:testing-wait-until (lambda () ein:%notebooklist%))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(search-forward notebook-name)
|
|
|
|
(move-beginning-of-line 1)
|
|
|
|
(search-forward "Delete")
|
2014-12-03 07:05:55 -06:00
|
|
|
(cl-flet ((y-or-n-p (ignore) t))
|
2012-12-30 15:59:16 +01:00
|
|
|
(widget-button-press (point))))
|
|
|
|
(ein:testing-wait-until (lambda () called-p))
|
|
|
|
(ein:log 'debug "TESTING-DELETE-NOTEBOOK-BY-NAME end")))))
|
2012-09-17 17:54:46 +02:00
|
|
|
|
|
|
|
(ert-deftest ein:testing-get-untitled0-or-create ()
|
|
|
|
(ein:log 'verbose "ERT TESTING-GET-UNTITLED0-OR-CREATE start")
|
|
|
|
(let ((notebook (ein:testing-get-untitled0-or-create ein:testing-port)))
|
|
|
|
(ein:testing-wait-until
|
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 20:12:40 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
2012-09-17 17:54:46 +02:00
|
|
|
(should (equal (ein:$notebook-notebook-name ein:%notebook%)
|
|
|
|
"Untitled0"))))
|
|
|
|
(ein:log 'verbose "ERT TESTING-GET-UNTITLED0-OR-CREATE end"))
|
2012-05-17 20:55:51 +02:00
|
|
|
|
2012-09-17 17:54:46 +02:00
|
|
|
(ert-deftest ein:testing-delete-untitled0 ()
|
|
|
|
(ein:log 'verbose "ERT TESTING-DELETE-UNTITLED0 start")
|
2012-05-26 22:39:39 +02:00
|
|
|
(loop
|
2012-08-23 21:10:40 +02:00
|
|
|
for i from 0 to 1
|
2012-09-17 17:54:46 +02:00
|
|
|
do (ein:log 'debug "ERT TESTING-DELETE-UNTITLED0 i=%s" i)
|
|
|
|
do (ein:log 'debug "ERT TESTING-DELETE-UNTITLED0 creating notebook")
|
|
|
|
do (let ((notebook (ein:testing-get-untitled0-or-create ein:testing-port)))
|
|
|
|
(ein:testing-wait-until
|
2012-05-26 22:39:39 +02:00
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it)))))
|
2012-09-17 17:54:46 +02:00
|
|
|
do (ein:log 'debug "ERT TESTING-DELETE-UNTITLED0 delete notebook")
|
|
|
|
do (ein:testing-delete-notebook-by-name ein:testing-port "Untitled0")
|
2012-08-23 21:10:40 +02:00
|
|
|
do (ein:log 'debug
|
2012-09-17 17:54:46 +02:00
|
|
|
"ERT TESTING-DELETE-UNTITLED0 check the notebook is delete")
|
2012-06-13 01:22:18 +02:00
|
|
|
do (let ((num-notebook
|
2012-09-17 17:54:46 +02:00
|
|
|
(length (ein:testing-get-notebook-by-name ein:testing-port
|
2014-12-04 13:52:36 -06:00
|
|
|
"Untitled0"
|
|
|
|
""))))
|
2012-08-23 21:10:40 +02:00
|
|
|
(should (= num-notebook 0))))
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:log 'debug "ERT TESTING-DELETE-UNTITLED0 end"))
|
2012-05-26 22:39:39 +02:00
|
|
|
|
2012-05-17 20:55:51 +02:00
|
|
|
(ert-deftest ein:notebook-execute-current-cell-simple ()
|
2012-09-17 17:54:46 +02:00
|
|
|
(let ((notebook (ein:testing-get-untitled0-or-create ein:testing-port)))
|
|
|
|
(ein:testing-wait-until
|
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 20:55:51 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
2012-08-20 10:41:59 +02:00
|
|
|
(call-interactively #'ein:worksheet-insert-cell-below)
|
2012-05-17 20:55:51 +02:00
|
|
|
(insert "a = 100\na")
|
2012-08-20 10:41:59 +02:00
|
|
|
(let ((cell (call-interactively #'ein:worksheet-execute-cell)))
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:testing-wait-until (lambda () (not (oref cell :running)))))
|
2012-05-17 20:55:51 +02:00
|
|
|
;; (message "%s" (buffer-string))
|
|
|
|
(save-excursion
|
|
|
|
(should (search-forward-regexp "Out \\[[0-9]+\\]" nil t))
|
2012-05-17 21:04:29 +02:00
|
|
|
(should (search-forward "100" nil t))))))
|
|
|
|
|
2012-09-17 22:39:11 +02:00
|
|
|
(defun ein:testing-image-type (image)
|
|
|
|
"Return the type of IMAGE.
|
|
|
|
See the definition of `create-image' for how it works."
|
|
|
|
(assert (and (listp image) (eq (car image) 'image)) nil
|
|
|
|
"%S is not an image." image)
|
|
|
|
(plist-get (cdr image) :type))
|
|
|
|
|
2012-05-17 21:04:29 +02:00
|
|
|
(ert-deftest ein:notebook-execute-current-cell-pyout-image ()
|
2012-09-17 17:54:46 +02:00
|
|
|
(let ((notebook (ein:testing-get-untitled0-or-create ein:testing-port)))
|
|
|
|
(ein:testing-wait-until
|
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 21:04:29 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
2012-08-20 10:41:59 +02:00
|
|
|
(call-interactively #'ein:worksheet-insert-cell-below)
|
2012-09-17 22:39:11 +02:00
|
|
|
;; Use IPython.core.display rather than IPython.display to
|
|
|
|
;; test it with older (< 0.13) IPython.
|
|
|
|
(insert (concat "from IPython.core.display import SVG\n"
|
|
|
|
(format "SVG(data=\"\"\"%s\"\"\")"
|
|
|
|
ein:testing-example-svg)))
|
2012-08-20 10:41:59 +02:00
|
|
|
(let ((cell (call-interactively #'ein:worksheet-execute-cell)))
|
2012-09-17 22:57:25 +02:00
|
|
|
;; It seems in this case, watching `:running' does not work
|
|
|
|
;; well sometimes. Probably "output reply" (iopub) comes
|
|
|
|
;; before "execute reply" in this case.
|
|
|
|
(ein:testing-wait-until (lambda () (oref cell :outputs)))
|
2012-09-17 22:39:11 +02:00
|
|
|
;; This cell has only one input
|
|
|
|
(should (= (length (oref cell :outputs)) 1))
|
|
|
|
;; This output is a SVG image
|
|
|
|
(let ((out (nth 0 (oref cell :outputs))))
|
|
|
|
(should (equal (plist-get out :output_type) "pyout"))
|
|
|
|
(should (plist-get out :svg))))
|
|
|
|
;; Check the actual output in the buffer:
|
2012-05-17 21:04:29 +02:00
|
|
|
(save-excursion
|
|
|
|
(should (search-forward-regexp "Out \\[[0-9]+\\]" nil t))
|
2012-09-17 22:39:11 +02:00
|
|
|
(should (= (forward-line) 0))
|
2012-09-17 23:07:15 +02:00
|
|
|
(if (image-type-available-p 'svg)
|
|
|
|
(let ((image (get-text-property (point) 'display)))
|
|
|
|
(should (eq (ein:testing-image-type image) 'svg)))
|
|
|
|
(ein:log 'info
|
|
|
|
"Skipping image check as SVG image type is not available."))))))
|
2012-05-17 21:04:29 +02:00
|
|
|
|
|
|
|
(ert-deftest ein:notebook-execute-current-cell-stream ()
|
2012-09-17 17:54:46 +02:00
|
|
|
(let ((notebook (ein:testing-get-untitled0-or-create ein:testing-port)))
|
|
|
|
(ein:testing-wait-until
|
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 21:04:29 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
2012-08-20 10:41:59 +02:00
|
|
|
(call-interactively #'ein:worksheet-insert-cell-below)
|
2012-05-17 21:04:29 +02:00
|
|
|
(insert "print 'Hello'")
|
2012-08-20 10:41:59 +02:00
|
|
|
(let ((cell (call-interactively #'ein:worksheet-execute-cell)))
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:testing-wait-until (lambda () (not (oref cell :running)))))
|
2012-05-17 21:04:29 +02:00
|
|
|
(save-excursion
|
|
|
|
(should-not (search-forward-regexp "Out \\[[0-9]+\\]" nil t))
|
|
|
|
(should (search-forward-regexp "^Hello$" nil t))))))
|
2012-05-23 02:03:23 +02:00
|
|
|
|
2012-05-23 02:20:33 +02:00
|
|
|
(ert-deftest ein:notebook-execute-current-cell-question ()
|
2012-09-17 17:54:46 +02:00
|
|
|
(let ((notebook (ein:testing-get-untitled0-or-create ein:testing-port)))
|
|
|
|
(ein:testing-wait-until
|
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
|
|
|
(ein:kernel-live-p it))))
|
2012-05-23 02:20:33 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
2012-08-20 10:41:59 +02:00
|
|
|
(call-interactively #'ein:worksheet-insert-cell-below)
|
2012-05-23 02:20:33 +02:00
|
|
|
(insert "range?")
|
2012-08-20 10:41:59 +02:00
|
|
|
(let ((cell (call-interactively #'ein:worksheet-execute-cell)))
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:testing-wait-until (lambda () (not (oref cell :running)))))
|
2012-08-23 17:39:06 +02:00
|
|
|
(with-current-buffer (get-buffer (ein:$notebook-pager notebook))
|
2012-05-23 02:20:33 +02:00
|
|
|
(should (search-forward "Docstring:\nrange"))))))
|
|
|
|
|
2012-05-23 02:03:23 +02:00
|
|
|
(ert-deftest ein:notebook-request-help ()
|
2012-09-17 17:54:46 +02:00
|
|
|
(let ((notebook (ein:testing-get-untitled0-or-create ein:testing-port)))
|
|
|
|
(ein:testing-wait-until
|
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
|
|
|
(ein:kernel-live-p it))))
|
2012-05-23 02:03:23 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
2012-08-20 10:41:59 +02:00
|
|
|
(call-interactively #'ein:worksheet-insert-cell-below)
|
2012-08-18 23:08:48 +02:00
|
|
|
(let ((pager-name (ein:$notebook-pager ein:%notebook%)))
|
2012-05-23 02:03:23 +02:00
|
|
|
(ein:aif (get-buffer pager-name)
|
|
|
|
(kill-buffer it))
|
|
|
|
(insert "file")
|
2012-08-19 21:45:40 +02:00
|
|
|
(call-interactively #'ein:pytools-request-help)
|
2012-05-23 02:03:23 +02:00
|
|
|
;; Pager buffer will be created when got the response
|
2012-09-17 17:54:46 +02:00
|
|
|
(ein:testing-wait-until (lambda () (get-buffer pager-name)))
|
2012-05-23 02:03:23 +02:00
|
|
|
(with-current-buffer (get-buffer pager-name)
|
|
|
|
(should (search-forward "Docstring:\nfile")))))))
|