test-ein-cell.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. (eval-when-compile (require 'cl))
  2. (require 'ert)
  3. (when load-file-name
  4. (add-to-list 'load-path
  5. (concat (file-name-directory load-file-name) "mocker")))
  6. (require 'mocker)
  7. (require 'ein-cell)
  8. (require 'ein-testing-cell)
  9. ;;; ein:cell-from-json
  10. (defun eintest:cell-from-json (data &rest args)
  11. (let ((cell (apply #'ein:cell-from-json data args)))
  12. (should-not (ein:cell-active-p cell))
  13. cell))
  14. (ert-deftest ein:cell-from-json-code ()
  15. (let* ((input-prompt-number 111)
  16. (output-prompt-number 222)
  17. (input (ein:join-str "\n" '("first input" "second input")))
  18. (output-0 (list :output_type "pyout"
  19. :prompt_number output-prompt-number
  20. :text (list "first output"
  21. "second output")))
  22. (data (ein:testing-codecell-data
  23. input input-prompt-number (list output-0)))
  24. (cell (eintest:cell-from-json data)))
  25. (should (ein:codecell-p cell))
  26. (should (equal (oref cell :input-prompt-number) input-prompt-number))
  27. (should (equal (oref cell :input) input))
  28. (should (equal (car (oref cell :outputs)) output-0))
  29. (should (equal (oref cell :collapsed) nil))))
  30. (ert-deftest ein:cell-from-json-text ()
  31. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  32. (data (list :cell_type "text" :source input))
  33. (cell (eintest:cell-from-json data)))
  34. (should (ein:textcell-p cell))
  35. (should (equal (oref cell :input) input))))
  36. (ert-deftest ein:cell-from-json-html ()
  37. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  38. (data (list :cell_type "html" :source input))
  39. (cell (eintest:cell-from-json data)))
  40. (should (ein:htmlcell-p cell))
  41. (should (equal (oref cell :input) input))))
  42. (ert-deftest ein:cell-from-json-markdown ()
  43. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  44. (data (list :cell_type "markdown" :source input))
  45. (cell (eintest:cell-from-json data)))
  46. (should (ein:markdowncell-p cell))
  47. (should (equal (oref cell :input) input))))
  48. (ert-deftest ein:cell-from-json-raw ()
  49. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  50. (data (list :cell_type "raw" :source input))
  51. (cell (eintest:cell-from-json data)))
  52. (should (ein:rawcell-p cell))
  53. (should (equal (oref cell :input) input))))
  54. (ert-deftest ein:cell-from-json-heading ()
  55. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  56. (data (list :cell_type "heading" :source input))
  57. (cell (eintest:cell-from-json data)))
  58. (should (ein:headingcell-p cell))
  59. (should (equal (oref cell :input) input))))
  60. ;; ein:cell-to-json
  61. (defun eintest:cell-to-json (cell input &optional discard-output)
  62. (mocker-let ((ein:cell-get-text
  63. (cell)
  64. ((:input (list cell) :output input))))
  65. (ein:cell-to-json cell discard-output)))
  66. (ert-deftest ein:cell-to-json-code ()
  67. (let* ((input-prompt-number 111)
  68. (output-prompt-number 222)
  69. (input (ein:join-str "\n" '("first input" "second input")))
  70. (output-0 (list :output_type "pyout"
  71. :prompt_number output-prompt-number
  72. :text (list "first output"
  73. "second output")))
  74. (data (ein:testing-codecell-data
  75. input input-prompt-number (list output-0)))
  76. (cell (eintest:cell-from-json data))
  77. (alist (eintest:cell-to-json cell input)))
  78. (should (equal (cdr (assq 'input alist)) "first input\nsecond input"))
  79. (should (equal (cdr (assq 'cell_type alist)) "code"))
  80. (should (equal (cdr (assq 'outputs alist)) `[,output-0]))
  81. (should (equal (cdr (assq 'language alist)) "python"))
  82. (should (equal (cdr (assq 'collapsed alist)) json-false))))
  83. (ert-deftest ein:cell-to-json-code-discard-output ()
  84. (let* ((input-prompt-number 111)
  85. (output-prompt-number 222)
  86. (input (ein:join-str "\n" '("first input" "second input")))
  87. (output-0 (list :output_type "pyout"
  88. :prompt_number output-prompt-number
  89. :text (list "first output"
  90. "second output")))
  91. (data (ein:testing-codecell-data
  92. input input-prompt-number (list output-0)))
  93. (cell (eintest:cell-from-json data))
  94. (alist (eintest:cell-to-json cell input t)))
  95. (should (equal (cdr (assq 'input alist)) "first input\nsecond input"))
  96. (should (equal (cdr (assq 'cell_type alist)) "code"))
  97. (should (equal (cdr (assq 'outputs alist)) []))
  98. (should (equal (cdr (assq 'language alist)) "python"))
  99. (should (equal (cdr (assq 'collapsed alist)) json-false))))
  100. (ert-deftest ein:cell-to-json-text ()
  101. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  102. (data (list :cell_type "text" :source input))
  103. (cell (eintest:cell-from-json data))
  104. (alist (eintest:cell-to-json cell input)))
  105. (should (equal (cdr (assq 'cell_type alist)) "text"))
  106. (should (equal (cdr (assq 'source alist)) "first input\nsecond input"))))
  107. (ert-deftest ein:cell-to-json-html ()
  108. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  109. (data (list :cell_type "html" :source input))
  110. (cell (eintest:cell-from-json data))
  111. (alist (eintest:cell-to-json cell input)))
  112. (should (equal (cdr (assq 'cell_type alist)) "html"))
  113. (should (equal (cdr (assq 'source alist)) "first input\nsecond input"))))
  114. (ert-deftest ein:cell-to-json-markdown ()
  115. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  116. (data (list :cell_type "markdown" :source input))
  117. (cell (eintest:cell-from-json data))
  118. (alist (eintest:cell-to-json cell input)))
  119. (should (equal (cdr (assq 'cell_type alist)) "markdown"))
  120. (should (equal (cdr (assq 'source alist)) "first input\nsecond input"))))
  121. (ert-deftest ein:cell-to-json-raw ()
  122. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  123. (data (list :cell_type "raw" :source input))
  124. (cell (eintest:cell-from-json data))
  125. (alist (eintest:cell-to-json cell input)))
  126. (should (equal (cdr (assq 'cell_type alist)) "raw"))
  127. (should (equal (cdr (assq 'source alist)) "first input\nsecond input"))))
  128. (ert-deftest ein:cell-to-json-heading ()
  129. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  130. (data (list :cell_type "heading" :source input))
  131. (cell (eintest:cell-from-json data))
  132. (alist (eintest:cell-to-json cell input)))
  133. (should (equal (cdr (assq 'cell_type alist)) "heading"))
  134. (should (equal (cdr (assq 'source alist)) "first input\nsecond input"))
  135. (should (equal (cdr (assq 'level alist)) 1))))
  136. ;;; ein:cell-convert/copy
  137. (ert-deftest ein:cell-convert-code-to-markdown ()
  138. (let* ((input-prompt-number 111)
  139. (output-prompt-number 222)
  140. (input (ein:join-str "\n" '("first input" "second input")))
  141. (output-0 (list :output_type "pyout"
  142. :prompt_number output-prompt-number
  143. :text (list "first output"
  144. "second output")))
  145. (data (ein:testing-codecell-data
  146. input input-prompt-number (list output-0)))
  147. (dummy-ewoc (ewoc-create 'dummy))
  148. (old (eintest:cell-from-json data :ewoc dummy-ewoc))
  149. (new (ein:cell-convert old "markdown")))
  150. (should (ein:codecell-p old))
  151. (should (ein:markdowncell-p new))
  152. (should (equal (oref new :input) input))))
  153. (ert-deftest ein:cell-convert-markdown-to-code ()
  154. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  155. (dummy-ewoc (ewoc-create 'dummy))
  156. (data (list :cell_type "markdown" :source input))
  157. (old (eintest:cell-from-json data :ewoc dummy-ewoc))
  158. (new (ein:cell-convert old "code")))
  159. (should (ein:markdowncell-p old))
  160. (should (ein:codecell-p new))
  161. (should (equal (oref new :input) input))))
  162. (ert-deftest ein:cell-copy-code ()
  163. (let* ((input-prompt-number 111)
  164. (output-prompt-number 222)
  165. (input (ein:join-str "\n" '("first input" "second input")))
  166. (output-0 (list :output_type "pyout"
  167. :prompt_number output-prompt-number
  168. :text (list "first output"
  169. "second output")))
  170. (data (ein:testing-codecell-data
  171. input input-prompt-number (list output-0)))
  172. (dummy-ewoc (ewoc-create 'dummy))
  173. (old (eintest:cell-from-json data :ewoc dummy-ewoc))
  174. (new (ein:cell-copy old)))
  175. (should (ein:codecell-p old))
  176. (should (ein:codecell-p new))
  177. (should-not (equal (oref old :cell-id)
  178. (oref new :cell-id)))
  179. (should (equal (oref old :input) input))
  180. (should (equal (oref new :input) input))))
  181. (ert-deftest ein:cell-copy-text-types ()
  182. (loop for cell-type in '("text" "html" "markdown" "raw" "heading")
  183. for cell-p = (intern (format "ein:%scell-p" cell-type))
  184. do
  185. (let* ((input (ein:join-str "\n" '("first input" "second input")))
  186. (data (list :cell_type cell-type :source input))
  187. (dummy-ewoc (ewoc-create 'dummy))
  188. (old (eintest:cell-from-json data :ewoc dummy-ewoc))
  189. (new (ein:cell-copy old)))
  190. (should (funcall cell-p old))
  191. (should (funcall cell-p new))
  192. (should-not (equal (oref old :cell-id)
  193. (oref new :cell-id)))
  194. (should (equal (oref old :input) input))
  195. (should (equal (oref new :input) input)))))
  196. ;;; ein:cell-element-get
  197. (ert-deftest ein:cell-element-get-basecell ()
  198. (let ((cell (ein:basecell "Cell")))
  199. ;; it's not supported
  200. (should-error (ein:cell-element-get :prompt))))
  201. (ert-deftest ein:cell-element-get-codecell ()
  202. (let* ((element (list :prompt 1
  203. :input 2
  204. :output '(3 4)
  205. :footer 5))
  206. (cell (ein:cell-from-type "code" :element element)))
  207. (mapc (lambda (kv)
  208. (should (equal (ein:cell-element-get cell (car kv)) (cdr kv))))
  209. (ein:plist-iter element))
  210. (should (equal (ein:cell-element-get cell :output 0) 3))
  211. (should (equal (ein:cell-element-get cell :output 1) 4))
  212. (should (equal (ein:cell-element-get cell :output 2) nil))
  213. (should (equal (ein:cell-element-get cell :after-input) 3))
  214. (should (equal (ein:cell-element-get cell :after-output) 5))
  215. (should (equal (ein:cell-element-get cell :before-input) 1))
  216. (should (equal (ein:cell-element-get cell :before-output) 2))
  217. (should (equal (ein:cell-element-get cell :last-output) 4))))
  218. (ert-deftest ein:cell-element-get-codecell-no-ouput ()
  219. (let* ((element (list :prompt 1
  220. :input 2
  221. :footer 5))
  222. (cell (ein:cell-from-type "code" :element element)))
  223. (mapc (lambda (kv)
  224. (should (equal (ein:cell-element-get cell (car kv)) (cdr kv))))
  225. (ein:plist-iter element))
  226. (should (equal (ein:cell-element-get cell :after-input) 5))
  227. (should (equal (ein:cell-element-get cell :last-output) 2))))
  228. (ert-deftest ein:cell-element-get-textcell ()
  229. (let* ((element (list :prompt 1
  230. :input 2
  231. :footer 5))
  232. (cell (ein:cell-from-type "text" :element element)))
  233. (mapc (lambda (kv)
  234. (should (equal (ein:cell-element-get cell (car kv)) (cdr kv))))
  235. (ein:plist-iter element))
  236. (should (equal (ein:cell-element-get cell :after-input) 5))
  237. (should (equal (ein:cell-element-get cell :before-input) 1))))