A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

125 行
4.8 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/"), wraptext("Example Web Page"))
  35. assert "[http://example.com/ Example Web Page]" == str(node4)
  36. def test_children():
  37. """test ExternalLink.__children__()"""
  38. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  39. node2 = ExternalLink(
  40. wraptext("http://example.com/"), wrap([Text("Example"), Text("Page")])
  41. )
  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 = [(getter, node1.url), "[", (getter, node2.url), (getter, node2.title), "]"]
  72. assert valid == output
  73. def test_url():
  74. """test getter/setter for the url attribute"""
  75. url = wraptext("http://example.com/")
  76. node1 = ExternalLink(url, brackets=False)
  77. node2 = ExternalLink(url, wraptext("Example"))
  78. assert url is node1.url
  79. assert url is node2.url
  80. node1.url = "mailto:héhehé@spam.com"
  81. node2.url = "mailto:héhehé@spam.com"
  82. assert_wikicode_equal(wraptext("mailto:héhehé@spam.com"), node1.url)
  83. assert_wikicode_equal(wraptext("mailto:héhehé@spam.com"), node2.url)
  84. def test_title():
  85. """test getter/setter for the title attribute"""
  86. title = wraptext("Example!")
  87. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  88. node2 = ExternalLink(wraptext("http://example.com/"), title)
  89. assert None is node1.title
  90. assert title is node2.title
  91. node2.title = None
  92. assert None is node2.title
  93. node2.title = "My Website"
  94. assert_wikicode_equal(wraptext("My Website"), node2.title)
  95. def test_brackets():
  96. """test getter/setter for the brackets attribute"""
  97. node1 = ExternalLink(wraptext("http://example.com/"), brackets=False)
  98. node2 = ExternalLink(wraptext("http://example.com/"), wraptext("Link"))
  99. assert node1.brackets is False
  100. assert node2.brackets is True
  101. node1.brackets = True
  102. node2.brackets = False
  103. assert node1.brackets is True
  104. assert node2.brackets is False
  105. assert "[http://example.com/]" == str(node1)
  106. assert "http://example.com/" == str(node2)