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.
 
 
 
 

120 line
4.9 KiB

  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. """
  21. Test cases for the ExternalLink node.
  22. """
  23. import pytest
  24. from mwparserfromhell.nodes import ExternalLink, Text
  25. from .conftest import assert_wikicode_equal, wrap, wraptext
  26. def test_str():
  27. """test ExternalLink.__str__()"""
  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():
  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():
  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():
  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():
  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. assert_wikicode_equal(wraptext("mailto:héhehé@spam.com"), node1.url)
  85. assert_wikicode_equal(wraptext("mailto:héhehé@spam.com"), node2.url)
  86. def test_title():
  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. assert_wikicode_equal(wraptext("My Website"), node2.title)
  97. def test_brackets():
  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)