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/>.
23 ;; Template test file for Org-mode tests. Many tests are also a howto
24 ;; example collection as a user documentation, more or less all those
25 ;; using `org-test-table-target-expect'. See also the doc string of
26 ;; `org-test-table-target-expect'.
30 (require 'org-table
) ; `org-table-make-reference'
32 (ert-deftest test-org-table
/simple-formula
/no-grouping
/no-title-row
()
33 "Simple sum without grouping rows, without title row."
34 (org-test-table-target-expect
49 "#+TBLFM: @>$1 = vsum(@<..@>>)"
51 "#+TBLFM: @>$1 = '(+ @<..@>>); N"))
53 (ert-deftest test-org-table
/simple-formula
/no-grouping
/with-title-row
()
54 "Simple sum without grouping rows, with title row."
55 (org-test-table-target-expect
74 "#+TBLFM: @>$1 = vsum(@I..@>>)"
76 "#+TBLFM: @>$1 = '(+ @I..@>>); N"))
78 (ert-deftest test-org-table
/simple-formula
/with-grouping
/no-title-row
()
79 "Simple sum with grouping rows, how not to do."
80 ;; The first example has a problem, see the second example in this
82 (org-test-table-target-expect
99 "#+TBLFM: $1 = vsum(@<..@>>)"
101 "#+TBLFM: $1 = '(+ @<..@>>); N")
103 ;; The problem is that the first three rows with the summands are
104 ;; considered the header and therefore column formulas are not
105 ;; applied on them as shown below. Also export behaves unexpected.
106 ;; See next ert-deftest how to group rows right.
107 (org-test-table-target-expect
112 |---------+---------|
113 | replace | replace |
124 "#+TBLFM: @>$1 = vsum(@<..@>>) :: $2 = 2 * $1"
126 "#+TBLFM: @>$1 = '(+ @<..@>>); N :: $2 = '(* 2 $1); N"))
128 (ert-deftest test-org-table
/simple-formula
/with-grouping
/with-title-row
()
129 "Simple sum with grouping rows, how to do it right."
130 ;; Always add a top row with the column names separated by hline to
131 ;; get the desired header when you want to group rows.
132 (org-test-table-target-expect
135 |---------+---------|
139 |---------+---------|
140 | replace | replace |
153 "#+TBLFM: @>$1 = vsum(@I..@>>) :: $2 = 2 * $1"
155 "#+TBLFM: @>$1 = '(+ @I..@>>); N :: $2 = '(* 2 $1); N"))
157 (ert-deftest test-org-table
/align
()
158 "Align columns within Org buffer, depends on `org-table-number-regexp'."
159 (org-test-table-target-expect "
160 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
161 | ab | 12 | 12.2 | 2.4e-08 | 2x10^12 | 4.034+-0.02 | 2.7(10) | >3.5 |
162 | ab | ab | ab | ab | ab | ab | ab | ab |
164 (org-test-table-target-expect "
165 | 0 | 0 | 0 | 0 | 0 | 0 |
166 | <-0x0ab.cf | >-36#0vw.yz | nan | uinf | -inf | inf |
167 | ab | ab | ab | ab | ab | ab |
170 (defconst references
/target-normal
"
171 | 0 | 1 | replace | replace | replace | replace | replace | replace |
172 | z | 1 | replace | replace | replace | replace | replace | replace |
173 | | 1 | replace | replace | replace | replace | replace | replace |
174 | | | replace | replace | replace | replace | replace | replace |
176 "Normal numbers and non-numbers for Lisp and Calc formula.")
178 (defconst references
/target-special
"
179 | nan | 1 | replace | replace | replace | replace | replace | replace |
180 | uinf | 1 | replace | replace | replace | replace | replace | replace |
181 | -inf | 1 | replace | replace | replace | replace | replace | replace |
182 | inf | 1 | replace | replace | replace | replace | replace | replace |
184 "Special numbers for Calc formula.")
186 (ert-deftest test-org-table
/references
/mode-string-EL
()
187 "Basic: Assign field reference, sum of field references, sum
188 and len of simple range reference (no row) and complex range
189 reference (with row). Mode string EL."
190 ;; Empty fields are kept during parsing field but lost as list
191 ;; elements within Lisp formula syntactically when used literally
192 ;; and not enclosed with " within fields, see last columns with len.
193 (org-test-table-target-expect
194 references
/target-normal
195 ;; All the #ERROR show that for Lisp calculations N has to be used.
197 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
198 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
199 | | 1 | | 1 | 1 | 1 | 1 | 1 |
200 | | | | 0 | 0 | 0 | 0 | 0 |
203 "#+TBLFM: $3 = '(identity \"$1\"); EL :: $4 = '(+ $1 $2); EL :: "
204 "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
205 "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL"))
207 ;; Empty fields are kept during parsing field _and_ as list elements
208 ;; within Lisp formula syntactically even when used literally when
209 ;; enclosed with " within fields, see last columns with len.
210 (org-test-table-target-expect
212 | \"0\" | \"1\" | repl | repl | repl | repl | repl | repl |
213 | \"z\" | \"1\" | repl | repl | repl | repl | repl | repl |
214 | \"\" | \"1\" | repl | repl | repl | repl | repl | repl |
215 | \"\" | \"\" | repl | repl | repl | repl | repl | repl |
218 | \"0\" | \"1\" | \"0\" | 1 | #ERROR | #ERROR | 2 | 2 |
219 | \"z\" | \"1\" | \"z\" | 1 | #ERROR | #ERROR | 2 | 2 |
220 | \"\" | \"1\" | \"\" | 1 | #ERROR | #ERROR | 2 | 2 |
221 | \"\" | \"\" | \"\" | 0 | #ERROR | #ERROR | 2 | 2 |
224 "#+TBLFM: $3 = '(concat \"\\\"\" $1 \"\\\"\"); EL :: "
225 "$4 = '(+ (string-to-number $1) (string-to-number $2)); EL :: "
226 "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
227 "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL")))
229 (ert-deftest test-org-table
/references
/mode-string-E
()
230 "Basic: Assign field reference, sum of field references, sum
231 and len of simple range reference (no row) and complex range
232 reference (with row). Mode string E."
235 "#+TBLFM: $3 = '(identity $1); E :: $4 = '(+ $1 $2); E :: "
236 "$5 = '(+ $1..$2); E :: $6 = '(+ @0$1..@0$2); E :: "
237 "$7 = '(length '($1..$2)); E :: $8 = '(length '(@0$1..@0$2)); E"))
240 "#+TBLFM: $3 = $1; E :: $4 = $1 + $2; E :: "
241 "$5 = vsum($1..$2); E :: $6 = vsum(@0$1..@0$2); E :: "
242 "$7 = vlen($1..$2); E :: $8 = vlen(@0$1..@0$2); E")))
243 (org-test-table-target-expect
244 references
/target-normal
245 ;; All the #ERROR show that for Lisp calculations N has to be used.
247 | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
248 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
249 | | 1 | | #ERROR | #ERROR | #ERROR | 2 | 2 |
250 | | | | #ERROR | #ERROR | #ERROR | 2 | 2 |
253 (org-test-table-target-expect
254 references
/target-normal
256 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
257 | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
258 | | 1 | nan | nan | nan | nan | 2 | 2 |
259 | | | nan | nan | nan | nan | 2 | 2 |
262 (org-test-table-target-expect
263 references
/target-special
265 | nan | 1 | nan | nan | nan | nan | 2 | 2 |
266 | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
267 | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
268 | inf | 1 | inf | inf | inf | inf | 2 | 2 |
272 (ert-deftest test-org-table
/references
/mode-string-EN
()
273 "Basic: Assign field reference, sum of field references, sum
274 and len of simple range reference (no row) and complex range
275 reference (with row). Mode string EN."
277 "#+TBLFM: $3 = '(identity $1); EN :: $4 = '(+ $1 $2); EN :: "
278 "$5 = '(+ $1..$2); EN :: $6 = '(+ @0$1..@0$2); EN :: "
279 "$7 = '(length '($1..$2)); EN :: "
280 "$8 = '(length '(@0$1..@0$2)); EN"))
282 "#+TBLFM: $3 = $1; EN :: $4 = $1 + $2; EN :: "
283 "$5 = vsum($1..$2); EN :: $6 = vsum(@0$1..@0$2); EN :: "
284 "$7 = vlen($1..$2); EN :: $8 = vlen(@0$1..@0$2); EN")))
285 (org-test-table-target-expect
286 references
/target-normal
288 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
289 | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
290 | | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
291 | | | 0 | 0 | 0 | 0 | 2 | 2 |
294 (org-test-table-target-expect
295 references
/target-special
297 | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
298 | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
299 | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
300 | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
304 (ert-deftest test-org-table
/references
/mode-string-L
()
305 "Basic: Assign field reference, sum of field references, sum
306 and len of simple range reference (no row) and complex range
307 reference (with row). Mode string L."
308 (org-test-table-target-expect
309 references
/target-normal
310 ;; All the #ERROR show that for Lisp calculations N has to be used.
312 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
313 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
314 | | 1 | | 1 | 1 | 1 | 1 | 1 |
315 | | | | 0 | 0 | 0 | 0 | 0 |
318 "#+TBLFM: $3 = '(identity \"$1\"); L :: $4 = '(+ $1 $2); L :: "
319 "$5 = '(+ $1..$2); L :: $6 = '(+ @0$1..@0$2); L :: "
320 "$7 = '(length '($1..$2)); L :: $8 = '(length '(@0$1..@0$2)); L")))
322 (ert-deftest test-org-table
/references
/mode-string-none
()
323 "Basic: Assign field reference, sum of field references, sum
324 and len of simple range reference (no row) and complex range
325 reference (with row). No mode string."
327 "#+TBLFM: $3 = '(identity $1) :: $4 = '(+ $1 $2) :: "
328 "$5 = '(+ $1..$2) :: $6 = '(+ @0$1..@0$2) :: "
329 "$7 = '(length '($1..$2)) :: $8 = '(length '(@0$1..@0$2))"))
331 "#+TBLFM: $3 = $1 :: $4 = $1 + $2 :: "
332 "$5 = vsum($1..$2) :: $6 = vsum(@0$1..@0$2) :: "
333 "$7 = vlen($1..$2) :: $8 = vlen(@0$1..@0$2)")))
334 (org-test-table-target-expect
335 references
/target-normal
336 ;; All the #ERROR show that for Lisp calculations N has to be used.
338 | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
339 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
340 | | 1 | | #ERROR | #ERROR | #ERROR | 1 | 1 |
341 | | | | #ERROR | 0 | 0 | 0 | 0 |
344 (org-test-table-target-expect
345 references
/target-normal
347 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
348 | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
349 | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
350 | | | 0 | 0 | 0 | 0 | 0 | 0 |
353 (org-test-table-target-expect
354 references
/target-special
356 | nan | 1 | nan | nan | nan | nan | 2 | 2 |
357 | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
358 | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
359 | inf | 1 | inf | inf | inf | inf | 2 | 2 |
363 (ert-deftest test-org-table
/references
/mode-string-N
()
364 "Basic: Assign field reference, sum of field references, sum
365 and len of simple range reference (no row) and complex range
366 reference (with row). Mode string N."
369 "#+TBLFM: $3 = '(identity $1); N :: $4 = '(+ $1 $2); N :: "
370 "$5 = '(+ $1..$2); N :: $6 = '(+ @0$1..@0$2); N :: "
371 "$7 = '(length '($1..$2)); N :: $8 = '(length '(@0$1..@0$2)); N"))
374 "#+TBLFM: $3 = $1; N :: $4 = $1 + $2; N :: "
375 "$5 = vsum($1..$2); N :: $6 = vsum(@0$1..@0$2); N :: "
376 "$7 = vlen($1..$2); N :: $8 = vlen(@0$1..@0$2); N")))
377 (org-test-table-target-expect
378 references
/target-normal
380 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
381 | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
382 | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
383 | | | 0 | 0 | 0 | 0 | 0 | 0 |
386 (org-test-table-target-expect
387 references
/target-special
389 | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
390 | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
391 | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
392 | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
396 (ert-deftest test-org-table
/compare
()
397 "Basic: Compare field references in Calc."
398 (org-test-table-target-expect
400 | | 0 | z | | nan | uinf | -inf | inf |
401 |------+------+------+------+------+------+------+------|
402 | 0 | repl | repl | repl | repl | repl | repl | repl |
403 | z | repl | repl | repl | repl | repl | repl | repl |
404 | | repl | repl | repl | repl | repl | repl | repl |
405 | nan | repl | repl | repl | repl | repl | repl | repl |
406 | uinf | repl | repl | repl | repl | repl | repl | repl |
407 | -inf | repl | repl | repl | repl | repl | repl | repl |
408 | inf | repl | repl | repl | repl | repl | repl | repl |
411 | | 0 | z | | nan | uinf | -inf | inf |
412 |------+---+---+---+-----+------+------+-----|
413 | 0 | x | | | | | | |
414 | z | | x | | | | | |
416 | nan | | | | x | | | |
417 | uinf | | | | | x | | |
418 | -inf | | | | | | x | |
419 | inf | | | | | | | x |
422 ;; Compare field reference ($1) with field reference (@1)
423 "#+TBLFM: @<<$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
424 ;; Compare field reference ($1) with absolute term
426 "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
427 "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
428 "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
429 "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
430 "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
431 "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
432 "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
434 ;; Check field reference converted from an empty field: Despite this
435 ;; field reference will not end up in a result, Calc evaluates it.
436 ;; Make sure that also then there is no Calc error.
437 (org-test-table-target-expect
450 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
452 (ert-deftest test-org-table
/empty-field
()
453 "Examples how to deal with empty fields."
454 ;; Test if one field is empty, else do a calculation
455 (org-test-table-target-expect
468 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
470 "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
472 ;; Test if several fields are empty, else do a calculation
473 (org-test-table-target-expect
488 (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
489 "string(\"\"), $1 + $2); E")
491 (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
492 "\"\" (+ $1 $2)); L"))
494 ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
495 (org-test-table-target-expect
510 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
511 "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
512 ;; Lisp formula not implemented yet
515 ;; Empty fields in simple and complex range reference
516 (org-test-table-target-expect
518 | | | | | repl | repl | repl | repl | repl | repl |
519 | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
520 | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
523 | | | | | | | | | 0 | 0 |
524 | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
525 | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
531 "$5 = if(typeof(vmean($1..$4)) == 12, "
532 "string(\"\"), vmean($1..$4)); E :: "
533 "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
534 "string(\"\"), vmean(@0$1..@0$4)); E :: "
535 "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
536 "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
537 "$9 = vmean($1..$4); EN :: "
538 "$10 = vmean(@0$1..@0$4); EN")
542 "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
543 "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
544 "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
545 "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
546 "$7 = '(let ((l '($1..$4))) "
547 "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
548 "$8 = '(let ((l '(@0$1..@0$4))) "
549 "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
550 "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
551 "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
554 (ert-deftest test-org-table
/copy-field
()
555 "Experiments on how to copy one field into another field.
556 See also `test-org-table/remote-reference-access'."
562 | 2012-12 | replace |
563 | [2012-12-31 Mon] | replace |
565 ;; Lisp formula to copy literally
566 (org-test-table-target-expect
573 | 2012-12 | 2012-12 |
574 | [2012-12-31 Mon] | [2012-12-31 Mon] |
576 1 "#+TBLFM: $2 = '(identity $1)")
578 ;; Calc formula to copy quite literally
579 (org-test-table-target-expect
586 | 2012-12 | 2012-12 |
587 | [2012-12-31 Mon] | <2012-12-31 Mon> |
589 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
590 "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
592 ;; Calc formula simple
593 (org-test-table-target-expect
601 | [2012-12-31 Mon] | <2012-12-31 Mon> |
603 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
605 (ert-deftest test-org-table
/sub-total
()
606 "Grouped rows with sub-total.
607 Begin range with \"@II\" to handle multiline header. Convert
608 integer to float with \"+.0\" for sub-total of items c1 and c2.
609 Sum empty fields as value zero but without ignoring them for
610 \"vlen\" with format specifier \"EN\". Format possibly empty
611 results with the Calc formatter \"f-1\" instead of the printf
613 (org-test-table-target-expect
615 |-------+---------+---------|
616 | Item | Item | Sub- |
617 | name | value | total |
618 |-------+---------+---------|
619 | a1 | 4.1 | replace |
620 | a2 | 8.2 | replace |
622 |-------+---------+---------|
623 | b1 | 16.0 | replace |
624 |-------+---------+---------|
625 | c1 | 32 | replace |
626 | c2 | 64 | replace |
627 |-------+---------+---------|
628 | Total | replace | replace |
629 |-------+---------+---------|
632 |-------+-------+-------|
633 | Item | Item | Sub- |
634 | name | value | total |
635 |-------+-------+-------|
639 |-------+-------+-------|
641 |-------+-------+-------|
644 |-------+-------+-------|
646 |-------+-------+-------|
648 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
649 "$3 = if(vlen(@0..@+I) == 1, "
650 "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
651 "@>$3 = string(\"\")")))
653 (ert-deftest test-org-table
/org-lookup-all
()
654 "Use `org-lookup-all' for several GROUP BY as in SQL and for ranking.
655 See also http://orgmode.org/worg/org-tutorials/org-lookups.html ."
658 | Purchase | Product | Shop | Rating |
659 |----------+---------+------+--------|
666 ;; Product rating and ranking by average purchase from "#+NAME: data"
667 (org-test-table-target-expect
669 | Product | Rating | Ranking |
670 |---------+---------+---------|
671 | p1 | replace | replace |
672 | p2 | replace | replace |
673 | p3 | replace | replace |
676 | Product | Rating | Ranking |
677 |---------+--------+---------|
683 "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
684 "'(remote(data, @I$2..@>$2)) '(remote(data, @I$4..@>$4))))) "
685 "(/ (apply '+ all) (length all) 1.0)); L :: "
686 "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))
688 ;; Shop rating and ranking by average purchase from "#+NAME: data"
689 (org-test-table-target-expect
691 | Shop | Rating | Ranking |
692 |------+---------+---------|
693 | s1 | replace | replace |
694 | s2 | replace | replace |
697 | Shop | Rating | Ranking |
698 |------+--------+---------|
703 "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
704 "'(remote(data, @I$3..@>$3)) '(remote(data, @I$4..@>$4))))) "
705 "(/ (apply '+ all) (length all) 1.0)); L :: "
706 "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))))
708 (ert-deftest test-org-table
/org-table-make-reference
/mode-string-EL
()
709 (fset 'f
'org-table-make-reference
)
710 ;; For Lisp formula only
711 (should (equal "0" (f "0" t nil
'literal
)))
712 (should (equal "z" (f "z" t nil
'literal
)))
713 (should (equal "" (f "" t nil
'literal
)))
714 (should (equal "0 1" (f '("0" "1") t nil
'literal
)))
715 (should (equal "z 1" (f '("z" "1") t nil
'literal
)))
716 (should (equal " 1" (f '("" "1") t nil
'literal
)))
717 (should (equal " " (f '("" "" ) t nil
'literal
))))
719 (ert-deftest test-org-table
/org-table-make-reference
/mode-string-E
()
720 (fset 'f
'org-table-make-reference
)
722 (should (equal "\"0\"" (f "0" t nil t
)))
723 (should (equal "\"z\"" (f "z" t nil t
)))
724 (should (equal "\"\"" (f "" t nil t
)))
725 (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t
)))
726 (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t
)))
727 (should (equal "\"\" \"1\"" (f '("" "1") t nil t
)))
728 (should (equal "\"\" \"\"" (f '("" "" ) t nil t
)))
730 (should (equal "(0)" (f "0" t nil nil
)))
731 (should (equal "(z)" (f "z" t nil nil
)))
732 (should (equal "nan" (f "" t nil nil
)))
733 (should (equal "[0,1]" (f '("0" "1") t nil nil
)))
734 (should (equal "[z,1]" (f '("z" "1") t nil nil
)))
735 (should (equal "[nan,1]" (f '("" "1") t nil nil
)))
736 (should (equal "[nan,nan]" (f '("" "" ) t nil nil
)))
737 ;; For Calc formula, special numbers
738 (should (equal "(nan)" (f "nan" t nil nil
)))
739 (should (equal "(uinf)" (f "uinf" t nil nil
)))
740 (should (equal "(-inf)" (f "-inf" t nil nil
)))
741 (should (equal "(inf)" (f "inf" t nil nil
)))
742 (should (equal "[nan,1]" (f '( "nan" "1") t nil nil
)))
743 (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil
)))
744 (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil
)))
745 (should (equal "[inf,1]" (f '( "inf" "1") t nil nil
))))
747 (ert-deftest test-org-table
/org-table-make-reference
/mode-string-EN
()
748 (fset 'f
'org-table-make-reference
)
750 (should (equal "0" (f "0" t t t
)))
751 (should (equal "0" (f "z" t t t
)))
752 (should (equal "0" (f "" t t t
)))
753 (should (equal "0 1" (f '("0" "1") t t t
)))
754 (should (equal "0 1" (f '("z" "1") t t t
)))
755 (should (equal "0 1" (f '("" "1") t t t
)))
756 (should (equal "0 0" (f '("" "" ) t t t
)))
758 (should (equal "(0)" (f "0" t t nil
)))
759 (should (equal "(0)" (f "z" t t nil
)))
760 (should (equal "(0)" (f "" t t nil
)))
761 (should (equal "[0,1]" (f '("0" "1") t t nil
)))
762 (should (equal "[0,1]" (f '("z" "1") t t nil
)))
763 (should (equal "[0,1]" (f '("" "1") t t nil
)))
764 (should (equal "[0,0]" (f '("" "" ) t t nil
)))
765 ;; For Calc formula, special numbers
766 (should (equal "(0)" (f "nan" t t nil
)))
767 (should (equal "(0)" (f "uinf" t t nil
)))
768 (should (equal "(0)" (f "-inf" t t nil
)))
769 (should (equal "(0)" (f "inf" t t nil
)))
770 (should (equal "[0,1]" (f '( "nan" "1") t t nil
)))
771 (should (equal "[0,1]" (f '("uinf" "1") t t nil
)))
772 (should (equal "[0,1]" (f '("-inf" "1") t t nil
)))
773 (should (equal "[0,1]" (f '( "inf" "1") t t nil
))))
775 (ert-deftest test-org-table
/org-table-make-reference
/mode-string-L
()
776 (fset 'f
'org-table-make-reference
)
777 ;; For Lisp formula only
778 (should (equal "0" (f "0" nil nil
'literal
)))
779 (should (equal "z" (f "z" nil nil
'literal
)))
780 (should (equal "" (f "" nil nil
'literal
)))
781 (should (equal "0 1" (f '("0" "1") nil nil
'literal
)))
782 (should (equal "z 1" (f '("z" "1") nil nil
'literal
)))
783 (should (equal "1" (f '("" "1") nil nil
'literal
)))
784 (should (equal "" (f '("" "" ) nil nil
'literal
))))
786 (ert-deftest test-org-table
/org-table-make-reference
/mode-string-none
()
787 (fset 'f
'org-table-make-reference
)
789 (should (equal "\"0\"" (f "0" nil nil t
)))
790 (should (equal "\"z\"" (f "z" nil nil t
)))
791 (should (equal "" (f "" nil nil t
)))
792 (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t
)))
793 (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t
)))
794 (should (equal "\"1\"" (f '("" "1") nil nil t
)))
795 (should (equal "" (f '("" "" ) nil nil t
)))
797 (should (equal "(0)" (f "0" nil nil nil
)))
798 (should (equal "(z)" (f "z" nil nil nil
)))
799 (should (equal "(0)" (f "" nil nil nil
)))
800 (should (equal "[0,1]" (f '("0" "1") nil nil nil
)))
801 (should (equal "[z,1]" (f '("z" "1") nil nil nil
)))
802 (should (equal "[1]" (f '("" "1") nil nil nil
)))
803 (should (equal "[]" (f '("" "" ) nil nil nil
)))
804 ;; For Calc formula, special numbers
805 (should (equal "(nan)" (f "nan" nil nil nil
)))
806 (should (equal "(uinf)" (f "uinf" nil nil nil
)))
807 (should (equal "(-inf)" (f "-inf" nil nil nil
)))
808 (should (equal "(inf)" (f "inf" nil nil nil
)))
809 (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil
)))
810 (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil
)))
811 (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil
)))
812 (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil
))))
814 (ert-deftest test-org-table
/org-table-make-reference
/mode-string-N
()
815 (fset 'f
'org-table-make-reference
)
817 (should (equal "0" (f "0" nil t t
)))
818 (should (equal "0" (f "z" nil t t
)))
819 (should (equal "" (f "" nil t t
)))
820 (should (equal "0 1" (f '("0" "1") nil t t
)))
821 (should (equal "0 1" (f '("z" "1") nil t t
)))
822 (should (equal "1" (f '("" "1") nil t t
)))
823 (should (equal "" (f '("" "" ) nil t t
)))
825 (should (equal "(0)" (f "0" nil t nil
)))
826 (should (equal "(0)" (f "z" nil t nil
)))
827 (should (equal "(0)" (f "" nil t nil
)))
828 (should (equal "[0,1]" (f '("0" "1") nil t nil
)))
829 (should (equal "[0,1]" (f '("z" "1") nil t nil
)))
830 (should (equal "[1]" (f '("" "1") nil t nil
)))
831 (should (equal "[]" (f '("" "" ) nil t nil
)))
832 ;; For Calc formula, special numbers
833 (should (equal "(0)" (f "nan" nil t nil
)))
834 (should (equal "(0)" (f "uinf" nil t nil
)))
835 (should (equal "(0)" (f "-inf" nil t nil
)))
836 (should (equal "(0)" (f "inf" nil t nil
)))
837 (should (equal "[0,1]" (f '( "nan" "1") nil t nil
)))
838 (should (equal "[0,1]" (f '("uinf" "1") nil t nil
)))
839 (should (equal "[0,1]" (f '("-inf" "1") nil t nil
)))
840 (should (equal "[0,1]" (f '( "inf" "1") nil t nil
))))
842 (ert-deftest test-org-table
/org-table-convert-refs-to-an
/1 ()
843 "Simple reference @1$1."
845 (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
848 ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
849 ;; "Self reference @1$1."
851 ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
853 (ert-deftest test-org-table
/org-table-convert-refs-to-an
/3 ()
856 (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
858 (ert-deftest test-org-table
/org-table-convert-refs-to-rc
/1 ()
859 "Simple reference @1$1."
861 (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
863 (ert-deftest test-org-table
/org-table-convert-refs-to-rc
/2 ()
866 (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
869 ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
870 ;; "Remote reference."
872 ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
874 (ert-deftest test-org-table
/remote-reference-access
()
875 "Access to remote reference.
876 See also `test-org-table/copy-field'."
877 (org-test-table-target-expect
882 | replace | replace |
890 1 (concat "#+TBLFM: "
891 ;; Copy text without calculation: Use Lisp formula
892 "$1 = '(identity remote(table, @1$2)) :: "
893 ;; Do a calculation: Use Calc (or Lisp ) formula
894 "$2 = 2 * remote(table, @1$2)")))
896 (ert-deftest test-org-table
/remote-reference-indirect
()
897 "Access to remote reference with indirection of name or ID."
898 (let ((source-tables "
906 #+TBLFM: @>$1 = vsum(@I..@II)
915 #+TBLFM: @>$1 = vsum(@I..@II)
918 ;; Read several remote references from same column
919 (org-test-table-target-expect
920 (concat source-tables
"
929 (concat source-tables
"
940 "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
942 (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
943 "@>$2 = '(+ @I..@II); N"))
945 ;; Read several remote references from same row
946 (org-test-table-target-expect
947 (concat source-tables
"
949 | year | 2012 | 2013 | total |
950 |--------+---------+---------+---------|
951 | amount | replace | replace | replace |
953 (concat source-tables
"
955 | year | 2012 | 2013 | total |
956 |--------+------+------+-------|
957 | amount | 3 | 12 | 15 |
961 "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
963 (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
964 "@2$> = '(+ $<<..$>>); N"))))
966 (ert-deftest test-org-table
/org-at-TBLFM-p
()
967 (org-test-with-temp-text-in-file
974 (goto-char (point-min))
976 (should (equal (org-at-TBLFM-p) nil
))
978 (goto-char (point-min))
980 (should (equal (org-at-TBLFM-p) t
))
982 (goto-char (point-min))
984 (should (equal (org-at-TBLFM-p) nil
))))
986 (ert-deftest test-org-table
/org-table-TBLFM-begin
()
987 (org-test-with-temp-text-in-file
994 (goto-char (point-min))
995 (should (equal (org-table-TBLFM-begin)
998 (goto-char (point-min))
1000 (should (equal (org-table-TBLFM-begin)
1003 (goto-char (point-min))
1005 (should (= (org-table-TBLFM-begin)
1008 (goto-char (point-min))
1010 (should (= (org-table-TBLFM-begin)
1015 (ert-deftest test-org-table
/org-table-TBLFM-begin-for-multiple-TBLFM-lines
()
1016 "For multiple #+TBLFM lines."
1017 (org-test-with-temp-text-in-file
1025 (goto-char (point-min))
1026 (should (equal (org-table-TBLFM-begin)
1029 (goto-char (point-min))
1031 (should (equal (org-table-TBLFM-begin)
1034 (goto-char (point-min))
1036 (should (= (org-table-TBLFM-begin)
1039 (goto-char (point-min))
1041 (should (= (org-table-TBLFM-begin)
1044 (goto-char (point-min))
1046 (should (= (org-table-TBLFM-begin)
1051 (ert-deftest test-org-table
/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks
()
1052 (org-test-with-temp-text-in-file
1065 (goto-char (point-min))
1066 (should (equal (org-table-TBLFM-begin)
1069 (goto-char (point-min))
1071 (should (equal (org-table-TBLFM-begin)
1074 (goto-char (point-min))
1076 (should (= (org-table-TBLFM-begin)
1079 (goto-char (point-min))
1081 (should (= (org-table-TBLFM-begin)
1084 (goto-char (point-min))
1086 (should (= (org-table-TBLFM-begin)
1089 (goto-char (point-min))
1091 (should (= (org-table-TBLFM-begin)
1094 (goto-char (point-min))
1096 (should (= (org-table-TBLFM-begin)
1099 (goto-char (point-min))
1101 (should (= (org-table-TBLFM-begin)
1104 (goto-char (point-min))
1106 (should (= (org-table-TBLFM-begin)
1109 (ert-deftest test-org-table
/org-table-calc-current-TBLFM
()
1110 (org-test-with-temp-text-in-file
1118 (let ((got (progn (goto-char (point-min))
1120 (org-table-calc-current-TBLFM)
1129 (should (string= got
1132 (let ((got (progn (goto-char (point-min))
1134 (org-table-calc-current-TBLFM)
1143 (should (string= got
1146 (ert-deftest test-org-table
/org-table-calc-current-TBLFM-when-stop-because-of-error
()
1147 "org-table-calc-current-TBLFM should preserve the input as it was."
1148 (org-test-with-temp-text-in-file
1153 #+TBLFM: $2=$1*2::$2=$1*2
1160 #+TBLFM: $2=$1*2::$2=$1*2
1163 (goto-char (point-min))
1165 (should-error (org-table-calc-current-TBLFM))
1166 (setq got
(buffer-string))
1168 (should (string= got
1171 (provide 'test-org-table
)
1173 ;;; test-org-table.el ends here