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.
 
 
 
 

121 lines
5.3 KiB

  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 pytest
  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. assert "http://example.com/" == str(node)
  30. node2 = ExternalLink(wraptext("http://example.com/"))
  31. assert "[http://example.com/]" == str(node2)
  32. node3 = ExternalLink(wraptext("http://example.com/"), wrap([]))
  33. assert "[http://example.com/ ]" == str(node3)
  34. node4 = ExternalLink(wraptext("http://example.com/"),
  35. wraptext("Example Web Page"))
  36. assert "[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. assert node1.url == next(gen1)
  45. assert node2.url == next(gen2)
  46. assert node2.title == next(gen2)
  47. with pytest.raises(StopIteration):
  48. next(gen1)
  49. with pytest.raises(StopIteration):
  50. next(gen2)
  51. def test_strip(self):
  52. """test ExternalLink.__strip__()"""
  53. node1 = ExternalLink(wraptext("http://example.com"), brackets=False)
  54. node2 = ExternalLink(wraptext("http://example.com"))
  55. node3 = ExternalLink(wraptext("http://example.com"), wrap([]))
  56. node4 = ExternalLink(wraptext("http://example.com"), wraptext("Link"))
  57. assert "http://example.com" == node1.__strip__()
  58. assert node2.__strip__() is None
  59. assert node3.__strip__() is None
  60. assert "Link" == node4.__strip__()
  61. def test_showtree(self):
  62. """test ExternalLink.__showtree__()"""
  63. output = []
  64. getter, marker = object(), object()
  65. get = lambda code: output.append((getter, code))
  66. mark = lambda: output.append(marker)
  67. node1 = ExternalLink(wraptext("http://example.com"), brackets=False)
  68. node2 = ExternalLink(wraptext("http://example.com"), wraptext("Link"))
  69. node1.__showtree__(output.append, get, mark)
  70. node2.__showtree__(output.append, get, mark)
  71. valid = [
  72. (getter, node1.url), "[", (getter, node2.url),
  73. (getter, node2.title), "]"]
  74. assert valid == output
  75. def test_url(self):
  76. """test getter/setter for the url attribute"""
  77. url = wraptext("http://example.com/")
  78. node1 = ExternalLink(url, brackets=False)
  79. node2 = ExternalLink(url, wraptext("Example"))
  80. assert url is node1.url
  81. assert url is node2.url
  82. node1.url = "mailto:héhehé@spam.com"
  83. node2.url = "mailto:héhehé@spam.com"
  84. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node1.url)
  85. self.assertWikicodeEqual(wraptext("mailto:héhehé@spam.com"), node2.url)
  86. def test_title(self):
  87. """test getter/setter for the title attribute"""
  88. title = wraptext("Example!")
  89. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  90. node2 = ExternalLink(wraptext("http://example.com/"), title)
  91. assert None is node1.title
  92. assert title is node2.title
  93. node2.title = None
  94. assert None is node2.title
  95. node2.title = "My Website"
  96. self.assertWikicodeEqual(wraptext("My Website"), node2.title)
  97. def test_brackets(self):
  98. """test getter/setter for the brackets attribute"""
  99. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  100. node2 = ExternalLink(wraptext("http://example.com/"), wraptext("Link"))
  101. assert node1.brackets is False
  102. assert node2.brackets is True
  103. node1.brackets = True
  104. node2.brackets = False
  105. assert node1.brackets is True
  106. assert node2.brackets is False
  107. assert "[http://example.com/]" == str(node1)
  108. assert "http://example.com/" == str(node2)