lisp/obsolete/*tls.el: Note when obsolescence was decided
[emacs.git] / test / lisp / ses-tests.el
blobc773c9b396fd41e4d7c97c22c08774cbe01b2a4f
1 ;;; ses-tests.el --- Tests for ses.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2018 Free Software Foundation, Inc.
5 ;; Author: Vincent Belaïche <vincentb1@users.sourceforge.net>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'ert)
25 (require 'ses)
28 ;; PLAIN FORMULA TESTS
29 ;; ======================================================================
31 (ert-deftest ses-tests-lowlevel-plain-formula ()
32 "Check that setting A1 to 1 and A2 to (1+ A1), makes A2 value
33 equal to 2. This is done with low level functions calls, not like
34 interactively."
35 (let ((ses-initial-size '(2 . 1)))
36 (with-temp-buffer
37 (ses-mode)
38 (dolist (c '((0 0 1) (1 0 (1+ A1))))
39 (apply 'ses-cell-set-formula c)
40 (apply 'ses-calculate-cell (list (car c) (cadr c) nil)))
41 (should (eq (bound-and-true-p A2) 2)))))
43 (ert-deftest ses-tests-plain-formula ()
44 "Check that setting A1 to 1 and A2 to (1+ A1), makes A2 value
45 equal to 2. This is done using interactive calls."
46 (let ((ses-initial-size '(2 . 1)))
47 (with-temp-buffer
48 (ses-mode)
49 (dolist (c '((0 0 1) (1 0 (1+ A1))))
50 (apply 'funcall-interactively 'ses-edit-cell c))
51 (ses-command-hook)
52 (should (eq (bound-and-true-p A2) 2)))))
54 ;; PLAIN CELL RENAMING TESTS
55 ;; ======================================================================
57 (defvar ses--foo)
58 (defvar ses--cells)
60 (ert-deftest ses-tests-lowlevel-renamed-cell ()
61 "Check that renaming A1 to `ses--foo' and setting `ses--foo' to 1 and A2 to (1+ ses--foo), makes A2 value equal to 2.
62 This is done using low level functions, `ses-rename-cell' is not
63 called but instead we use text replacement in the buffer
64 previously passed in text mode."
65 (let ((ses-initial-size '(2 . 1)))
66 (with-temp-buffer
67 (ses-mode)
68 (dolist (c '((0 0 1) (1 0 (1+ A1))))
69 (apply 'ses-cell-set-formula c)
70 (apply 'ses-calculate-cell (list (car c) (cadr c) nil)))
71 (ses-write-cells)
72 (text-mode)
73 (goto-char (point-min))
74 (while (re-search-forward "\\<A1\\>" nil t)
75 (replace-match "ses--foo" t t))
76 (ses-mode)
77 (should-not (local-variable-p 'A1))
78 (should (eq ses--foo 1))
79 (should (equal (ses-cell-formula 1 0) '(ses-safe-formula (1+ ses--foo))))
80 (should (eq (bound-and-true-p A2) 2)))))
82 (ert-deftest ses-tests-renamed-cell ()
83 "Check that renaming A1 to `ses--foo' and setting `ses--foo' to 1 and A2
84 to (1+ ses--foo), makes A2 value equal to 2."
85 (let ((ses-initial-size '(2 . 1)))
86 (with-temp-buffer
87 (ses-mode)
88 (ses-rename-cell 'ses--foo (ses-get-cell 0 0))
89 (dolist (c '((0 0 1) (1 0 (1+ ses--foo))))
90 (apply 'funcall-interactively 'ses-edit-cell c))
91 (ses-command-hook)
92 (should-not (local-variable-p 'A1))
93 (should (eq ses--foo 1))
94 (should (equal (ses-cell-formula 1 0) '(1+ ses--foo)))
95 (should (eq (bound-and-true-p A2) 2)))))
97 (ert-deftest ses-tests-renamed-cell-after-setting ()
98 "Check that setting A1 to 1 and A2 to (1+ A1), and then
99 renaming A1 to `ses--foo' makes `ses--foo' value equal to 2."
100 (let ((ses-initial-size '(2 . 1)))
101 (with-temp-buffer
102 (ses-mode)
103 (dolist (c '((0 0 1) (1 0 (1+ A1))))
104 (apply 'funcall-interactively 'ses-edit-cell c))
105 (ses-command-hook); deferred recalc
106 (ses-rename-cell 'ses--foo (ses-get-cell 0 0))
107 (should-not (local-variable-p 'A1))
108 (should (eq ses--foo 1))
109 (should (equal (ses-cell-formula 1 0) '(1+ ses--foo)))
110 (should (eq (bound-and-true-p A2) 2)))))
112 (ert-deftest ses-tests-renaming-cell-with-one-symbol-formula ()
113 "Check that setting A1 to 1 and A2 to A1, and then renaming A1
114 to `ses--foo' makes `ses--foo' value equal to 1. Then set A1 to 2 and check
115 that `ses--foo' becomes 2."
116 (let ((ses-initial-size '(3 . 1)))
117 (with-temp-buffer
118 (ses-mode)
119 (dolist (c '((0 0 1) (1 0 A1)))
120 (apply 'funcall-interactively 'ses-edit-cell c))
121 (ses-command-hook); deferred recalc
122 (ses-rename-cell 'ses--foo (ses-get-cell 0 0))
123 (ses-command-hook); deferred recalc
124 (should-not (local-variable-p 'A1))
125 (should (eq ses--foo 1))
126 (should (equal (ses-cell-formula 1 0) 'ses--foo))
127 (should (eq (bound-and-true-p A2) 1))
128 (funcall-interactively 'ses-edit-cell 0 0 2)
129 (ses-command-hook); deferred recalc
130 (should (eq (bound-and-true-p A2) 2))
131 (should (eq ses--foo 2)))))
134 ;; ROW INSERTION TESTS
135 ;; ======================================================================
137 (ert-deftest ses-tests-plain-row-insertion ()
138 "Check that setting A1 to 1 and A2 to (1+ A1), and then jumping
139 to A2 and inserting a row, makes A2 value empty, and A3 equal to
141 (let ((ses-initial-size '(2 . 1)))
142 (with-temp-buffer
143 (ses-mode)
144 (dolist (c '((0 0 1) (1 0 (1+ A1))))
145 (apply 'funcall-interactively 'ses-edit-cell c))
146 (ses-command-hook)
147 (ses-jump 'A2)
148 (ses-insert-row 1)
149 (ses-command-hook)
150 (should-not (bound-and-true-p A2))
151 (should (eq (bound-and-true-p A3) 2)))))
153 (defvar ses--bar)
155 (ert-deftest ses-tests-renamed-cells-row-insertion ()
156 "Check that setting A1 to 1 and A2 to (1+ A1), and then renaming A1 to `ses--foo' and A2 to `ses--bar' jumping
157 to `ses--bar' and inserting a row, makes A2 value empty, and `ses--bar' equal to
159 (let ((ses-initial-size '(2 . 1)))
160 (with-temp-buffer
161 (ses-mode)
162 (dolist (c '((0 0 1) (1 0 (1+ A1))))
163 (apply 'funcall-interactively 'ses-edit-cell c))
164 (ses-command-hook)
165 (ses-rename-cell 'ses--foo (ses-get-cell 0 0))
166 (ses-command-hook)
167 (ses-rename-cell 'ses--bar (ses-get-cell 1 0))
168 (ses-command-hook)
169 (should (eq ses--bar 2))
170 (ses-jump 'ses--bar)
171 (ses-insert-row 1)
172 (ses-command-hook)
173 (should-not (bound-and-true-p A2))
174 (should (eq ses--bar 2)))))
177 (provide 'ses-tests)