emacs-ipython-notebook/tests/test-ein-cell-notebook.el
John Miller c49352e3b7 Fixing tests for IPython 3.x and Jupyter
Fixing test suite for changes in IPython introduced in 3.x and Jupyter. Still a long ways to go, but some improvements in how we generate test notebooks.
2015-08-26 20:49:20 -05:00

249 lines
7.7 KiB
EmacsLisp
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;; Tests for cell function that requires notebook buffer
(eval-when-compile (require 'cl))
(require 'ert)
(require 'ein-notebook)
(require 'ein-testing-notebook)
;; ein:cell-location
(ert-deftest ein:cell-location-codecell-prompt-beg ()
(ein:testing-with-one-cell 'code
(should (equal (marker-position (ein:cell-location cell :prompt))
(save-excursion
(goto-char (point-max))
(search-backward "In [ ]:")
(point))))))
(ert-deftest ein:cell-location-codecell-prompt-end ()
(ein:testing-with-one-cell 'code
(should (equal (marker-position (ein:cell-location cell :prompt t))
(1- (point))))))
(ert-deftest ein:cell-location-codecell-input-beg ()
(ein:testing-with-one-cell 'code
(insert "some text")
(should (equal (marker-position (ein:cell-location cell :input))
(1- (point-at-bol))))))
(ert-deftest ein:cell-location-codecell-input-end ()
(ein:testing-with-one-cell 'code
(insert "some text")
(should (equal (marker-position (ein:cell-location cell :input t))
(1+ (point))))))
;; from-json
(ert-deftest eintest:cell-input-prompt-number ()
(ein:testing-with-one-cell
(ein:cell-from-json
(list :cell_type "code"
:source "some input"
:metadata (list :collapsed json-false :autoscroll json-false)
:execution_count 111)
:ewoc (oref ein:%worksheet% :ewoc))
(goto-char (ein:cell-location cell))
(should (looking-at "\
In \\[111\\]:
some input
"))))
(ert-deftest eintest:cell-input-prompt-star ()
(ein:testing-with-one-cell
(ein:cell-from-json
(list :cell_type "code"
:source "some input"
:metadata (list :collapsed json-false :autoscroll json-false)
:execution_count "*")
:ewoc (oref ein:%worksheet% :ewoc))
(goto-char (ein:cell-location cell))
(should (looking-at "\
In \\[\\*\\]:
some input
"))))
(ert-deftest eintest:cell-input-prompt-empty ()
(ein:testing-with-one-cell
(ein:cell-from-json
(list :cell_type "code"
:metadata (list :collapsed json-false :autoscroll json-false)
:source "some input")
:ewoc (oref ein:%worksheet% :ewoc))
(goto-char (ein:cell-location cell))
(should (looking-at "\
In \\[ \\]:
some input
"))))
;; Insert pyout/display_data
(defun eintest:cell-insert-output (outputs regexp)
(let ((ein:output-type-preference
'(emacs-lisp image/svg image/png jpeg text/plain text/html text/latex text/javascript)))
(message "%S" (list :cell_type "code"
:outputs outputs
:source "Some input"
:metadata (list :collapsed json-false :autoscroll json-false)
:execution_count 111))
(ein:testing-with-one-cell
(ein:cell-from-json
(list :cell_type "code"
:outputs outputs
:source "some input"
:metadata (list :collapsed json-false :autoscroll json-false)
:execution_count 111)
:ewoc (oref ein:%worksheet% :ewoc))
(goto-char (ein:cell-location cell))
(should (looking-at (format "\
In \\[111\\]:
some input
%s" regexp))))))
(defmacro eintest:gene-test-cell-insert-output-pyout-and-display-data
(name regexps outputs)
(declare (indent defun))
(let ((test-pyout
(intern (format "ein:cell-insert-output-pyout-%s" name)))
(test-display-data
(intern (format "ein:cell-insert-output-display-data-%s" name)))
(outputs-pyout
(loop for i from 1
for x in outputs
collect
(append x (list :output_type "execute_result" :execution_count i :metadata nil))))
(outputs-display-data
(mapcar (lambda (x) (append '(:output_type "display_data" :metadata nil) x))
outputs))
(regexp-pyout
(ein:join-str
""
(loop for i from 1
for x in regexps
collect (format "Out \\[%s\\]:\n%s\n" i x))))
(regexp-display-data
(concat (ein:join-str "\n" regexps) "\n")))
`(progn
(ert-deftest ,test-pyout ()
(eintest:cell-insert-output ',outputs-pyout
,regexp-pyout))
(ert-deftest ,test-display-data ()
(eintest:cell-insert-output ',outputs-display-data
,regexp-display-data)))))
(eintest:gene-test-cell-insert-output-pyout-and-display-data
text ("some output") ((:data (:text/plain "some output"))))
(eintest:gene-test-cell-insert-output-pyout-and-display-data
latex
("some output \\\\LaTeX")
((:data (:text/latex "some output \\LaTeX"))))
(when (image-type-available-p 'svg)
(eintest:gene-test-cell-insert-output-pyout-and-display-data
svg
(" ")
((:data (:text/plain "some output text" :svg ein:testing-example-svg)))))
(eintest:gene-test-cell-insert-output-pyout-and-display-data
html
("some output text")
((:data (:text/plain ("some output text") :text/html ("<b>not shown</b>")))))
(eintest:gene-test-cell-insert-output-pyout-and-display-data
javascript
("some output text")
((:data (:text/plain "some output text" :text/javascript "$.do.something()"))))
(eintest:gene-test-cell-insert-output-pyout-and-display-data
text-two
("first output text" "second output text")
((:data (:text/plain "first output text")) (:text/plain "second output text")))
(eintest:gene-test-cell-insert-output-pyout-and-display-data
text-javascript
("first output text" "second output text")
((:data (:text/plain "first output text"))
(:data (:text/plain "second output text" :text/javascript "$.do.something()"))))
(when (image-type-available-p 'svg)
(eintest:gene-test-cell-insert-output-pyout-and-display-data
text-latex-svg
("first output text" "second output \\\\LaTeX" " ")
((:data (:text/plain "first output text"))
(:data (:text/latex "second output \\LaTeX"))
(:data (:text/plain "some output text" :image/svg ein:testing-example-svg)))))
;; Insert pyerr
(ert-deftest ein:cell-insert-output-pyerr-simple ()
(eintest:cell-insert-output
(list (list :output_type "pyerr"
:traceback '("some traceback 1"
"some traceback 2")))
"\
some traceback 1
some traceback 2
"))
;; Insert stream
(ert-deftest ein:cell-insert-output-stream-simple-stdout ()
(eintest:cell-insert-output
(list (list :output_type "stream"
:name "stdout"
:text "some stdout 1"))
"\
some stdout 1
"))
(ert-deftest ein:cell-insert-output-stream-stdout-stderr ()
(eintest:cell-insert-output
(list (list :output_type "stream"
:name "stdout"
:text "some stdout 1")
(list :output_type "stream"
:name "stderr"
:text "some stderr 1"))
"\
some stdout 1
some stderr 1
"))
(ert-deftest ein:cell-insert-output-stream-flushed-stdout ()
(eintest:cell-insert-output
(list (list :output_type "stream"
:name "stdout"
:text "some stdout 1")
(list :output_type "stream"
:name "stdout"
:text "some stdout 2"))
"\
some stdout 1some stdout 2
"))
(ert-deftest ein:cell-insert-output-stream-flushed-stdout-and-stderr ()
(eintest:cell-insert-output
(list (list :output_type "stream"
:name "stdout"
:text "some stdout 1")
(list :output_type "stream"
:name "stderr"
:text "some stderr 1")
(list :output_type "stream"
:name "stdout"
:text "some stdout 2")
(list :output_type "stream"
:name "stderr"
:text "some stderr 2"))
"\
some stdout 1
some stderr 1
some stdout 2
some stderr 2
"))