test-org-macs.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;;; test-org-macs.el --- Tests for Org Macs library -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017, 2019 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. ;;; String manipulation
  16. (ert-deftest test-org/split-string ()
  17. "Test `org-split-string' specifications."
  18. ;; Regular test.
  19. (should (equal '("a" "b") (org-split-string "a b" " ")))
  20. ;; Empty parts are not removed.
  21. (should (equal '("a" "" "b") (org-split-string "a||b" "|")))
  22. ;; However, empty parts at beginning or end of string are removed.
  23. (should (equal '("a" "b") (org-split-string "|a|b|" "|")))
  24. ;; Pathological case: call on an empty string. Since empty parts
  25. ;; are not removed, it shouldn't return nil.
  26. (should (equal '("") (org-split-string "")))
  27. ;; SEPARATORS, when non-nil, is a regexp. In particular, do not
  28. ;; match more than specified.
  29. (should-not (equal '("a" "b") (org-split-string "a b" " ")))
  30. ;; When nil, SEPARATORS matches any number of blank characters.
  31. (should (equal '("a" "b") (org-split-string "a \t\nb"))))
  32. (ert-deftest test-org/string-width ()
  33. "Test `org-string-width' specifications."
  34. (should (= 1 (org-string-width "a")))
  35. (should (= 0 (org-string-width "")))
  36. ;; Ignore invisible characters.
  37. (should (= 0 (org-string-width #("a" 0 1 (invisible t)))))
  38. (should (= 1 (org-string-width #("ab" 0 1 (invisible t)))))
  39. (should (= 1 (org-string-width #("ab" 1 2 (invisible t)))))
  40. (should (= 3 (org-string-width
  41. #("abcde" 1 2 (invisible t) 3 4 (invisible t)))))
  42. ;; Check if `invisible' value really means invisibility.
  43. (should (= 0 (let ((buffer-invisibility-spec t))
  44. (org-string-width #("a" 0 1 (invisible foo))))))
  45. (should (= 0 (let ((buffer-invisibility-spec '(foo)))
  46. (org-string-width #("a" 0 1 (invisible foo))))))
  47. (should (= 0 (let ((buffer-invisibility-spec '((foo . t))))
  48. (org-string-width #("a" 0 1 (invisible foo))))))
  49. (should (= 1 (let ((buffer-invisibility-spec '(bar)))
  50. (org-string-width #("a" 0 1 (invisible foo))))))
  51. ;; Check `display' property.
  52. (should (= 3 (org-string-width #("a" 0 1 (display "abc")))))
  53. (should (= 5 (org-string-width #("1a3" 1 2 (display "abc")))))
  54. ;; `display' string can also contain invisible characters.
  55. (should (= 4 (org-string-width
  56. #("123" 1 2 (display #("abc" 1 2 (invisible t)))))))
  57. ;; Test `space' property in `display'.
  58. (should (= 2 (org-string-width #(" " 0 1 (display (space :width 2))))))
  59. ;; Test `wrap-prefix' property.
  60. (should (= 2 (org-string-width #("ab" 0 2 (wrap-prefix " ")))))
  61. ;; Test `line-prefix' property.
  62. (should (= 2 (org-string-width #("ab" 0 2 (line-prefix " "))))))
  63. ;;; Regexp
  64. (ert-deftest test-org/in-regexp ()
  65. "Test `org-in-regexp' specifications."
  66. ;; Standard tests.
  67. (should
  68. (org-test-with-temp-text "xx ab<point>c xx"
  69. (org-in-regexp "abc")))
  70. (should-not
  71. (org-test-with-temp-text "xx abc <point>xx"
  72. (org-in-regexp "abc")))
  73. ;; Return non-nil even with multiple matching regexps in the same
  74. ;; line.
  75. (should
  76. (org-test-with-temp-text "abc xx ab<point>c xx"
  77. (org-in-regexp "abc")))
  78. ;; With optional argument NLINES, check extra lines around point.
  79. (should-not
  80. (org-test-with-temp-text "A\nB<point>\nC"
  81. (org-in-regexp "A\nB\nC")))
  82. (should
  83. (org-test-with-temp-text "A\nB<point>\nC"
  84. (org-in-regexp "A\nB\nC" 1)))
  85. (should-not
  86. (org-test-with-temp-text "A\nB\nC<point>"
  87. (org-in-regexp "A\nB\nC" 1)))
  88. ;; When optional argument VISUALLY is non-nil, return nil if at
  89. ;; regexp boundaries.
  90. (should
  91. (org-test-with-temp-text "xx abc<point> xx"
  92. (org-in-regexp "abc")))
  93. (should-not
  94. (org-test-with-temp-text "xx abc<point> xx"
  95. (org-in-regexp "abc" nil t))))
  96. ;;; Template
  97. (ert-deftest test-org/fill-template ()
  98. "Test `org-fill-template'"
  99. (should
  100. (string= "working"
  101. (org-fill-template "%var-long"
  102. '(("var" . "broken")
  103. ("var-long" . "working"))))))
  104. ;;; Time
  105. (ert-deftest test-org-matcher-time ()
  106. "Test `org-matcher-time'."
  107. (let ((system-time-locale "en_US"))
  108. (org-test-at-time "<2021-01-11 Mon 13:00>"
  109. (should (equal (list 0 0 13 11 1 2021)
  110. (butlast (decode-time (org-matcher-time "<now>"))
  111. 3)))
  112. (should (equal (list 0 0 0 14 1 2021)
  113. (butlast (decode-time (org-matcher-time "<+3d>"))
  114. 3)))
  115. (should (equal (list 0 0 0 9 1 2021)
  116. (butlast (decode-time (org-matcher-time "<-2d>"))
  117. 3)))
  118. (should (equal (list 0 0 0 18 1 2021)
  119. (butlast (decode-time (org-matcher-time "<+1w>"))
  120. 3)))
  121. (should (equal (list 0 0 17 11 1 2021)
  122. (butlast (decode-time (org-matcher-time "<+4h>"))
  123. 3)))
  124. (should (equal (list 0 0 11 11 1 2021)
  125. (butlast (decode-time (org-matcher-time "<-2h>"))
  126. 3)))
  127. (should (equal (list 0 0 3 12 1 2021)
  128. (butlast (decode-time (org-matcher-time "<+14h>"))
  129. 3))))))
  130. (provide 'test-org-macs)
  131. ;;; test-org-macs.el ends here