A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

129 lines
5.7 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. self.assertEqual("http://example.com", node1.__strip__())
  62. self.assertEqual(None, node2.__strip__())
  63. self.assertEqual(None, node3.__strip__())
  64. self.assertEqual("Link", node4.__strip__())
  65. def test_showtree(self):
  66. """test ExternalLink.__showtree__()"""
  67. output = []
  68. getter, marker = object(), object()
  69. get = lambda code: output.append((getter, code))
  70. mark = lambda: output.append(marker)
  71. node1 = ExternalLink(wraptext("http://example.com"), brackets=False)
  72. node2 = ExternalLink(wraptext("http://example.com"), wraptext("Link"))
  73. node1.__showtree__(output.append, get, mark)
  74. node2.__showtree__(output.append, get, mark)
  75. valid = [
  76. (getter, node1.url), "[", (getter, node2.url),
  77. (getter, node2.title), "]"]
  78. self.assertEqual(valid, output)
  79. def test_url(self):
  80. """test getter/setter for the url attribute"""
  81. url = wraptext("http://example.com/")
  82. node1 = ExternalLink(url, brackets=False)
  83. node2 = ExternalLink(url, wraptext("Example"))
  84. self.assertIs(url, node1.url)
  85. self.assertIs(url, node2.url)
  86. node1.url = "mailto:héhehé@spam.com"
  87. node2.url = "mailto:héhehé@spam.com"
  88. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node1.url)
  89. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node2.url)
  90. def test_title(self):
  91. """test getter/setter for the title attribute"""
  92. title = wraptext("Example!")
  93. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  94. node2 = ExternalLink(wraptext("http://example.com/"), title)
  95. self.assertIs(None, node1.title)
  96. self.assertIs(title, node2.title)
  97. node2.title = None
  98. self.assertIs(None, node2.title)
  99. node2.title = "My Website"
  100. self.assertWikicodeEqual(wraptext("My Website"), node2.title)
  101. def test_brackets(self):
  102. """test getter/setter for the brackets attribute"""
  103. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  104. node2 = ExternalLink(wraptext("http://example.com/"), wraptext("Link"))
  105. self.assertFalse(node1.brackets)
  106. self.assertTrue(node2.brackets)
  107. node1.brackets = True
  108. node2.brackets = False
  109. self.assertTrue(node1.brackets)
  110. self.assertFalse(node2.brackets)
  111. self.assertEqual("[http://example.com/]", str(node1))
  112. self.assertEqual("http://example.com/", str(node2))
  113. if __name__ == "__main__":
  114. unittest.main(verbosity=2)