A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

130 righe
5.8 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. try:
  24. import unittest2 as unittest
  25. except ImportError:
  26. import unittest
  27. from mwparserfromhell.compat import str
  28. from mwparserfromhell.nodes import ExternalLink, Text
  29. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  30. class TestExternalLink(TreeEqualityTestCase):
  31. """Test cases for the ExternalLink node."""
  32. def test_unicode(self):
  33. """test ExternalLink.__unicode__()"""
  34. node = ExternalLink(wraptext("http://example.com/"), brackets=False)
  35. self.assertEqual("http://example.com/", str(node))
  36. node2 = ExternalLink(wraptext("http://example.com/"))
  37. self.assertEqual("[http://example.com/]", str(node2))
  38. node3 = ExternalLink(wraptext("http://example.com/"), wrap([]))
  39. self.assertEqual("[http://example.com/ ]", str(node3))
  40. node4 = ExternalLink(wraptext("http://example.com/"),
  41. wraptext("Example Web Page"))
  42. self.assertEqual("[http://example.com/ Example Web Page]", str(node4))
  43. def test_children(self):
  44. """test ExternalLink.__children__()"""
  45. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  46. node2 = ExternalLink(wraptext("http://example.com/"),
  47. wrap([Text("Example"), Text("Page")]))
  48. gen1 = node1.__children__()
  49. gen2 = node2.__children__()
  50. self.assertEqual(node1.url, next(gen1))
  51. self.assertEqual(node2.url, next(gen2))
  52. self.assertEqual(node2.title, next(gen2))
  53. self.assertRaises(StopIteration, next, gen1)
  54. self.assertRaises(StopIteration, next, gen2)
  55. def test_strip(self):
  56. """test ExternalLink.__strip__()"""
  57. node1 = ExternalLink(wraptext("http://example.com"), brackets=False)
  58. node2 = ExternalLink(wraptext("http://example.com"))
  59. node3 = ExternalLink(wraptext("http://example.com"), wrap([]))
  60. node4 = ExternalLink(wraptext("http://example.com"), wraptext("Link"))
  61. for a in (True, False):
  62. for b in (True, False):
  63. self.assertEqual("http://example.com", node1.__strip__(a, b))
  64. self.assertEqual(None, node2.__strip__(a, b))
  65. self.assertEqual(None, node3.__strip__(a, b))
  66. self.assertEqual("Link", node4.__strip__(a, b))
  67. def test_showtree(self):
  68. """test ExternalLink.__showtree__()"""
  69. output = []
  70. getter, marker = object(), object()
  71. get = lambda code: output.append((getter, code))
  72. mark = lambda: output.append(marker)
  73. node1 = ExternalLink(wraptext("http://example.com"), brackets=False)
  74. node2 = ExternalLink(wraptext("http://example.com"), wraptext("Link"))
  75. node1.__showtree__(output.append, get, mark)
  76. node2.__showtree__(output.append, get, mark)
  77. valid = [
  78. (getter, node1.url), "[", (getter, node2.url),
  79. (getter, node2.title), "]"]
  80. self.assertEqual(valid, output)
  81. def test_url(self):
  82. """test getter/setter for the url attribute"""
  83. url = wraptext("http://example.com/")
  84. node1 = ExternalLink(url, brackets=False)
  85. node2 = ExternalLink(url, wraptext("Example"))
  86. self.assertIs(url, node1.url)
  87. self.assertIs(url, node2.url)
  88. node1.url = "mailto:héhehé@spam.com"
  89. node2.url = "mailto:héhehé@spam.com"
  90. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node1.url)
  91. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node2.url)
  92. def test_title(self):
  93. """test getter/setter for the title attribute"""
  94. title = wraptext("Example!")
  95. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  96. node2 = ExternalLink(wraptext("http://example.com/"), title)
  97. self.assertIs(None, node1.title)
  98. self.assertIs(title, node2.title)
  99. node2.title = None
  100. self.assertIs(None, node2.title)
  101. node2.title = "My Website"
  102. self.assertWikicodeEqual(wraptext("My Website"), node2.title)
  103. def test_brackets(self):
  104. """test getter/setter for the brackets attribute"""
  105. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  106. node2 = ExternalLink(wraptext("http://example.com/"), wraptext("Link"))
  107. self.assertFalse(node1.brackets)
  108. self.assertTrue(node2.brackets)
  109. node1.brackets = True
  110. node2.brackets = False
  111. self.assertTrue(node1.brackets)
  112. self.assertFalse(node2.brackets)
  113. self.assertEqual("[http://example.com/]", str(node1))
  114. self.assertEqual("http://example.com/", str(node2))
  115. if __name__ == "__main__":
  116. unittest.main(verbosity=2)