-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathtest-ein-notebook.el
1285 lines (1121 loc) · 51.8 KB
/
test-ein-notebook.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- lexical-binding:t -*-
(require 'ert)
(when load-file-name
(add-to-list 'load-path
(concat (file-name-directory load-file-name) "mocker")))
(require 'mocker)
(require 'ein-notebook)
(require 'ein-testing-notebook)
(require 'ein-testing-cell)
(require 'ein-markdown-mode)
;; Test utils
;;; This is the content portion of a response fromt he content API.
(defvar eintest:notebook-data-simple-json
"{
\"name\": \"Untitled0.ipynb\",
\"path\": \"\",
\"type\": \"notebook\",
\"content\" : {
\"metadata\": {
},
\"nbformat\": 3,
\"nbformat_minor\": 0,
\"worksheets\": [
{
\"cells\": [
{
\"cell_type\": \"code\",
\"collapsed\": false,
\"input\": \"1 + 1\",
\"language\": \"python\",
\"outputs\": [
{
\"output_type\": \"pyout\",
\"prompt_number\": 1,
\"text\": \"2\"
}
],
\"prompt_number\": 1
}
]
}
]
}
}
")
(defvar eintest:notebook-data-simple-json-v4
"{
\"metadata\": {
\"name\": \"Untitled0.ipynb\"
},
\"nbformat\": 4,
\"nbformat_minor\": 0,
\"cells\": [
{
\"cell_type\": \"code\",
\"metadata\" : {
\"collapsed\": false,
},
\"source\": \"1 + 1\",
\"outputs\": [
{
\"name\": \"stdout\",
\"output_type\": \"stream\",
\"text\": \"2\"
}
],
\"prompt_number\": 1
}
]
}
")
(defun eintest:kernel-fake-execute-reply (kernel msg-id _execution-count)
(let* ((payload nil)
(content (list :execution_count 1 :payload payload))
(packet (list :header (list :msg_type "execute_reply")
:parent_header (list :msg_id msg-id)
:content content)))
(ein:kernel--handle-shell-reply kernel (ein:json-encode packet))))
(defun eintest:kernel-fake-stream (kernel msg-id data)
(let* ((content (list :text data
:name "stdout"))
(packet (list :header (list :msg_type "stream")
:parent_header (list :msg_id msg-id)
:content content)))
(ein:kernel--handle-iopub-reply kernel (ein:json-encode packet))))
(defun eintest:check-search-forward-from (start string &optional null-string)
"Search STRING from START and check it is found.
When non-`nil' NULL-STRING is given, it is searched from the
position where the search of the STRING ends and check that it
is not found."
(save-excursion
(goto-char start)
(should (search-forward string nil t))
(when null-string
(should-not (search-forward null-string nil t)))))
(defun eintest:cell-check-output (cell regexp)
(save-excursion
(goto-char (ein:cell-location cell :after-input))
(should (looking-at-p (concat "\\=" regexp "\n")))))
;; from-json
(ert-deftest ein:notebook-from-json-simple ()
(with-current-buffer (ein:testing-notebook-from-json
eintest:notebook-data-simple-json)
(should (ein:$notebook-p ein:%notebook%))
(should (equal (ein:$notebook-notebook-name ein:%notebook%) "Untitled0.ipynb"))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 1))
(let ((cell (car (ein:worksheet-get-cells ein:%worksheet%))))
(should (ein:codecell-p cell))
(should (equal (oref cell :input) "1 + 1"))
(should (equal (oref cell :input-prompt-number) 1))
(let ((outputs (oref cell :outputs)))
(should (equal (length outputs) 1))
(let ((o1 (car outputs)))
(should (equal (plist-get o1 :output_type) "pyout"))
(should (equal (plist-get o1 :prompt_number) 1))
(should (equal (plist-get o1 :text) "2")))))))
(ert-deftest ein:notebook-from-json-empty ()
(with-current-buffer (ein:testing-notebook-make-empty)
(should (ein:$notebook-p ein:%notebook%))
(should (equal (ein:$notebook-notebook-name ein:%notebook%) ein:testing-notebook-dummy-name))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 0))))
(ert-deftest ein:notebook-from-json-all-cell-types ()
(with-current-buffer
(ein:testing-notebook-make-new
ein:testing-notebook-dummy-name
nil
(list (ein:testing-codecell-data "import numpy")
(ein:testing-markdowncell-data "*markdown* text")
(ein:testing-rawcell-data "`raw` cell text")
(ein:testing-htmlcell-data "<b>HTML</b> text")))
(should (ein:$notebook-p ein:%notebook%))
(should (equal (ein:$notebook-notebook-name ein:%notebook%)
ein:testing-notebook-dummy-name))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 4))
(let ((cells (ein:worksheet-get-cells ein:%worksheet%)))
(should (ein:codecell-p (nth 0 cells)))
(should (ein:markdowncell-p (nth 1 cells)))
(should (ein:rawcell-p (nth 2 cells)))
(should (ein:htmlcell-p (nth 3 cells)))
(should (equal (ein:cell-get-text (nth 0 cells)) "import numpy"))
(should (equal (ein:cell-get-text (nth 1 cells)) "*markdown* text"))
(should (equal (ein:cell-get-text (nth 2 cells)) "`raw` cell text"))
(should (equal (ein:cell-get-text (nth 3 cells)) "<b>HTML</b> text")))))
;;; Destructor
(defun ein:testing-notebook-close-scratchsheet-open-and-close
(num-open num-close)
"Test for closing scratch sheet using `ein:notebook-close-worksheet'.
1. Open NUM-OPEN scratch sheets.
2. Close an existing worksheet.
3. Close NUM-CLOSE scratch sheets.
When NUM-OPEN = NUM-CLOSE, notebook should be closed."
(should (> num-open 0))
(with-current-buffer (ein:testing-notebook-make-empty)
(cl-symbol-macrolet ((ss-list (ein:$notebook-scratchsheets ein:%notebook%)))
;; Add scratchsheets. They can be just empty instance for this test.
(dotimes (_ num-open)
(ein:notebook-scratchsheet-render-new ein:%notebook%))
(let ((ss (car ss-list)))
(kill-buffer (ein:worksheet-buffer ss)))
(should (= (length ss-list) (1- num-open)))
(dotimes (_ (1- num-close))
(kill-buffer (ein:worksheet-buffer (car ss-list))))
(should (= (length ss-list) (- num-open num-close)))
(let ((my-buffer (buffer-name)))
(kill-buffer (current-buffer))
(should-not (seq-some (lambda (b)
(cl-search my-buffer (buffer-name b)))
(buffer-list)))))))
(ert-deftest ein:notebook-close-scratchsheet/open-one-close-one ()
(ein:testing-notebook-close-scratchsheet-open-and-close 1 1))
(ert-deftest ein:notebook-close-scratchsheet/open-two-close-two ()
(ein:testing-notebook-close-scratchsheet-open-and-close 2 2))
(ert-deftest ein:notebook-close-scratchsheet/open-two-close-one ()
(ein:testing-notebook-close-scratchsheet-open-and-close 2 1))
;;; Insertion and deletion of cells
(ert-deftest ein:notebook-insert-cell-below-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-below)
(call-interactively #'ein:worksheet-insert-cell-below)
(call-interactively #'ein:worksheet-insert-cell-below)
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))))
(ert-deftest ein:notebook-insert-cell-above-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-above)
(call-interactively #'ein:worksheet-insert-cell-above)
(call-interactively #'ein:worksheet-insert-cell-above)
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))))
(ert-deftest ein:notebook-delete-cell-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-insert-cell-above))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-delete-cell))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 0))))
(ert-deftest ein:notebook-delete-cell-command-no-undo ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-above)
(insert "some text")
(should (equal (buffer-string) "
In [ ]:
some text
"))
(call-interactively #'ein:worksheet-delete-cell)
(should (equal (buffer-string) "\n"))
(should-error (undo)) ; should be ignore-error?
(should (equal (buffer-string) "\n"))))
(ert-deftest ein:notebook-kill-cell-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let (ein:kill-ring ein:kill-ring-yank-pointer)
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-insert-cell-above))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
(cl-loop for i from 1 to 3
do (call-interactively #'ein:worksheet-kill-cell)
do (should (equal (length ein:kill-ring) i))
do (should (equal (ein:worksheet-ncells ein:%worksheet%) (- 3 i)))))))
(ert-deftest ein:notebook-copy-cell-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let (ein:kill-ring ein:kill-ring-yank-pointer)
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-insert-cell-above))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-copy-cell))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
(should (equal (length ein:kill-ring) 3)))))
(ert-deftest ein:notebook-yank-cell-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let (ein:kill-ring ein:kill-ring-yank-pointer)
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-insert-cell-above))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-kill-cell))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 0))
(should (equal (length ein:kill-ring) 3))
(cl-loop repeat 3
do (call-interactively #'ein:worksheet-yank-cell))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
(cl-loop for cell in (ein:worksheet-get-cells ein:%worksheet%)
do (should (ein:codecell-p cell))
do (should (slot-boundp cell :kernel))
do (should (slot-boundp cell :events))))))
(ert-deftest ein:notebook-yank-cell-command-two-buffers ()
(let (ein:kill-ring ein:kill-ring-yank-pointer)
(with-current-buffer (ein:testing-notebook-make-empty "NB1")
(call-interactively #'ein:worksheet-insert-cell-above)
(should (equal (ein:worksheet-ncells ein:%worksheet%) 1))
(call-interactively #'ein:worksheet-kill-cell)
(should (equal (ein:worksheet-ncells ein:%worksheet%) 0))
(kill-buffer))
(with-current-buffer (ein:testing-notebook-make-empty "NB2")
(call-interactively #'ein:worksheet-yank-cell)
(should (equal (ein:worksheet-ncells ein:%worksheet%) 1)))))
(ert-deftest ein:notebook-toggle-cell-type-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-above)
(insert "some text")
(should (ein:codecell-p (ein:worksheet-get-current-cell)))
(should (slot-boundp (ein:worksheet-get-current-cell) :kernel))
;; toggle to markdown
(call-interactively #'ein:worksheet-toggle-cell-type)
(should (ein:markdowncell-p (ein:worksheet-get-current-cell)))
(should (looking-back "some text"))
;; toggle to raw
(call-interactively #'ein:worksheet-toggle-cell-type)
(should (ein:rawcell-p (ein:worksheet-get-current-cell)))
(should (looking-back "some text"))
(when (< (ein:$notebook-nbformat ein:%notebook%) 4)
;; toggle to heading
(call-interactively #'ein:worksheet-toggle-cell-type)
(should (looking-back "some text"))
;; toggle to code
(call-interactively #'ein:worksheet-toggle-cell-type)
(should (ein:codecell-p (ein:worksheet-get-current-cell)))
(should (slot-boundp (ein:worksheet-get-current-cell) :kernel))
(should (looking-back "some text")))))
(ert-deftest ein:notebook-change-cell-type-cycle-through ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-above)
(insert "some text")
;; start with code cell
(should (ein:codecell-p (ein:worksheet-get-current-cell)))
(should (slot-boundp (ein:worksheet-get-current-cell) :kernel))
(let ((check
(lambda (type)
(let ((cell-p (intern (format "ein:%scell-p" type)))
(cell (ein:worksheet-get-current-cell)))
(ein:worksheet-change-cell-type ein:%worksheet% cell t)
(let ((new (ein:worksheet-get-current-cell)))
(should-not (eq new cell))
(should (funcall cell-p new)))
(should (looking-back "some text"))))))
;; change type: code (no change) -> markdown -> raw
(cl-loop for type in '("markdown" "raw" "code")
do (funcall check type))
;; back to code
(should (slot-boundp (ein:worksheet-get-current-cell) :kernel)))))
(defun eintest:notebook-split-cell-at-point
(insert-text search-text head-text tail-text &optional no-trim)
"Test `ein:notebook-split-cell-at-point' by the following procedure.
1. Insert, INSERT-TEXT.
2. Split cell just before SEARCH-TEXT.
3. Check that head cell has HEAD-TEXT.
4. Check that tail cell has TAIL-TEXT.
NO-TRIM is passed to `ein:notebook-split-cell-at-point'."
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-above)
(insert insert-text)
(when search-text
(search-backward search-text))
;; do it
(let ((current-prefix-arg no-trim))
(call-interactively #'ein:worksheet-split-cell-at-point))
;; check the "tail" cell
(let ((cell (ein:worksheet-get-current-cell)))
(ein:cell-goto cell)
(should (equal (ein:cell-get-text cell) tail-text))
(should (ein:codecell-p cell))
(should (slot-boundp cell :kernel)))
;; check the "head" cell
(call-interactively #'ein:worksheet-goto-prev-input)
(let ((cell (ein:worksheet-get-current-cell)))
(ein:cell-goto cell)
(should (equal (ein:cell-get-text cell) head-text))
(should (ein:codecell-p cell))
(should (slot-boundp cell :kernel)))))
(ert-deftest ein:notebook-split-cell-at-point-before-newline ()
(eintest:notebook-split-cell-at-point
"some\ntext" "text" "some" "text"))
(ert-deftest ein:notebook-split-cell-at-point-after-newline ()
(eintest:notebook-split-cell-at-point
"some\ntext" "\ntext" "some" "text"))
(ert-deftest ein:notebook-split-cell-at-point-before-newline-no-trim ()
(eintest:notebook-split-cell-at-point
"some\ntext" "text" "some\n" "text" t))
(ert-deftest ein:notebook-split-cell-at-point-after-newline-no-trim ()
(eintest:notebook-split-cell-at-point
"some\ntext" "\ntext" "some" "\ntext" t))
(ert-deftest ein:notebook-split-cell-at-point-no-head ()
(eintest:notebook-split-cell-at-point
"some" "some" "" "some"))
(ert-deftest ein:notebook-split-cell-at-point-no-tail ()
(eintest:notebook-split-cell-at-point
"some" nil "some" ""))
(ert-deftest ein:notebook-merge-cell-command-next ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-above)
(insert "Cell 1")
(call-interactively #'ein:worksheet-insert-cell-above)
(insert "Cell 0")
(let ((current-prefix-arg t))
(call-interactively #'ein:worksheet-merge-cell))
(ein:cell-goto (ein:worksheet-get-current-cell))
(should (looking-at "Cell 0\nCell 1"))))
(ert-deftest ein:notebook-merge-cell-command-prev ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-below)
(insert "Cell 0")
(call-interactively #'ein:worksheet-insert-cell-below)
(insert "Cell 1")
(call-interactively #'ein:worksheet-merge-cell)
(ein:cell-goto (ein:worksheet-get-current-cell))
(should (looking-at "Cell 0\nCell 1"))))
;;; Cell selection.
(ert-deftest ein:notebook-goto-next-input-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(cl-loop for i downfrom 2 to 0
do (call-interactively #'ein:worksheet-insert-cell-above)
do (insert (format "Cell %s" i)))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
;; (message "%s" (buffer-string))
(cl-loop for i from 0 below 2
do (beginning-of-line) ; This is required, I need to check why
do (should (looking-at (format "Cell %s" i)))
do (call-interactively #'ein:worksheet-goto-next-input)
do (should (looking-at (format "Cell %s" (1+ i)))))))
(ert-deftest ein:notebook-goto-prev-input-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(cl-loop for i from 0 below 3
do (call-interactively #'ein:worksheet-insert-cell-below)
do (insert (format "Cell %s" i)))
(should (equal (ein:worksheet-ncells ein:%worksheet%) 3))
;; (message "%s" (buffer-string))
(cl-loop for i downfrom 2 to 1
do (beginning-of-line) ; This is required, I need to check why
do (should (looking-at (format "Cell %s" i)))
do (call-interactively #'ein:worksheet-goto-prev-input)
do (should (looking-at (format "Cell %s" (1- i)))))))
(defun ein:testing-beginning-of-cell-input (num-cells
initial-point
before-callback
arg
should-looking-at-this)
(with-current-buffer (ein:testing-notebook-make-empty)
(ein:testing-insert-cells-with-format num-cells)
(goto-char (point-min))
(search-forward initial-point)
(when before-callback (funcall before-callback))
(should-not (looking-at-p should-looking-at-this))
(ein:worksheet-beginning-of-cell-input arg)
(should (looking-at-p should-looking-at-this))))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-no-arg ()
(ein:testing-beginning-of-cell-input 1 "Cell 0" nil nil "Cell 0"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-arg-two ()
(ein:testing-beginning-of-cell-input 2 "Cell 1" nil 2 "Cell 0"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-arg-minus-one ()
(ein:testing-beginning-of-cell-input 2 "Cell 0" nil -1 "Cell 1"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-no-arg-at-footer ()
(ein:testing-beginning-of-cell-input 1 "Cell 0"
#'forward-line
nil "Cell 0"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-arg-two-at-footer ()
(ein:testing-beginning-of-cell-input 2 "Cell 1"
#'forward-line
2 "Cell 0"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-arg-minus-one-at-footer
()
(ein:testing-beginning-of-cell-input 2 "Cell 0"
#'forward-line
-1 "Cell 1"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-no-arg-at-prompt ()
(ein:testing-beginning-of-cell-input 2 "Cell 1"
(lambda () (forward-line -1))
nil "Cell 0"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-arg-two-at-prompt ()
(ein:testing-beginning-of-cell-input 2 "Cell 1"
(lambda () (forward-line -1))
2 "Cell 0"))
(ert-deftest ein:worksheet-beginning-of-cell-input-with-arg-minus-one-at-prompt
()
(ein:testing-beginning-of-cell-input 2 "Cell 0"
;; I need two cells to make it fail
;; without (forward-line -1)
(lambda () (forward-line -1))
-1 "Cell 0"))
(ert-deftest ein:worksheet-beginning-of-cell-input-repeat ()
(with-current-buffer (ein:testing-notebook-make-empty)
(ein:testing-insert-cells-with-format 3)
(goto-char (point-min))
(search-forward "Cell 2")
(should-not (looking-at-p "Cell 2"))
(ein:worksheet-beginning-of-cell-input)
(should (looking-at-p "Cell 2"))
(should-not (looking-at-p "Cell 1"))
(ein:worksheet-beginning-of-cell-input)
(should (looking-at-p "Cell 1"))
(should-not (looking-at-p "Cell 0"))
(ein:worksheet-beginning-of-cell-input)
(should (looking-at-p "Cell 0"))))
(defun ein:testing-end-of-cell-input (num-cells
initial-point
before-callback
arg
should-looking-back-this)
(with-current-buffer (ein:testing-notebook-make-empty)
(ein:testing-insert-cells-with-format num-cells)
(goto-char (point-min))
(search-forward initial-point)
(beginning-of-line)
(when before-callback (funcall before-callback))
(should-not (looking-back should-looking-back-this))
(ein:worksheet-end-of-cell-input arg)
(should (looking-back should-looking-back-this))))
(ert-deftest ein:worksheet-end-of-cell-input-with-no-arg ()
(ein:testing-end-of-cell-input 1 "Cell 0" nil nil "Cell 0"))
(ert-deftest ein:worksheet-end-of-cell-input-with-arg-two ()
(ein:testing-end-of-cell-input 2 "Cell 0" nil 2 "Cell 1"))
(ert-deftest ein:worksheet-end-of-cell-input-with-arg-minus-one ()
(ein:testing-end-of-cell-input 2 "Cell 1" nil -1 "Cell 0"))
(ert-deftest ein:worksheet-end-of-cell-input-with-no-arg-at-footer ()
(ein:testing-end-of-cell-input 2 "Cell 0"
#'forward-line
nil "Cell 1"))
(ert-deftest ein:worksheet-end-of-cell-input-with-arg-two-at-footer ()
(ein:testing-end-of-cell-input 3 "Cell 0"
#'forward-line
2 "Cell 2"))
(ert-deftest ein:worksheet-end-of-cell-input-with-arg-minus-one-at-footer
()
(ein:testing-end-of-cell-input 2 "Cell 0"
#'forward-line
-1 "Cell 0"))
(ert-deftest ein:worksheet-end-of-cell-input-with-no-arg-at-prompt ()
(ein:testing-end-of-cell-input 2 "Cell 1"
(lambda () (forward-line -1))
nil "Cell 1"))
(ert-deftest ein:worksheet-end-of-cell-input-with-arg-two-at-prompt ()
(ein:testing-end-of-cell-input 3 "Cell 0"
(lambda () (forward-line -1))
2 "Cell 1"))
(ert-deftest ein:worksheet-end-of-cell-input-with-arg-minus-one-at-prompt
()
(ein:testing-end-of-cell-input 2 "Cell 1"
(lambda () (forward-line -1))
-1 "Cell 0"))
;;; Cell movement
(ert-deftest ein:notebook-move-cell-up-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(cl-loop for i from 0 below 3
do (call-interactively #'ein:worksheet-insert-cell-below)
do (insert (format "Cell %s" i)))
(beginning-of-line)
(should (looking-at "Cell 2"))
(cl-loop repeat 2
do (call-interactively #'ein:worksheet-move-cell-up))
;; (message "%s" (buffer-string))
(beginning-of-line)
(should (looking-at "Cell 2"))
(should (search-forward "Cell 0" nil t))
(should (search-forward "Cell 1" nil t))
(should-not (search-forward "Cell 2" nil t))))
(ert-deftest ein:notebook-move-cell-down-command-simple ()
(with-current-buffer (ein:testing-notebook-make-empty)
(cl-loop for i from 0 below 3
do (call-interactively #'ein:worksheet-insert-cell-above)
do (insert (format "Cell %s" i)))
(cl-loop repeat 2
do (call-interactively #'ein:worksheet-move-cell-down))
(beginning-of-line)
(should (looking-at "Cell 2"))
(should (search-backward "Cell 0" nil t))
(should (search-backward "Cell 1" nil t))
(should-not (search-backward "Cell 2" nil t))))
;;; Cell collapsing and output clearing
(ert-deftest ein:worksheet-toggle-output ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let ((cell (call-interactively #'ein:worksheet-insert-cell-below)))
(should-not (oref cell :collapsed))
(call-interactively #'ein:worksheet-toggle-output)
(should (oref cell :collapsed))
(call-interactively #'ein:worksheet-toggle-output)
(should-not (oref cell :collapsed)))))
(defun ein:testing-insert-cells (list-type-or-cell &optional pivot callback)
(cl-loop with ws = ein:%worksheet%
with cell = pivot
for type in list-type-or-cell
for i from 0
do (setq cell (ein:worksheet-insert-cell-below ws type cell t))
if callback
do (funcall callback i cell)))
(cl-defun ein:testing-insert-cells-with-format (num &optional
(format "Cell %s")
(type 'code))
(ein:testing-insert-cells (cl-loop repeat num collect type)
nil
(lambda (i &rest _) (insert (format format i))))
(should (equal (ein:worksheet-ncells ein:%worksheet%) num)))
(defun ein:testing-test-output-visibility-all (collapsed)
(mapc (lambda (cell) (should (eq (oref cell :collapsed) collapsed)))
(ein:worksheet-get-cells ein:%worksheet%)))
(ert-deftest ein:worksheet-set-output-visibility-all/visible-from-all-hidden ()
(with-current-buffer (ein:testing-notebook-make-empty)
;; Prepare cells
(ein:testing-insert-cells '(code code code))
(mapc (lambda (cell) (ein:cell-set-collapsed cell nil))
(ein:worksheet-get-cells ein:%worksheet%))
;; Call the command
(call-interactively #'ein:worksheet-set-output-visibility-all)
;; Check it worked
(ein:testing-test-output-visibility-all nil)))
(ert-deftest ein:worksheet-set-output-visibility-all/hidden-from-all-visible ()
(with-current-buffer (ein:testing-notebook-make-empty)
;; Prepare cells
(ein:testing-insert-cells '(code code code))
;; Call the command
(let ((current-prefix-arg t))
(call-interactively #'ein:worksheet-set-output-visibility-all))
;; Check it worked
(ein:testing-test-output-visibility-all t)))
(ert-deftest ein:worksheet-set-output-visibility-all/visible-from-part-hidden ()
(with-current-buffer (ein:testing-notebook-make-empty)
;; Prepare cells
(ein:testing-insert-cells '(code code code))
(ein:cell-set-collapsed
(nth 1 (ein:worksheet-get-cells ein:%worksheet%)) nil)
;; Call the command
(call-interactively #'ein:worksheet-set-output-visibility-all)
;; Check it worked
(ein:testing-test-output-visibility-all nil)))
(ert-deftest ein:worksheet-set-output-visibility-all/hidden-from-part-visible ()
(with-current-buffer (ein:testing-notebook-make-empty)
;; Prepare cells
(ein:testing-insert-cells '(code code code))
(ein:cell-set-collapsed
(nth 1 (ein:worksheet-get-cells ein:%worksheet%)) nil)
;; Call the command
(call-interactively #'ein:worksheet-set-output-visibility-all)
(let ((current-prefix-arg t))
(call-interactively #'ein:worksheet-set-output-visibility-all))
;; Check it worked
(ein:testing-test-output-visibility-all t)))
(defun ein:testing-assert-cell-output-num (cell num-outputs)
(should (ein:codecell-p cell))
(should (= (length (oref cell :outputs)) num-outputs)))
(ert-deftest ein:worksheet-clear-output/simple ()
(with-current-buffer
(ein:testing-make-notebook-with-outputs '(("'cell output'")
("'cell output'")))
(should (= (ein:worksheet-ncells ein:%worksheet%) 2))
(let* ((cells (ein:worksheet-get-cells ein:%worksheet%)))
(ein:testing-assert-cell-output-num (nth 0 cells) 1)
(ein:testing-assert-cell-output-num (nth 1 cells) 1)
(ein:cell-goto (nth 0 cells))
(call-interactively #'ein:worksheet-clear-output)
(ein:testing-assert-cell-output-num (nth 0 cells) 0) ; cleared
(ein:testing-assert-cell-output-num (nth 1 cells) 1))))
(ert-deftest ein:worksheet-clear-output/preserve-input-prompt ()
(with-current-buffer
(ein:testing-make-notebook-with-outputs '(("'cell output'")
("'cell output'")
("'cell output'")))
(should (= (ein:worksheet-ncells ein:%worksheet%) 3))
(let* ((cells (ein:worksheet-get-cells ein:%worksheet%)))
(ein:cell-set-input-prompt (nth 0 cells) 111)
(ein:cell-set-input-prompt (nth 1 cells) 222)
(ein:cell-set-input-prompt (nth 2 cells) 333)
;; Call `ein:worksheet-clear-output' with/without prefix argument.
(ein:cell-goto (nth 0 cells))
(call-interactively #'ein:worksheet-clear-output)
(ein:cell-goto (nth 2 cells))
(let ((current-prefix-arg '(4)))
(call-interactively #'ein:worksheet-clear-output))
;; Check cells' prompt number
(should (eq (oref (nth 0 cells) :input-prompt-number) nil))
(should (eq (oref (nth 1 cells) :input-prompt-number) 222))
(should (eq (oref (nth 2 cells) :input-prompt-number) 333)))))
(ert-deftest ein:worksheet-clear-all-output/simple ()
(with-current-buffer
(ein:testing-make-notebook-with-outputs '(("'cell output'")
("'cell output'")))
(should (= (ein:worksheet-ncells ein:%worksheet%) 2))
(let* ((cells (ein:worksheet-get-cells ein:%worksheet%)))
(ein:testing-assert-cell-output-num (nth 0 cells) 1)
(ein:testing-assert-cell-output-num (nth 1 cells) 1)
(call-interactively #'ein:worksheet-clear-all-output)
(ein:testing-assert-cell-output-num (nth 0 cells) 0)
(ein:testing-assert-cell-output-num (nth 1 cells) 0))))
;; Kernel related things
(defun eintest:notebook-check-kernel-and-codecell (kernel cell)
(should (ein:$kernel-p kernel))
(should (ein:codecell-p cell))
(should (ein:$kernel-p (oref cell :kernel))))
(defun eintest:notebook-fake-execution (kernel text msg-id callbacks)
(mocker-let ((ein:kernel-execute
(kernel code callbacks kwd-silent silent)
((:input (list kernel text callbacks :silent nil))))
(ein:kernel-live-p
(kernel)
((:input (list kernel) :output t))))
(call-interactively #'ein:worksheet-execute-cell))
(ein:kernel-set-callbacks-for-msg kernel msg-id callbacks))
(ert-deftest ein:notebook-execute-current-cell ()
(with-current-buffer (ein:testing-notebook-make-empty)
(call-interactively #'ein:worksheet-insert-cell-below)
(let* ((text "print 'Hello World'")
(cell (ein:worksheet-get-current-cell))
(kernel (ein:$notebook-kernel ein:%notebook%))
(msg-id "DUMMY-MSG-ID")
(callbacks (ein:cell-make-callbacks cell)))
(eintest:notebook-check-kernel-and-codecell kernel cell)
;; Execute
(insert text)
(eintest:notebook-fake-execution kernel text msg-id callbacks)
;; Execute reply
(should-error (eintest:check-search-forward-from (point-min) "In [1]:"))
(eintest:kernel-fake-execute-reply kernel msg-id 1)
(should (= (oref cell :input-prompt-number) 1))
(eintest:check-search-forward-from (point-min) "In [1]:")
;; Stream output
(eintest:kernel-fake-stream kernel msg-id "Hello World")
(should (= (ein:cell-num-outputs cell) 1))
(save-excursion
(goto-char (point-min))
(should (search-forward "In [1]:" nil t))
(should (search-forward "print 'Hello World'" nil t))
(should (search-forward "\nHello World\n" nil t)) ; stream output
(should-not (search-forward "Hello World" nil t))))))
(defmacro eintest:worksheet-execute-cell-and-*-deftest
(do-this cell-type has-next-p insert-p)
"Define:
ein:worksheet-execute-cell-and-{DO-THIS}/on-{CELL-TYPE}cell-{no,with}-next
For example, when `goto-next', \"code\", `nil', `nil' is given,
`ein:worksheet-execute-cell-and-goto-next/on-codecell-no-next' is
defined."
(let ((test-name
(intern (format "ein:worksheet-execute-cell-and-%s/on-%scell-%s"
do-this cell-type
(if has-next-p "with-next" "no-next"))))
(command
(intern (format "ein:worksheet-execute-cell-and-%s" do-this))))
`(ert-deftest ,test-name ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let* ((ws ein:%worksheet%)
(current (ein:worksheet-insert-cell-below ws ,cell-type nil t))
,@(when has-next-p
'((next
(ein:worksheet-insert-cell-below ws "code" current)))))
(mocker-let ((ein:worksheet-execute-cell
(ws cell)
(,@(when (equal cell-type "code")
'((:input (list ein:%worksheet% current)))))))
(call-interactively #',command)
(let ((cell (ein:worksheet-get-current-cell)))
(should (eq (ein:cell-prev cell) current))
,(when has-next-p
(if insert-p
'(should-not (eq cell next))
'(should (eq cell next)))))))))))
(eintest:worksheet-execute-cell-and-*-deftest goto-next "code" nil t )
(eintest:worksheet-execute-cell-and-*-deftest goto-next "code" t nil)
(eintest:worksheet-execute-cell-and-*-deftest goto-next "markdown" nil t )
(eintest:worksheet-execute-cell-and-*-deftest goto-next "markdown" t nil)
(eintest:worksheet-execute-cell-and-*-deftest insert-below "code" nil t )
(eintest:worksheet-execute-cell-and-*-deftest insert-below "code" t t )
(eintest:worksheet-execute-cell-and-*-deftest insert-below "markdown" nil t )
(eintest:worksheet-execute-cell-and-*-deftest insert-below "markdown" t t )
;;; Persistence and loading
(defun ein:testin-notebook-close (num-ws num-ss)
(should (= num-ws 1)) ; currently EIN only supports 1 WS
(should (>= num-ss 0))
(let* ((buf (ein:testing-notebook-make-empty))
(buffers (with-current-buffer buf
(ein:notebook-buffer-list ein:%notebook%))))
(with-current-buffer buf
(dotimes (_ num-ss)
(ein:notebook-scratchsheet-render-new ein:%notebook%))
(should (= (+ num-ws num-ss)
(length (seq-filter
(lambda (b) (not (buffer-base-buffer (get-buffer b))))
(ein:notebook-buffer-list ein:%notebook%))))))
(kill-buffer buf)
(mapc (lambda (b) (should-not (get-buffer b))) buffers)))
(ert-deftest ein:notebook-close/one-ws-no-ss ()
(ein:testin-notebook-close 1 0))
(ert-deftest ein:notebook-close/one-ws-one-ss ()
(ein:testin-notebook-close 1 1))
(ert-deftest ein:notebook-close/one-ws-five-ss ()
(ein:testin-notebook-close 1 5))
(defun ein:testing-notebook-data-assert-nb3-worksheet-contents (notebook &optional text)
(let* ((data (ein:notebook-to-json notebook))
(worksheets (assoc-default 'worksheets data #'eq))
(cells (assoc-default 'cells (elt worksheets 0) #'eq)))
(should (= (length worksheets) 1))
(if text
(let* ((cell-0 (elt cells 0))
(input (assoc-default 'input cell-0 #'eq)))
(should (equal input text)))
(should (= (length cells) 0)))))
(defun ein:testing-notebook-data-assert-nb4-worksheet-contents (notebook &optional text)
(let* ((data (ein:notebook-to-json notebook))
(cells (assoc-default 'cells data)))
(if text
(progn
(should (= (length cells) 1))
(should (equal (assoc-default 'source (elt cells 0) #'eq) text)))
(should (zerop (length cells))))))
(ert-deftest ein:notebook-saves-latin ()
(with-current-buffer (ein:testing-notebook-make-new)
(let ((latin "«utf-8 cannot handle these»"))
;; Edit notebook.
(ein:cell-goto (ein:get-cell-at-point))
(insert latin)
(cl-letf (((symbol-function 'request) #'ignore))
(should-not (ein:notebook-save-notebook (ein:get-notebook)))))))
(ert-deftest ein:notebook-to-json-after-closing-a-worksheet ()
(with-current-buffer (ein:testing-notebook-make-new)
(let ((notebook ein:%notebook%))
;; Edit notebook.
(ein:cell-goto (ein:get-cell-at-point))
(insert "some text")
(if (< (ein:$notebook-nbformat notebook) 4)
(ein:testing-notebook-data-assert-nb3-worksheet-contents notebook "some text")
(ein:testing-notebook-data-assert-nb4-worksheet-contents notebook "some text"))
(should (ein:notebook-modified-p notebook))
(ein:notebook-save-notebook-success notebook)
(should-not (ein:notebook-modified-p notebook))
;; Kill scratch sheet.
(kill-buffer (ein:worksheet-buffer (ein:notebook-scratchsheet-open notebook)))
(should (ein:notebook-live-p notebook))
;; to-json should still work
(if (< (ein:$notebook-nbformat notebook) 4)
(ein:testing-notebook-data-assert-nb3-worksheet-contents notebook "some text")
(ein:testing-notebook-data-assert-nb4-worksheet-contents notebook "some text")))))
(ert-deftest ein:notebook-to-json-after-discarding-a-worksheet ()
(with-current-buffer (ein:testing-notebook-make-new)
(let (asked
(buffer (current-buffer))
(notebook ein:%notebook%))
;; Edit notebook.
(ein:cell-goto (ein:get-cell-at-point))
(insert "some text")
(if (< (ein:$notebook-nbformat notebook) 4)
(ein:testing-notebook-data-assert-nb3-worksheet-contents notebook "some text")
(ein:testing-notebook-data-assert-nb4-worksheet-contents notebook "some text"))
(should (ein:notebook-modified-p notebook))
(let ((ss-buf (save-current-buffer (ein:notebook-scratchsheet-open notebook))))
;; Discard a worksheet buffer
(cl-letf (((symbol-function 'y-or-n-p)
(lambda (&rest _args) (setq asked t) nil)))
(should-not (kill-buffer buffer)))
(should asked)
(should-not (buffer-live-p buffer))
(should-not (ein:notebook-live-p notebook))
(should-not (buffer-live-p ss-buf))))))
(defun ein:testing-notebook-should-be-closed (notebook buffer)
(should-not (buffer-live-p buffer))
(should-not (ein:notebook-live-p notebook)))
(ert-deftest ein:notebook-kill-kernel-then-close-when-its-alive ()
(with-current-buffer (ein:testing-notebook-make-new)
(let ((buffer (current-buffer))
(notebook ein:%notebook%))
(cl-letf (((symbol-function 'ein:kernel-live-p) (lambda (&rest _args) t))
((symbol-function 'ein:kernel-delete-session)
(cl-function (lambda (callback &key kernel) (funcall callback kernel)))))
(call-interactively #'ein:notebook-kill-kernel-then-close-command))
(ein:testing-notebook-should-be-closed notebook buffer))))
(ert-deftest ein:notebook-kill-kernel-then-close-when-already-dead ()
(with-current-buffer (ein:testing-notebook-make-new)
(let ((buffer (current-buffer))
(notebook ein:%notebook%)
(kernel (ein:$notebook-kernel ein:%notebook%)))
(mocker-let
((ein:kernel-live-p
(kernel)
((:input (list kernel) :output nil))))
(call-interactively #'ein:notebook-kill-kernel-then-close-command))
(ein:testing-notebook-should-be-closed notebook buffer))))
;; Notebook undo
(defun eintest:notebook-undo-after-insert-above ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let ((text "some text"))
(call-interactively #'ein:worksheet-insert-cell-above)
(insert text)
(undo-boundary)
(call-interactively #'ein:worksheet-insert-cell-above)
(call-interactively #'ein:worksheet-goto-next-input)
(should (equal (ein:cell-get-text (ein:worksheet-get-current-cell)) text))
(if ein:worksheet-enable-undo
(undo)
(should-error (undo)))
(when ein:worksheet-enable-undo
(should (equal (buffer-string) "
In [ ]:
In [ ]:
"))))))
(defun eintest:notebook-undo-after-split ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let ((line-1 "first line")
(line-2 "second line"))
(call-interactively #'ein:worksheet-insert-cell-below)
(insert line-1 "\n" line-2)
(undo-boundary)
(move-beginning-of-line 1)
(call-interactively #'ein:worksheet-split-cell-at-point)
(undo-boundary)
(should (equal (ein:cell-get-text (ein:worksheet-get-current-cell))
line-2))
(if ein:worksheet-enable-undo
(undo)
(should-error (undo)))
(when ein:worksheet-enable-undo
(should (equal (buffer-string) "
In [ ]:
In [ ]:
first line
second line
"))))))
(defun eintest:notebook-undo-after-merge ()
(with-current-buffer (ein:testing-notebook-make-empty)
(let ((line-1 "first line")
(line-2 "second line"))
(call-interactively #'ein:worksheet-insert-cell-below)
(call-interactively #'ein:worksheet-insert-cell-below)