Fix tests wrt commit 0c31a4fc
[org-mode.git] / testing / lisp / test-org-table.el
blob5386726bca06909fd76e6040fa012e78758da117
1 ;;; test-org-table.el --- tests for org-table.el
3 ;; Copyright (c) David Maus
4 ;; Authors: David Maus, Michael Brand
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;;; Comments:
23 ;; Template test file for Org-mode tests. First the tests that are
24 ;; also a howto example collection as a user documentation, more or
25 ;; less all those using `org-test-table-target-expect'. Then the
26 ;; internal and more abstract tests. See also the doc string of
27 ;; `org-test-table-target-expect'.
29 ;;; Code:
31 (require 'org-table) ; `org-table-make-reference'
33 (ert-deftest test-org-table/simple-formula/no-grouping/no-title-row ()
34 "Simple sum without grouping rows, without title row."
35 (org-test-table-target-expect
37 | 2 |
38 | 4 |
39 | 8 |
40 | replace |
43 | 2 |
44 | 4 |
45 | 8 |
46 | 14 |
49 ;; Calc formula
50 "#+TBLFM: @>$1 = vsum(@<..@>>)"
51 ;; Lisp formula
52 "#+TBLFM: @>$1 = '(+ @<..@>>); N"))
54 (ert-deftest test-org-table/simple-formula/no-grouping/with-title-row ()
55 "Simple sum without grouping rows, with title row."
56 (org-test-table-target-expect
58 | foo |
59 |---------|
60 | 2 |
61 | 4 |
62 | 8 |
63 | replace |
66 | foo |
67 |-----|
68 | 2 |
69 | 4 |
70 | 8 |
71 | 14 |
74 ;; Calc formula
75 "#+TBLFM: @>$1 = vsum(@I..@>>)"
76 ;; Lisp formula
77 "#+TBLFM: @>$1 = '(+ @I..@>>); N"))
79 (ert-deftest test-org-table/simple-formula/with-grouping/no-title-row ()
80 "Simple sum with grouping rows, how not to do."
81 ;; The first example has a problem, see the second example in this
82 ;; ert-deftest.
83 (org-test-table-target-expect
85 | 2 |
86 | 4 |
87 | 8 |
88 |---------|
89 | replace |
92 | 2 |
93 | 4 |
94 | 8 |
95 |----|
96 | 14 |
99 ;; Calc formula
100 "#+TBLFM: $1 = vsum(@<..@>>)"
101 ;; Lisp formula
102 "#+TBLFM: $1 = '(+ @<..@>>); N")
104 ;; The problem is that the first three rows with the summands are
105 ;; considered the header and therefore column formulas are not
106 ;; applied on them as shown below. Also export behaves unexpected.
107 ;; See next ert-deftest how to group rows right.
108 (org-test-table-target-expect
110 | 2 | replace |
111 | 4 | replace |
112 | 8 | replace |
113 |---------+---------|
114 | replace | replace |
117 | 2 | replace |
118 | 4 | replace |
119 | 8 | replace |
120 |----+---------|
121 | 14 | 28 |
124 ;; Calc formula
125 "#+TBLFM: @>$1 = vsum(@<..@>>) :: $2 = 2 * $1"
126 ;; Lisp formula
127 "#+TBLFM: @>$1 = '(+ @<..@>>); N :: $2 = '(* 2 $1); N"))
129 (ert-deftest test-org-table/simple-formula/with-grouping/with-title-row ()
130 "Simple sum with grouping rows, how to do it right."
131 ;; Always add a top row with the column names separated by hline to
132 ;; get the desired header when you want to group rows.
133 (org-test-table-target-expect
135 | foo | bar |
136 |---------+---------|
137 | 2 | replace |
138 | 4 | replace |
139 | 8 | replace |
140 |---------+---------|
141 | replace | replace |
144 | foo | bar |
145 |-----+-----|
146 | 2 | 4 |
147 | 4 | 8 |
148 | 8 | 16 |
149 |-----+-----|
150 | 14 | 28 |
153 ;; Calc formula
154 "#+TBLFM: @>$1 = vsum(@I..@>>) :: $2 = 2 * $1"
155 ;; Lisp formula
156 "#+TBLFM: @>$1 = '(+ @I..@>>); N :: $2 = '(* 2 $1); N"))
158 (ert-deftest test-org-table/align ()
159 "Align columns within Org buffer, depends on `org-table-number-regexp'."
160 (org-test-table-target-expect "
161 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
162 | ab | 12 | 12.2 | 2.4e-08 | 2x10^12 | 4.034+-0.02 | 2.7(10) | >3.5 |
163 | ab | ab | ab | ab | ab | ab | ab | ab |
165 (org-test-table-target-expect "
166 | 0 | 0 | 0 | 0 | 0 | 0 |
167 | <-0x0ab.cf | >-36#0vw.yz | nan | uinf | -inf | inf |
168 | ab | ab | ab | ab | ab | ab |
171 (defconst references/target-normal "
172 | 0 | 1 | replace | replace | replace | replace | replace | replace |
173 | z | 1 | replace | replace | replace | replace | replace | replace |
174 | | 1 | replace | replace | replace | replace | replace | replace |
175 | | | replace | replace | replace | replace | replace | replace |
177 "Normal numbers and non-numbers for Lisp and Calc formula.")
179 (defconst references/target-special "
180 | nan | 1 | replace | replace | replace | replace | replace | replace |
181 | uinf | 1 | replace | replace | replace | replace | replace | replace |
182 | -inf | 1 | replace | replace | replace | replace | replace | replace |
183 | inf | 1 | replace | replace | replace | replace | replace | replace |
185 "Special numbers for Calc formula.")
187 (ert-deftest test-org-table/references/mode-string-EL ()
188 "Basic: Assign field reference, sum of field references, sum
189 and len of simple range reference (no row) and complex range
190 reference (with row). Mode string EL."
191 ;; Empty fields are kept during parsing field but lost as list
192 ;; elements within Lisp formula syntactically when used literally
193 ;; and not enclosed with " within fields, see last columns with len.
194 (org-test-table-target-expect
195 references/target-normal
196 ;; All the #ERROR show that for Lisp calculations N has to be used.
198 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
199 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
200 | | 1 | | 1 | 1 | 1 | 1 | 1 |
201 | | | | 0 | 0 | 0 | 0 | 0 |
203 1 (concat
204 "#+TBLFM: $3 = '(identity \"$1\"); EL :: $4 = '(+ $1 $2); EL :: "
205 "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
206 "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL"))
208 ;; Empty fields are kept during parsing field _and_ as list elements
209 ;; within Lisp formula syntactically even when used literally when
210 ;; enclosed with " within fields, see last columns with len.
211 (org-test-table-target-expect
213 | \"0\" | \"1\" | repl | repl | repl | repl | repl | repl |
214 | \"z\" | \"1\" | repl | repl | repl | repl | repl | repl |
215 | \"\" | \"1\" | repl | repl | repl | repl | repl | repl |
216 | \"\" | \"\" | repl | repl | repl | repl | repl | repl |
219 | \"0\" | \"1\" | \"0\" | 1 | #ERROR | #ERROR | 2 | 2 |
220 | \"z\" | \"1\" | \"z\" | 1 | #ERROR | #ERROR | 2 | 2 |
221 | \"\" | \"1\" | \"\" | 1 | #ERROR | #ERROR | 2 | 2 |
222 | \"\" | \"\" | \"\" | 0 | #ERROR | #ERROR | 2 | 2 |
224 1 (concat
225 "#+TBLFM: $3 = '(concat \"\\\"\" $1 \"\\\"\"); EL :: "
226 "$4 = '(+ (string-to-number $1) (string-to-number $2)); EL :: "
227 "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
228 "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL")))
230 (ert-deftest test-org-table/references/mode-string-E ()
231 "Basic: Assign field reference, sum of field references, sum
232 and len of simple range reference (no row) and complex range
233 reference (with row). Mode string E."
234 (let ((lisp
235 (concat
236 "#+TBLFM: $3 = '(identity $1); E :: $4 = '(+ $1 $2); E :: "
237 "$5 = '(+ $1..$2); E :: $6 = '(+ @0$1..@0$2); E :: "
238 "$7 = '(length '($1..$2)); E :: $8 = '(length '(@0$1..@0$2)); E"))
239 (calc
240 (concat
241 "#+TBLFM: $3 = $1; E :: $4 = $1 + $2; E :: "
242 "$5 = vsum($1..$2); E :: $6 = vsum(@0$1..@0$2); E :: "
243 "$7 = vlen($1..$2); E :: $8 = vlen(@0$1..@0$2); E")))
244 (org-test-table-target-expect
245 references/target-normal
246 ;; All the #ERROR show that for Lisp calculations N has to be used.
248 | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
249 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
250 | | 1 | | #ERROR | #ERROR | #ERROR | 2 | 2 |
251 | | | | #ERROR | #ERROR | #ERROR | 2 | 2 |
253 1 lisp)
254 (org-test-table-target-expect
255 references/target-normal
257 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
258 | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
259 | | 1 | nan | nan | nan | nan | 2 | 2 |
260 | | | nan | nan | nan | nan | 2 | 2 |
262 1 calc)
263 (org-test-table-target-expect
264 references/target-special
266 | nan | 1 | nan | nan | nan | nan | 2 | 2 |
267 | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
268 | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
269 | inf | 1 | inf | inf | inf | inf | 2 | 2 |
271 1 calc)))
273 (ert-deftest test-org-table/references/mode-string-EN ()
274 "Basic: Assign field reference, sum of field references, sum
275 and len of simple range reference (no row) and complex range
276 reference (with row). Mode string EN."
277 (let ((lisp (concat
278 "#+TBLFM: $3 = '(identity $1); EN :: $4 = '(+ $1 $2); EN :: "
279 "$5 = '(+ $1..$2); EN :: $6 = '(+ @0$1..@0$2); EN :: "
280 "$7 = '(length '($1..$2)); EN :: "
281 "$8 = '(length '(@0$1..@0$2)); EN"))
282 (calc (concat
283 "#+TBLFM: $3 = $1; EN :: $4 = $1 + $2; EN :: "
284 "$5 = vsum($1..$2); EN :: $6 = vsum(@0$1..@0$2); EN :: "
285 "$7 = vlen($1..$2); EN :: $8 = vlen(@0$1..@0$2); EN")))
286 (org-test-table-target-expect
287 references/target-normal
289 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
290 | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
291 | | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
292 | | | 0 | 0 | 0 | 0 | 2 | 2 |
294 1 lisp calc)
295 (org-test-table-target-expect
296 references/target-special
298 | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
299 | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
300 | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
301 | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
303 1 calc)))
305 (ert-deftest test-org-table/references/mode-string-L ()
306 "Basic: Assign field reference, sum of field references, sum
307 and len of simple range reference (no row) and complex range
308 reference (with row). Mode string L."
309 (org-test-table-target-expect
310 references/target-normal
311 ;; All the #ERROR show that for Lisp calculations N has to be used.
313 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
314 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
315 | | 1 | | 1 | 1 | 1 | 1 | 1 |
316 | | | | 0 | 0 | 0 | 0 | 0 |
318 1 (concat
319 "#+TBLFM: $3 = '(identity \"$1\"); L :: $4 = '(+ $1 $2); L :: "
320 "$5 = '(+ $1..$2); L :: $6 = '(+ @0$1..@0$2); L :: "
321 "$7 = '(length '($1..$2)); L :: $8 = '(length '(@0$1..@0$2)); L")))
323 (ert-deftest test-org-table/references/mode-string-none ()
324 "Basic: Assign field reference, sum of field references, sum
325 and len of simple range reference (no row) and complex range
326 reference (with row). No mode string."
327 (let ((lisp (concat
328 "#+TBLFM: $3 = '(identity $1) :: $4 = '(+ $1 $2) :: "
329 "$5 = '(+ $1..$2) :: $6 = '(+ @0$1..@0$2) :: "
330 "$7 = '(length '($1..$2)) :: $8 = '(length '(@0$1..@0$2))"))
331 (calc (concat
332 "#+TBLFM: $3 = $1 :: $4 = $1 + $2 :: "
333 "$5 = vsum($1..$2) :: $6 = vsum(@0$1..@0$2) :: "
334 "$7 = vlen($1..$2) :: $8 = vlen(@0$1..@0$2)")))
335 (org-test-table-target-expect
336 references/target-normal
337 ;; All the #ERROR show that for Lisp calculations N has to be used.
339 | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
340 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
341 | | 1 | | #ERROR | #ERROR | #ERROR | 1 | 1 |
342 | | | | #ERROR | 0 | 0 | 0 | 0 |
344 1 lisp)
345 (org-test-table-target-expect
346 references/target-normal
348 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
349 | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
350 | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
351 | | | 0 | 0 | 0 | 0 | 0 | 0 |
353 1 calc)
354 (org-test-table-target-expect
355 references/target-special
357 | nan | 1 | nan | nan | nan | nan | 2 | 2 |
358 | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
359 | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
360 | inf | 1 | inf | inf | inf | inf | 2 | 2 |
362 1 calc)))
364 (ert-deftest test-org-table/references/mode-string-N ()
365 "Basic: Assign field reference, sum of field references, sum
366 and len of simple range reference (no row) and complex range
367 reference (with row). Mode string N."
368 (let ((lisp
369 (concat
370 "#+TBLFM: $3 = '(identity $1); N :: $4 = '(+ $1 $2); N :: "
371 "$5 = '(+ $1..$2); N :: $6 = '(+ @0$1..@0$2); N :: "
372 "$7 = '(length '($1..$2)); N :: $8 = '(length '(@0$1..@0$2)); N"))
373 (calc
374 (concat
375 "#+TBLFM: $3 = $1; N :: $4 = $1 + $2; N :: "
376 "$5 = vsum($1..$2); N :: $6 = vsum(@0$1..@0$2); N :: "
377 "$7 = vlen($1..$2); N :: $8 = vlen(@0$1..@0$2); N")))
378 (org-test-table-target-expect
379 references/target-normal
381 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
382 | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
383 | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
384 | | | 0 | 0 | 0 | 0 | 0 | 0 |
386 1 lisp calc)
387 (org-test-table-target-expect
388 references/target-special
390 | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
391 | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
392 | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
393 | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
395 1 calc)))
397 (ert-deftest test-org-table/compare ()
398 "Basic: Compare field references in Calc."
399 (org-test-table-target-expect
401 | | 0 | z | | nan | uinf | -inf | inf |
402 |------+------+------+------+------+------+------+------|
403 | 0 | repl | repl | repl | repl | repl | repl | repl |
404 | z | repl | repl | repl | repl | repl | repl | repl |
405 | | repl | repl | repl | repl | repl | repl | repl |
406 | nan | repl | repl | repl | repl | repl | repl | repl |
407 | uinf | repl | repl | repl | repl | repl | repl | repl |
408 | -inf | repl | repl | repl | repl | repl | repl | repl |
409 | inf | repl | repl | repl | repl | repl | repl | repl |
412 | | 0 | z | | nan | uinf | -inf | inf |
413 |------+---+---+---+-----+------+------+-----|
414 | 0 | x | | | | | | |
415 | z | | x | | | | | |
416 | | | | x | | | | |
417 | nan | | | | x | | | |
418 | uinf | | | | | x | | |
419 | -inf | | | | | | x | |
420 | inf | | | | | | | x |
423 ;; Compare field reference ($1) with field reference (@1)
424 "#+TBLFM: @I$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
425 ;; Compare field reference ($1) with absolute term
426 (concat "#+TBLFM: "
427 "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
428 "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
429 "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
430 "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
431 "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
432 "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
433 "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
435 ;; Check field reference converted from an empty field: Despite this
436 ;; field reference will not end up in a result, Calc evaluates it.
437 ;; Make sure that also then there is no Calc error.
438 (org-test-table-target-expect
440 | 0 | replace |
441 | z | replace |
442 | | replace |
443 | nan | replace |
446 | 0 | 1 |
447 | z | z + 1 |
448 | | |
449 | nan | nan |
451 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
453 (ert-deftest test-org-table/empty-field ()
454 "Examples how to deal with empty fields."
455 ;; Test if one field is empty, else do a calculation
456 (org-test-table-target-expect
458 | -1 | replace |
459 | 0 | replace |
460 | | replace |
463 | -1 | 0 |
464 | 0 | 1 |
465 | | |
468 ;; Calc formula
469 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
470 ;; Lisp formula
471 "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
473 ;; Test if several fields are empty, else do a calculation
474 (org-test-table-target-expect
476 | 1 | 2 | replace |
477 | 4 | | replace |
478 | | 8 | replace |
479 | | | replace |
482 | 1 | 2 | 3 |
483 | 4 | | |
484 | | 8 | |
485 | | | |
488 ;; Calc formula
489 (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
490 "string(\"\"), $1 + $2); E")
491 ;; Lisp formula
492 (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
493 "\"\" (+ $1 $2)); L"))
495 ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
496 (org-test-table-target-expect
498 | 1.5 | 0 |
499 | 3.5 | |
500 | | 5 |
501 | | |
504 | 1.5 | 2.0 |
505 | 3.5 | 4.0 |
506 | | 5.0 |
507 | | |
510 ;; Calc formula
511 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
512 "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
513 ;; Lisp formula not implemented yet
516 ;; Empty fields in simple and complex range reference
517 (org-test-table-target-expect
519 | | | | | repl | repl | repl | repl | repl | repl |
520 | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
521 | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
524 | | | | | | | | | 0 | 0 |
525 | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
526 | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
529 ;; Calc formula
530 (concat
531 "#+TBLFM: "
532 "$5 = if(typeof(vmean($1..$4)) == 12, "
533 "string(\"\"), vmean($1..$4)); E :: "
534 "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
535 "string(\"\"), vmean(@0$1..@0$4)); E :: "
536 "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
537 "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
538 "$9 = vmean($1..$4); EN :: "
539 "$10 = vmean(@0$1..@0$4); EN")
540 ;; Lisp formula
541 (concat
542 "#+TBLFM: "
543 "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
544 "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
545 "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
546 "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
547 "$7 = '(let ((l '($1..$4))) "
548 "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
549 "$8 = '(let ((l '(@0$1..@0$4))) "
550 "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
551 "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
552 "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
555 (ert-deftest test-org-table/copy-field ()
556 "Experiments on how to copy one field into another field."
557 (let ((target
559 | 0 | replace |
560 | a b | replace |
561 | c d | replace |
562 | | replace |
563 | 2012-12 | replace |
564 | [2012-12-31 Mon] | replace |
566 ;; Lisp formula to copy literally
567 (org-test-table-target-expect
568 target
570 | 0 | 0 |
571 | a b | a b |
572 | c d | c d |
573 | | |
574 | 2012-12 | 2012-12 |
575 | [2012-12-31 Mon] | [2012-12-31 Mon] |
577 1 "#+TBLFM: $2 = '(identity $1)")
579 ;; Calc formula to copy quite literally
580 (org-test-table-target-expect
581 target
583 | 0 | 0 |
584 | a b | a b |
585 | c d | c d |
586 | | |
587 | 2012-12 | 2012-12 |
588 | [2012-12-31 Mon] | <2012-12-31 Mon> |
590 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
591 "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
593 ;; Calc formula simple
594 (org-test-table-target-expect
595 target
597 | 0 | 0 |
598 | a b | a b |
599 | c d | c d |
600 | | |
601 | 2012-12 | 2000 |
602 | [2012-12-31 Mon] | <2012-12-31 Mon> |
604 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
606 ;; End of table examples and beginning of internal tests.
608 (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
609 (fset 'f 'org-table-make-reference)
610 ;; For Lisp formula only
611 (should (equal "0" (f "0" t nil 'literal)))
612 (should (equal "z" (f "z" t nil 'literal)))
613 (should (equal "" (f "" t nil 'literal)))
614 (should (equal "0 1" (f '("0" "1") t nil 'literal)))
615 (should (equal "z 1" (f '("z" "1") t nil 'literal)))
616 (should (equal " 1" (f '("" "1") t nil 'literal)))
617 (should (equal " " (f '("" "" ) t nil 'literal))))
619 (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
620 (fset 'f 'org-table-make-reference)
621 ;; For Lisp formula
622 (should (equal "\"0\"" (f "0" t nil t)))
623 (should (equal "\"z\"" (f "z" t nil t)))
624 (should (equal "\"\"" (f "" t nil t)))
625 (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
626 (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
627 (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
628 (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
629 ;; For Calc formula
630 (should (equal "(0)" (f "0" t nil nil)))
631 (should (equal "(z)" (f "z" t nil nil)))
632 (should (equal "nan" (f "" t nil nil)))
633 (should (equal "[0,1]" (f '("0" "1") t nil nil)))
634 (should (equal "[z,1]" (f '("z" "1") t nil nil)))
635 (should (equal "[nan,1]" (f '("" "1") t nil nil)))
636 (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
637 ;; For Calc formula, special numbers
638 (should (equal "(nan)" (f "nan" t nil nil)))
639 (should (equal "(uinf)" (f "uinf" t nil nil)))
640 (should (equal "(-inf)" (f "-inf" t nil nil)))
641 (should (equal "(inf)" (f "inf" t nil nil)))
642 (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
643 (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
644 (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
645 (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
647 (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
648 (fset 'f 'org-table-make-reference)
649 ;; For Lisp formula
650 (should (equal "0" (f "0" t t t)))
651 (should (equal "0" (f "z" t t t)))
652 (should (equal "0" (f "" t t t)))
653 (should (equal "0 1" (f '("0" "1") t t t)))
654 (should (equal "0 1" (f '("z" "1") t t t)))
655 (should (equal "0 1" (f '("" "1") t t t)))
656 (should (equal "0 0" (f '("" "" ) t t t)))
657 ;; For Calc formula
658 (should (equal "(0)" (f "0" t t nil)))
659 (should (equal "(0)" (f "z" t t nil)))
660 (should (equal "(0)" (f "" t t nil)))
661 (should (equal "[0,1]" (f '("0" "1") t t nil)))
662 (should (equal "[0,1]" (f '("z" "1") t t nil)))
663 (should (equal "[0,1]" (f '("" "1") t t nil)))
664 (should (equal "[0,0]" (f '("" "" ) t t nil)))
665 ;; For Calc formula, special numbers
666 (should (equal "(0)" (f "nan" t t nil)))
667 (should (equal "(0)" (f "uinf" t t nil)))
668 (should (equal "(0)" (f "-inf" t t nil)))
669 (should (equal "(0)" (f "inf" t t nil)))
670 (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
671 (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
672 (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
673 (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
675 (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
676 (fset 'f 'org-table-make-reference)
677 ;; For Lisp formula only
678 (should (equal "0" (f "0" nil nil 'literal)))
679 (should (equal "z" (f "z" nil nil 'literal)))
680 (should (equal "" (f "" nil nil 'literal)))
681 (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
682 (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
683 (should (equal "1" (f '("" "1") nil nil 'literal)))
684 (should (equal "" (f '("" "" ) nil nil 'literal))))
686 (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
687 (fset 'f 'org-table-make-reference)
688 ;; For Lisp formula
689 (should (equal "\"0\"" (f "0" nil nil t)))
690 (should (equal "\"z\"" (f "z" nil nil t)))
691 (should (equal "" (f "" nil nil t)))
692 (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
693 (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
694 (should (equal "\"1\"" (f '("" "1") nil nil t)))
695 (should (equal "" (f '("" "" ) nil nil t)))
696 ;; For Calc formula
697 (should (equal "(0)" (f "0" nil nil nil)))
698 (should (equal "(z)" (f "z" nil nil nil)))
699 (should (equal "(0)" (f "" nil nil nil)))
700 (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
701 (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
702 (should (equal "[1]" (f '("" "1") nil nil nil)))
703 (should (equal "[]" (f '("" "" ) nil nil nil)))
704 ;; For Calc formula, special numbers
705 (should (equal "(nan)" (f "nan" nil nil nil)))
706 (should (equal "(uinf)" (f "uinf" nil nil nil)))
707 (should (equal "(-inf)" (f "-inf" nil nil nil)))
708 (should (equal "(inf)" (f "inf" nil nil nil)))
709 (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
710 (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
711 (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
712 (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
714 (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
715 (fset 'f 'org-table-make-reference)
716 ;; For Lisp formula
717 (should (equal "0" (f "0" nil t t)))
718 (should (equal "0" (f "z" nil t t)))
719 (should (equal "" (f "" nil t t)))
720 (should (equal "0 1" (f '("0" "1") nil t t)))
721 (should (equal "0 1" (f '("z" "1") nil t t)))
722 (should (equal "1" (f '("" "1") nil t t)))
723 (should (equal "" (f '("" "" ) nil t t)))
724 ;; For Calc formula
725 (should (equal "(0)" (f "0" nil t nil)))
726 (should (equal "(0)" (f "z" nil t nil)))
727 (should (equal "(0)" (f "" nil t nil)))
728 (should (equal "[0,1]" (f '("0" "1") nil t nil)))
729 (should (equal "[0,1]" (f '("z" "1") nil t nil)))
730 (should (equal "[1]" (f '("" "1") nil t nil)))
731 (should (equal "[]" (f '("" "" ) nil t nil)))
732 ;; For Calc formula, special numbers
733 (should (equal "(0)" (f "nan" nil t nil)))
734 (should (equal "(0)" (f "uinf" nil t nil)))
735 (should (equal "(0)" (f "-inf" nil t nil)))
736 (should (equal "(0)" (f "inf" nil t nil)))
737 (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
738 (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
739 (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
740 (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
742 (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
743 "Simple reference @1$1."
744 (should
745 (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
747 ;; TODO: Test broken
748 ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
749 ;; "Self reference @1$1."
750 ;; (should
751 ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
753 (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
754 "Remote reference."
755 (should
756 (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
758 (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
759 "Simple reference @1$1."
760 (should
761 (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
763 (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
764 "Self reference $0."
765 (should
766 (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
768 ;; TODO: Test Broken
769 ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
770 ;; "Remote reference."
771 ;; (should
772 ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
774 (ert-deftest test-org-table/remote-reference-access ()
775 "Access to remote reference."
776 (org-test-table-target-expect
778 #+NAME: table
779 | | 42 |
781 | replace | |
784 #+NAME: table
785 | | 42 |
787 | 42 | |
789 1 "#+TBLFM: $1 = remote(table, @1$2)"))
791 (ert-deftest test-org-table/org-at-TBLFM-p ()
792 (org-test-with-temp-text-in-file
794 | 1 |
795 | 2 |
796 #+TBLFM: $2=$1*2
799 (goto-char (point-min))
800 (forward-line 2)
801 (should (equal (org-at-TBLFM-p) nil))
803 (goto-char (point-min))
804 (forward-line 3)
805 (should (equal (org-at-TBLFM-p) t))
807 (goto-char (point-min))
808 (forward-line 4)
809 (should (equal (org-at-TBLFM-p) nil))))
811 (ert-deftest test-org-table/org-table-TBLFM-begin ()
812 (org-test-with-temp-text-in-file
814 | 1 |
815 | 2 |
816 #+TBLFM: $2=$1*2
819 (goto-char (point-min))
820 (should (equal (org-table-TBLFM-begin)
821 nil))
823 (goto-char (point-min))
824 (forward-line 1)
825 (should (equal (org-table-TBLFM-begin)
826 nil))
828 (goto-char (point-min))
829 (forward-line 3)
830 (should (= (org-table-TBLFM-begin)
831 14))
833 (goto-char (point-min))
834 (forward-line 4)
835 (should (= (org-table-TBLFM-begin)
836 14))
840 (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
841 "For multiple #+TBLFM lines."
842 (org-test-with-temp-text-in-file
844 | 1 |
845 | 2 |
846 #+TBLFM: $2=$1*1
847 #+TBLFM: $2=$1*2
850 (goto-char (point-min))
851 (should (equal (org-table-TBLFM-begin)
852 nil))
854 (goto-char (point-min))
855 (forward-line 1)
856 (should (equal (org-table-TBLFM-begin)
857 nil))
859 (goto-char (point-min))
860 (forward-line 3)
861 (should (= (org-table-TBLFM-begin)
862 14))
864 (goto-char (point-min))
865 (forward-line 4)
866 (should (= (org-table-TBLFM-begin)
867 14))
869 (goto-char (point-min))
870 (forward-line 5)
871 (should (= (org-table-TBLFM-begin)
872 14))
876 (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
877 (org-test-with-temp-text-in-file
879 | 1 |
880 | 2 |
881 #+TBLFM: $2=$1*1
882 #+TBLFM: $2=$1*2
884 | 6 |
885 | 7 |
886 #+TBLFM: $2=$1*1
887 #+TBLFM: $2=$1*2
890 (goto-char (point-min))
891 (should (equal (org-table-TBLFM-begin)
892 nil))
894 (goto-char (point-min))
895 (forward-line 1)
896 (should (equal (org-table-TBLFM-begin)
897 nil))
899 (goto-char (point-min))
900 (forward-line 3)
901 (should (= (org-table-TBLFM-begin)
902 14))
904 (goto-char (point-min))
905 (forward-line 4)
906 (should (= (org-table-TBLFM-begin)
907 14))
909 (goto-char (point-min))
910 (forward-line 5)
911 (should (= (org-table-TBLFM-begin)
912 14))
914 (goto-char (point-min))
915 (forward-line 6)
916 (should (= (org-table-TBLFM-begin)
917 14))
919 (goto-char (point-min))
920 (forward-line 8)
921 (should (= (org-table-TBLFM-begin)
922 61))
924 (goto-char (point-min))
925 (forward-line 9)
926 (should (= (org-table-TBLFM-begin)
927 61))
929 (goto-char (point-min))
930 (forward-line 10)
931 (should (= (org-table-TBLFM-begin)
932 61))))
934 (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
935 (org-test-with-temp-text-in-file
937 | 1 | |
938 | 2 | |
939 #+TBLFM: $2=$1*1
940 #+TBLFM: $2=$1*2
941 #+TBLFM: $2=$1*3
943 (let ((got (progn (goto-char (point-min))
944 (forward-line 3)
945 (org-table-calc-current-TBLFM)
946 (buffer-string)))
947 (expect "
948 | 1 | 1 |
949 | 2 | 2 |
950 #+TBLFM: $2=$1*1
951 #+TBLFM: $2=$1*2
952 #+TBLFM: $2=$1*3
954 (should (string= got
955 expect)))
957 (let ((got (progn (goto-char (point-min))
958 (forward-line 4)
959 (org-table-calc-current-TBLFM)
960 (buffer-string)))
961 (expect "
962 | 1 | 2 |
963 | 2 | 4 |
964 #+TBLFM: $2=$1*1
965 #+TBLFM: $2=$1*2
966 #+TBLFM: $2=$1*3
968 (should (string= got
969 expect)))))
971 (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
972 "org-table-calc-current-TBLFM should preserve the input as it was."
973 (org-test-with-temp-text-in-file
975 | 1 | 1 |
976 | 2 | 2 |
977 #+TBLFM: $2=$1*1
978 #+TBLFM: $2=$1*2::$2=$1*2
979 #+TBLFM: $2=$1*3
981 (let ((expect "
982 | 1 | 1 |
983 | 2 | 2 |
984 #+TBLFM: $2=$1*1
985 #+TBLFM: $2=$1*2::$2=$1*2
986 #+TBLFM: $2=$1*3
988 (goto-char (point-min))
989 (forward-line 4)
990 (should-error (org-table-calc-current-TBLFM))
991 (setq got (buffer-string))
992 (message "%s" got)
993 (should (string= got
994 expect)))))
996 (provide 'test-org-table)
998 ;;; test-org-table.el ends here