1 ;;;; testcover-ses.el -- Example use of `testcover' to test "SES"
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Jonathan Yavner <jyavner@engineer.com>
7 ;; Maintainer: Jonathan Yavner <jyavner@engineer.com>
8 ;; Keywords: spreadsheet lisp utility
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 (defvar ses-initial-global-parameters
)
28 (declare-function ses-set-curcell
"ses")
29 (declare-function ses-update-cells
"ses")
30 (declare-function ses-load
"ses")
31 (declare-function ses-vector-delete
"ses")
32 (declare-function ses-create-header-string
"ses")
33 (declare-function ses-read-cell
"ses")
34 (declare-function ses-read-symbol
"ses")
35 (declare-function ses-command-hook
"ses")
36 (declare-function ses-jump
"ses")
39 ;;;Here are some macros that exercise SES. Set `pause' to t if you want the
40 ;;;macros to pause after each step.
42 (x (if pause
"\x18q" ""))
43 (y "\x18\x06ses-test.ses\r\e<"))
44 ;;Fiddle with the existing spreadsheet
45 (fset 'ses-exercise-example
46 (concat "\x18\x06" data-directory
"ses-example.ses\r\e<"
50 x
"\x10\x10\x06pses-center\r"
54 x
"\x15\x0e\x02\x02\x02"
61 x
"(+ \x18o\x0e\x0e\x06\0\x06\x06"
62 x
"\x15-1\x18o\x03\x12 \x03\x13\r\x02"
65 ;;Create a new spreadsheet
66 (fset 'ses-exercise-new
68 x
"\x03\x10\"%.8g\"\r"
80 x
"\r\x7f\x7f\x7fB3\r"
84 (fset 'ses-exercise-display
85 (concat y
"\e:(revert-buffer t t)\r"
111 x
"\x02\x02\x02\"1234567-1234567-1234567\r\x02"
114 x
"\x0e\"1234567-1234567-1234567\r\x02"
117 x
"\x02\x02\"1234567\r"
121 (fset 'ses-exercise-formulas
122 (concat y
"\e:(revert-buffer t t)\r"
125 x
"(* B1 B2 D1\r\x02"
127 x
"\x0e(apply '+ (ses-range B1 B3)\r\x02"
128 x
"(apply 'ses+ (ses-range B1 B3)\r\x02"
129 x
"\x0e(apply 'ses+ (ses-range A2 A3)\r\x02"
130 x
"\x0e(mapconcat'number-to-string(ses-range B2 B4) \"-\"\r\x02"
131 x
"\x02(apply 'concat (reverse (ses-range A3 D3))\r\x02"
132 x
"\x02(* (+ A2 A3) (ses+ B2 B3)\r\x02"
136 x
"\x10(apply 'ses+ (ses-range E1 E2)\r\x02"
137 x
"\x10(apply 'ses+ (ses-range A5 B5)\r\x02"
138 x
"\x10(apply 'ses+ (ses-range E1 F1)\r\x02"
139 x
"\x10(apply 'ses+ (ses-range D1 E1)\r\x02"
141 x
"(ses-average (ses-range A2 A5)\r\x02"
142 x
"\x0e(apply 'ses+ (ses-range A5 A6)\r\x02"
151 x
"\x06(ses-average (ses-range B3 E3)\r\x02"
153 x
"\x0e\x1012345678\r\x02"
155 ;;Recalculating and reconstructing
156 (fset 'ses-exercise-recalc
157 (concat y
"\e:(revert-buffer t t)\r"
165 x
"\x03\x10\"%.6g\"\r"
167 x
"\e>\x18nw\x06\x06\x06"
168 x
"\0\e>\exdelete-region\r"
171 x
"\0\e>\exdelete-region\r"
176 x
"\x02\"Very long\r"
182 x
"\x02\x02\"Very long2\r"
185 x
"\r\x7f\x7f\x7fC3\r"
186 x
"\x0e\r\x7f\x7f\x7fC2\r"
187 x
"\x10\0\x0e\x06\x03\x03"
189 x
"\x0e\x0e\r\x7f\x7f\x7fC2\r"
190 x
"\x06\0\x02\x10\x10"
197 (fset 'ses-exercise-header-row
198 (concat y
"\e:(revert-buffer t t)\r"
213 ;;Detecting unsafe formulas and printers
214 (fset 'ses-exercise-unsafe
215 (concat y
"\e:(revert-buffer t t)\r"
216 x
"p(lambda (x) (delete-file x))\rn"
217 x
"p(lambda (x) (delete-file \"ses-nothing\"))\ry"
219 x
"\x0e(delete-file \"x\"\rn"
220 x
"(delete-file \"ses-nothing\"\ry\x02"
222 x
"(open-network-stream \"x\" nil \"localhost\" \"smtp\"\ry\x02"
225 ;;Inserting and deleting rows
226 (fset 'ses-exercise-rows
227 (concat y
"\e:(revert-buffer t t)\r"
229 x
"\x03\x10\"%s=\"\r"
237 x
"\x10\x10(not B25\r\x02"
242 x
"\x15100\x0f" ;Make this approx your CPU speed in MHz
244 ;;Inserting and deleting columns
245 (fset 'ses-exercise-columns
246 (concat y
"\e:(revert-buffer t t)\r"
247 x
"\x03\x10\"%s@\"\r"
259 x
"\x03\x10\"%.6g\"\r"
267 x
"\0\x0e\x0e\x06\x06\x03\e\x13D"
269 (fset 'ses-exercise-editing
270 (concat y
"\e:(revert-buffer t t)\r"
271 x
"\x0e\x0e\x0e1\r\x02"
272 x
"\x06(\x02'\x06x\r\x02"
273 x
"\x02\x10\x10\x10\x0f"
278 x
"\x0e\x06\r\x02 10\r"
287 x
"\x02\"Very long\r\x02"
295 x
"\x06\x06\x152\x7f"
300 x
"\"Also very long\r\x02"
304 x
"\x0e\x02'qwerty\r\x02"
305 x
"\x06(concat \x18o\e<\0\x0e\x0e"
306 x
"\x15-1\x18o\x03\x12\r\x02"
307 x
"(apply '+ \x18o\e<\0\x0e\x06\x15-1\x18o\x03\x13\r\x02"
315 x
"\"Another long one\r\x02"
324 (fset 'ses-exercise-sort-column
325 (concat y
"\e:(revert-buffer t t)\r"
331 x
"\0\x10\x10\x10\x03\e\x13A\r"
332 x
"\x0e\0\x10\x10\x10\x03\e\x13B\r"
333 x
"\x10\x10\x06\0\x0e\x0e\x06\x06\x03\e\x13C\r"
335 x
"\x02\0\x0e\x0e\x0e\x15\x03\e\x13C\r"
337 ;;Simple cell printers
338 (fset 'ses-exercise-cell-printers
339 (concat y
"\e:(revert-buffer t t)\r"
340 x
"\x06\"4\x11\t76\r\x02"
354 x
"\x02\0\x06\x06pnil\r"
362 x
"\x03\x10\"%.6g#\"\r"
363 x
"\x03\x10\"%.6g.\"\r"
364 x
"\x03\x10\"%.6g.\"\r"
367 x
"\x0e\"UPCASE\r\x02"
370 x
"p(lambda\x11 (x)\x11 '(\"Hi\"))\r"
371 x
"p(lambda\x11 (x)\x11 '(\"Bye\"))\r"
373 ;;Spanning cell printers
374 (fset 'ses-exercise-spanning-printers
375 (concat y
"\e:(revert-buffer t t)\r"
377 x
"pses-dashfill-span\r"
379 x
"pses-tildefill-span\r"
385 x
"\t\"12345678\r\x02"
386 x
"pses-dashfill-span\r"
401 ;;Cut/copy/paste - within same buffer
402 (fset 'ses-exercise-paste-1buf
403 (concat y
"\e:(revert-buffer t t)\r"
405 x
"\x03\x03\x10\x06\x19"
416 x
"\x10\x10\x06\x15\x19y"
422 x
"\x06pses-dashfill\r"
423 x
"\x02\0\x06\x06\x06\x0e\x0e\x0e"
427 x
"\r\0\x02\x02\x02\ew"
429 x
"\x153\x10(+ G2 H1\r"
433 x
"\x02\x158\x10(ses-average (ses-range G2 H2)\r\x02"
437 x
"\x10\x02(ses-average (ses-range E7 E9)\r\x02"
440 x
"\x02\x02\x10(ses-average (ses-range E7 F7)\r\x02"
443 x
"\x02\x02\x10(ses-average (ses-range D6 E6)\r\x02"
448 x
"pses-tildefill-span\r"
449 x
"\x0e\x06\"Subline A(1)\r\x02"
450 x
"pses-dashfill-span\r"
451 x
"\x02\x10\0\x0e\x0e\x0e\ew\x03\x03"
452 x
"\x01\x10\x10\x10\x10\x10\x10"
454 x
"\0\x0e\x06\x06\ew\x03\x03"
457 ;;Cut/copy/paste - between two buffers
458 (fset 'ses-exercise-paste-2buf
459 (concat y
"\e:(revert-buffer t t)\r"
460 x
"\x06\x0e\eo\"middle\r\x02\0\x06\x0e\x06"
462 x
"\x184bses-test.txt\r"
464 x
"\x05\"xxx\0\x02\x02\x02\x02"
468 x
"\x18o\x05\"\0\x02\x02\x02\x02\x02"
470 x
"\x18o123.45\0\x02\x02\x02\x02\x02\x02"
472 x
"\x18o1 \x02\x02\0\x06\x06\x06\x06\x06\x06\x06"
475 x
"\x06\x18o symb\0\x02\x02\x02\x02"
476 x
"\x17\x18o\x15\x19\ey\x152\ey"
477 x
"\x18o1\t\0\x02\x02"
478 x
"\x17\x18o\x02\x19"
479 x
"w9\n\ep\"<%s>\"\n"
480 x
"\x18o\n2\t\"3\nxxx\t5\n\0\x10\x10"
483 ;;Export text, import it back
484 (fset 'ses-exercise-import-export
485 (concat y
"\e:(revert-buffer t t)\r"
486 x
"\x0e\x0e\x06\0\x06xt"
487 x
"\x184bses-test.txt\r"
488 x
"\n\x19\x15-1\x18o"
489 x
"xT\x18o\x19\x15-1\x18o"
490 x
"\x03\x03\x06'crunch\r\x02"
491 x
"\x10\x10\x10pses-center-span\r"
492 x
"\0\x0e\x0e\x0e\x0exT"
493 x
"\x18o\n\x19\x15-1\x18o"
495 x
"\x06\0\x02\x10\x10xt"
496 x
"\x0e\x0e\0\x15\x19y"
502 (defun ses-exercise-macros ()
503 "Executes all SES coverage-test macros."
504 (dolist (x '(ses-exercise-example
507 ses-exercise-formulas
509 ses-exercise-header-row
514 ses-exercise-sort-column
515 ses-exercise-cell-printers
516 ses-exercise-spanning-printers
517 ses-exercise-paste-1buf
518 ses-exercise-paste-2buf
519 ses-exercise-import-export
))
520 (message "<Testing %s>" x
)
521 (execute-kbd-macro x
)))
523 (defun ses-exercise-signals ()
524 "Exercise code paths that lead to error signals, other than those for
525 spreadsheet files with invalid formatting."
526 (message "<Checking for expected errors>")
527 (switch-to-buffer "ses-test.ses")
531 (dolist (x '((ses-column-widths 14)
532 (ses-column-printers "%s")
533 (ses-column-printers ["%s" "%s" "%s"]) ;Should be two
534 (ses-column-widths [14])
535 (ses-delete-column -99)
536 (ses-delete-column 2)
538 (ses-goto-data 'hogwash)
541 (ses-insert-column -14)
543 (ses-jump 'B8) ;Covered by preceding cell
544 (ses-printer-validate '("%s" t))
545 (ses-printer-validate '([47]))
546 (ses-read-header-row -1)
547 (ses-read-header-row 32767)
548 (ses-relocate-all 0 0 -1 1)
549 (ses-relocate-all 0 0 1 -1)
550 (ses-select (ses-range A1 A2) 'x (ses-range B1 B1))
551 (ses-set-cell 0 0 'hogwash nil)
552 (ses-set-column-width 0 0)
553 (ses-yank-cells #("a\nb"
554 0 1 (ses (A1 nil nil))
555 2 3 (ses (A3 nil nil)))
557 (ses-yank-cells #("ab"
558 0 1 (ses (A1 nil nil))
559 1 2 (ses (A2 nil nil)))
562 (ses-yank-tsf "1\t2\n3" nil)
563 (let ((curcell nil)) (ses-check-curcell))
564 (let ((curcell 'A1)) (ses-check-curcell 'needrange))
565 (let ((curcell '(A1 . A2))) (ses-check-curcell 'end))
566 (let ((curcell '(A1 . A2))) (ses-sort-column "B"))
567 (let ((curcell '(C1 . D2))) (ses-sort-column "B"))
568 (execute-kbd-macro "jB10\n\x152\x04")
569 (execute-kbd-macro [?j ?B ?9 ?\n ?\C-@ ?\C-f ?\C-f cut])
570 (progn (kill-new "x") (execute-kbd-macro "\e>\x19n"))
571 (execute-kbd-macro "\x02\0\ew")))
575 (signal 'singularity-error nil)) ;Shouldn't get here
576 (singularity-error (error "No error from %s?" x))
578 ;;Test quit-handling in ses-update-cells. Cant' use `eval' here.
579 (let ((inhibit-quit t))
583 (ses-update-cells '(A1))
584 (signal 'singularity-error nil))
585 (singularity-error (error "Quit failure in ses-update-cells"))
587 (setq quit-flag nil)))
589 (defun ses-exercise-invalid-spreadsheets ()
590 "Execute code paths that detect invalid spreadsheet files."
591 ;;Detect invalid spreadsheets
592 (let ((p&d "\n\n\f\n(ses-cell A1 nil nil nil nil)\n\n")
593 (cw "(ses-column-widths [7])\n")
594 (cp "(ses-column-printers [ses-center])\n")
595 (dp "(ses-default-printer \"%.7g\")\n")
596 (hr "(ses-header-row 0)\n")
598 (igp ses-initial-global-parameters))
599 (dolist (x (list "(1)"
607 "\n\n\f\n(ses-cell)(2 1 1)"
608 "\n\n\f\n(x)\n(2 1 1)"
609 "\n\n\n\f\n(ses-cell A2)\n(2 2 2)"
610 "\n\n\n\f\n(ses-cell B1)\n(2 2 2)"
611 "\n\n\f\n(ses-cell A1 nil nil nil nil)\n(2 1 1)"
612 (concat p&d "(x)\n(x)\n(x)\n(x)\n" p11)
613 (concat p&d "(ses-column-widths)(x)\n(x)\n(x)\n" p11)
614 (concat p&d cw "(x)\n(x)\n(x)\n(2 1 1)")
615 (concat p&d cw "(ses-column-printers)(x)\n(x)\n" p11)
616 (concat p&d cw cp "(x)\n(x)\n" p11)
617 (concat p&d cw cp "(ses-default-printer)(x)\n" p11)
618 (concat p&d cw cp dp "(x)\n" p11)
619 (concat p&d cw cp dp "(ses-header-row)" p11)
620 (concat p&d cw cp dp hr p11)
621 (concat p&d cw cp dp "\n" hr igp)))
626 (signal 'singularity-error nil)) ;Shouldn't get here
627 (singularity-error (error "%S is an invalid spreadsheet!" x))
630 (defun ses-exercise-startup ()
631 "Prepare for coverage tests"
632 ;;Clean up from any previous runs
633 (condition-case nil (kill-buffer "ses-example.ses") (error nil))
634 (condition-case nil (kill-buffer "ses-test.ses") (error nil))
635 (condition-case nil (delete-file "ses-test.ses") (file-error nil))
636 (delete-other-windows) ;Needed for "\C-xo" in ses-exercise-editing
637 (setq ses-mode-map nil) ;Force rebuild
638 (testcover-unmark-all "ses.el")
640 (let ((testcover-1value-functions
641 ;;forward-line always returns 0, for us.
642 ;;remove-text-properties always returns t for us.
643 ;;ses-recalculate-cell returns the same " " any time curcell is a cons
644 ;;Macros ses-dorange and ses-dotimes-msg generate code that always
646 (append '(forward-line remove-text-properties ses-recalculate-cell
647 ses-dorange ses-dotimes-msg)
648 testcover-1value-functions))
650 ;;These maps get initialized, then never changed again
651 (append '(ses-mode-map ses-mode-print-map ses-mode-edit-map)
652 testcover-constants)))
653 (testcover-start "ses.el" t))
654 (require 'unsafep)) ;In case user has safe-functions = t!
657 ;;;#########################################################################
658 (defun ses-exercise ()
659 "Executes all SES coverage tests and displays the results."
661 (ses-exercise-startup)
662 ;;Run the keyboard-macro tests
663 (let ((safe-functions nil)
664 (ses-initial-size '(1 . 1))
665 (ses-initial-column-width 7)
666 (ses-initial-default-printer "%.7g")
667 (ses-after-entry-functions '(forward-char))
669 (ses-exercise-macros)
670 (ses-exercise-signals)
671 (ses-exercise-invalid-spreadsheets)
672 ;;Upgrade of old-style spreadsheet
674 (insert " \n\n\f\n(ses-cell A1 nil nil nil nil)\n\n(ses-column-widths [7])\n(ses-column-printers [nil])\n(ses-default-printer \"%.7g\")\n\n( ;Global parameters (these are read first)\n 1 ;SES file-format\n 1 ;numrows\n 1 ;numcols\n)\n\n")
676 ;;ses-vector-delete is always called from buffer-undo-list with the same
677 ;;symbol as argument. We'll give it a different one here.
679 (ses-vector-delete 'x 0 0))
680 ;;ses-create-header-string behaves differently in a non-window environment
681 ;;but we always test under windows.
682 (let ((window-system (not window-system)))
684 (ses-create-header-string))
685 ;;Test for nonstandard after-entry functions
686 (let ((ses-after-entry-functions '(forward-line))
688 (ses-read-cell 0 0 1)
689 (ses-read-symbol 0 0 t)))
690 ;;Tests with unsafep disabled
691 (let ((safe-functions t)
693 (message "<Checking safe-functions = t>")
694 (kill-buffer "ses-example.ses")
695 (find-file "ses-example.ses"))
696 ;;Checks for nonstandard default values for new spreadsheets
698 (dolist (x '(("%.6g" 8 (2 . 2))
700 (let ((ses-initial-size (nth 2 x))
701 (ses-initial-column-width (nth 1 x))
702 (ses-initial-default-printer (nth 0 x)))
704 (set-buffer-modified-p t)
706 ;;Test error-handling in command hook, outside a macro.
707 ;;This will ring the bell.
708 (let (curcell-overlay)
710 ;;Due to use of run-with-timer, ses-command-hook sometimes gets called
711 ;;after we switch to another buffer.
712 (switch-to-buffer "*scratch*")
715 (message "<Marking source code>")
716 (testcover-mark-all "ses.el")
717 (testcover-next-mark)
719 (delete-other-windows)
720 (kill-buffer "ses-test.txt")
721 ;;Could do this here: (testcover-end "ses.el")
724 ;; arch-tag: 87052ba4-5cf8-46cf-9375-fe245f3360b8
725 ;; testcover-ses.el ends here.