test-org-info.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; test-org-info.el --- Tests for "org-info.el" -*- 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. (ert-deftest test-org-info/export ()
  16. "Test `org-info-export' specifications."
  17. ;; Export to HTML. Without node, refer to "Top".
  18. (should
  19. (equal (org-info-export "filename#node" nil 'html)
  20. "<a href=\"filename.html#node\">filename#node</a>"))
  21. (should
  22. (equal (org-info-export "filename" nil 'html)
  23. "<a href=\"filename.html#Top\">filename</a>"))
  24. ;; Directory index. Top anchor actually should not be added,
  25. ;; but it should be rather rare case to add special code path.
  26. (should
  27. (equal (org-info-export "dir" nil 'html)
  28. "<a href=\"https://www.gnu.org/manual/manual.html#Top\">dir</a>"))
  29. ;; When exporting to HTML, ensure node names are expanded according
  30. ;; to (info "(texinfo) HTML Xref Node Name Expansion").
  31. (should
  32. (equal "_005f"
  33. (let ((name (org-info-export "#_" nil 'html)))
  34. (and (string-match "#\\(.*\\)\"" name)
  35. (match-string 1 name)))))
  36. (should
  37. (equal "_002d"
  38. (let ((name (org-info-export "#-" nil 'html)))
  39. (and (string-match "#\\(.*\\)\"" name)
  40. (match-string 1 name)))))
  41. (should
  42. (equal "A-node"
  43. (let ((name (org-info-export "#A node" nil 'html)))
  44. (and (string-match "#\\(.*\\)\"" name)
  45. (match-string 1 name)))))
  46. (should
  47. (equal "A-node-_002d_002d_002d-with-_005f_0027_0025"
  48. (let ((name (org-info-export "#A node --- with _'%" nil 'html)))
  49. (and (string-match "#\\(.*\\)\"" name)
  50. (match-string 1 name)))))
  51. ;; Export to Texinfo. Without a node name, refer to "Top".
  52. (should
  53. (equal (org-info-export "filename" nil 'texinfo)
  54. "@ref{Top,,,filename,}"))
  55. (should
  56. (equal (org-info-export "filename#node" nil 'texinfo)
  57. "@ref{node,,,filename,}"))
  58. ;; "Top" is preserved, "::" as node separator.
  59. (should
  60. (equal "@ref{Top,,,emacs,}"
  61. (org-info-export "emacs::Top" nil 'texinfo)))
  62. ;; Description.
  63. (should
  64. (equal "@ref{Top,Emacs,,emacs,}"
  65. (org-info-export "emacs" "Emacs" 'texinfo)))
  66. (should
  67. (equal "@ref{Destructuring with pcase Patterns,pcase-let,,emacs,}"
  68. (org-info-export "emacs#Destructuring with pcase Patterns"
  69. "pcase-let" 'texinfo))))
  70. (ert-deftest test-org-info/link-file-node ()
  71. "Test parse info links by `org-info--link-file-node'."
  72. (should (equal '("success" . "Hash Separator")
  73. (org-info--link-file-node "success#Hash Separator")))
  74. ;; Other separators.
  75. (should (equal '("success" . "Single Colon Separator")
  76. (org-info--link-file-node "success:Single Colon Separator")))
  77. (should (equal '("success" . "Double Colon Separator")
  78. (org-info--link-file-node "success::Double Colon Separator")))
  79. (should (equal '("success" . "Hash Colon Separator")
  80. (org-info--link-file-node "success#:Hash Colon Separator")))
  81. ;; Partial specification.
  82. (should (equal '("nodeless" . "Top")
  83. (org-info--link-file-node "nodeless")))
  84. (should (equal '("dir" . "Top")
  85. (org-info--link-file-node "")))
  86. (should (equal '("dir" . "Top")
  87. (org-info--link-file-node nil)))
  88. ;; Feel free to change behavior of underspecified links,
  89. ;; the case is added to check that it does not signal some error.
  90. (should (equal '("dir" . "broken")
  91. (org-info--link-file-node "#broken")))
  92. ;; Trailing separator.
  93. (should (equal '("trailing-hash" . "Top")
  94. (org-info--link-file-node "trailing-hash#")))
  95. (should (equal '("trailing-single-colon" . "Top")
  96. (org-info--link-file-node "trailing-single-colon:")))
  97. (should (equal '("trailing-double-colon" . "Top")
  98. (org-info--link-file-node "trailing-double-colon::")))
  99. (should (equal '("trailing-hash-colon" . "Top")
  100. (org-info--link-file-node "trailing-hash-colon#:")))
  101. ;; Trim spaces.
  102. (should (equal '("trim" . "Spaces")
  103. (org-info--link-file-node " trim # Spaces \t"))))
  104. (ert-deftest test-org-info/description-as-command ()
  105. "Test `org-info-description-as-command'."
  106. (let ((cases
  107. '(("info file" "info:file")
  108. ("info strip-top-hash" "info:strip-top-hash#Top")
  109. ("info strip-top-single-colon" "info:strip-top-single-colon:Top")
  110. ("info strip-top-double-colon" "info:strip-top-double-colon::Top")
  111. ("info \"(pass) Hash\"" "info:pass#Hash")
  112. ("info \"(pass) Double Colon\"" "info:pass:: Double Colon")
  113. ("info \"(info) Advanced\"" "info:info:Advanced")
  114. ("info \"(dir)\"" "info:")
  115. ;; It actually works as "(dir) Top", test that no errors is signalled.
  116. ("info \"(dir) Invalid\"" "info::Invalid")
  117. (nil "http://orgmode.org/index.html#Not-info-link"))))
  118. (dolist (expectation-input cases)
  119. (let ((expectation (car expectation-input))
  120. (input (cadr expectation-input)))
  121. (should (equal
  122. expectation
  123. (org-info-description-as-command input nil))))))
  124. (let ((cases
  125. '(("Override link" "info:ignored#Link" "Override link")
  126. ("Fallback description" "http://not.info/link" "Fallback description")
  127. ("Link is nil" nil "Link is nil"))))
  128. (dolist (expectation-input-desc cases)
  129. (let ((expectation (car expectation-input-desc))
  130. (input (cadr expectation-input-desc))
  131. (desc (nth 2 expectation-input-desc)))
  132. (should (equal
  133. expectation
  134. (org-info-description-as-command input desc)))))))
  135. (provide 'test-org-info)
  136. ;;; test-org-info.el ends here