test-ein-cell-notebook.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ;; Tests for cell function that requires notebook buffer
  2. (eval-when-compile (require 'cl))
  3. (require 'ert)
  4. (require 'ein-notebook)
  5. (require 'ein-testing-notebook)
  6. ;; ein:cell-location
  7. (ert-deftest ein:cell-location-codecell-prompt-beg ()
  8. (ein:testing-with-one-cell 'code
  9. (should (equal (marker-position (ein:cell-location cell :prompt))
  10. (save-excursion
  11. (goto-char (point-max))
  12. (search-backward "In [ ]:")
  13. (point))))))
  14. (ert-deftest ein:cell-location-codecell-prompt-end ()
  15. (ein:testing-with-one-cell 'code
  16. (should (equal (marker-position (ein:cell-location cell :prompt t))
  17. (1- (point))))))
  18. (ert-deftest ein:cell-location-codecell-input-beg ()
  19. (ein:testing-with-one-cell 'code
  20. (insert "some text")
  21. (should (equal (marker-position (ein:cell-location cell :input))
  22. (1- (point-at-bol))))))
  23. (ert-deftest ein:cell-location-codecell-input-end ()
  24. (ein:testing-with-one-cell 'code
  25. (insert "some text")
  26. (should (equal (marker-position (ein:cell-location cell :input t))
  27. (1+ (point))))))
  28. ;; from-json
  29. (ert-deftest eintest:cell-input-prompt-number ()
  30. (ein:testing-with-one-cell
  31. (ein:cell-from-json
  32. (list :cell_type "code"
  33. :input "some input"
  34. :prompt_number 111)
  35. :ewoc (oref ein:%worksheet% :ewoc))
  36. (goto-char (ein:cell-location cell))
  37. (should (looking-at "\
  38. In \\[111\\]:
  39. some input
  40. "))))
  41. (ert-deftest eintest:cell-input-prompt-star ()
  42. (ein:testing-with-one-cell
  43. (ein:cell-from-json
  44. (list :cell_type "code"
  45. :input "some input"
  46. :prompt_number "*")
  47. :ewoc (oref ein:%worksheet% :ewoc))
  48. (goto-char (ein:cell-location cell))
  49. (should (looking-at "\
  50. In \\[\\*\\]:
  51. some input
  52. "))))
  53. (ert-deftest eintest:cell-input-prompt-empty ()
  54. (ein:testing-with-one-cell
  55. (ein:cell-from-json
  56. (list :cell_type "code"
  57. :input "some input")
  58. :ewoc (oref ein:%worksheet% :ewoc))
  59. (goto-char (ein:cell-location cell))
  60. (should (looking-at "\
  61. In \\[ \\]:
  62. some input
  63. "))))
  64. ;; Insert pyout/display_data
  65. (defun eintest:cell-insert-output (outputs regexp)
  66. (let ((ein:output-type-preference
  67. '(emacs-lisp svg png jpeg text html latex javascript)))
  68. (ein:testing-with-one-cell
  69. (ein:cell-from-json
  70. (list :cell_type "code"
  71. :outputs outputs
  72. :input "some input"
  73. :prompt_number 111)
  74. :ewoc (oref ein:%worksheet% :ewoc))
  75. (goto-char (ein:cell-location cell))
  76. (should (looking-at (format "\
  77. In \\[111\\]:
  78. some input
  79. %s" regexp))))))
  80. (defmacro eintest:gene-test-cell-insert-output-pyout-and-display-data
  81. (name regexps outputs)
  82. (declare (indent defun))
  83. (let ((test-pyout
  84. (intern (format "ein:cell-insert-output-pyout-%s" name)))
  85. (test-display-data
  86. (intern (format "ein:cell-insert-output-display-data-%s" name)))
  87. (outputs-pyout
  88. (loop for i from 1
  89. for x in outputs
  90. collect
  91. (append x (list :output_type "pyout" :prompt_number i))))
  92. (outputs-display-data
  93. (mapcar (lambda (x) (append '(:output_type "display_data") x))
  94. outputs))
  95. (regexp-pyout
  96. (ein:join-str
  97. ""
  98. (loop for i from 1
  99. for x in regexps
  100. collect (format "Out \\[%s\\]:\n%s\n" i x))))
  101. (regexp-display-data
  102. (concat (ein:join-str "\n" regexps) "\n")))
  103. `(progn
  104. (ert-deftest ,test-pyout ()
  105. (eintest:cell-insert-output ',outputs-pyout
  106. ,regexp-pyout))
  107. (ert-deftest ,test-display-data ()
  108. (eintest:cell-insert-output ',outputs-display-data
  109. ,regexp-display-data)))))
  110. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  111. text ("some output") ((:text "some output")))
  112. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  113. latex
  114. ("some output \\\\LaTeX")
  115. ((:latex "some output \\LaTeX")))
  116. (when (image-type-available-p 'svg)
  117. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  118. svg
  119. (" ")
  120. ((:text "some output text" :svg ein:testing-example-svg))))
  121. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  122. html
  123. ("some output text")
  124. ((:text "some output text" :html "<b>not shown</b>")))
  125. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  126. javascript
  127. ("some output text")
  128. ((:text "some output text" :javascript "$.do.something()")))
  129. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  130. text-two
  131. ("first output text" "second output text")
  132. ((:text "first output text") (:text "second output text")))
  133. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  134. text-javascript
  135. ("first output text" "second output text")
  136. ((:text "first output text")
  137. (:text "second output text" :javascript "$.do.something()")))
  138. (when (image-type-available-p 'svg)
  139. (eintest:gene-test-cell-insert-output-pyout-and-display-data
  140. text-latex-svg
  141. ("first output text" "second output \\\\LaTeX" " ")
  142. ((:text "first output text")
  143. (:latex "second output \\LaTeX")
  144. (:text "some output text" :svg ein:testing-example-svg))))
  145. ;; Insert pyerr
  146. (ert-deftest ein:cell-insert-output-pyerr-simple ()
  147. (eintest:cell-insert-output
  148. (list (list :output_type "pyerr"
  149. :traceback '("some traceback 1"
  150. "some traceback 2")))
  151. "\
  152. some traceback 1
  153. some traceback 2
  154. "))
  155. ;; Insert stream
  156. (ert-deftest ein:cell-insert-output-stream-simple-stdout ()
  157. (eintest:cell-insert-output
  158. (list (list :output_type "stream"
  159. :stream "stdout"
  160. :text "some stdout 1"))
  161. "\
  162. some stdout 1
  163. "))
  164. (ert-deftest ein:cell-insert-output-stream-stdout-stderr ()
  165. (eintest:cell-insert-output
  166. (list (list :output_type "stream"
  167. :stream "stdout"
  168. :text "some stdout 1")
  169. (list :output_type "stream"
  170. :stream "stderr"
  171. :text "some stderr 1"))
  172. "\
  173. some stdout 1
  174. some stderr 1
  175. "))
  176. (ert-deftest ein:cell-insert-output-stream-flushed-stdout ()
  177. (eintest:cell-insert-output
  178. (list (list :output_type "stream"
  179. :stream "stdout"
  180. :text "some stdout 1")
  181. (list :output_type "stream"
  182. :stream "stdout"
  183. :text "some stdout 2"))
  184. "\
  185. some stdout 1some stdout 2
  186. "))
  187. (ert-deftest ein:cell-insert-output-stream-flushed-stdout-and-stderr ()
  188. (eintest:cell-insert-output
  189. (list (list :output_type "stream"
  190. :stream "stdout"
  191. :text "some stdout 1")
  192. (list :output_type "stream"
  193. :stream "stderr"
  194. :text "some stderr 1")
  195. (list :output_type "stream"
  196. :stream "stdout"
  197. :text "some stdout 2")
  198. (list :output_type "stream"
  199. :stream "stderr"
  200. :text "some stderr 2"))
  201. "\
  202. some stdout 1
  203. some stderr 1
  204. some stdout 2
  205. some stderr 2
  206. "))