2012-05-17 20:12:40 +02:00
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
(require 'ert)
|
|
|
|
|
|
|
|
(require 'ein-notebooklist)
|
2012-05-26 22:39:39 +02:00
|
|
|
(require 'wid-edit)
|
2012-05-17 20:12:40 +02:00
|
|
|
|
|
|
|
;; Execute `eintest:dz-ipython-start' before starting the following
|
|
|
|
;; test to setup server.
|
|
|
|
;; See: ./setup-server.el
|
|
|
|
|
|
|
|
|
|
|
|
(defvar eintest:port 8889)
|
|
|
|
|
2012-05-17 20:47:23 +02:00
|
|
|
(defun eintest: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-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))
|
|
|
|
(error "Timeout")))
|
2012-05-17 20:12:40 +02:00
|
|
|
|
|
|
|
(defun eintest:get-notebook-by-name (url-or-port notebook-name)
|
2012-06-13 01:19:49 +02:00
|
|
|
;; Kill notebook list buffer here to make sure next
|
|
|
|
;; `eintest:wait-until' works properly.
|
|
|
|
(kill-buffer (ein:notebooklist-get-buffer url-or-port))
|
2012-05-17 20:12:40 +02:00
|
|
|
(with-current-buffer (ein:notebooklist-open url-or-port nil)
|
2012-05-17 20:47:23 +02:00
|
|
|
(eintest:wait-until (lambda () ein:notebooklist))
|
2012-07-05 00:14:52 +02:00
|
|
|
(ein:notebooklist-open-notebook-by-name notebook-name)))
|
2012-05-17 20:12:40 +02:00
|
|
|
|
|
|
|
(defun eintest:get-untitled0-or-create (url-or-port)
|
|
|
|
(let ((notebook (eintest:get-notebook-by-name url-or-port "Untitled0")))
|
|
|
|
(if notebook
|
|
|
|
notebook
|
2012-05-26 22:37:26 +02:00
|
|
|
(with-current-buffer (ein:notebooklist-open url-or-port t)
|
2012-05-17 20:47:23 +02:00
|
|
|
(setq ein:notebooklist nil)
|
|
|
|
(eintest:wait-until (lambda () ein:notebooklist))
|
|
|
|
(ein:notebooklist-new-notebook url-or-port)
|
2012-05-26 22:37:26 +02:00
|
|
|
(eintest:wait-until
|
|
|
|
(lambda () (eintest:get-notebook-by-name url-or-port "Untitled0"))))
|
2012-05-17 20:12:40 +02:00
|
|
|
(eintest:get-notebook-by-name url-or-port "Untitled0"))))
|
|
|
|
|
2012-05-26 22:39:39 +02:00
|
|
|
(defun eintest:delete-notebook-by-name (url-or-port notebook-name)
|
|
|
|
(with-current-buffer (ein:notebooklist-open url-or-port nil)
|
|
|
|
(eintest:wait-until (lambda () ein:notebooklist))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(search-forward notebook-name)
|
|
|
|
(move-beginning-of-line 1)
|
|
|
|
(search-forward "Delete")
|
|
|
|
(flet ((y-or-n-p (ignore) t))
|
|
|
|
(widget-button-press (point))))
|
|
|
|
(setq ein:notebooklist nil)
|
|
|
|
(eintest:wait-until (lambda () ein:notebooklist))))
|
|
|
|
|
2012-05-17 20:12:40 +02:00
|
|
|
(ert-deftest eintest:get-untitled0-or-create ()
|
|
|
|
(let ((notebook (eintest:get-untitled0-or-create eintest:port)))
|
2012-05-17 20:47:23 +02:00
|
|
|
(eintest:wait-until (lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 20:12:40 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
|
|
|
(should (equal (ein:$notebook-notebook-name ein:notebook) "Untitled0")))))
|
2012-05-17 20:55:51 +02:00
|
|
|
|
2012-05-26 22:39:39 +02:00
|
|
|
(ert-deftest eintest:delete-untitled0 ()
|
|
|
|
(loop
|
|
|
|
repeat 2
|
|
|
|
do (let ((notebook (eintest:get-untitled0-or-create eintest:port)))
|
|
|
|
(eintest:wait-until
|
|
|
|
(lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it)))))
|
2012-05-26 22:39:39 +02:00
|
|
|
do (eintest:delete-notebook-by-name eintest:port "Untitled0")
|
2012-06-13 01:22:18 +02:00
|
|
|
do (let ((num-notebook
|
|
|
|
(length (eintest:get-notebook-by-name eintest:port "Untitled0"))))
|
|
|
|
(should (= num-notebook 0)))))
|
2012-05-26 22:39:39 +02:00
|
|
|
|
2012-05-17 20:55:51 +02:00
|
|
|
(ert-deftest ein:notebook-execute-current-cell-simple ()
|
|
|
|
(let ((notebook (eintest:get-untitled0-or-create eintest:port)))
|
|
|
|
(eintest:wait-until (lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 20:55:51 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
|
|
|
(ein:notebook-insert-cell-below-command)
|
|
|
|
(insert "a = 100\na")
|
|
|
|
(let ((cell (ein:notebook-execute-current-cell)))
|
2012-05-17 21:04:29 +02:00
|
|
|
(eintest: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))))))
|
|
|
|
|
|
|
|
(ert-deftest ein:notebook-execute-current-cell-pyout-image ()
|
|
|
|
(let ((notebook (eintest:get-untitled0-or-create eintest:port)))
|
|
|
|
(eintest:wait-until (lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 21:04:29 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
|
|
|
(ein:notebook-insert-cell-below-command)
|
|
|
|
(insert (ein:join-str "\n" '("import pylab"
|
|
|
|
"pylab.plot([1,2,3])")))
|
|
|
|
(let ((cell (ein:notebook-execute-current-cell)))
|
|
|
|
(eintest:wait-until (lambda () (not (oref cell :running)))))
|
|
|
|
(save-excursion
|
|
|
|
(should (search-forward-regexp "Out \\[[0-9]+\\]" nil t))
|
|
|
|
(should (search-forward-regexp
|
|
|
|
"<matplotlib\\.lines\\.Line2D at .*>" nil t))))))
|
|
|
|
|
|
|
|
(ert-deftest ein:notebook-execute-current-cell-stream ()
|
|
|
|
(let ((notebook (eintest:get-untitled0-or-create eintest:port)))
|
|
|
|
(eintest:wait-until (lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it))))
|
2012-05-17 21:04:29 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
|
|
|
(ein:notebook-insert-cell-below-command)
|
|
|
|
(insert "print 'Hello'")
|
|
|
|
(let ((cell (ein:notebook-execute-current-cell)))
|
|
|
|
(eintest:wait-until (lambda () (not (oref cell :running)))))
|
|
|
|
(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 ()
|
|
|
|
(let ((notebook (eintest:get-untitled0-or-create eintest:port)))
|
|
|
|
(eintest:wait-until (lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it))))
|
2012-05-23 02:20:33 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
|
|
|
(ein:notebook-insert-cell-below-command)
|
|
|
|
(insert "range?")
|
|
|
|
(let ((cell (ein:notebook-execute-current-cell)))
|
|
|
|
(eintest:wait-until (lambda () (not (oref cell :running)))))
|
|
|
|
(with-current-buffer (get-buffer (ein:$notebook-pager ein:notebook))
|
|
|
|
(should (search-forward "Docstring:\nrange"))))))
|
|
|
|
|
2012-05-23 02:03:23 +02:00
|
|
|
(ert-deftest ein:notebook-request-help ()
|
|
|
|
(let ((notebook (eintest:get-untitled0-or-create eintest:port)))
|
|
|
|
(eintest:wait-until (lambda () (ein:aand (ein:$notebook-kernel notebook)
|
2012-07-19 00:16:00 +02:00
|
|
|
(ein:kernel-live-p it))))
|
2012-05-23 02:03:23 +02:00
|
|
|
(with-current-buffer (ein:notebook-buffer notebook)
|
|
|
|
(ein:notebook-insert-cell-below-command)
|
|
|
|
(let ((pager-name (ein:$notebook-pager ein:notebook)))
|
|
|
|
(ein:aif (get-buffer pager-name)
|
|
|
|
(kill-buffer it))
|
|
|
|
(insert "file")
|
|
|
|
(ein:notebook-request-help-command)
|
|
|
|
;; Pager buffer will be created when got the response
|
|
|
|
(eintest:wait-until (lambda () (get-buffer pager-name)))
|
|
|
|
(with-current-buffer (get-buffer pager-name)
|
|
|
|
(should (search-forward "Docstring:\nfile")))))))
|