sgml-mode-tests.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ;;; sgml-mode-tests.el --- Tests for sgml-mode
  2. ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
  3. ;; Author: Przemysław Wojnowski <esperanto@cumego.com>
  4. ;; Keywords: tests
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'sgml-mode)
  19. (require 'ert)
  20. (defmacro sgml-with-content (content &rest body)
  21. "Insert CONTENT into a temporary `sgml-mode' buffer and execute BODY on it.
  22. The point is set to the beginning of the buffer."
  23. `(with-temp-buffer
  24. (sgml-mode)
  25. (insert ,content)
  26. (goto-char (point-min))
  27. ,@body))
  28. ;;; sgml-delete-tag
  29. (ert-deftest sgml-delete-tag-should-not-delete-tags-when-wrong-args ()
  30. "Don't delete tag, when number of tags to delete is not positive number."
  31. (let ((content "<p>Valar Morghulis</p>"))
  32. (sgml-with-content
  33. content
  34. (sgml-delete-tag -1)
  35. (should (string= content (buffer-string)))
  36. (sgml-delete-tag 0)
  37. (should (string= content (buffer-string))))))
  38. (ert-deftest sgml-delete-tag-should-delete-tags-n-times ()
  39. ;; Delete only 1, when 1 available:
  40. (sgml-with-content
  41. "<br />"
  42. (sgml-delete-tag 1)
  43. (should (string= "" (buffer-string))))
  44. ;; Delete from position on whitespaces before tag:
  45. (sgml-with-content
  46. " \t\n<br />"
  47. (sgml-delete-tag 1)
  48. (should (string= "" (buffer-string))))
  49. ;; Delete from position on tag:
  50. (sgml-with-content
  51. "<br />"
  52. (goto-char 3)
  53. (sgml-delete-tag 1)
  54. (should (string= "" (buffer-string))))
  55. ;; Delete one by one:
  56. (sgml-with-content
  57. "<h1><p>You know nothing, Jon Snow.</p></h1>"
  58. (sgml-delete-tag 1)
  59. (should (string= "<p>You know nothing, Jon Snow.</p>" (buffer-string)))
  60. (sgml-delete-tag 1)
  61. (should (string= "You know nothing, Jon Snow." (buffer-string))))
  62. ;; Delete 2 at a time, when 2 available:
  63. (sgml-with-content
  64. "<h1><p>You know nothing, Jon Snow.</p></h1>"
  65. (sgml-delete-tag 2)
  66. (should (string= "You know nothing, Jon Snow." (buffer-string)))))
  67. (ert-deftest sgml-delete-tag-should-delete-unclosed-tag ()
  68. (sgml-with-content
  69. "<ul><li>Keep your stones connected.</ul>"
  70. (goto-char 5) ; position on "li" tag
  71. (sgml-delete-tag 1)
  72. (should (string= "<ul>Keep your stones connected.</ul>" (buffer-string)))))
  73. (ert-deftest sgml-delete-tag-should-signal-error-for-malformed-tags ()
  74. (let ((content "<h1><h2>Drakaris!</h1></h2>"))
  75. ;; Delete outside tag:
  76. (sgml-with-content
  77. content
  78. (sgml-delete-tag 1)
  79. (should (string= "<h2>Drakaris!</h2>" (buffer-string))))
  80. ;; Delete inner tag:
  81. (sgml-with-content
  82. content
  83. (goto-char 5) ; position the inner tag
  84. (sgml-delete-tag 1)
  85. (should (string= "<h1>Drakaris!</h1>" (buffer-string))))))
  86. (ert-deftest sgml-delete-tag-should-signal-error-when-deleting-too-much ()
  87. (let ((content "<emph>Drakaris!</emph>"))
  88. ;; No tags to delete:
  89. (sgml-with-content
  90. "Drakaris!"
  91. (should-error (sgml-delete-tag 1) :type 'error)
  92. (should (string= "Drakaris!" (buffer-string))))
  93. ;; Trying to delete 2 tags, when only 1 available:
  94. (sgml-with-content
  95. content
  96. (should-error (sgml-delete-tag 2) :type 'error)
  97. (should (string= "Drakaris!" (buffer-string))))
  98. ;; Trying to delete a tag, but not on/before a tag:
  99. (sgml-with-content
  100. content
  101. (goto-char 7) ; D in Drakaris
  102. (should-error (sgml-delete-tag 1) :type 'error)
  103. (should (string= content (buffer-string))))
  104. ;; Trying to delete a tag from position outside tag:
  105. (sgml-with-content
  106. content
  107. (goto-char (point-max))
  108. (should-error (sgml-delete-tag 1) :type 'error)
  109. (should (string= content (buffer-string))))))
  110. (ert-deftest sgml-delete-tag-bug-8203-should-not-delete-apostrophe ()
  111. :expected-result :failed
  112. (sgml-with-content
  113. "<title>Winter is comin'</title>"
  114. (sgml-delete-tag 1)
  115. (should (string= "Winter is comin'" (buffer-string)))))
  116. (provide 'sgml-mode-tests)
  117. ;;; sgml-mode-tests.el ends here