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

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