A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test_external_link.py 5.6 KiB

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