Fix Bug#28139
[emacs.git] / test / lisp / ses-tests.el
blob3194b261565a3c97264ca7d6f798f5de02c9fb63
1 ;;; ses-tests.el --- Tests for ses.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2017 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 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 A2 2)))))
54 ;; PLAIN CELL RENAMING TESTS
55 ;; ======================================================================
57 (ert-deftest ses-tests-lowlevel-renamed-cell ()
58 "Check that renaming A1 to `foo' and setting `foo' to 1 and A2 to (1+ foo), makes A2 value equal to 2.
59 This is done using low level functions, `ses-rename-cell' is not
60 called but instead we use text replacement in the buffer
61 previously passed in text mode."
62 (let ((ses-initial-size '(2 . 1)))
63 (with-temp-buffer
64 (ses-mode)
65 (dolist (c '((0 0 1) (1 0 (1+ A1))))
66 (apply 'ses-cell-set-formula c)
67 (apply 'ses-calculate-cell (list (car c) (cadr c) nil)))
68 (ses-write-cells)
69 (text-mode)
70 (goto-char (point-min))
71 (while (re-search-forward "\\<A1\\>" nil t)
72 (replace-match "foo" t t))
73 (ses-mode)
74 (should-not (local-variable-p 'A1))
75 (should (eq foo 1))
76 (should (equal (ses-cell-formula 1 0) '(ses-safe-formula (1+ foo))))
77 (should (eq A2 2)))))
79 (ert-deftest ses-tests-renamed-cell ()
80 "Check that renaming A1 to `foo' and setting `foo' to 1 and A2
81 to (1+ foo), makes A2 value equal to 2."
82 (let ((ses-initial-size '(2 . 1)))
83 (with-temp-buffer
84 (ses-mode)
85 (ses-rename-cell 'foo (ses-get-cell 0 0))
86 (dolist (c '((0 0 1) (1 0 (1+ foo))))
87 (apply 'funcall-interactively 'ses-edit-cell c))
88 (ses-command-hook)
89 (should-not (local-variable-p 'A1))
90 (should (eq foo 1))
91 (should (equal (ses-cell-formula 1 0) '(1+ foo)))
92 (should (eq A2 2)))))
94 (ert-deftest ses-tests-renamed-cell-after-setting ()
95 "Check that setting A1 to 1 and A2 to (1+ A1), and then
96 renaming A1 to `foo' makes `foo' value equal to 2."
97 (let ((ses-initial-size '(2 . 1)))
98 (with-temp-buffer
99 (ses-mode)
100 (dolist (c '((0 0 1) (1 0 (1+ A1))))
101 (apply 'funcall-interactively 'ses-edit-cell c))
102 (ses-command-hook); deferred recalc
103 (ses-rename-cell 'foo (ses-get-cell 0 0))
104 (should-not (local-variable-p 'A1))
105 (should (eq foo 1))
106 (should (equal (ses-cell-formula 1 0) '(1+ foo)))
107 (should (eq A2 2)))))
109 (ert-deftest ses-tests-renaming-cell-with-one-symbol-formula ()
110 "Check that setting A1 to 1 and A2 to A1, and then renaming A1
111 to `foo' makes `foo' value equal to 1. Then set A1 to 2 and check
112 that `foo' becomes 2."
113 (let ((ses-initial-size '(3 . 1)))
114 (with-temp-buffer
115 (ses-mode)
116 (dolist (c '((0 0 1) (1 0 A1)))
117 (apply 'funcall-interactively 'ses-edit-cell c))
118 (ses-command-hook); deferred recalc
119 (ses-rename-cell 'foo (ses-get-cell 0 0))
120 (ses-command-hook); deferred recalc
121 (should-not (local-variable-p 'A1))
122 (should (eq foo 1))
123 (should (equal (ses-cell-formula 1 0) 'foo))
124 (should (eq A2 1))
125 (funcall-interactively 'ses-edit-cell 0 0 2)
126 (ses-command-hook); deferred recalc
127 (should (eq A2 2))
128 (should (eq foo 2)))))
131 ;; ROW INSERTION TESTS
132 ;; ======================================================================
134 (ert-deftest ses-tests-plain-row-insertion ()
135 "Check that setting A1 to 1 and A2 to (1+ A1), and then jumping
136 to A2 and inserting a row, makes A2 value empty, and A3 equal to
138 (let ((ses-initial-size '(2 . 1)))
139 (with-temp-buffer
140 (ses-mode)
141 (dolist (c '((0 0 1) (1 0 (1+ A1))))
142 (apply 'funcall-interactively 'ses-edit-cell c))
143 (ses-command-hook)
144 (ses-jump 'A2)
145 (ses-insert-row 1)
146 (ses-command-hook)
147 (should-not A2)
148 (should (eq A3 2)))))
150 ; (defvar ses-tests-trigger nil)
152 (ert-deftest ses-tests-renamed-cells-row-insertion ()
153 "Check that setting A1 to 1 and A2 to (1+ A1), and then renaming A1 to `foo' and A2 to `bar' jumping
154 to `bar' and inserting a row, makes A2 value empty, and `bar' equal to
156 (setq ses-tests-trigger nil)
157 (let ((ses-initial-size '(2 . 1)))
158 (with-temp-buffer
159 (ses-mode)
160 (dolist (c '((0 0 1) (1 0 (1+ A1))))
161 (apply 'funcall-interactively 'ses-edit-cell c))
162 (ses-command-hook)
163 (ses-rename-cell 'foo (ses-get-cell 0 0))
164 (ses-command-hook)
165 (ses-rename-cell 'bar (ses-get-cell 1 0))
166 (ses-command-hook)
167 (should (eq bar 2))
168 (ses-jump 'bar)
169 (ses-insert-row 1)
170 (ses-command-hook)
171 (should-not A2)
172 (should (eq bar 2)))))
175 (provide 'ses-tests)