A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

test_external_link.py 6.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. import unittest
  24. from mwparserfromhell.compat import str
  25. from mwparserfromhell.nodes import ExternalLink, Text
  26. from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext
  27. class TestExternalLink(TreeEqualityTestCase):
  28. """Test cases for the ExternalLink node."""
  29. def test_unicode(self):
  30. """test ExternalLink.__unicode__()"""
  31. node = ExternalLink(wraptext("http://example.com/"), brackets=False)
  32. self.assertEqual("http://example.com/", str(node))
  33. node2 = ExternalLink(wraptext("http://example.com/"))
  34. self.assertEqual("[http://example.com/]", str(node2))
  35. node3 = ExternalLink(wraptext("http://example.com/"), wrap([]))
  36. self.assertEqual("[http://example.com/ ]", str(node3))
  37. node4 = ExternalLink(wraptext("http://example.com/"),
  38. wraptext("Example Web Page"))
  39. self.assertEqual("[http://example.com/ Example Web Page]", str(node4))
  40. def test_iternodes(self):
  41. """test ExternalLink.__iternodes__()"""
  42. node1n1 = Text("http://example.com/")
  43. node2n1 = Text("http://example.com/")
  44. node2n2, node2n3 = Text("Example"), Text("Page")
  45. node1 = ExternalLink(wrap([node1n1]), brackets=False)
  46. node2 = ExternalLink(wrap([node2n1]), wrap([node2n2, node2n3]))
  47. gen1 = node1.__iternodes__(getnodes)
  48. gen2 = node2.__iternodes__(getnodes)
  49. self.assertEqual((None, node1), next(gen1))
  50. self.assertEqual((None, node2), next(gen2))
  51. self.assertEqual((node1.url, node1n1), next(gen1))
  52. self.assertEqual((node2.url, node2n1), next(gen2))
  53. self.assertEqual((node2.title, node2n2), next(gen2))
  54. self.assertEqual((node2.title, node2n3), next(gen2))
  55. self.assertRaises(StopIteration, next, gen1)
  56. self.assertRaises(StopIteration, next, gen2)
  57. def test_strip(self):
  58. """test ExternalLink.__strip__()"""
  59. node1 = ExternalLink(wraptext("http://example.com"), brackets=False)
  60. node2 = ExternalLink(wraptext("http://example.com"))
  61. node3 = ExternalLink(wraptext("http://example.com"), wrap([]))
  62. node4 = ExternalLink(wraptext("http://example.com"), wraptext("Link"))
  63. for a in (True, False):
  64. for b in (True, False):
  65. self.assertEqual("http://example.com", node1.__strip__(a, b))
  66. self.assertEqual(None, node2.__strip__(a, b))
  67. self.assertEqual(None, node3.__strip__(a, b))
  68. self.assertEqual("Link", node4.__strip__(a, b))
  69. def test_showtree(self):
  70. """test ExternalLink.__showtree__()"""
  71. output = []
  72. getter, marker = object(), object()
  73. get = lambda code: output.append((getter, code))
  74. mark = lambda: output.append(marker)
  75. node1 = ExternalLink(wraptext("http://example.com"), brackets=False)
  76. node2 = ExternalLink(wraptext("http://example.com"), wraptext("Link"))
  77. node1.__showtree__(output.append, get, mark)
  78. node2.__showtree__(output.append, get, mark)
  79. valid = [
  80. (getter, node1.url), "[", (getter, node2.url),
  81. (getter, node2.title), "]"]
  82. self.assertEqual(valid, output)
  83. def test_url(self):
  84. """test getter/setter for the url attribute"""
  85. url = wraptext("http://example.com/")
  86. node1 = ExternalLink(url, brackets=False)
  87. node2 = ExternalLink(url, wraptext("Example"))
  88. self.assertIs(url, node1.url)
  89. self.assertIs(url, node2.url)
  90. node1.url = "mailto:héhehé@spam.com"
  91. node2.url = "mailto:héhehé@spam.com"
  92. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node1.url)
  93. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node2.url)
  94. def test_title(self):
  95. """test getter/setter for the title attribute"""
  96. title = wraptext("Example!")
  97. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  98. node2 = ExternalLink(wraptext("http://example.com/"), title)
  99. self.assertIs(None, node1.title)
  100. self.assertIs(title, node2.title)
  101. node2.title = None
  102. self.assertIs(None, node2.title)
  103. node2.title = "My Website"
  104. self.assertWikicodeEqual(wraptext("My Website"), node2.title)
  105. def test_brackets(self):
  106. """test getter/setter for the brackets attribute"""
  107. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  108. node2 = ExternalLink(wraptext("http://example.com/"), wraptext("Link"))
  109. self.assertFalse(node1.brackets)
  110. self.assertTrue(node2.brackets)
  111. node1.brackets = True
  112. node2.brackets = False
  113. self.assertTrue(node1.brackets)
  114. self.assertFalse(node2.brackets)
  115. self.assertEqual("[http://example.com/]", str(node1))
  116. self.assertEqual("http://example.com/", str(node2))
  117. if __name__ == "__main__":
  118. unittest.main(verbosity=2)