A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

133 linhas
4.9 KiB

  1. # Copyright (C) 2012-2025 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. from __future__ import annotations
  24. import pytest
  25. from mwparserfromhell.nodes import ExternalLink, Text
  26. from .conftest import assert_wikicode_equal, wrap, wraptext
  27. def test_str():
  28. """test ExternalLink.__str__()"""
  29. node = ExternalLink(wraptext("http://example.com/"), brackets=False)
  30. assert "http://example.com/" == str(node)
  31. node2 = ExternalLink(wraptext("http://example.com/"))
  32. assert "[http://example.com/]" == str(node2)
  33. node3 = ExternalLink(wraptext("http://example.com/"), wrap([]))
  34. assert "[http://example.com/ ]" == str(node3)
  35. node4 = ExternalLink(wraptext("http://example.com/"), 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(
  41. wraptext("http://example.com/"), wrap([Text("Example"), Text("Page")])
  42. )
  43. gen1 = node1.__children__()
  44. gen2 = node2.__children__()
  45. assert node1.url == next(gen1)
  46. assert node2.url == next(gen2)
  47. assert node2.title == next(gen2)
  48. with pytest.raises(StopIteration):
  49. next(gen1)
  50. with pytest.raises(StopIteration):
  51. next(gen2)
  52. def test_strip():
  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. assert "http://example.com" == node1.__strip__()
  59. assert node2.__strip__() is None
  60. assert node3.__strip__() is None
  61. assert "Link" == node4.__strip__()
  62. def test_showtree():
  63. """test ExternalLink.__showtree__()"""
  64. output = []
  65. getter, marker = object(), object()
  66. def get(code):
  67. return output.append((getter, code))
  68. def mark():
  69. return 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 = [(getter, node1.url), "[", (getter, node2.url), (getter, node2.title), "]"]
  75. assert valid == output
  76. def test_url():
  77. """test getter/setter for the url attribute"""
  78. url = wraptext("http://example.com/")
  79. node1 = ExternalLink(url, brackets=False)
  80. node2 = ExternalLink(url, wraptext("Example"))
  81. assert url is node1.url
  82. assert url is node2.url
  83. node1.url = "mailto:héhehé@spam.com"
  84. node2.url = "mailto:héhehé@spam.com"
  85. assert_wikicode_equal(wraptext("mailto:héhehé@spam.com"), node1.url)
  86. assert_wikicode_equal(wraptext("mailto:héhehé@spam.com"), node2.url)
  87. def test_title():
  88. """test getter/setter for the title attribute"""
  89. title = wraptext("Example!")
  90. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  91. node2 = ExternalLink(wraptext("http://example.com/"), title)
  92. assert None is node1.title
  93. assert title is node2.title
  94. node2.title = None
  95. assert None is node2.title
  96. node2.title = "My Website"
  97. assert_wikicode_equal(wraptext("My Website"), node2.title)
  98. def test_brackets():
  99. """test getter/setter for the brackets attribute"""
  100. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  101. node2 = ExternalLink(wraptext("http://example.com/"), wraptext("Link"))
  102. assert node1.brackets is False
  103. assert node2.brackets is True
  104. node1.brackets = True
  105. node2.brackets = False
  106. assert node1.brackets is True
  107. assert node2.brackets is False
  108. assert "[http://example.com/]" == str(node1)
  109. assert "http://example.com/" == str(node2)