ein-testing-notebook.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; ein-testing-notebook.el --- Testing utilities for notebook module
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-testing-notebook.el is free software: you can redistribute it
  6. ;; and/or modify it under the terms of the GNU General Public License
  7. ;; as published by the Free Software Foundation, either version 3 of
  8. ;; the License, or (at your option) any later version.
  9. ;; ein-testing-notebook.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-testing-notebook.el.
  15. ;; If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (require 'ein-notebook)
  21. (require 'ein-testing-cell)
  22. (defun ein:testing-notebook-from-json (json-string &optional notebook-id)
  23. (unless notebook-id (setq notebook-id "NOTEBOOK-ID"))
  24. (flet ((pop-to-buffer (buf) buf)
  25. (ein:notebook-start-kernel (notebook)))
  26. (let ((notebook (ein:notebook-new "DUMMY-URL" notebook-id)))
  27. (setf (ein:$notebook-kernel notebook)
  28. (ein:kernel-new 8888 "/kernels" (ein:$notebook-events notebook)))
  29. (setf (ein:$kernel-events (ein:$notebook-kernel notebook))
  30. (ein:events-new))
  31. (ein:notebook-request-open-callback
  32. notebook :data (ein:json-read-from-string json-string))
  33. (ein:notebook-buffer notebook))))
  34. (defun ein:testing-notebook-make-data (cells &optional name)
  35. (setq cells
  36. (ein:testing-notebook--preprocess-cells-data-for-json-encode cells))
  37. (unless name (setq name "Dummy Name"))
  38. `((metadata . ((name . ,name)))
  39. (nbformat . 2)
  40. (name . ,name)
  41. (worksheets . [((cells . ,(apply #'vector cells)))])))
  42. (defun ein:testing-notebook--preprocess-cells-data-for-json-encode (cells)
  43. "Preprocess CELLS data to make it work nice with `json-encode'."
  44. (mapcar (lambda (c)
  45. (cond
  46. ((equal (plist-get c :cell_type) "code")
  47. ;; turn `:outputs' into an array.
  48. (plist-put c :outputs (apply #'vector (plist-get c :outputs))))
  49. (t c)))
  50. cells))
  51. (defun ein:testing-notebook-make-new (&optional name notebook-id cells-data)
  52. "Make new notebook. One empty cell will be inserted
  53. automatically if CELLS-DATA is nil."
  54. (ein:testing-notebook-from-json
  55. (json-encode (ein:testing-notebook-make-data cells-data name)) notebook-id))
  56. (defun ein:testing-notebook-make-empty (&optional name notebook-id)
  57. "Make empty notebook and return its buffer.
  58. Automatically inserted cell for new notebook is deleted."
  59. (let ((buffer (ein:testing-notebook-make-new name notebook-id)))
  60. (with-current-buffer buffer
  61. (call-interactively #'ein:worksheet-delete-cell))
  62. buffer))
  63. (defmacro ein:testing-with-one-cell (cell-type &rest body)
  64. "Insert new cell of CELL-TYPE in a clean notebook and execute BODY.
  65. The new cell is bound to a variable `cell'."
  66. (declare (indent 1))
  67. `(with-current-buffer (ein:testing-notebook-make-empty)
  68. (let ((cell (ein:worksheet-insert-cell-below ein:%worksheet%
  69. ,cell-type nil t)))
  70. ,@body)))
  71. (defun ein:testing-make-notebook-with-outputs (list-outputs)
  72. "Make a new notebook with cells with output.
  73. LIST-OUTPUTS is a list of list of strings (pyout text). Number
  74. of LIST-OUTPUTS equals to the number cells to be contained in the
  75. notebook."
  76. (ein:testing-notebook-make-new
  77. nil nil
  78. (mapcar (lambda (outputs)
  79. (ein:testing-codecell-data
  80. nil nil (mapcar #'ein:testing-codecell-pyout-data outputs)))
  81. list-outputs)))
  82. (provide 'ein-testing-notebook)
  83. ;;; ein-testing-notebook.el ends here