A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

test_external_link.py 5.3 KiB

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