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.
 
 
 
 

126 lines
5.7 KiB

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