A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test_external_link.py 5.5 KiB

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